-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-unix-module.tscript
370 lines (326 loc) · 10.1 KB
/
make-unix-module.tscript
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
module
use: make-module.tscript
function: createPkgConfCommandUnix($command)
$commandPrefix = ""
$commandSuffix = ""
if ($$.application::OS == "Windows-MINGW")
# TODO: works only for standard installation
$commandPrefix = "C:\\msys64\\usr\\bin\\bash -c \"export PKG_CONFIG_PATH=/mingw64/lib/pkgconfig:/mingw64/share/pkgconfig && "
$commandSuffix = "\""
end
return ($commandPrefix + $command + $commandSuffix)
end
function: determineLibraryFlagsUnix($libraries, &$librariesIncludes, &$librariesLdFlags, $optional)
$librariesIncludesArray = []
$librariesLdFlagsArray = []
forEach($library in $libraries)
# includes
$command = createPkgConfCommandUnix("pkgconf --cflags " + $library)
# execute
$exitCode = $$.application::EXITCODE_SUCCESS
$error = null
$result = application.execute($command, $exitCode, $error)
if ($exitCode != $$.application::EXITCODE_SUCCESS)
console.printLine("pkgconf exited with exit code " + $exitCode + ", see error: " + $error)
if ($optional == false)
console.printLine()
application.exit($$.application::EXITCODE_FAILURE)
end
return(false)
end
# FIXME: String::trim($result->replace("\n", "")) should be $result->replace("\n", "")->trim()
$libraryIncludesArray = String::tokenize(String::trim($result->replace("\n", "")), " ")
forEach($libraryInclude in $libraryIncludesArray)
if ($librariesIncludesArray->contains($libraryInclude) == true)
continue
end
$librariesIncludesArray[] = $libraryInclude->trim()
end
# ld flags
$command = createPkgConfCommandUnix("pkgconf --libs " + $library)
# execute
$exitCode = $$.application::EXITCODE_SUCCESS
$error = null
$result = application.execute($command, $exitCode, $error)
if ($exitCode != $$.application::EXITCODE_SUCCESS)
console.printLine("pkgconf exited with exit code " + $exitCode + ", see error: " + $error)
if ($optional == false)
console.printLine()
application.exit($$.application::EXITCODE_FAILURE)
end
return(false)
end
# FIXME: String::trim($result->replace("\n", "")) should be $result->replace("\n", "")->trim()
$libraryLdFlagsArray = String::tokenize(String::trim($result->replace("\n", "")), " ")
forEach($libraryLdFlag in $libraryLdFlagsArray)
if ($librariesLdFlagsArray->contains($libraryLdFlag) == true)
continue
end
$librariesLdFlagsArray[] = $libraryLdFlag->trim()
end
end
$librariesIncludes = $librariesIncludesArray->concatenate(" ")
$librariesIncludes = $librariesIncludes->trim()
$librariesLdFlags = $librariesLdFlagsArray->concatenate(" ")
$librariesLdFlags = $librariesLdFlags->trim()
return(true)
end
function: buildLibraryUnix($name, $libraries, $definitions, $includes, $ldFlags, $files, $optional)
console.printLine("Building library: " + $name)
console.printLine()
$librariesIncludes = ""
$librariesLdFlags = ""
if (determineLibraryFlagsUnix($libraries, $librariesIncludes, $librariesLdFlags, $optional) == false)
return
end
$concurrency = integer(math.ceil(concurrency.getHardwareThreadCount() / 1.5))
console.printLine("concurrency: " + $concurrency)
console.printLine("definitions: " + $definitions)
console.printLine("includes: " + $includes)
console.printLine("ld flags: " + $ldFlags)
console.printLine("libraries: " + $libraries)
console.printLine("libraries includes: " + $librariesIncludes)
console.printLine("libraries ld flags: " + $librariesLdFlags)
console.printLine()
$objectPath = "obj"
$libraryPath = "lib"
$cxx = "g++"
if ($$.application::OS == "FreeBSD" ||
$$.application::OS == "MacOSX" ||
$$.application::OS == "OpenBSD")
$cxx = "clang++"
end
$includes = $librariesIncludes + " " + $includes
$cFlags = "-fPIC -g -O3 -pipe -MMD -MP -DNDEBUG" + " " + $definitions
$cxxFlags = "-fPIC -g -O3 -pipe -MMD -MP -DNDEBUG -std=c++2a -Wno-enum-constexpr-conversion" + " " + $definitions
$libraryExtension = ".so"
if ($$.application::OS == "MacOSX")
$libraryExtension = ".dylib"
elseif ($$.application::OS == "Windows-MINGW" || $$.application::OS == "Windows-MSC")
$libraryExtension = ".dll"
end
try
# create paths
createPathRecusively($objectPath)
createPathRecusively($libraryPath)
# compile each compilation units
$commands = []
$compilationUnits = ""
forEach($file in $files)
#
$fileObjectPath = filesystem.getPathName($file)
# create file object path
createPathRecusively($objectPath + "/" + $fileObjectPath)
#
$compilationUnit = $objectPath + "/" + filesystem.removeFileExtension($file) + ".obj"
if ($compilationUnits->isEmpty() == false)
$compilationUnits = $compilationUnits + " "
end
$compilationUnits = $compilationUnits + $compilationUnit
#
$timeStampFile = 0
$timeStampCompilationUnit = 0;
if (filesystem.exists($file) == true)
$timeStampFile = filesystem.getFileTimeStamp(
filesystem.getPathName($file),
filesystem.getFileName($file)
)
end
if (filesystem.exists($compilationUnit) == true)
$timeStampCompilationUnit = filesystem.getFileTimeStamp(
filesystem.getPathName($compilationUnit),
filesystem.getFileName($compilationUnit)
)
end
if ($timeStampFile != 0 &&
$timeStampCompilationUnit != 0 &&
$timeStampCompilationUnit > $timeStampFile)
continue
end
# compile
$command = null
if ($file->endsWith(".c") == true)
$command =
$cxx +
" " +
"-x c" +
" " +
$cFlags +
" " +
$librariesIncludes +
" " +
$includes +
" " +
" -c -o " +
$compilationUnit +
" " +
$file
elseif ($file->endsWith(".cpp") == true)
$command =
$cxx +
" " +
$cxxFlags +
" " +
$librariesIncludes +
" " +
$includes +
" " +
" -c -o " +
$compilationUnit +
" " +
$file
else
console.printLine("Dont know how to build: " + $file)
end
#
if ($command == null)
continue
end
#
$commands[] = $command
end
# execute commands
if (application.executeMultiple($commands, $concurrency) == false)
console.printLine()
console.printLine("Not all files have been compiled. Stopping")
if ($optional == false)
console.printLine()
application.exit($$.application::EXITCODE_FAILURE)
end
else
$mingwLinkSuffix = ""
if ($$.application::OS == "Windows-MINGW")
$mingwLinkSuffix = "-Wl,--out-implib," + $libraryPath + "/" + $name + $libraryExtension + ".a" + " " + "-limagehlp"
end
# link
$command =
$cxx +
" " +
"-shared" +
" " +
$compilationUnits +
" " +
"-o " +
$libraryPath + "/" + $name + $libraryExtension +
" " +
$librariesLdFlags +
" " +
$ldFlags +
" " +
$mingwLinkSuffix
console.printLine($command)
# execute
$exitCode = $$.application::EXITCODE_SUCCESS
$error = null
$result = application.execute($command, $exitCode, $error)
if ($exitCode != $$.application::EXITCODE_SUCCESS)
console.printLine($cxx + " exited with exit code " + $exitCode + ", see error: " + $error)
if ($optional == false)
console.printLine()
application.exit($$.application::EXITCODE_FAILURE)
end
end
end
catch ($exception)
console.printLine("An error occurred: " + $exception)
end
console.printLine()
end
function: buildExecutablesUnix($name, $libraries, $definitions, $includes, $ldFlags, $files, $optional)
console.printLine("Building executables: " + $name)
console.printLine()
$librariesIncludes = ""
$librariesLdFlags = ""
if (determineLibraryFlagsUnix($libraries, $librariesIncludes, $librariesLdFlags) == false)
return
end
$concurrency = integer(math.ceil(concurrency.getHardwareThreadCount() / 1.5))
console.printLine("concurrency: " + $concurrency)
console.printLine("definitions: " + $definitions)
console.printLine("includes: " + $includes)
console.printLine("ld flags: " + $ldFlags)
console.printLine("libraries: " + $libraries)
console.printLine("libraries includes: " + $librariesIncludes)
console.printLine("libraries ld flags: " + $librariesLdFlags)
$binaryPath = "bin"
$cxx = "g++"
if ($$.application::OS == "FreeBSD" ||
$$.application::OS == "MacOSX" ||
$$.application::OS == "OpenBSD")
$cxx = "clang++"
end
$includes = $librariesIncludes + " " + $includes
$cxxFlags = "-fPIC -g -O3 -pipe -MMD -MP -DNDEBUG -std=c++2a -Wno-enum-constexpr-conversion" + " " + $definitions
try
# create paths
createPathRecusively($binaryPath)
# compile each executable
$commands = []
forEach($file in $files)
#
$sourceFile = $file
#
if ($file->startsWith("src/") == true)
# FIXME: $file->substring("src/"->getSize()) does not work here!
$file = $file->substring(String("src/")->getSize())
end
$fileObjectPath = filesystem.getPathName($file)
# create file object path
createPathRecusively($binaryPath + "/" + $fileObjectPath)
#
$executableFile = $binaryPath + "/" + $file
$executableFile = $executableFile->replace("-main.cpp", "")
#
$timeStampFile = 0
$timeStampCompilationUnit = 0;
if (filesystem.exists($sourceFile) == true)
$timeStampFile = filesystem.getFileTimeStamp(
filesystem.getPathName($sourceFile),
filesystem.getFileName($sourceFile)
)
end
if (filesystem.exists($executableFile) == true)
$timeStampCompilationUnit = filesystem.getFileTimeStamp(
filesystem.getPathName($executableFile),
filesystem.getFileName($executableFile)
)
end
if ($timeStampFile != 0 &&
$timeStampCompilationUnit != 0 &&
$timeStampCompilationUnit > $timeStampFile)
continue
end
# compile and link
$command =
$cxx +
" " +
$cxxFlags +
" " +
$librariesIncludes +
" " +
$includes +
" " +
"-o " +
$executableFile +
" " +
$sourceFile +
" " +
$librariesLdFlags +
" " +
$ldFlags
#
$commands[] = $command
end
# execute commands
if (application.executeMultiple($commands, $concurrency) == false)
console.printLine("Not all files have been compiled. Stopping")
if ($optional == false)
console.printLine()
application.exit($$.application::EXITCODE_FAILURE)
end
end
catch ($exception)
console.printLine("An error occurred: " + $exception)
end
console.printLine()
end