| 1 |
;; A tool to create symlinks from main source tree to extension modules, |
| 2 |
;; or to remove them. This is useful to test gosh in the source tree, |
| 3 |
;; without installing them. |
| 4 |
;; Such links are only of developers' interest; they must be cleaned out |
| 5 |
;; to create a distribution tarball. |
| 6 |
|
| 7 |
;; NB: this script is called before all the necessary 'wiring' is |
| 8 |
;; done during the build, so most useful extension modules are not |
| 9 |
;; available. We have to do things in primitive way. |
| 10 |
|
| 11 |
;; When an environemnt variable GAUCHE_PRE_GENERATE_FOR_WINVC is defined, |
| 12 |
;; xlink behaves in peculiar way: |
| 13 |
;; - it ignores files except *.scm. |
| 14 |
;; - it copies the files instead of symlinks. |
| 15 |
;; The feature is to pre-generate and place files that are cumbersome |
| 16 |
;; to do during Windows/VC++ build. Pre-generation itself should be |
| 17 |
;; done on windows. |
| 18 |
|
| 19 |
(define (usage) |
| 20 |
(print "Usage: gosh xlink [-l|-u][-g group][-b top_builddir][-s srcdir] <scm-or-so-file> ...\n" |
| 21 |
" -l creates symlinks from the source tree to the given\n" |
| 22 |
" files. If the given file is a Scheme file, the link\n" |
| 23 |
" is created in $(top_builddir)/lib/$(group)/. If the given\n" |
| 24 |
" file is a compiled DSO, the link is created in\n" |
| 25 |
" $(top_builddir)/src.\n" |
| 26 |
" -u removes symlinks created by -l option.\n" |
| 27 |
" -g group - extra category of library path.\n" |
| 28 |
" -b top_builddir - $(top_builddir) passed from Makefile.\n" |
| 29 |
" -s srcdir - $(srcdir) passed from Makefile." |
| 30 |
) |
| 31 |
(exit 1)) |
| 32 |
|
| 33 |
(define *link* #f) |
| 34 |
(define *unlink* #f) |
| 35 |
(define *group* #f) |
| 36 |
(define *builddir* #f) |
| 37 |
(define *srcdir* #f) |
| 38 |
|
| 39 |
(define *pre-gen-winvc* |
| 40 |
(sys-getenv "GAUCHE_PRE_GENERATE_FOR_WINVC")) |
| 41 |
|
| 42 |
(define (main args) |
| 43 |
(let1 files (parse-args (cdr args)) |
| 44 |
(unless (or *link* *unlink*) (usage)) |
| 45 |
(dolist [file files] |
| 46 |
(let ([path (find-source file)] |
| 47 |
[target (find-target file)]) |
| 48 |
(if *unlink* |
| 49 |
(sys-unlink target) |
| 50 |
(make-link path target))))) |
| 51 |
0) |
| 52 |
|
| 53 |
(define (make-link file target) |
| 54 |
(make-directory* (sys-dirname target)) |
| 55 |
(unless (or (link-exists? file target) |
| 56 |
(and *pre-gen-winvc* (not (string-suffix? ".scm" file)))) |
| 57 |
(sys-unlink target) |
| 58 |
(print "link "file" <- "target) |
| 59 |
(if (and (symbol-bound? 'sys-symlink) (not *pre-gen-winvc*)) |
| 60 |
(sys-symlink file target) |
| 61 |
(sys-system #`"cp ,file ,target")))) |
| 62 |
|
| 63 |
(define (link-exists? file target) |
| 64 |
(and (global-variable-bound? 'gauche 'sys-lstat) |
| 65 |
(file-exists? target) |
| 66 |
(file-is-symlink? target) |
| 67 |
(equal? file (sys-readlink target)))) |
| 68 |
|
| 69 |
(define (find-target file) |
| 70 |
(if (or (string-suffix? ".scm" file) |
| 71 |
(string-suffix? ".sci" file)) |
| 72 |
(build-path *builddir* "lib" *group* file) |
| 73 |
(build-path *builddir* "src" file))) |
| 74 |
|
| 75 |
(define (find-source file) |
| 76 |
(sys-normalize-pathname |
| 77 |
(if (equal? *srcdir* ".") |
| 78 |
(build-path (sys-getcwd) file) |
| 79 |
(let ([candidate (build-path *srcdir* file)] |
| 80 |
[prefix (make-source-prefix file)]) |
| 81 |
(if (and (not (file-exists? file)) |
| 82 |
(file-exists? candidate)) |
| 83 |
(build-path prefix candidate) |
| 84 |
(build-path (sys-getcwd) file)))) |
| 85 |
:canonicalize #t)) |
| 86 |
|
| 87 |
;; file="foo" => prefix="." |
| 88 |
;; file="foo/bar" => prefix="../." |
| 89 |
;; file="foo/bar/baz" => prefix="../../." |
| 90 |
(define (make-source-prefix file) |
| 91 |
(if (equal? (sys-dirname file) ".") |
| 92 |
"." |
| 93 |
(build-path ".." (make-source-prefix (sys-dirname file))))) |
| 94 |
|
| 95 |
|
| 96 |
(define (parse-args args) |
| 97 |
(cond [(null? args) '()] |
| 98 |
[(string=? (car args) "-l") |
| 99 |
(set! *link* #t) (parse-args (cdr args))] |
| 100 |
[(string=? (car args) "-u") |
| 101 |
(set! *unlink* #t) (parse-args (cdr args))] |
| 102 |
[(string=? (car args) "-g") |
| 103 |
(set! *group* (cadr args)) (parse-args (cddr args))] |
| 104 |
[(string=? (car args) "-b") |
| 105 |
(set! *builddir* (cadr args)) (parse-args (cddr args))] |
| 106 |
[(string=? (car args) "-s") |
| 107 |
(set! *srcdir* (cadr args)) (parse-args (cddr args))] |
| 108 |
[(#/^-/ (car args)) (usage)] |
| 109 |
[else args])) |
| 110 |
|
| 111 |
;; simpler versions of file.util procedures |
| 112 |
(define (build-path . args) |
| 113 |
(if (null? args) |
| 114 |
#f |
| 115 |
(let ([pfx (car args)] |
| 116 |
[sfx (apply build-path (cdr args))]) |
| 117 |
(if (string? sfx) |
| 118 |
(cond [(and (>= (string-length sfx) 1) |
| 119 |
(eqv? (string-ref sfx 0) #\/)) |
| 120 |
;; we allow absolute path appear in args; anything before |
| 121 |
;; is ignored |
| 122 |
sfx] |
| 123 |
[(string=? pfx "") sfx] |
| 124 |
[else (string-append pfx "/" sfx)]) |
| 125 |
pfx)))) |
| 126 |
|
| 127 |
(define (make-directory* dir) |
| 128 |
(let1 up (sys-dirname dir) |
| 129 |
(unless (member up '("." "/")) |
| 130 |
(make-directory* up) |
| 131 |
(unless (file-exists? dir) |
| 132 |
(sys-mkdir dir #o755))))) |
| 133 |
|
| 134 |
(define (file-is-symlink? path) |
| 135 |
(eq? (ref (sys-lstat path) 'type) 'symlink)) |
| 136 |
|
| 137 |
(define (string-suffix? suffix str) |
| 138 |
(let ([suffix-len (string-length suffix)] |
| 139 |
[str-len (string-length str)]) |
| 140 |
(and (>= str-len suffix-len) |
| 141 |
(string=? (substring str (- str-len suffix-len) str-len) suffix)))) |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
;; Local variables: |
| 146 |
;; mode: scheme |
| 147 |
;; end: |