forked from racket/zuo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zuo
105 lines (94 loc) · 4.45 KB
/
build.zuo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#lang zuo
(require "local/image.zuo")
;; Exports `targets` and also defines a `main` submodule
;; that handles command-line arguments and builds a target
;; in a `make`-like way
(provide-targets targets-at)
;; The `targets-at` function generates targets, and `to-dir` determines
;; the build directory --- so these targets could be used by another
;; build script that wants the output in a subdirectory, for example
(define (targets-at at-dir [vars (hash)])
;; The `configure` script writes configuration info to "Makefile", so
;; use that if it's available, or use defaults otherwise
(define Makefile (at-dir "Makefile"))
(define config-in
(cond
[(file-exists? Makefile) (config-file->hash Makefile)]
;; no `configure`-generated `Makefile`, so use defaults
[(eq? (hash-ref (runtime-env) 'system-type) 'unix)
(hash 'INSTALL_PREFIX "/usr/local"
'CC "cc"
'CFLAGS "-O2")]
[else
(hash 'INSTALL_PREFIX "C:\\Program Files\\Zuo"
'CC "cl.exe"
'CFLAGS "/O2")]))
(define config (foldl (lambda (key config)
(hash-set config key (hash-ref vars key)))
config-in
(hash-keys vars)))
(define install-prefix (hash-ref config 'INSTALL_PREFIX))
;; Get a target for "image_zuo.c" from `image.zuo`
(define image_zuo.c
(image-target (hash 'output (at-dir "image_zuo.c")
'libs (map string->symbol (string-split (hash-ref config 'EMBED_LIBS "zuo")))
'keep-collects? #t)))
;; We'll build two executables; they are the same except for the
;; embedded libary path, so we have a target maker parameterized
;; over that choice
(define (exe-target name lib-path)
(target (at-dir (add-exe name))
(lambda (path token)
(rule (list image_zuo.c
(input-data-target 'config config)
(quote-module-path))
(lambda ()
(define l (split-path path))
(when (car l) (mkdir-p (car l)))
(c-compile path
(list (target-path image_zuo.c))
(config-merge config
'CPPFLAGS
(string->shell (~a "-DZUO_LIB_PATH=" lib-path)))))))))
(define (add-exe name)
(if (eq? (hash-ref (runtime-env) 'system-type) 'windows)
(~a name ".exe")
name))
;; The library path gets used as a C string constant, which isn't
;; trivial because there are likely to be backslashes on Windows
(define (as-c-string path) (~s path)) ; probably a good enough approximation
;; The two executable targets
(define zuo-to-run (exe-target "to-run/zuo" (as-c-string (find-relative-path "to-run"
(at-source "lib")))))
(define zuo-to-install (exe-target "to-install/zuo" (as-c-string (build-path install-prefix "lib"))))
;; A phony target to build both executables, which we'll list first
;; so it's used as the default target
(define zuos-to-run-and-install
(target 'zuos-to-run-and-install
(lambda (token)
(phony-rule (list zuo-to-run zuo-to-install)
void))))
;; A phony target to install
(define install
(target 'install
(lambda (token)
(phony-rule (list zuo-to-install)
(lambda ()
(define (say-copy cp a b)
(displayln (~a "copying " a " to " b))
(cp a b))
(mkdir-p install-prefix)
(mkdir-p (build-path install-prefix "bin"))
(define dest-exe (build-path install-prefix "bin" "zuo"))
(when (file-exists? dest-exe) (rm dest-exe)) ; needed for macOS
(say-copy cp (target-name zuo-to-install) dest-exe)
(mkdir-p (build-path install-prefix "lib"))
(say-copy cp*
(at-source "lib" "zuo")
(build-path install-prefix "lib" "zuo")))))))
;; Return all the targets
(list zuos-to-run-and-install
image_zuo.c
zuo-to-run
zuo-to-install
install))