misc minor improvements

svn: r16553
This commit is contained in:
Eli Barzilay 2009-11-05 07:15:30 +00:00
parent 37adbb42a8
commit c696c7e88c

View File

@ -1,9 +1,9 @@
#lang scheme/unit #lang scheme/unit
(require scheme/path (require scheme/path
scheme/file scheme/file
scheme/list scheme/list
scheme/string
compiler/embed compiler/embed
setup/dirs setup/dirs
@ -33,8 +33,8 @@
(eq? kind 'mzscheme))) (eq? kind 'mzscheme)))
(let ([bin-dir (find-console-bin-dir)]) (let ([bin-dir (find-console-bin-dir)])
(and bin-dir (and bin-dir
(file-exists? (build-path (file-exists?
bin-dir (build-path bin-dir
(format "~a~a" kind (variant-suffix variant #f))))))] (format "~a~a" kind (variant-suffix variant #f))))))]
[(eq? 'macosx (system-type)) [(eq? 'macosx (system-type))
;; kind must be mred, because mzscheme case is caught above ;; kind must be mred, because mzscheme case is caught above
@ -43,12 +43,10 @@
cased-kind-name cased-kind-name
(variant-suffix variant #f))))] (variant-suffix variant #f))))]
[(eq? 'windows (system-type)) [(eq? 'windows (system-type))
(file-exists? (build-path (if (eq? kind 'mzscheme) (file-exists?
(find-console-bin-dir) (build-path
(find-gui-bin-dir)) (if (eq? kind 'mzscheme) (find-console-bin-dir) (find-gui-bin-dir))
(format "~a~a.exe" (format "~a~a.exe" cased-kind-name (variant-suffix variant #t))))]
cased-kind-name
(variant-suffix variant #t))))]
[else (error "unknown system type")])) [else (error "unknown system type")]))
(define (available-variants kind) (define (available-variants kind)
@ -99,64 +97,46 @@
(memq v '(script-3m script-cgc))) (memq v '(script-3m script-cgc)))
(define (add-file-suffix path variant mred?) (define (add-file-suffix path variant mred?)
(let ([s (variant-suffix variant (case (system-type) (let ([s (variant-suffix
variant
(case (system-type)
[(unix) #f] [(unix) #f]
[(windows) #t] [(windows) #t]
[(macosx) (and mred? [(macosx) (and mred? (not (script-variant? variant)))]))])
(not (script-variant? variant)))]))])
(if (string=? "" s) (if (string=? "" s)
path path
(path-replace-suffix
path
(string->bytes/utf-8
(if (and (eq? 'windows (system-type)) (if (and (eq? 'windows (system-type))
(regexp-match #rx#"[.]exe$" (path->bytes path))) (regexp-match #rx#"[.]exe$" (path->bytes path)))
(path-replace-suffix path (string->bytes/utf-8 (format "~a.exe" s)
(format "~a.exe" s))) s))))))
(path-replace-suffix path (string->bytes/utf-8 s))))))
(define (string-append/spaces f flags) (define (string-append/spaces f flags)
(if (null? flags) (string-append* (append-map (lambda (x) (list (f x) " ")) flags)))
""
(string-append
(f (car flags))
" "
(string-append/spaces f (cdr flags)))))
(define (str-list->sh-str flags) (define (str-list->sh-str flags)
(letrec ([trans (string-append/spaces
(lambda (s) (lambda (s)
(cond (string-append "'" (regexp-replace* #rx"'" s "'\"'\"'") "'"))
[(regexp-match "(.*)'(.*)" s) flags))
=> (lambda (m)
(string-append (trans (cadr m))
"\"'\""
(trans (caddr m))))]
[else (format "'~a'" s)]))])
(string-append/spaces trans flags)))
(define (str-list->dos-str flags) (define (str-list->dos-str flags)
(letrec ([trans (define (trans s)
(lambda (s) (if (not (regexp-match? #rx"[ \n\t\r\v\"\\]" s))
(if (or (regexp-match (string #\[ #\space #\newline #\tab #\return #\vtab #\]) s) s
(regexp-match "\"" s)
(regexp-match "\\\\" s))
(list->string (list->string
(let loop ([l (string->list s)][wrote-slash 0]) (let loop ([l (string->list s)] [slashes '()])
(cond (cond [(null? l) '()]
[(null? l) null]
[(char-whitespace? (car l)) [(char-whitespace? (car l))
(append `(,@slashes #\" ,(car l) #\" ,@(loop (cdr l) '()))]
(string->list (make-string wrote-slash #\\)) [(eq? #\\ (car l))
(list #\" (car l) #\") `(#\\ ,@(loop (cdr l) (cons #\\ slashes)))]
(loop (cdr l) 0))] [(eq? #\" (car l))
[else `(,@slashes #\" #\\ #\" #\" ,@(loop (cdr l) '()))]
(case (car l) [else `(,(car l) ,@(loop (cdr l) '()))])))))
[(#\\) (cons #\\ (loop (cdr l) (add1 wrote-slash)))] (string-append/spaces trans flags))
[(#\") (append
(string->list (make-string wrote-slash #\\))
`(#\" #\\ #\" #\")
(loop (cdr l) 0))]
[else (cons (car l) (loop (cdr l) 0))])])))
s))])
(string-append/spaces trans flags)))
(define one-arg-x-flags '((xa "-display") (define one-arg-x-flags '((xa "-display")
(xb "-geometry") (xb "-geometry")
@ -177,28 +157,15 @@
(define (skip-x-flags flags) (define (skip-x-flags flags)
(let ([xfmem (lambda (flag) (lambda (xf) (member flag (cdr xf))))]) (let ([xfmem (lambda (flag) (lambda (xf) (member flag (cdr xf))))])
(let loop ([f flags]) (let loop ([f flags])
(if (null? f) (cond [(null? f) null]
null [(ormap (xfmem (car f)) one-arg-x-flags)
(if (ormap (xfmem (car f)) one-arg-x-flags) (if (null? (cdr f)) null (loop (cddr f)))]
(if (null? (cdr f)) [(ormap (xfmem (car f)) no-arg-x-flags) (loop (cdr f))]
null [else f]))))
(loop (cddr f)))
(if (ormap (xfmem (car f)) no-arg-x-flags)
(loop (cdr f))
f))))))
(define (output-x-arg-getter exec args) (define (output-x-arg-getter exec args)
(let ([or-flags (let ([or-flags (lambda (l) (string-append* (add-between l " | ")))])
(lambda (l) (string-append*
(if (null? (cdr l))
(car l)
(string-append
(car l)
(apply
string-append
(map (lambda (s) (string-append " | " s)) (cdr l))))))])
(apply
string-append
(append (append
(list "# Find X flags and shift them to the front\n" (list "# Find X flags and shift them to the front\n"
"findxend() {\n" "findxend() {\n"
@ -230,14 +197,14 @@
" findxend ${1+\"$@\"}\n" " findxend ${1+\"$@\"}\n"
"}\nfindxend ${1+\"$@\"}\n") "}\nfindxend ${1+\"$@\"}\n")
exec exec
(apply (string-append*
string-append
(append (append
(map (map (lambda (f)
(lambda (f) (format " ${~a+\"~a\"} ${~a+\"$~a\"}" (car f) (cadr f) (car f) (car f))) (format " ${~a+\"~a\"} ${~a+\"$~a\"}"
(car f) (cadr f) (car f) (car f)))
one-arg-x-flags) one-arg-x-flags)
(map (map (lambda (f)
(lambda (f) (format " ${~a+\"~a\"}" (car f) (cadr f))) (format " ${~a+\"~a\"}" (car f) (cadr f)))
no-arg-x-flags))) no-arg-x-flags)))
args)))))) args))))))
@ -253,9 +220,7 @@
(if (and (pair? b) (equal? (car b) (car d))) (if (and (pair? b) (equal? (car b) (car d)))
(loop (cdr b) (cdr d)) (loop (cdr b) (cdr d))
(let ([p (append (map (lambda (x) 'up) (cdr d)) b)]) (let ([p (append (map (lambda (x) 'up) (cdr d)) b)])
(if (null? p) (if (null? p) #f (apply build-path p))))))
#f
(apply build-path p))))))
(define (make-relative-path-header dest bindir) (define (make-relative-path-header dest bindir)
;; rely only on binaries in /usr/bin:/bin ;; rely only on binaries in /usr/bin:/bin
@ -310,10 +275,7 @@
"\n" "\n"
"bindir=\"$D" "bindir=\"$D"
(let ([s (relativize bindir-explode dest-explode)]) (let ([s (relativize bindir-explode dest-explode)])
(if s (if s (string-append "/" (protect-shell-string s)) ""))
(string-append "/"
(protect-shell-string s))
""))
"\"\n" "\"\n"
"PATH=\"$saveP\"\n") "PATH=\"$saveP\"\n")
;; fallback to absolute path header ;; fallback to absolute path header
@ -397,12 +359,8 @@
(if (not (and (let ([m (assq 'independent? aux)]) (if (not (and (let ([m (assq 'independent? aux)])
(and m (cdr m))))) (and m (cdr m)))))
;; Normal launcher: ;; Normal launcher:
(make-embedding-executable dest (eq? kind 'mred) #f (make-embedding-executable
null null null dest (eq? kind 'mred) #f null null null flags aux #t variant)
flags
aux
#t
variant)
;; Independent launcher (needed for Setup PLT): ;; Independent launcher (needed for Setup PLT):
(begin (begin
(install-template dest kind "mzstart.exe" "mrstart.exe") (install-template dest kind "mzstart.exe" "mrstart.exe")
@ -433,12 +391,9 @@
(car m) (car m)
(begin (begin
(close-input-port p) (close-input-port p)
(when (file-exists? dest) (when (file-exists? dest) (delete-file dest))
(delete-file dest)) (error 'make-windows-launcher
(error "Couldn't find ~a position in template" s)))))]
'make-windows-launcher
(format
"Couldn't find ~a position in template" s))))))]
[exedir-poslen (find-it x "executable path")] [exedir-poslen (find-it x "executable path")]
[command-poslen (find-it m "command-line")] [command-poslen (find-it m "command-line")]
[variant-poslen (find-it v "variant")] [variant-poslen (find-it v "variant")]
@ -456,8 +411,7 @@
[check-len [check-len
(lambda (len s es) (lambda (len s es)
(when (> (bytes-length s) len) (when (> (bytes-length s) len)
(when (file-exists? dest) (when (file-exists? dest) (delete-file dest))
(delete-file dest))
(error (error
(format (format
"~a exceeds limit of ~a characters with ~a characters: ~a" "~a exceeds limit of ~a characters with ~a characters: ~a"
@ -469,11 +423,11 @@
(write-magic p exedir pos-exedir len-exedir) (write-magic p exedir pos-exedir len-exedir)
(write-magic p (bytes-append bstr #"\0\0") pos-command len-command) (write-magic p (bytes-append bstr #"\0\0") pos-command len-command)
(let* ([suffix (variant-suffix (current-launcher-variant) #t)] (let* ([suffix (variant-suffix (current-launcher-variant) #t)]
[suffix-bytes (bytes-append [suffix-bytes
(bytes-append
(list->bytes (list->bytes
(apply append (append-map (lambda (c) (list c 0))
(map (lambda (c) (list c 0)) (bytes->list (string->bytes/latin-1 suffix))))
(bytes->list (string->bytes/latin-1 suffix)))))
#"\0\0")]) #"\0\0")])
(write-magic p suffix-bytes pos-variant (bytes-length suffix-bytes))) (write-magic p suffix-bytes pos-variant (bytes-length suffix-bytes)))
(close-output-port p))))))) (close-output-port p)))))))
@ -482,8 +436,7 @@
; make-macosx-launcher : symbol (listof str) pathname -> ; make-macosx-launcher : symbol (listof str) pathname ->
(define (make-macosx-launcher kind variant flags dest aux) (define (make-macosx-launcher kind variant flags dest aux)
(if (or (eq? kind 'mzscheme) (if (or (eq? kind 'mzscheme) (script-variant? variant))
(script-variant? variant))
;; MzScheme or script launcher is the same as for Unix ;; MzScheme or script launcher is the same as for Unix
(make-unix-launcher kind variant flags dest aux) (make-unix-launcher kind variant flags dest aux)
;; MrEd "launcher" is a stand-alone executable ;; MrEd "launcher" is a stand-alone executable
@ -496,28 +449,23 @@
(define (make-macos-launcher kind variant flags dest aux) (define (make-macos-launcher kind variant flags dest aux)
(install-template dest kind "GoMr" "GoMr") (install-template dest kind "GoMr" "GoMr")
(let ([p (open-input-file dest)]) (let* ([p (open-input-file dest)]
(let ([m (regexp-match-positions #rx#"<Insert offset here>" p)]) [m (regexp-match-positions #rx#"<Insert offset here>" p)])
;; fast-forward to the end: ;; fast-forward to the end:
(let ([s (make-bytes 4096)]) (let ([s (make-bytes 4096)])
(let loop () (let loop ()
(if (eof-object? (read-bytes! s p)) (if (eof-object? (read-bytes! s p)) (file-position p) (loop))))
(file-position p)
(loop))))
(let ([data-fork-size (file-position p)]) (let ([data-fork-size (file-position p)])
(close-input-port p) (close-input-port p)
(let ([p (open-output-file dest #:exists 'update)] (let ([p (open-output-file dest #:exists 'update)]
[str (str-list->sh-str (append [str (str-list->sh-str
(if (eq? kind 'mred) (append (if (eq? kind 'mred) null '("-Z")) flags))])
null
'("-Z"))
flags))])
(file-position p (caar m)) (file-position p (caar m))
(display (integer->integer-bytes (string-length str) 4 #t #t) p) (display (integer->integer-bytes (string-length str) 4 #t #t) p)
(display (integer->integer-bytes data-fork-size 4 #t #t) p) (display (integer->integer-bytes data-fork-size 4 #t #t) p)
(file-position p data-fork-size) (file-position p data-fork-size)
(display str p) (display str p)
(close-output-port p)))))) (close-output-port p)))))
(define (get-maker) (define (get-maker)
(case (system-type) (case (system-type)
@ -526,28 +474,20 @@
[(macos) make-macos-launcher] [(macos) make-macos-launcher]
[(macosx) make-macosx-launcher])) [(macosx) make-macosx-launcher]))
(define make-mred-launcher (define (make-mred-launcher flags dest [aux null])
(lambda (flags dest [aux null]) ((get-maker) 'mred (current-launcher-variant) flags dest aux))
(let ([variant (current-launcher-variant)])
((get-maker) 'mred variant flags dest aux))))
(define make-mzscheme-launcher (define (make-mzscheme-launcher flags dest [aux null])
(lambda (flags dest [aux null]) ((get-maker) 'mzscheme (current-launcher-variant) flags dest aux))
(let ([variant (current-launcher-variant)])
((get-maker) 'mzscheme variant flags dest aux))))
(define (strip-suffix s) (define (strip-suffix s)
(path-replace-suffix s #"")) (path-replace-suffix s #""))
(define (build-aux-from-path aux-root) (define (build-aux-from-path aux-root)
(let ([aux-root (if (string? aux-root) (let ([aux-root (if (string? aux-root) (string->path aux-root) aux-root)])
(string->path aux-root) (define (try key suffix)
aux-root)])
(let ([try (lambda (key suffix)
(let ([p (path-replace-suffix aux-root suffix)]) (let ([p (path-replace-suffix aux-root suffix)])
(if (file-exists? p) (if (file-exists? p) (list (cons key p)) null)))
(list (cons key p))
null)))])
(append (append
(try 'icns #".icns") (try 'icns #".icns")
(try 'ico #".ico") (try 'ico #".ico")
@ -559,31 +499,28 @@
(with-input-from-file (cdar l) (with-input-from-file (cdar l)
(lambda () (lambda ()
(let ([s (read-string 4)]) (let ([s (read-string 4)])
(if s (if s (list (cons (caar l) s)) null)))))))
(list (cons (caar l) s))
null)))))))
(let ([l (try 'file-types #".filetypes")]) (let ([l (try 'file-types #".filetypes")])
(if (null? l) (if (null? l)
l l
(with-handlers ([exn:fail:filesystem? (lambda (x) null)]) (with-handlers ([exn:fail:filesystem? (lambda (x) null)])
(with-input-from-file (cdar l) (with-input-from-file (cdar l)
(lambda () (lambda ()
(let ([d (read)]) (let*-values ([(d) (read)]
(let-values ([(local-dir base dir?) (split-path aux-root)]) [(local-dir base dir?) (split-path aux-root)]
(let ([icon-files [(icon-files)
(apply (append-map
append (lambda (spec)
(map (lambda (spec)
(let ([m (assoc "CFBundleTypeIconFile" spec)]) (let ([m (assoc "CFBundleTypeIconFile" spec)])
(if m (if m
(list (build-path (list (build-path
(path->complete-path local-dir) (path->complete-path local-dir)
(format "~a.icns" (cadr m)))) (format "~a.icns" (cadr m))))
null))) null)))
d))]) d)])
(list (list (cons 'file-types d)
(cons 'file-types d) (cons 'resource-files
(cons 'resource-files (remove-duplicates icon-files))))))))))) (remove-duplicates icon-files)))))))))
(let ([l (try 'file-types #".utiexports")]) (let ([l (try 'file-types #".utiexports")])
(if (null? l) (if (null? l)
l l
@ -591,8 +528,7 @@
(with-input-from-file (cdar l) (with-input-from-file (cdar l)
(lambda () (lambda ()
(let ([d (read)]) (let ([d (read)])
(list (list (cons 'uti-exports d)))))))))))
(cons 'uti-exports d))))))))))))
(define (make-mred-program-launcher file collection dest) (define (make-mred-program-launcher file collection dest)
(make-mred-launcher (list "-l-" (string-append collection "/" file)) (make-mred-launcher (list "-l-" (string-append collection "/" file))
@ -609,19 +545,13 @@
(strip-suffix file))))) (strip-suffix file)))))
(define (unix-sfx file mred?) (define (unix-sfx file mred?)
(list->string (string-downcase (regexp-replace* #px"\\s" file "-")))
(map
(lambda (c)
(if (char-whitespace? c)
#\-
(char-downcase c)))
(string->list file))))
(define (sfx file mred?) (define (sfx file mred?)
(case (system-type) (case (system-type)
[(unix) (unix-sfx file mred?)] [(unix) (unix-sfx file mred?)]
[(windows) (string-append (if mred? file (unix-sfx file mred?)) [(windows)
".exe")] (string-append (if mred? file (unix-sfx file mred?)) ".exe")]
[else file])) [else file]))
(define (program-launcher-path name mred?) (define (program-launcher-path name mred?)
@ -646,8 +576,8 @@
(define (mzscheme-program-launcher-path name) (define (mzscheme-program-launcher-path name)
(case (system-type) (case (system-type)
[(macosx) (add-file-suffix [(macosx)
(build-path (find-console-bin-dir) (unix-sfx name #f)) (add-file-suffix (build-path (find-console-bin-dir) (unix-sfx name #f))
(current-launcher-variant) (current-launcher-variant)
#f)] #f)]
[else (program-launcher-path name #f)])) [else (program-launcher-path name #f)]))
@ -685,16 +615,12 @@
(define (mzscheme-launcher-put-file-extension+style+filters) (define (mzscheme-launcher-put-file-extension+style+filters)
(put-file-extension+style+filters (put-file-extension+style+filters
(if (eq? 'macosx (system-type)) (if (eq? 'macosx (system-type)) 'unix (system-type))))
'unix
(system-type))))
(define mred-launcher-up-to-date? (define (mred-launcher-up-to-date? dest [aux null])
(lambda (dest [aux null]) (mzscheme-launcher-up-to-date? dest aux))
(mzscheme-launcher-up-to-date? dest aux)))
(define mzscheme-launcher-up-to-date? (define (mzscheme-launcher-up-to-date? dest [aux null])
(lambda (dest [aux null])
(cond (cond
;; When running Setup PLT under Windows, the ;; When running Setup PLT under Windows, the
;; launcher process stays running until MzScheme ;; launcher process stays running until MzScheme
@ -703,16 +629,17 @@
;; that a Setup-PLT-style independent launcher ;; that a Setup-PLT-style independent launcher
;; is always up-to-date. ;; is always up-to-date.
[(eq? 'windows (system-type)) [(eq? 'windows (system-type))
(and (let ([m (assq 'independent? aux)]) (and (let ([m (assq 'independent? aux)]) (and m (cdr m)))
(and m (cdr m)))
(file-exists? dest))] (file-exists? dest))]
;; For any other setting, we could implement ;; For any other setting, we could implement
;; a fancy check, but for now always re-create ;; a fancy check, but for now always re-create
;; launchers. ;; launchers.
[else #f]))) [else #f]))
(define (install-mred-program-launcher file collection name) (define (install-mred-program-launcher file collection name)
(make-mred-program-launcher file collection (mred-program-launcher-path name))) (make-mred-program-launcher file collection
(mred-program-launcher-path name)))
(define (install-mzscheme-program-launcher file collection name) (define (install-mzscheme-program-launcher file collection name)
(make-mzscheme-program-launcher file collection (mzscheme-program-launcher-path name))) (make-mzscheme-program-launcher file collection
(mzscheme-program-launcher-path name)))