Sync with apel-10_3-1.
[elisp/lemi.git] / emacs-lisp / install.el
1 ;;; install.el --- Emacs Lisp package install utility
2
3 ;; Copyright (C) 1996,97,98,99,2000,01,02 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Created: 1996/08/18
7 ;; Keywords: install, byte-compile, directory detection
8
9 ;; This file is part of APEL (A Portable Emacs Library).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 ;; (require 'poe)                       ; make-directory for v18
29 (require 'path-util)                    ; default-load-path
30
31
32 ;;; @ compile Emacs Lisp files
33 ;;;
34
35 (defun compile-elisp-module (module &optional path every-time)
36   (setq module (expand-file-name (symbol-name module) path))
37   (let ((el-file (concat module ".el"))
38         (elc-file (concat module ".elc")))
39     (if (or every-time
40             (file-newer-than-file-p el-file elc-file))
41         (byte-compile-file el-file))))
42
43 (defun compile-elisp-modules (modules &optional path every-time)
44   (mapcar
45    (function
46     (lambda (module)
47       (compile-elisp-module module path every-time)))
48    modules))
49
50
51 ;;; @ install files
52 ;;;
53
54 (defvar install-overwritten-file-modes (+ (* 64 6)(* 8 4) 4)) ; 0644
55
56 (defun install-file (file src dest &optional move overwrite just-print)
57   (if just-print
58       (princ (format "%s -> %s\n" file dest))
59     (let ((src-file (expand-file-name file src)))
60       (if (file-exists-p src-file)
61           (let ((full-path (expand-file-name file dest)))
62             (if (and (file-exists-p full-path) overwrite)
63                 (delete-file full-path))
64             (copy-file src-file full-path t t)
65             (set-file-modes full-path install-overwritten-file-modes)
66             (if move
67                 (catch 'tag
68                   (while (and (file-exists-p src-file)
69                               (file-writable-p src-file))
70                     (condition-case err
71                         (progn
72                           (delete-file src-file)
73                           (throw 'tag nil))
74                       (error (princ (format "%s\n" (nth 1 err))))))))
75             (princ (format "%s -> %s\n" file dest)))))))
76
77 (defun install-files (files src dest &optional move overwrite just-print)
78   (or just-print
79       (file-exists-p dest)
80       (make-directory dest t))
81   (mapcar
82    (function
83     (lambda (file)
84       (install-file file src dest move overwrite just-print)))
85    files))
86
87
88 ;;; @@ install Emacs Lisp files
89 ;;;
90
91 (defun install-elisp-module (module src dest &optional just-print)
92   (let (el-file elc-file)
93     (let ((name (symbol-name module)))
94       (setq el-file (concat name ".el"))
95       (setq elc-file (concat name ".elc")))
96     (let ((src-file (expand-file-name el-file src)))
97       (if (not (file-exists-p src-file))
98           nil 
99         (if just-print
100             (princ (format "%s -> %s\n" el-file dest))
101           (let ((full-path (expand-file-name el-file dest)))
102             (if (file-exists-p full-path)
103                 (delete-file full-path))
104             (copy-file src-file full-path t t)
105             (set-file-modes full-path install-overwritten-file-modes)
106             (princ (format "%s -> %s\n" el-file dest)))))
107       (setq src-file (expand-file-name elc-file src))
108       (if (not (file-exists-p src-file))
109           nil 
110         (if just-print
111             (princ (format "%s -> %s\n" elc-file dest))
112           (let ((full-path (expand-file-name elc-file dest)))
113             (if (file-exists-p full-path)
114                 (delete-file full-path))
115             (copy-file src-file full-path t t)
116             (set-file-modes full-path install-overwritten-file-modes)
117             (catch 'tag
118               (while (file-exists-p src-file)
119                 (condition-case err
120                     (progn
121                       (delete-file src-file)
122                       (throw 'tag nil))
123                   (error (princ (format "%s\n" (nth 1 err)))))))
124             (princ (format "%s -> %s\n" elc-file dest))))))))
125
126 (defun install-elisp-modules (modules src dest &optional just-print)
127   (or just-print
128       (file-exists-p dest)
129       (make-directory dest t))
130   (mapcar
131    (function
132     (lambda (module)
133       (install-elisp-module module src dest just-print)))
134    modules))
135
136
137 ;;; @ detect install path
138 ;;;
139
140 ;; install to shared directory (maybe "/usr/local")
141 (defvar install-prefix
142   (if (or (<= emacs-major-version 18)
143           (featurep 'xemacs)
144           (and (boundp 'system-configuration-options) ; 19.29 or later
145                (string= system-configuration-options "NT"))) ; for Meadow
146       (expand-file-name "../../.." exec-directory)
147     (expand-file-name "../../../.." data-directory)))
148
149 (defvar install-elisp-prefix
150   (if (>= emacs-major-version 19)
151       "site-lisp"
152     ;; v18 does not have standard site directory.
153     "local.lisp"))
154
155 (defun install-detect-elisp-directory (&optional prefix elisp-prefix
156                                                  allow-version-specific)
157   (or prefix
158       (setq prefix install-prefix))
159   (or elisp-prefix
160       (setq elisp-prefix install-elisp-prefix))
161   (or (catch 'tag
162         (let ((rest default-load-path)
163               (regexp (concat "^"
164                               (expand-file-name (concat ".*/" elisp-prefix)
165                                                 prefix)
166                               "/?$")))
167           (while rest
168             (if (string-match regexp (car rest))
169                 (if (or allow-version-specific
170                         (not (string-match (format "/%d\\.%d"
171                                                    emacs-major-version
172                                                    emacs-minor-version)
173                                            (car rest))))
174                     (throw 'tag (car rest))))
175             (setq rest (cdr rest)))))
176       (expand-file-name (concat (if (and (not (featurep 'xemacs))
177                                          (or (>= emacs-major-version 20)
178                                              (and (= emacs-major-version 19)
179                                                   (> emacs-minor-version 28))))
180                                     "share/"
181                                   "lib/")
182                                 (cond
183                                  ((featurep 'xemacs)
184                                   (if (featurep 'mule)
185                                       "xmule/"
186                                     "xemacs/"))
187                                  ;; unfortunately, unofficial mule based on
188                                  ;; 19.29 and later use "emacs/" by default.
189                                  ((boundp 'MULE) "mule/")
190                                  ((boundp 'NEMACS) "nemacs/")
191                                  (t "emacs/"))
192                                 elisp-prefix)
193                         prefix)))
194
195 (defvar install-default-elisp-directory
196   (install-detect-elisp-directory))
197
198
199 ;;; @ for XEmacs package system
200 ;;;
201
202 ;; (defun install-update-package-files (package dir &optional just-print)
203 ;;   (cond
204 ;;    (just-print
205 ;;     (princ (format "Updating autoloads in directory %s..\n\n" dir))
206 ;; 
207 ;;     (princ (format "Processing %s\n" dir))
208 ;;     (princ "Generating custom-load.el...\n\n")
209 ;; 
210 ;;     (princ (format "Compiling %s...\n"
211 ;;                    (expand-file-name "auto-autoloads.el" dir)))
212 ;;     (princ (format "Wrote %s\n"
213 ;;                    (expand-file-name "auto-autoloads.elc" dir)))
214 ;; 
215 ;;     (princ (format "Compiling %s...\n"
216 ;;                    (expand-file-name "custom-load.el" dir)))
217 ;;     (princ (format "Wrote %s\n"
218 ;;                    (expand-file-name "custom-load.elc" dir))))
219 ;;    (t
220 ;;     (setq autoload-package-name package)
221 ;; 
222 ;;     (let ((command-line-args-left (list dir)))
223 ;;       (batch-update-directory))
224 ;; 
225 ;;     (let ((command-line-args-left (list dir)))
226 ;;       (Custom-make-dependencies))
227 ;; 
228 ;;     (byte-compile-file (expand-file-name "auto-autoloads.el" dir))
229 ;;     (byte-compile-file (expand-file-name "custom-load.el" dir)))))
230
231
232 ;;; @ Other Utilities
233 ;;;
234
235 (defun install-just-print-p ()
236   (let ((flag (getenv "MAKEFLAGS"))
237         (case-fold-search nil))
238     (princ (format "%s\n" flag))
239     (if flag
240         (string-match "^\\(\\(--[^ ]+ \\)+-\\|[^ =-]\\)*n" flag))))
241
242
243 ;;; @ end
244 ;;;
245
246 (require 'product)
247 (product-provide (provide 'install) (require 'apel-ver))
248
249 ;;; install.el ends here