Debian Codegraph @

src/cross-compile-ios.scm

raw
1 #!/usr/bin/env gosh
2
3 (use gauche.generator)
4 (use gauche.process)
5 (use srfi.13)
6 (use file.util)
7
8 ;; Global parameters
9
10 ;; we build stuff under *builddir*/$ARCH
11 (define *builddir* "build-ios")
12
13 ;; the final output destination
14 (define *outdir* (build-path *builddir* "Gauche-iOS-core.framework"))
15
16 ;; gauche abi version
17 (define *abi-version* "0.9")
18
19 ;; SDK versions
20 (define *ios-deploy-target-version* "7.1")
21 ;(define *ios-build-sdk-version* "8.3") ;unused for now
22
23 ;;
24 ;; Architecture-dependent stuff
25 ;;
26 (define (devroot target)
27 (ecase target
28 [(armv7 armv7s arm64)
29 "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer"]
30 [(i386 x86_64)
31 "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer"]))
32
33 (define (sdkroot target)
34 (ecase target
35 [(armv7 armv7s arm64)
36 (build-path (devroot target)
37 "SDKs/iPhoneOS.sdk")]
38 [(i386 x86_64)
39 (build-path (devroot target)
40 "SDKs/iPhoneSimulator.sdk")]))
41
42 (define (environment-alist target)
43 (let ([dev (devroot target)]
44 [sdkdir (sdkroot target)]
45 [cflags-xtra (case target
46 [(i386 x86_64) " -DNO_DYLD_BIND_FULLY_IMAGE"]
47 [else ""])]
48 [sdk (case target
49 [(i386 x86_64) "iphonesimulator"]
50 [else "iphoneos"])])
51 `(("CC" . ,(process-output->string `("xcrun" "-find" "-sdk" ,sdk "clang")))
52 ("LD" . ,(build-path dev "usr/bin/ld"))
53 ("AR" . ,(process-output->string `("xcrun" "-find" "-sdk" ,sdk "ar")))
54 ("NM" . ,(build-path dev "usr/bin/nm"))
55 ("RANLIB" . ,(process-output->string `("xcrun" "-find" "-sdk" ,sdk "ranlib")))
56 ("CFLAGS" . ,#"-arch ~target -pipe -no-cpp-precomp -isysroot ~sdkdir -miphoneos-version-min=~*ios-deploy-target-version* -I~|sdkdir|/usr/include/ ~cflags-xtra")
57 ("LDFLAGS" . ,#"-framework CoreFoundation -L~|sdkdir|/usr/lib/"))))
58
59 (define (build-1 target)
60 (let* ([envs (map (^p #"~(car p)=~(cdr p)") (environment-alist target))]
61 [build (process-output->string "./config.guess")]
62 [builddir (build-path *builddir* (x->string target))]
63 [host (ecase target
64 [(armv7) "arm-apple-darwin7"]
65 [(armv7s) "arm-apple-darwin7s"]
66 [(arm64) "arm-apple-darwin8"]
67 [(i386) "i386-apple-darwin12.4"]
68 [(x86_64) "x86_64-apple-darwin12.4"])]
69 [configure-cmd `("/usr/bin/env" ,@envs
70 "../../configure"
71 ,#"--build=~build"
72 ,#"--host=~host"
73 "--with-dbm=no")]
74 [make-cmd `("/usr/bin/env" ,@envs "make" "-j")]
75 [static-cmd `("/usr/bin/env" ,@envs "make" "static")])
76 (define (run1 cmd dir)
77 (print cmd)
78 (let1 p (run-process cmd :wait #t :directory dir)
79 (unless (= (process-exit-status p) 0)
80 (error "Process failed:" cmd))))
81
82 (make-directory* builddir)
83 (run1 configure-cmd builddir)
84 (run1 make-cmd builddir)
85 (run1 static-cmd (build-path builddir "src"))))
86
87 (define (run-lipo)
88 (define (archive target)
89 (build-path *builddir* (x->string target) "src"
90 #"libgauche-static-~|*abi-version*|.a"))
91 (make-directory* *outdir*)
92 (let* ([cmd `("xcrun" "-sdk" "iphoneos" "lipo"
93 "-arch" "armv7s" ,(archive 'armv7s)
94 "-arch" "armv7" ,(archive 'armv7)
95 "-arch" "arm64" ,(archive 'arm64)
96 "-arch" "i386" ,(archive 'i386)
97 "-arch" "x86_64" ,(archive 'x86_64)
98 "-create"
99 "-output" ,(build-path *outdir*
100 #"libgauche-~|*abi-version*|.a"))]
101 [p (run-process cmd :wait #t)])
102 (unless (= (process-exit-status p) 0)
103 (error "Process failed:" cmd))))
104
105 (define (run-all)
106 (when (file-exists? *builddir*)
107 (remove-directory* *builddir*))
108 (build-1 'armv7)
109 (build-1 'armv7s)
110 (build-1 'arm64)
111 (build-1 'i386)
112 (build-1 'x86_64)
113 (run-lipo))
114
115 (define (copy-includes)
116 (define (copy file dst)
117 (let1 src (or (any (^p (and (file-exists? p) p))
118 (list (build-path "src" file)
119 (build-path *builddir* "i386/src" file)
120 (build-path *builddir* "x86_64/src" file)))
121 (error "Missing file:" file))
122 (print "Copying " (sys-normalize-pathname src :absolute #t) " -> " dst)
123 (with-output-to-file dst
124 (^[]
125 (do-generator [line (file->line-generator src)]
126 (print (regexp-replace-all*
127 line
128 #/#include <gc\.h>/ "#include <Gauche-iOS-core/gc.h>"
129 #/#\s*include <(gauche.*)\.h>/ "#include <Gauche-iOS-core/\\1.h>"
130 #/#\s*include \"(gc.*)\.h\"/ "#include \"Gauche-iOS-core/\\1.h\"")))))))
131 (make-directory* (build-path *outdir* "Headers/gauche"))
132 (dolist [hdr (mfvar-ref "src/Makefile.in" "INSTALL_HEADERS")]
133 (copy hdr (build-path *outdir* "Headers" (sys-basename hdr))))
134 (dolist [hdr (mfvar-ref "src/Makefile.in" "INSTALL_SUBHEADERS")]
135 (copy hdr (build-path *outdir* "Headers" hdr))))
136
137 (define (copy-resources)
138 (make-directory* (build-path *outdir* "Resources"))
139 (copy-file "src/Info.plist" (build-path *outdir* "Resources/Info.plist")))
140
141 (define (wire-stuff)
142 (sys-symlink #"libgauche-~|*abi-version*|.a"
143 (build-path *outdir* "Gauche-iOS-core")))
144
145 (define (preparation)
146 (sys-system "./DIST gen")
147 (sys-system "./configure")
148 (sys-system "(cd src; make pre-package)")
149 (sys-system "make distclean"))
150
151 (define (main args)
152 (preparation)
153 (run-all)
154 (copy-includes)
155 (copy-resources)
156 (wire-stuff)
157 0)
158
159 ;;
160 ;; Utilities
161 ;;
162 (define read-line/continuation
163 (gbuffer-filter (^[v s]
164 (if-let1 m (#/\\$/ v)
165 (values '() (cons (m 'before) s))
166 (values `(,(string-concatenate-reverse (cons v s))) '())))
167 '()
168 read-line
169 (^[s] `(,(string-concatenate-reverse s)))))
170
171 (define (mfvar-ref makefile var :optional default)
172 (if-let1 line (with-input-from-file makefile
173 (cute generator-find (string->regexp #"^~|var|\\b")
174 read-line/continuation))
175 (remove string-null?
176 (string-split
177 (rxmatch->string (string->regexp #"^~|var|\\s*=\\s*")
178 line 'after)
179 #[\s+]))
180 (if (undefined? default)
181 (errorf "Cannot find ~a definition in ~a" var makefile)
182 default)))