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