1 | ;;;; base.scm |
---|
2 | |
---|
3 | |
---|
4 | #+(not csi) |
---|
5 | (declare |
---|
6 | (export CC C++ OPTIM LINK LINKFLAGS LINKLIBS CSC AR SUFSHR CSCFLAGS SHAREDFLAGS |
---|
7 | CSCOPTIM RANLIB CSCLIBS SHELLMODE FILEMODE EXEMODE CCFLAGS C++FLAGS |
---|
8 | SCANHEADERS BUILD_VERSION SUFDLSHR CSCLINKFLAGS SUFEXE SUFOBJ SUFLIB HDRSCAN INCLUDEDIRS |
---|
9 | link main-from-objects main cc c++ library object objects link-library scheme-extension |
---|
10 | link-shared-library csc user-object scheme-main scheme-link clean install-file |
---|
11 | install-bin install-man install-shell shared-library install-lib) ) |
---|
12 | |
---|
13 | |
---|
14 | (define BUILD_VERSION +version+) |
---|
15 | (define CC "cc") |
---|
16 | (define C++ "c++") |
---|
17 | (define OPTIM "-g") |
---|
18 | (define CSCOPTIM "-O2 -d1") |
---|
19 | (define CSC "csc") |
---|
20 | (define AR "ar ru") |
---|
21 | (define RANLIB "ranlib") |
---|
22 | (define SUFSHR (if (eq? (software-version) 'macosx) ".dylib" ".so")) |
---|
23 | (define SHAREDFLAGS (if (eq? (software-version) 'macosx) "-dynamiclib" "-shared")) |
---|
24 | (define SHELLMODE #o755) |
---|
25 | (define FILEMODE #o644) |
---|
26 | (define EXEMODE #o755) |
---|
27 | (define LINKLIBS "") |
---|
28 | (define CSCLIBS "") |
---|
29 | (define LINKFLAGS "") |
---|
30 | (define C++FLAGS "") |
---|
31 | (define CCFLAGS "") |
---|
32 | (define CSCFLAGS "") |
---|
33 | (define CSCLINKFLAGS "") |
---|
34 | (define SCANHEADERS #t) |
---|
35 | (define LINK #f) |
---|
36 | (define SUFDLSHR "so") |
---|
37 | (define SUFEXE "") |
---|
38 | (define SUFOBJ "o") |
---|
39 | (define SUFLIB "a") |
---|
40 | (define HDRSCAN "(^|\n)[ \t]*#[ \t]*include[ \t]*[\"]([^\"]+)[\"]") |
---|
41 | (define INCLUDEDIRS '()) |
---|
42 | |
---|
43 | |
---|
44 | (define (link image . objs) |
---|
45 | (actions |
---|
46 | "link" |
---|
47 | image |
---|
48 | (lambda _ |
---|
49 | (run (,($ LINK CC) -o ,image ,@(normalize objs) ,($ LINKFLAGS) ,($ LINKLIBS)))))) |
---|
50 | |
---|
51 | (define (scheme-link image . objs) |
---|
52 | (actions |
---|
53 | "link Scheme" |
---|
54 | image |
---|
55 | (lambda _ |
---|
56 | (run (,($ CSC) -o ,image ,@(normalize objs) ,($ CSCLINKFLAGS) ,($ CSCLIBS)))))) |
---|
57 | |
---|
58 | (define (link-shared-library image . objs) |
---|
59 | (let ((image (suffix ($ SUFSHR) image)) |
---|
60 | (objs (normalize objs)) ) |
---|
61 | (depends "all" image) |
---|
62 | (depends image objs) |
---|
63 | (clean image) |
---|
64 | (actions |
---|
65 | "link" image |
---|
66 | (lambda _ (run (,($ LINK CC) ,($ SHAREDFLAGS) -o ,image ,@objs ,($ LINKFLAGS) ,($ LINKLIBS) )))) ) ) |
---|
67 | |
---|
68 | (define (scheme-extension ext . srcs) |
---|
69 | (let* ((ext (suffix ($ SUFDLSHR) ext)) |
---|
70 | (srcs (normalize srcs)) |
---|
71 | (os (map (cut suffix SUFOBJ <>) (normalize srcs)))) |
---|
72 | (objects srcs) |
---|
73 | (for-each (lambda (o) (set! (on o 'CSCFLAGS) (conc (on o 'CSCFLAGS ($ CSCFLAGS)) " -s "))) os) |
---|
74 | (depends "all" ext) |
---|
75 | (depends ext os) |
---|
76 | (clean ext) |
---|
77 | (actions |
---|
78 | "link scheme extension" ext |
---|
79 | ^{,($ CSC) -s -o ,ext ,@os ,($ CSCLINKFLAGS) ,($ CSCLIBS)} ) ) ) |
---|
80 | |
---|
81 | (define (link-library lib . objs) |
---|
82 | (let ((lib (suffix SUFLIB lib)) |
---|
83 | (objs (normalize objs)) ) |
---|
84 | (depends "all" lib) |
---|
85 | (depends lib objs) |
---|
86 | (clean lib) |
---|
87 | (actions |
---|
88 | "link library" lib |
---|
89 | (lambda _ |
---|
90 | (run (,($ AR) ,lib ,@objs)) |
---|
91 | (run (,($ RANLIB) ,lib)) ) ) ) ) |
---|
92 | |
---|
93 | (define (main-from-objects image . objs) |
---|
94 | (let ((objs (normalize objs))) |
---|
95 | (depends "all" image) |
---|
96 | (clean image objs) |
---|
97 | (depends image objs) |
---|
98 | (apply link image objs) ) ) |
---|
99 | |
---|
100 | (define (main image . srcs) |
---|
101 | (objects srcs) |
---|
102 | (let ((os (map (cut suffix SUFOBJ <>) (normalize srcs)))) |
---|
103 | (apply main-from-objects image os) ) ) |
---|
104 | |
---|
105 | (define (shared-library image . srcs) |
---|
106 | (objects srcs) |
---|
107 | (let ((os (map (cut suffix SUFOBJ <>) (normalize srcs)))) |
---|
108 | (apply link-shared-library image os) ) ) |
---|
109 | |
---|
110 | (define (scheme-main image . srcs) |
---|
111 | (let ((objs (suffix SUFOBJ srcs))) |
---|
112 | (objects srcs) |
---|
113 | (depends "all" image) |
---|
114 | (clean image objs) |
---|
115 | (depends image objs) |
---|
116 | (apply scheme-link image objs) ) ) |
---|
117 | |
---|
118 | (define (library lib . srcs) |
---|
119 | (let ((srcs (normalize srcs))) |
---|
120 | (let ((os (map (cut suffix SUFOBJ <>) srcs))) |
---|
121 | (apply link-library lib os) |
---|
122 | (for-each cc os srcs) ) ) ) |
---|
123 | |
---|
124 | (define (c-headers obj src) |
---|
125 | (when (->boolean SCANHEADERS) |
---|
126 | (when (file-exists? src) |
---|
127 | (depends obj (scan-headers src *VERBOSE* INCLUDEDIRS HDRSCAN))) ) ) |
---|
128 | |
---|
129 | (define (cc obj src) |
---|
130 | (let ((obj (suffix SUFOBJ obj))) |
---|
131 | (depends obj src) |
---|
132 | (clean obj) |
---|
133 | (c-headers obj src) |
---|
134 | (actions |
---|
135 | "cc" obj |
---|
136 | (lambda _ |
---|
137 | (run (,($ CC) ,($ CCFLAGS) ,($ OPTIM) -c ,src -o ,obj) ) ) ) ) ) |
---|
138 | |
---|
139 | (define (c++ obj src) |
---|
140 | (let ((obj (suffix SUFOBJ obj))) |
---|
141 | (depends obj src) |
---|
142 | (clean obj) |
---|
143 | (c-headers obj src) |
---|
144 | (actions |
---|
145 | "c++" obj |
---|
146 | (lambda _ |
---|
147 | (run (,($ C++) ,($ C++FLAGS) ,($ OPTIM) -c ,src -o ,obj) ) ) ) ) ) |
---|
148 | |
---|
149 | (define (csc obj src) |
---|
150 | (let ((obj (suffix SUFOBJ obj))) |
---|
151 | (depends obj src) |
---|
152 | (clean obj) |
---|
153 | (actions |
---|
154 | "csc" obj |
---|
155 | (lambda _ |
---|
156 | (run (,($ CSC) ,($ CSCFLAGS) ,($ CSCOPTIM) -c ,src -o ,obj) ) ) ) ) ) |
---|
157 | |
---|
158 | (define (object t s) |
---|
159 | (let ((ext (suffix s))) |
---|
160 | (cond ((not ext) (user-object t s)) |
---|
161 | ((member ext '("cc" "cpp" "c++" "cxx" "C")) (c++ t s)) |
---|
162 | ((string=? ext "c") (cc t s)) |
---|
163 | ((member ext '("scm" "ss")) (csc t s)) |
---|
164 | (else (user-object t s)) ) |
---|
165 | (depends t s) ) ) |
---|
166 | |
---|
167 | (define (objects . srcs) |
---|
168 | (for-each |
---|
169 | (lambda (s) |
---|
170 | (object (suffix SUFOBJ s) s) ) |
---|
171 | (normalize srcs)) ) |
---|
172 | |
---|
173 | (define clean |
---|
174 | (let ((cleaned '())) |
---|
175 | (lambda targets |
---|
176 | (set! cleaned (lset-union string=? cleaned (normalize targets))) |
---|
177 | (actions |
---|
178 | "clean" |
---|
179 | "clean" |
---|
180 | (lambda _ (run (rm -f ,@cleaned))) ) ) ) ) |
---|
181 | |
---|
182 | (define (user-object t s) |
---|
183 | (quit "unable to infer source type of ~a for target ~a" s t) ) |
---|
184 | |
---|
185 | (define (ensuredir dir) |
---|
186 | (unless (file-exists? dir) (run (mkdir -p ,dir)))) |
---|
187 | |
---|
188 | (define *installed-bins* '()) |
---|
189 | (define *installed-mans* '()) |
---|
190 | (define *installed-scripts* '()) |
---|
191 | (define *installed-files* '()) |
---|
192 | (define *installed-libs* '()) |
---|
193 | |
---|
194 | (notfile "all" "clean" "install" "install-bin" "install-lib" "install-file" "install-man" "install-shell") |
---|
195 | |
---|
196 | (actions |
---|
197 | "install-bin" |
---|
198 | (lambda _ |
---|
199 | (for-each |
---|
200 | (lambda (i) |
---|
201 | (let ((dir (car i)) |
---|
202 | (f (cdr i)) ) |
---|
203 | (ensuredir dir) |
---|
204 | (run (install -m ,(->mode ($ EXEMODE)) ,f ,dir)) ) ) |
---|
205 | *installed-bins*) ) ) |
---|
206 | |
---|
207 | (actions |
---|
208 | "install-file" |
---|
209 | (lambda _ |
---|
210 | (for-each |
---|
211 | (lambda (i) |
---|
212 | (let ((dir (car i)) |
---|
213 | (f (cdr i)) ) |
---|
214 | (ensuredir dir) |
---|
215 | (run (install -m ,(->mode ($ FILEMODE)) ,f ,dir)) ) ) |
---|
216 | *installed-files*) ) ) |
---|
217 | |
---|
218 | (actions |
---|
219 | "install-lib" |
---|
220 | (lambda _ |
---|
221 | (for-each |
---|
222 | (lambda (i) |
---|
223 | (let ((dir (car i)) |
---|
224 | (f (cdr i)) ) |
---|
225 | (ensuredir dir) |
---|
226 | (run (install -m ,(->mode ($ FILEMODE)) ,f ,dir)) |
---|
227 | (when (eq? 'macosx (software-version)) (run (,RANLIB ,(prefix dir f))) ) ) ) |
---|
228 | *installed-libs*) ) ) |
---|
229 | |
---|
230 | (actions |
---|
231 | "install-man" |
---|
232 | (lambda _ |
---|
233 | (for-each |
---|
234 | (lambda (i) |
---|
235 | (let ((dir (car i)) |
---|
236 | (f (cdr i)) ) |
---|
237 | (let ((dir2 (make-pathname dir (conc "man" (suffix f))))) |
---|
238 | (ensuredir dir2) |
---|
239 | (run (install -m ,(->mode ($ FILEMODE)) ,f ,dir2))) ) ) |
---|
240 | *installed-mans*) ) ) |
---|
241 | |
---|
242 | (actions |
---|
243 | "install-shell" |
---|
244 | (lambda _ |
---|
245 | (for-each |
---|
246 | (lambda (i) |
---|
247 | (let ((dir (car i)) |
---|
248 | (f (cdr i)) ) |
---|
249 | (ensuredir dir) |
---|
250 | (run (install -m ,(->mode ($ SHELLMODE)) ,f ,dir)) ) ) |
---|
251 | *installed-scripts*) ) ) |
---|
252 | |
---|
253 | (define (install-bin dir . srcs) |
---|
254 | (depends "install-bin" srcs) |
---|
255 | (set! *installed-bins* (lset-union equal? *installed-bins* (map (cut cons dir <>) (normalize srcs)))) ) |
---|
256 | |
---|
257 | (define (install-file dir . srcs) |
---|
258 | (depends "install-file" srcs) |
---|
259 | (set! *installed-files* (lset-union equal? *installed-files* (map (cut cons dir <>) (normalize srcs))) ) ) |
---|
260 | |
---|
261 | (define (install-lib dir . srcs) |
---|
262 | (depends "install-lib" srcs) |
---|
263 | (set! *installed-libs* (lset-union equal? *installed-libs* (map (cut cons dir <>) (normalize srcs))) ) ) |
---|
264 | |
---|
265 | (define (install-man dir . srcs) |
---|
266 | (depends "install-man" srcs) |
---|
267 | (set! *installed-mans* (lset-union equal? *installed-mans* (map (cut cons dir <>) (normalize srcs))))) |
---|
268 | |
---|
269 | (define (install-shell dir . srcs) |
---|
270 | (depends "install-shell" srcs) |
---|
271 | (set! *installed-scripts* (lset-union equal? *installed-scripts* (map (cut cons dir <>) (normalize srcs))))) |
---|
272 | |
---|
273 | (depends "install" "install-bin" "install-lib" "install-file" "install-man" "install-shell") |
---|