-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathRakefile
248 lines (208 loc) · 8.27 KB
/
Rakefile
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
TRANQUIL = '/usr/local/tranquil'
LLVM = "#{TRANQUIL}/llvm"
CC = "clang"
CXX = "clang++"
LD = CC
RAGEL = "/usr/local/bin/ragel"
LEMON = "/usr/local/bin/lemon"
LLVMCONFIG = "#{LLVM}/bin/llvm-config"
MAXARGS = 32 # The number of block dispatchers to compile
BUILD_DIR = 'Build'
RUNTIMELIB = 'libtranquil.a'
CODEGENLIB = 'libtranquil_codegen.dylib'
LEXER_OUTPATH = "#{BUILD_DIR}/lex.mm"
PARSER_H_OUTPATH = "#{BUILD_DIR}/parse.h"
PARSER_OUTPATH = "#{BUILD_DIR}/parse.mm"
CXXFLAGS = {
:release => [
'-Wno-objc-root-class',
'-Wno-objc-protocol-method-implementation',
'-Wno-cast-of-sel-type',
'-I`pwd`/Source',
"-I#{TRANQUIL}/include",
'-I`pwd`/Build',
"-I#{TRANQUIL}/gmp/include",
'-O3',
].join(' '),
:development => [
'-DDEBUG',
'-Wno-objc-root-class',
'-Wno-objc-protocol-method-implementation',
'-Wno-cast-of-sel-type',
'-I`pwd`/Source',
"-I#{TRANQUIL}/include",
'-I`pwd`/Build',
"-I#{TRANQUIL}/gmp/include",
'-O0',
'-g',
#'-DTQ_PROFILE',
#'--analyze'
].join(' ')
}
TOOL_LDFLAGS = [
"-L#{BUILD_DIR}",
'-ObjC++',
'-lobjc',
"-rpath #{TRANQUIL}/lib",
'-ltranquil',
'-ltranquil_codegen',
'-lffi',
'-lreadline',
#'-lprofiler',
'-all_load',
'-framework Foundation',
'-g'
].join(' ')
PATHMAP = 'build/%n.o'
STUB_OUTPATH = 'Build/block_stubs.m'
STUB_SCRIPT = 'Source/Tranquil/gen_stubs.rb'
STUB_H_OUTPATH = 'Build/TQStubs.h'
STUB_H_SCRIPT = 'Source/Tranquil/gen_stubs_header.rb'
MSGSEND_SOURCES = { :x86_64 => 'Source/Tranquil/Runtime/msgsend.x86_64.s',
:i386 => 'Source/Tranquil/Runtime/msgsend.i386.s',
:armv7 => 'Source/Tranquil/Runtime/msgsend.arm.s' }
MSGSEND_OUT = 'Build/msgsend.o'
RUNTIME_SOURCES = FileList['Source/Tranquil/BridgeSupport/*.m*'].add('Source/Tranquil/Runtime/*.m*').add('Source/Tranquil/Shared/*.m*').add(STUB_OUTPATH)
RUNTIME_O_FILES = RUNTIME_SOURCES.pathmap(PATHMAP)
RUNTIME_O_FILES << MSGSEND_OUT
CODEGEN_SOURCES = FileList['Source/Tranquil/CodeGen/**/*.m*'] + [LEXER_OUTPATH]
CODEGEN_O_FILES = CODEGEN_SOURCES.pathmap(PATHMAP)
HEADERS = FileList['Source/Tranquil/BridgeSupport/*.h'].add('Source/Tranquil/Dispatch/*.h').add('Source/Tranquil/Runtime/*.h').add('Source/Tranquil/Shared/*.h').add('Source/Tranquil/CodeGen/**/*.h')
HEADER_PATHMAP = "#{TRANQUIL}/include/Tranquil/%-1d/%f"
HEADERS_OUT = [STUB_H_OUTPATH] + HEADERS.pathmap(HEADER_PATHMAP)
LEXER_SOURCE = "Source/Tranquil/lex.rl"
PARSER_SOURCE = "Source/Tranquil/parse.y"
ARC_FILES = ['Source/Tranquil/Runtime/TQWeak.m']
MAIN_SOURCE = 'Source/main.m'
MAIN_OUTPATH = 'Build/main.o'
@buildMode = :development
def compile(file, flags=CXXFLAGS[@buildMode], cc=CXX, arch="x86_64", ios=false)
if ios == true then
flags = flags + ' -DTQ_NO_BIGNUM' # libgmp is not well supported on iOS
flags << " -I#{TRANQUIL}/libffi-ios/include"
if arch == :armv7 then
flags << ' -miphoneos-version-min=6.0'
flags << ' -isysroot `xcrun --sdk iphoneos --show-sdk-path`'
else
flags << ' -fobjc-abi-version=2 -fobjc-legacy-dispatch'
flags << ' -mios-simulator-version-min=6.0'
flags << ' -isysroot `xcrun --sdk iphonesimulator --show-sdk-path`'
end
else
flags = flags + ' -mmacosx-version-min=10.7'
flags << " -I#{TRANQUIL}/libffi/include"
flags << " -isysroot `xcrun --sdk macosx --show-sdk-path`"
flags << " -F`xcrun --sdk macosx --show-sdk-path`/System/Library/Frameworks/ApplicationServices.framework/Frameworks"
end
cmd = "#{cc} #{file[:in].join(' ')} -arch #{arch} #{flags} -c -o #{file[:out]}"
unless file[:in].size == 1 and file[:in][0].end_with? '.s'
cmd << " -fobjc-arc" if ARC_FILES.member? file[:in].first
cmd << " -std=c++11 -stdlib=libc++ -ObjC++" if cc == CXX
cmd << " -ObjC" if cc == CC
end
sh cmd
end
HEADERS.each { |header|
file header.pathmap(HEADER_PATHMAP) => header do |f|
sh "mkdir -p #{f.name.pathmap('%d')}"
sh "cp #{header} #{f.name}"
end
}
file LEXER_OUTPATH => LEXER_SOURCE do |f|
sh "#{RAGEL} #{LEXER_SOURCE} -o #{LEXER_OUTPATH}"
end
file PARSER_OUTPATH => PARSER_SOURCE do |f|
sh "#{LEMON} #{PARSER_SOURCE}"
sh "mv Source/Tranquil/parse.c #{PARSER_OUTPATH}"
sh "mv Source/Tranquil/parse.h #{PARSER_H_OUTPATH}"
end
file STUB_OUTPATH => STUB_SCRIPT do |f|
sh "ruby #{STUB_SCRIPT} #{MAXARGS} > #{STUB_OUTPATH}"
end
file STUB_H_OUTPATH => STUB_H_SCRIPT do |f|
sh "ruby #{STUB_H_SCRIPT} #{MAXARGS} > Build/TQStubs.h"
sh "mkdir -p #{TRANQUIL}/include/Tranquil/Runtime"
sh "cp Build/TQStubs.h #{TRANQUIL}/include/Tranquil/Runtime/TQStubs.h"
end
MSGSEND_SOURCES.each do |arch, asm|
file MSGSEND_OUT => asm do |f|
paths = []
MSGSEND_SOURCES.each do |arch, asm|
outPath = MSGSEND_OUT + ".#{arch.to_s}"
paths << outPath
compile({:in => [asm], :out => outPath}, '', CC, arch, arch != :x86_64)
end
sh "lipo -create -output #{MSGSEND_OUT} #{paths.join(' ')}"
end
end
RUNTIME_SOURCES.each { |src|
file src.pathmap(PATHMAP) => src do |f|
compile({:in => f.prerequisites, :out => f.name + ".x64"}, CXXFLAGS[@buildMode], CC, :x86_64)
compile({:in => f.prerequisites, :out => f.name + ".i386"}, CXXFLAGS[@buildMode], CC, :i386, true)
compile({:in => f.prerequisites, :out => f.name + ".arm"}, CXXFLAGS[@buildMode], CC, :armv7, true)
sh "lipo -create -output #{f.name} #{f.name}.x64 #{f.name}.i386 #{f.name}.arm"
end
}
CODEGEN_SOURCES.each { |src|
file src.pathmap(PATHMAP) => src do |f|
flags = CXXFLAGS[@buildMode] + " `#{LLVMCONFIG} --cxxflags`"
compile({:in => f.prerequisites, :out => f.name + ".x64"}, flags, CXX, :x86_64)
compile({:in => f.prerequisites, :out => f.name + ".i386"}, flags, CXX, :i386, true)
sh "lipo -create -output #{f.name} #{f.name}.x64 #{f.name}.i386"
end
}
file :build_dir do
sh "mkdir -p #{File.dirname(__FILE__)}/Build"
end
file :libtranquil => HEADERS_OUT + RUNTIME_O_FILES do |t|
sh "libtool -static -o #{BUILD_DIR}/#{RUNTIMELIB} #{RUNTIME_O_FILES} #{TRANQUIL}/gmp/lib/libgmp.a #{TRANQUIL}/libffi/lib/libffi.a #{TRANQUIL}/libffi-ios/lib/libffi.a"
sh "mkdir -p #{TRANQUIL}/lib"
sh "cp Build/#{RUNTIMELIB} #{TRANQUIL}/lib"
end
file :libtranquil_codegen => [PARSER_OUTPATH] + CODEGEN_O_FILES do |t|
sh "#{CC} -install_name \"@rpath/#{CODEGENLIB}\" -dynamiclib -undefined suppress -flat_namespace -o #{BUILD_DIR}/#{CODEGENLIB} #{CODEGEN_O_FILES} `#{LLVMCONFIG} --libfiles core jit nativecodegen armcodegen bitwriter ipo option` #{LLVM}/lib/libclang*.a -framework Foundation"
sh "mkdir -p #{TRANQUIL}/lib"
sh "cp Build/#{CODEGENLIB} #{TRANQUIL}/lib"
end
def _buildMain
sh "#{CXX} #{MAIN_SOURCE} #{CXXFLAGS[@buildMode]} -isysroot `xcrun --sdk macosx --show-sdk-path` -c -o #{MAIN_OUTPATH}"
end
file MAIN_OUTPATH => MAIN_SOURCE do |t|
end
file :tranquil => [:libtranquil, :libtranquil_codegen, MAIN_OUTPATH] do |t|
_buildMain
sh "#{LD} #{TOOL_LDFLAGS} #{MAIN_OUTPATH} -rpath #{TRANQUIL}/lib -o #{BUILD_DIR}/tranquil"
end
task :setReleaseOpts do
p "Release build"
@buildMode = :release
end
task :run => [:default] do
sh "#{BUILD_DIR}/tranquil"
end
task :gdb => [:default] do
sh "gdb #{BUILD_DIR}/tranquil"
end
task :lldb => [:default] do
sh "lldb #{BUILD_DIR}/tranquil"
end
task :clean do
sh "rm -rf Build/*"
end
task :install => [:tranquil] do
end
def _install
sh "mkdir -p #{TRANQUIL}/bin"
sh "mkdir -p #{TRANQUIL}/share"
sh "cp Source/Tranquil/tqmain.m #{TRANQUIL}/share"
sh "cp Build/tranquil #{TRANQUIL}/bin"
sh "cp Tools/tqlive.tq #{TRANQUIL}/bin/tqlive"
sh "#{TRANQUIL}/bin/tranquil Tools/tqc.tq Tools/tqc.tq -o #{TRANQUIL}/bin/tqc"
sh "#{TRANQUIL}/bin/tqc Tools/repl.tq -lreadline -L#{TRANQUIL}/lib -ltranquil_codegen -lstdc++ -o #{TRANQUIL}/bin/tqrepl -rpath #{TRANQUIL}/lib"
end
task :default => [:build_dir, :tranquil] do |t|
_install
end
task :release => [:clean, :setReleaseOpts, :build_dir, :tranquil] do |t|
_install
end