0.9.13.47: Thread safety miscellania
[sbcl.git] / tools-for-build / wxs.lisp
1 ;;;; Generate WiX XML Source, from which we eventually generate the .MSI
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 ;;;; XML generation
13
14 (defvar *indent-level* 0)
15
16 (defun print-xml (sexp &optional (stream *standard-output*))
17   (destructuring-bind (tag &optional attributes &body children) sexp
18     (when attributes (assert (evenp (length attributes))))
19     (format stream "~VT<~A~{ ~A='~A'~}~@[/~]>~%"
20             *indent-level* tag attributes (not children))
21       (let ((*indent-level* (+ *indent-level* 3)))
22         (dolist (child children)
23           (unless (listp child)
24             (error "Malformed child: ~S in ~S" child children))
25           (print-xml child stream)))
26       (when children
27         (format stream "~VT</~A>~%" *indent-level* tag))))
28
29 (defun xml-1.0 (pathname sexp)
30   (with-open-file (xml pathname :direction :output :if-exists :supersede
31                        :external-format :ascii)
32      (format xml "<?xml version='1.0'?>")
33      (print-xml sexp xml)))
34
35 (defun application-name ()
36   (format nil "SBCL ~A" (lisp-implementation-version)))
37
38 ;;;; GUID generation
39 ;;;;
40 ;;;; Apparently this willy-nilly regeneration of GUIDs is a bad thing, and
41 ;;;; we should probably have a single GUID per release / Component, so
42 ;;;; that no matter by whom the .MSI is built the GUIDs are the same.
43 ;;;;
44 ;;;; Something to twiddle on a rainy day, I think.
45
46 (load-shared-object "OLE32.DLL")
47
48 (define-alien-type uuid
49     (struct uuid
50             (data1 unsigned-long)
51             (data2 unsigned-short)
52             (data3 unsigned-short)
53             (data4 (array unsigned-char 8))))
54
55 (define-alien-routine ("CoCreateGuid" co-create-guid) int (guid (* uuid)))
56
57 (defun uuid-string (uuid)
58   (declare (type (alien (* uuid)) uuid))
59   (let ((data4 (slot uuid 'data4)))
60     (format nil "~8,'0X-~4,'0X-~4,'0X-~2,'0X~2,'0X-~{~2,'0X~}"
61             (slot uuid 'data1)
62             (slot uuid 'data2)
63             (slot uuid 'data3)
64             (deref data4 0)
65             (deref data4 1)
66             (loop for i from 2 upto 7 collect (deref data4 i)))))
67
68 (defun make-guid ()
69   (let (guid)
70     (unwind-protect
71          (progn
72            (setf guid (make-alien (struct uuid)))
73            (co-create-guid guid)
74            (uuid-string guid))
75       (free-alien guid))))
76
77 (defun list-all-contribs ()
78   (loop for flag in (directory "../contrib/*/test-passed")
79         collect (car (last (pathname-directory flag)))))
80
81 (defun id (string)
82   ;; Mangle a string till it can be used as an Id. A-Z, a-z, 0-9, and
83   ;; _ are ok, nothing else is.
84   (nsubstitute #\_ #\-
85                (nsubstitute #\. #\:
86                             (nsubstitute #\. #\/
87                                          (substitute #\. #\\ string)))))
88
89 (defun directory-id (name)
90   (id (format nil "Directory_~A" (enough-namestring name))))
91
92 (defun directory-names (pathname)
93   (let ((name (car (last (pathname-directory pathname)))))
94     (if (< 8 (length name))
95         (list "Name" (subseq name 0 8)
96               "LongName" name)
97         (list "Name" name))))
98
99 (defun file-id (pathname)
100   (id (format nil "File_~A" (enough-namestring pathname))))
101
102 (defparameter *ignored-directories* '("CVS" ".svn"))
103
104 (defparameter *pathname-type-abbrevs*
105   '(("lisp" . "lsp")
106     ("fasl" . "fas")
107     ("SBCL" . "txt") ; README.SBCL -> README.txt
108     ("texinfo" . "tfo")
109     ("lisp-temp" . "lmp")))
110
111 (defun file-names (pathname)
112   (if (or (< 8 (length (pathname-name pathname)))
113           (< 3 (length (pathname-type pathname))))
114       (let ((short-name (let ((name (pathname-name pathname)))
115                           (if (< 8 (length name))
116                               (subseq name 0 8)
117                               name)))
118             (short-type (let ((type (pathname-type pathname)))
119                           (if (< 3 (length type))
120                               (or (cdr (assoc type *pathname-type-abbrevs* :test #'equalp))
121                                   (error "No abbreviation for type: ~A" type))
122                               type))))
123         (list "Name" (if short-type
124                          (format nil "~A.~A" short-name short-type)
125                          short-name)
126               "LongName" (file-namestring pathname)))
127       (list "Name" (file-namestring pathname))))
128
129 (defparameter *components* nil)
130
131 (defun component-id (pathname)
132   (let ((id (id (format nil "Contrib_~A" (enough-namestring pathname)))))
133     (push id *components*)
134     id))
135
136 (defun ref-all-components ()
137   (prog1
138       (mapcar (lambda (id)
139                 `("ComponentRef" ("Id" ,id)))
140               *components*)
141     (setf *components* nil)))
142
143 (defun collect-1-component (root)
144   `("Directory" ("Id" ,(directory-id root)
145                  ,@(directory-names root))
146     ("Component" ("Id" ,(component-id root)
147                   "Guid" ,(make-guid)
148                   "DiskId" 1)
149      ,@(loop for file in (directory
150                           (make-pathname :name :wild :type :wild :defaults root))
151              when (or (pathname-name file) (pathname-type file))
152              collect `("File" ("Id" ,(file-id file)
153                                ,@(file-names file)
154                                "Source" ,(enough-namestring file)))))))
155
156 (defun collect-components (root)
157   (cons (collect-1-component root)
158         (loop for directory in
159               (directory
160                (merge-pathnames (make-pathname
161                                  :directory '(:relative :wild)
162                                  :name nil :type nil)
163                                 root))
164               unless (member (car (last (pathname-directory directory)))
165                              *ignored-directories* :test #'equal)
166               append (collect-components directory))))
167
168 (defun collect-contrib-components ()
169   (loop for contrib in (directory "../contrib/*/test-passed")
170         append (collect-components (make-pathname :name nil
171                                                   :type nil
172                                                   :version nil
173                                                   :defaults contrib))))
174
175 (defun make-extension (type mime)
176   `("Extension" ("Id" ,type "ContentType" ,mime)
177     ("Verb" ("Id" ,(format nil "load_~A" type)
178              "Argument" "--core \"[#sbcl.core]\" --load \"%1\""
179              "Command" "Load with SBCL"
180              "Target" "[#sbcl.exe]"))))
181
182 (defun write-wxs (pathname)
183   ;; both :INVERT and :PRESERVE could be used here, but this seemed
184   ;; better at the time
185   (xml-1.0
186    pathname
187    `("Wix" ("xmlns" "http://schemas.microsoft.com/wix/2003/01/wi")
188      ("Product" ("Id" "????????-????-????-????-????????????"
189                  "Name" ,(application-name)
190                  "Version" ,(lisp-implementation-version)
191                  "Manufacturer" "http://www.sbcl.org"
192                  "Language" 1033)
193       ("Package" ("Id" "????????-????-????-????-????????????"
194                   "Manufacturer" "http://www.sbcl.org"
195                   "InstallerVersion" 200
196                   "Compressed" "yes"))
197       ("Media" ("Id" 1
198                 "Cabinet" "sbcl.cab"
199                 "EmbedCab" "yes"))
200       ("Directory" ("Id" "TARGETDIR"
201                     "Name" "SourceDir")
202        ("Directory" ("Id" "ProgramMenuFolder"
203                      "Name" "PMFolder"))
204        ("Directory" ("Id" "ProgramFilesFolder"
205                      "Name" "PFiles")
206         ("Directory" ("Id" "BaseFolder"
207                       "Name" "SBCL"
208                       "LongName" "Steel Bank Common Lisp")
209          ("Directory" ("Id" "VersionFolder"
210                        "Name" ,(lisp-implementation-version))
211           ("Directory" ("Id" "INSTALLDIR")
212            ("Component" ("Id" "SBCL_Base"
213                          "Guid" ,(make-guid)
214                          "DiskId" 1)
215             ("Environment" ("Id" "Env_SBCL_HOME"
216                             "Action" "set"
217                             "Name" "SBCL_HOME"
218                             "Part" "all"
219                             "Value" "[INSTALLDIR]"))
220             ("Environment" ("Id" "Env_PATH"
221                             "Action" "set"
222                             "Name" "PATH"
223                             "Part" "first"
224                             "Value" "[INSTALLDIR]"))
225             ,(make-extension "fasl" "application/x-lisp-fasl")
226             ,(make-extension "lisp" "text/x-lisp-source")
227             ("File" ("Id" "sbcl.exe"
228                      "Name" "sbcl.exe"
229                      "Source" "../src/runtime/sbcl.exe")
230              ("Shortcut" ("Id" "sbcl.lnk"
231                           "Directory" "ProgramMenuFolder"
232                           "Name" "SBCL"
233                           "LongName" ,(application-name)
234                           "Arguments" "--core \"[#sbcl.core]\"")))
235             ("File" ("Id" "sbcl.core"
236                      "Name" "sbcl.cre"
237                      "LongName" "sbcl.core"
238                      "Source" "sbcl.core")))
239            ,@(collect-contrib-components))))))
240       ("Feature" ("Id" "Minimal"
241                   "ConfigurableDirectory" "INSTALLDIR"
242                   "Level" 1)
243        ("ComponentRef" ("Id" "SBCL_Base"))
244        ,@(ref-all-components))
245       ("Property" ("Id" "WIXUI_INSTALLDIR" "Value" "INSTALLDIR"))
246       ("UIRef" ("Id" "WixUI_InstallDir"))))))