-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmake.rkt
159 lines (139 loc) · 4.43 KB
/
make.rkt
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#lang racket/base
(require racket/system
racket/file
racket/port
racket/string
racket/runtime-path
racket/set
file/glob
"runtime-paths.rkt"
"common.rkt")
(provide (all-defined-out))
(define cc "clang++")
(define ldflags+= "-shared -Wl,-undefined,dynamic_lookup")
(define cflags+= "-fPIC")
(define cxxflags+= "-fno-rtti -O3")
(define lflags+=
(string-join
(list "-lclangAST"
" -lclangASTMatchers"
" -lclangAnalysis"
" -lclangBasic"
" -lclangDriver"
" -lclangEdit"
" -lclangFrontend"
" -lclangFrontendTool"
" -lclangLex"
" -lclangParse"
" -lclangSema"
" -lclangEdit"
" -lclangRewrite"
" -lclangRewriteFrontend"
" -lclangStaticAnalyzerFrontend"
" -lclangStaticAnalyzerCheckers"
" -lclangStaticAnalyzerCore"
" -lclangSerialization"
" -lclangToolingCore"
" -lclangTooling"
" -lclangFormat"
" -lLLVM")))
(define (get-llvm-config)
(define llvm-config
(with-output-to-string
(λ () (system "llvm-config --cxxflags --ldflags --libs --libfiles --system-libs"))))
(string-replace llvm-config "\n" " "))
(define clang-include
(list
"clang/AST/AST.h"
"clang/AST/ASTConsumer.h"
"clang/AST/RecursiveASTVisitor.h"
"clang/Frontend/FrontendPluginRegistry.h"
"clang/Frontend/CompilerInstance.h"
"clang/Frontend/FrontendActions.h"
"llvm/Support/raw_ostream.h"
"clang/Frontend/FrontendActions.h"
"clang/Tooling/CommonOptionsParser.h"
"clang/Tooling/Tooling.h"))
(define (clang-headers-exist?)
(unless (find-executable-path "llvm-config")
#f)
(define llvm-include-dir
(string-trim
(with-output-to-string
(λ () (system "llvm-config --includedir")))))
(printf "llvm include dir: ~a\n" llvm-include-dir)
(for/and ([h clang-include])
(printf "~a\n" (build-path llvm-include-dir h))))
(define (libclang-exists?)
(case (system-type)
[(macosx) #t]
[else (system "ldconfig -p | grep -q libclang")]))
(define (clang++-exists?)
(find-executable-path "clang++"))
;; Like string join but works with any
;; formattable type
(define (format-append . a)
(define fmt
(string-join
(build-list (length a) (λ (x) "~a"))))
(apply format (cons fmt a)))
(define-syntax-rule (make-ffi-shared-lib out-path in ...)
(let ([make-plugin-cmd
(format-append cc "-v" in ... (get-llvm-config)
lflags+= "-o" out-path cflags+= cxxflags+= ldflags+=)])
(unless
(and (file-exists? out-path)
(for/and ([i (list in ...)])
(and (file-exists? i)
(timestamp<? i out-path))))
(unless (system make-plugin-cmd)
(error "clang-export: could not compile shared library")))
out-path))
(define (make-native-libs)
(when (and (file-exists? dynamic-ffi-core_rkt.so)
(for/and ([i (list dynamic-ffi.c clang-export.so)])
(file-exists? i)
(timestamp<? i dynamic-ffi-core_rkt.so)))
(void))
(define cwd (current-directory))
(current-directory shared-object-dir)
(when
(or (not (file-exists? dynamic-ffi_3m.o)) (timestamp<? dynamic-ffi_3m.o dynamic-ffi.c))
(define ctool-xform (format "raco ctool --xform ~a" dynamic-ffi.c))
(define ctool-3m (format "raco ctool --3m --cc ~a" dynamic-ffi.3m.c))
(printf "~a\n" ctool-xform)
(system ctool-xform)
(printf "making object\n~a\n" ctool-3m)
(system ctool-3m))
(current-directory cwd))
(define (raco-link)
(define ctool-ld
(format "raco ctool --3m --ld ~a ~a ~a"
dynamic-ffi-core_rkt.so dynamic-ffi_3m.o clang-export.so))
(printf "making extension\n~a\n" ctool-ld)
(system ctool-ld))
(define (make-dynamic-ffi-pkg)
(make-ffi-shared-lib wrap-fork.so wrap-fork.c)
(make-ffi-shared-lib clang-plugin.so clang-plugin.cc)
(make-ffi-shared-lib clang-export.so clang-export.c clang-plugin.so wrap-fork.so)
(make-native-libs))
(define (make-native-dirs)
(unless (directory-exists? compiled-dir)
(make-directory* compiled-dir))
(unless (directory-exists? dynamic-extension-dir)
(make-directory* dynamic-extension-dir))
(unless (directory-exists? compiled-native-dir)
(system (format "ln -s ~a ~a" shared-object-dir compiled-native-dir))))
(define (post-installer x)
(cond [(and (clang++-exists?)
(clang-headers-exist?)
(libclang-exists?))
(and
(make-native-dirs)
(make-dynamic-ffi-pkg)
(raco-link))]
[else
(warn-dependencies)
#f]))
(module+ main
(post-installer #t))