;;; ;;; install - Generic installation utility ;;; ;;; Copyright (c) 2004-2024 Shiro Kawai ;;; ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; ;;; 1. Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; ;;; 2. Redistributions in binary form must reproduce the above copyright ;;; notice, this list of conditions and the following disclaimer in the ;;; documentation and/or other materials provided with the distribution. ;;; ;;; 3. Neither the name of the authors nor the names of its contributors ;;; may be used to endorse or promote products derived from this ;;; software without specific prior written permission. ;;; ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ;;; ;; This is intended to replace 'install' program, in order to avoid ;; variations of system's install program. Although most 'install' ;; programs have various extensions, we can't reliably use those ;; extended features since we don't know such extended install program ;; is avilable on the target system. Assuming minimum featured install ;; program makes makefile messy. (use file.util) (use gauche.parseopt) (use scheme.list) (use srfi.13) (use util.match) (define (error-exit fmtstr . args) (apply exit 1 #"gauche-install: ~fmtstr" args)) (define (p . args) (for-each print args)) (define (usage) (p "Usage: gauche-install [options] file dest (1st format*)" " gauche-install [options] file ... directory (2nd format*)" " gauche-install -d [options] directory ... (3rd format*)" " gauche-install -T directory [options] file ... (4th format)" " gauche-install -U directory [options] file ... (5th format)" "(*: 1st,2nd and 3rd format are compatible with BSD install)" "Options:" (option-parser-help-string) ) (exit 0)) (define write-permission-message (cond-expand [gauche.os.windows "\nNote:\n\ Administrator privilege is required. If you want to force installation,\n\ right-click the shortcut of shell and select 'Run as administrator' menu,\n\ and then retry the command."] [else ""])) (define verbose (make-parameter #f)) (define dry-run (make-parameter #f)) (define-syntax do-it (syntax-rules () [(_ mesg . actions) (begin (when (and (verbose) mesg) (print mesg) (flush)) (unless (dry-run) . actions))])) (define check-write-permission (cond-expand [gauche.os.windows (define path-cache (make-hash-table 'equal?)) (^[path] (do-it #"check write permission of ~path" (unless (file-is-directory? path) (error-exit "non-directory file gets in my way: ~s" path)) (unless (hash-table-get path-cache path #f) (guard (e [else (error-exit "no write permission of ~s~a" path write-permission-message)]) (receive (out name) (sys-mkstemp (build-path path "checkwp")) (close-output-port out) (sys-unlink name) (hash-table-put! path-cache path #t))))))] [else (^[path])])) (define (ensure-directory path :optional (mode #f) (owner #f) (group #f)) (if (file-exists? path) (if (file-is-directory? path) (check-write-permission path) (error-exit "non-directory file gets in my way: ~s" path)) (do-it #"creating directory ~path" (guard (e [else (error-exit "can't create directory: ~s~a" (ref e 'message) write-permission-message)]) (make-directory* path) (when mode (sys-chmod path mode)) (when (or owner group) (sys-chown path (->ugid sys-user-name->uid owner "user") (->ugid sys-group-name->gid group "group")))) ))) ;; user/group -> uid/gid (define (->ugid str->id arg type) (cond [(not arg) -1] [(integer? arg) arg] [(and (string? arg) (str->id arg))] [else (error-exit "bad ~a name: ~a" type arg)])) ;; find source path (define (ensure-src file srcdir) (or (and-let* ([ srcdir ] [srcpath (build-path srcdir file)] [ (file-exists? srcpath) ]) srcpath) file)) ;; copy, possibly with appending prelude (define (cp src dest prelude) (if prelude (receive (out name) (sys-mkstemp src) (display prelude out) (call-with-input-file src (cut copy-port <> out :unit 65536)) (close-output-port out) (move-file name dest :if-exists :supersede)) (copy-file src dest :if-exists :supersede :safe #t))) ;; standard install (define (install src dest prelude mode owner group canonical?) (ensure-directory (sys-dirname dest)) (do-it #"installing ~src to ~dest" (and (cp src dest prelude) (begin (sys-chmod dest mode) (when (or owner group) (sys-chown dest (->ugid sys-user-name->uid owner "user") (->ugid sys-group-name->gid group "group"))) (when (and canonical? (string-suffix? ".sci" dest)) (sys-rename dest (path-swap-extension dest "scm"))))))) ;; strip PREFIX from path if possible. (define (strip-dir prefix path) (cond [(not prefix) path] [(eq? prefix #t) (sys-basename path)] [else (let1 pre (if (#/[\/\\]$/ prefix) prefix (string-append prefix "/")) (if (string-prefix? pre path) (string-drop path (string-length pre)) path))])) ;; Entry point (define (main args) (let-args (cdr args) ([mkdir "d|directory" ? "Creates directories listed in the arguments. \ (3rd format only)."] [mode "m|mode=s{MODE}" #o755 => (cut string->number <> 8) ? "Change mode of the installed file."] [owner "o|owner=s{OWNER}" ? "Change owner of the installed file(s)."] [group "g|group=s{GROUP}" ? "Change group of the installed file(s)."] [csfx "C|canonical-suffix" ? "If installed file has a suffix *.sci, replace it for \ *.scm. This is Gauche specific convention."] [srcdir "S|srcdir=s{DIR}" ? "Look for files within {DIR}; useful if VPATH is used."] [target "T|target=s{DIR}" ? "Installs files to the {DIR}, creating paths if needed. \ Partial path of files are preserved. (4th format only)."] [utarget "U|uninstall=s{DIR}" ? "Reverse of -T, e.g. removes files from its destination."] [shebang "shebang=s{PATH}" ? "Adds #!{PATH} before the file contents. \ Useful to install scripts."] [verb "v|verbose" ? "Work verbosely"] [dry "n|dry-run" ? "Just prints what actions to be done."] [sprefix "p|strip-prefix=s{PREFIX}" ? "Strip prefix dirs from FILEs before \ installation. (4th/5th format only)."] [#f "h|help" => usage ? "Show this help message."] [#f "c" ? "This option is ignored. Recognized for the \ compatibility."] [else (opt . _) (print "Unknown option : " opt) (usage)] . args) (parameterize ([verbose (or verb dry)] [dry-run dry]) (when shebang (set! shebang #"#!~shebang\n")) (cond [mkdir (for-each (cut ensure-directory <> mode owner group) args)] [target (dolist [src args] (install (ensure-src src srcdir) (build-path target (strip-dir sprefix src)) shebang mode owner group csfx))] [utarget (guard (e [else (error-exit "can't remove file: ~s~a" (ref e 'message) write-permission-message)]) (dolist [src args] (sys-unlink (build-path utarget (strip-dir sprefix src)))))] [else (match args [() (usage)] [(_) #f] ; no op [(src dst) ; file to file or file to dir (install (ensure-src src srcdir) (if (file-is-directory? (cadr args)) (build-path (cadr args) (sys-basename src)) (cadr args)) shebang mode owner group csfx)] [(args ...) (let1 target (car (last-pair args)) (dolist [src (drop-right args 1)] (install (ensure-src src srcdir) (build-path target (sys-basename src)) shebang mode owner group csfx)))] )]))) 0) ;; Local variables: ;; mode: scheme ;; end: