* install.el (install-files): Do not make DEST directory when
[elisp/apel.git] / install.el
1 ;;; install.el --- Emacs Lisp package install utility
2
3 ;; Copyright (C) 1996,1997,1998,1999,2001 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
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             (if move
66                 (catch 'tag
67                   (while (and (file-exists-p src-file)
68                               (file-writable-p src-file))
69                     (condition-case err
70                         (progn
71                           (delete-file src-file)
72                           (throw 'tag nil))
73                       (error (princ (format "%s\n" (nth 1 err))))))))
74             (princ (format "%s -> %s\n" file dest)))))))
75
76 (defun install-files (files src dest &optional move overwrite just-print)
77   (or just-print
78       (file-exists-p dest)
79       (make-directory dest t))
80   (mapcar
81    (function
82     (lambda (file)
83       (install-file file src dest move overwrite just-print)))
84    files))
85
86
87 ;;; @@ install Emacs Lisp files
88 ;;;
89
90 (defun install-elisp-module (module src dest &optional just-print)
91   (let (el-file elc-file)
92     (let ((name (symbol-name module)))
93       (setq el-file (concat name ".el"))
94       (setq elc-file (concat name ".elc")))
95     (let ((src-file (expand-file-name el-file src)))
96       (if (not (file-exists-p src-file))
97           nil 
98         (if just-print
99             (princ (format "%s -> %s\n" el-file dest))
100           (let ((full-path (expand-file-name el-file dest)))
101             (if (file-exists-p full-path)
102                 (delete-file full-path))
103             (copy-file src-file full-path t t)
104             (princ (format "%s -> %s\n" el-file dest)))))
105       (setq src-file (expand-file-name elc-file src))
106       (if (not (file-exists-p src-file))
107           nil 
108         (if just-print
109             (princ (format "%s -> %s\n" elc-file dest))
110           (let ((full-path (expand-file-name elc-file dest)))
111             (if (file-exists-p full-path)
112                 (delete-file full-path))
113             (copy-file src-file full-path t t)
114             (catch 'tag
115               (while (file-exists-p src-file)
116                 (condition-case err
117                     (progn
118                       (delete-file src-file)
119                       (throw 'tag nil))
120                   (error (princ (format "%s\n" (nth 1 err)))))))
121             (princ (format "%s -> %s\n" elc-file dest))))))))
122
123 (defun install-elisp-modules (modules src dest &optional just-print)
124   (or just-print
125       (file-exists-p dest)
126       (make-directory dest t))
127   (mapcar
128    (function
129     (lambda (module)
130       (install-elisp-module module src dest just-print)))
131    modules))
132
133
134 ;;; @ detect install path
135 ;;;
136
137 ;; install to shared directory (maybe "/usr/local")
138 (defvar install-prefix
139   (if (or (<= emacs-major-version 18)
140           (featurep 'xemacs)
141           (and (boundp 'system-configuration-options) ; 19.29 or later
142                (string= system-configuration-options "NT"))) ; for Meadow
143       (expand-file-name "../../.." exec-directory)
144     (expand-file-name "../../../.." data-directory)))
145
146 (defvar install-elisp-prefix
147   (if (>= emacs-major-version 19)
148       "site-lisp"
149     ;; v18 does not have standard site directory.
150     "local.lisp"))
151
152 (defun install-detect-elisp-directory (&optional prefix elisp-prefix
153                                                  allow-version-specific)
154   (or prefix
155       (setq prefix install-prefix))
156   (or elisp-prefix
157       (setq elisp-prefix install-elisp-prefix))
158   (or (catch 'tag
159         (let ((rest default-load-path)
160               (regexp (concat "^"
161                               (expand-file-name (concat ".*/" elisp-prefix)
162                                                 prefix)
163                               "/?$")))
164           (while rest
165             (if (string-match regexp (car rest))
166                 (if (or allow-version-specific
167                         (not (string-match (format "/%d\\.%d"
168                                                    emacs-major-version
169                                                    emacs-minor-version)
170                                            (car rest))))
171                     (throw 'tag (car rest))))
172             (setq rest (cdr rest)))))
173       (expand-file-name (concat (if (and (not (featurep 'xemacs))
174                                          (or (>= emacs-major-version 20)
175                                              (and (= emacs-major-version 19)
176                                                   (> emacs-minor-version 28))))
177                                     "share/"
178                                   "lib/")
179                                 (cond
180                                  ((featurep 'xemacs)
181                                   (if (featurep 'mule)
182                                       "xmule/"
183                                     "xemacs/"))
184                                  ;; unfortunately, unofficial mule based on
185                                  ;; 19.29 and later use "emacs/" by default.
186                                  ((boundp 'MULE) "mule/")
187                                  ((boundp 'NEMACS) "nemacs/")
188                                  (t "emacs/"))
189                                 elisp-prefix)
190                         prefix)))
191
192 (defvar install-default-elisp-directory
193   (install-detect-elisp-directory))
194
195
196 ;;; @ for XEmacs package system
197 ;;;
198
199 (defun install-update-package-files (package dir &optional just-print)
200   (cond
201    (just-print
202     (princ (format "Updating autoloads in directory %s..\n\n" dir))
203
204     (princ (format "Processing %s\n" dir))
205     (princ "Generating custom-load.el...\n\n")
206
207     (princ (format "Compiling %s...\n"
208                    (expand-file-name "auto-autoloads.el" dir)))
209     (princ (format "Wrote %s\n"
210                    (expand-file-name "auto-autoloads.elc" dir)))
211
212     (princ (format "Compiling %s...\n"
213                    (expand-file-name "custom-load.el" dir)))
214     (princ (format "Wrote %s\n"
215                    (expand-file-name "custom-load.elc" dir))))
216    (t
217     (setq autoload-package-name package)
218
219     (let ((command-line-args-left (list dir)))
220       (batch-update-directory))
221
222     (let ((command-line-args-left (list dir)))
223       (Custom-make-dependencies))
224
225     (byte-compile-file (expand-file-name "auto-autoloads.el" dir))
226     (byte-compile-file (expand-file-name "custom-load.el" dir)))))
227
228
229 ;;; @ Other Utilities
230 ;;;
231
232 (defun install-just-print-p ()
233   (let ((flag (getenv "MAKEFLAGS"))
234         (case-fold-search nil))
235     (princ (format "%s\n" flag))
236     (if flag
237         (string-match "^\\(\\(--[^ ]+ \\)+-\\|[^ =-]\\)*n" flag))))
238
239
240 ;;; @ end
241 ;;;
242
243 (require 'product)
244 (product-provide (provide 'install) (require 'apel-ver))
245
246 ;;; install.el ends here