-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomnimax.nim
515 lines (420 loc) · 20 KB
/
omnimax.nim
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# MIT License
#
# Copyright (c) 2020-2021 Francesco Cameli
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import cligen, terminal, os, strutils
when not defined(Windows):
import osproc
#the CMakeLists doesn't need to be re-evaluated at each loop
include "omnimaxpkg/Static/CMakeLists.txt.nim"
#Package version is passed as argument when building. It will be constant and set correctly
const
NimblePkgVersion {.strdefine.} = ""
omnimax_ver = NimblePkgVersion
#-v / --version
let version_flag = "OmniMax - version " & $omnimax_ver & "\n(c) 2020-2021 Francesco Cameli"
#Default to the omni nimble folder, which should have it installed if omnimax has been installed correctly
const default_max_api_path = "~/.nimble/pkgs/omnimax-" & omnimax_ver & "/omnimaxpkg/deps/max-api"
#Extension for static lib
when defined(Windows):
const
static_lib_extension = ".lib"
lib_prepend = ""
else:
const
static_lib_extension = ".a"
lib_prepend = "lib"
when defined(Windows):
const max_object_extension = ".mxe64"
else:
const max_object_extension = ".mxo"
#It's the same in MacOS and Windows
const default_packages_path = "~/Documents/Max 8/Packages"
template printError(msg : string) : untyped =
setForegroundColor(fgRed)
writeStyled("ERROR: ", {styleBright})
setForegroundColor(fgWhite, true)
writeStyled(msg & "\n")
template printDone(msg : string) : untyped =
setForegroundColor(fgGreen)
writeStyled("\nSUCCESS: ", {styleBright})
setForegroundColor(fgWhite, true)
writeStyled(msg & "\n")
proc omnimax_single_file(is_multi : bool = false, fileFullPath : string, outDir : string = "", maxPath : string = "", architecture : string = "native", mc : bool = true, removeBuildFiles : bool = true) : int =
var
omniFile = splitFile(fileFullPath)
omniFileDir = omniFile.dir
omniFileName = omniFile.name
omniFileExt = omniFile.ext
#Check file first charcter, must be a capital letter
if not omniFileName[0].isUpperAscii:
omniFileName[0] = omniFileName[0].toUpperAscii()
#Check file extension
if not(omniFileExt == ".omni") and not(omniFileExt == ".oi"):
printError($fileFullPath & " is not an Omni file.")
return 1
var expanded_max_path : string
if maxPath == "":
expanded_max_path = default_max_api_path
else:
expanded_max_path = maxPath
expanded_max_path = expanded_max_path.normalizedPath().expandTilde().absolutePath()
#Check maxPath
if not expanded_max_path.dirExists():
printError("maxPath: " & $expanded_max_path & " does not exist.")
return 1
var expanded_out_dir : string
if outDir == "":
expanded_out_dir = default_packages_path
else:
expanded_out_dir = outDir
expanded_out_dir = expanded_out_dir.normalizedPath().expandTilde().absolutePath()
#Check outDir
if not expanded_out_dir.dirExists():
printError("outDir: " & $expanded_out_dir & " does not exist.")
return 1
#x86_64 and amd64 are aliases for x86-64
var real_architecture = architecture
if real_architecture == "x86_64" or real_architecture == "amd64":
real_architecture = "x86-64"
let
omni_max_object_name = omniFileName.toLowerAscii()
omni_max_object_name_tilde = omni_max_object_name & "_tilde"
omni_max_object_name_tilde_symbol = omni_max_object_name & "~"
#Full paths to the new file in omniFileName directory
let
#New folder named with the name of the Omni file
fullPathToNewFolder = $omniFileDir & "/" & $omni_max_object_name_tilde
#This is the Omni file copied to the new folder
fullPathToOmniFile = $fullPathToNewFolder & "/" & $omniFileName & $omniFileExt
#These are the .cpp, .sc and cmake files in new folder
fullPathToCppFile = $fullPathToNewFolder & "/" & $omni_max_object_name_tilde & ".cpp"
fullPathToCMakeFile = $fullPathToNewFolder & "/" & "CMakeLists.txt"
#This is the path to the compiled static lib
fullPathToStaticLib = $fullPathToNewFolder & "/" & $lib_prepend & $omniFileName & $static_lib_extension
#Create directory in same folder as .omni file
removeDir(fullPathToNewFolder)
createDir(fullPathToNewFolder)
#Copy omniFile to folder
copyFile(fileFullPath, fullPathToOmniFile)
# ================ #
# COMPILE NIM FILE #
# ================ #
#Compile nim file.
let omni_command = "omni \"" & $fileFullPath & "\" --silent:true --architecture:" & $real_architecture & " --lib:static --wrapper:omnimax_lang --performBits:64 --exportIO:true --outDir:\"" & $fullPathToNewFolder & "\""
#Windows requires powershell to figure out the .nimble path...
when defined(Windows):
let failedOmniCompilation = execShellCmd(omni_command)
else:
let failedOmniCompilation = execCmd(omni_command)
#error code from execCmd is usually some 8bit number saying what error arises. I don't care which one for now.
if failedOmniCompilation > 0:
removeDir(fullPathToNewFolder)
if is_multi:
printError("Failed compilation of '" & omniFileName & omniFileExt & "'.")
return 1
# ================ #
# RETRIEVE I / O #
# ================ #
let
fullPathToIOFile = fullPathToNewFolder & "/" & omniFileName & "_io.txt"
io_file = readFile(fullPathToIOFile)
io_file_seq = io_file.split('\n')
if io_file_seq.len != 11:
printError("Invalid io file: " & fullPathToIOFile & ".")
removeDir(fullPathToNewFolder)
return 1
let
num_inputs = parseInt(io_file_seq[0])
inputs_names_string = io_file_seq[1]
inputs_names = inputs_names_string.split(',')
inputs_defaults_string = io_file_seq[2]
inputs_defaults = inputs_defaults_string.split(',')
num_params = parseInt(io_file_seq[3])
params_names_string = io_file_seq[4]
params_names = params_names_string.split(',')
params_defaults_string = io_file_seq[5]
params_defaults = params_defaults_string.split(',')
num_buffers = parseInt(io_file_seq[6])
buffers_names_string = io_file_seq[7]
buffers_names = buffers_names_string.split(',')
buffers_defaults_string = io_file_seq[8]
buffers_defaults = buffers_defaults_string.split(',')
num_outputs = parseInt(io_file_seq[9])
outputs_names_string = io_file_seq[10]
outputs_names = outputs_names_string.split(',')
# ======= #
# SET I/O #
# ======= #
var
define_obj_name = "#define OBJ_NAME \"" & $omni_max_object_name_tilde_symbol & "\""
define_num_ins = "#define NUM_INS " & $num_inputs
define_num_params = "#define NUM_PARAMS " & $num_params
define_num_buffers = "#define NUM_BUFFERS " & $num_buffers
define_num_outs = "#define NUM_OUTS " & $num_outputs
const_inputs_names = "const std::array<std::string,NUM_INS> inputs_names = { "
const_inputs_defaults = "const std::array<double,NUM_INS> inputs_defaults = { "
const_params_names = "const std::array<std::string,NUM_PARAMS> params_names = { "
const_params_defaults = "const std::array<double,NUM_PARAMS> params_defaults = { "
const_buffers_names = "const std::array<std::string,NUM_BUFFERS> buffers_names = { "
const_buffers_defaults = "const std::array<std::string,NUM_BUFFERS> buffers_defaults = { "
const_outputs_names = "const std::array<std::string,NUM_OUTS> outputs_names = { "
if num_inputs == 0:
const_inputs_names.add("};")
const_inputs_defaults.add("};")
else:
for index, input_name in inputs_names:
let default_val = inputs_defaults[index]
if index == num_inputs - 1:
const_inputs_names.add("\"" & $input_name & "\" };")
const_inputs_defaults.add($default_val & " };")
else:
const_inputs_names.add("\"" & $input_name & "\", ")
const_inputs_defaults.add($default_val & ", ")
if num_params == 0:
const_params_names.add("};")
const_params_defaults.add("};")
else:
for index, param_name in params_names:
let default_val = params_defaults[index]
if index == num_params - 1:
const_params_names.add("\"" & $param_name & "\" };")
const_params_defaults.add($default_val & " };")
else:
const_params_names.add("\"" & $param_name & "\", ")
const_params_defaults.add($default_val & ", ")
if num_buffers == 0:
const_buffers_names.add("};")
const_buffers_defaults.add("};")
else:
for index, buffer_name in buffers_names:
let default_val = buffers_defaults[index]
if index == num_buffers - 1:
const_buffers_names.add("\"" & $buffer_name & "\" };")
const_buffers_defaults.add("\"" & $default_val & "\" };")
else:
const_buffers_names.add("\"" & $buffer_name & "\", ")
const_buffers_defaults.add("\"" & $default_val & "\", ")
if num_outputs == 0:
const_outputs_names.add("};")
else:
for index, output_name in outputs_names:
if index == num_outputs - 1:
const_outputs_names.add("\"" & $output_name & "\" };")
else:
const_outputs_names.add("\"" & $output_name & "\", ")
#This is the cpp file to overwrite! Need it at every iteration
include "omnimaxpkg/Static/Omni_PROTO.cpp.nim"
#Reconstruct the cpp file
OMNI_PROTO_CPP = (
$OMNI_PROTO_INCLUDES &
$define_obj_name & "\n" &
$define_num_ins & "\n" &
$define_num_params & "\n" &
$define_num_buffers & "\n" &
$define_num_outs & "\n" &
$const_inputs_names & "\n" &
$const_inputs_defaults & "\n" &
$const_params_names & "\n" &
$const_params_defaults & "\n" &
$const_buffers_names & "\n" &
$const_buffers_defaults & "\n" &
$const_outputs_names & "\n" &
$OMNI_PROTO_CPP
)
# =========== #
# WRITE FILES #
# =========== #
let
cppFile = open(fullPathToCppFile, fmWrite)
cmakeFile = open(fullPathToCMakeFile, fmWrite)
cppFile.write(OMNI_PROTO_CPP)
cmakeFile.write(OMNI_PROTO_CMAKE)
cppFile.close
cmakeFile.close
# ============ #
# BUILD object #
# ============ #
#Create build folder
removeDir($fullPathToNewFolder & "/build")
createDir($fullPathToNewFolder & "/build")
var cmake_cmd : string
when defined(Windows):
#Cmake wants a path in unix style, not windows! Replace "/" with "\"
let fullPathToNewFolder_Unix = fullPathToNewFolder.replace("\\", "/")
let fullPathToMaxApi_Unix = expanded_max_path.replace("\\", "/")
cmake_cmd = "cmake -G \"MinGW Makefiles\" -DCMAKE_MAKE_PROGRAM:PATH=\"make\" -DOMNI_BUILD_DIR=\"" & $fullPathToNewFolder_Unix & "\" -DOMNI_LIB_NAME=\"" & $omni_file_name & "\" -DC74_MAX_API_DIR=\"" & $fullPathToMaxApi_Unix & "\" -DCMAKE_BUILD_TYPE=Release -DBUILD_MARCH=" & $real_architecture & " .."
else:
cmake_cmd = "cmake -DOMNI_BUILD_DIR=\"" & $fullPathToNewFolder & "\" -DOMNI_LIB_NAME=\"" & $omni_file_name & "\" -DC74_MAX_API_DIR=\"" & $expanded_max_path & "\" -DCMAKE_BUILD_TYPE=Release -DBUILD_MARCH=" & $real_architecture & " .."
#cd into the build directory
setCurrentDir(fullPathToNewFolder & "/build")
#Execute CMake
when defined(Windows):
let failedCmake = execShellCmd(cmake_cmd)
else:
let failedCmake = execCmd(cmake_cmd)
#error code from execCmd is usually some 8bit number saying what error arises. I don't care which one for now.
if failedCmake > 0:
printError("Unsuccessful cmake generation of the object file \"" & $omni_max_object_name_tilde & ".cpp\".")
removeDir(fullPathToNewFolder)
return 1
#make command
let compilation_cmd = "cmake --build . --config Release"
when defined(Windows):
let failedCompilation = execShellCmd(compilation_cmd)
else:
let failedCompilation = execCmd(compilation_cmd)
if failedCompilation > 0:
printError("Unsuccessful compilation the object file \"" & $omni_max_object_name_tilde & ".cpp\".")
removeDir(fullPathToNewFolder)
return 1
let
external_name_and_extension = $omni_max_object_name_tilde_symbol & $max_object_extension
fullPathToBuiltExternal = $fullPathToNewFolder & "/" & $external_name_and_extension
#On windows, the external is getting called lib... strip lib from the name
when defined(Windows):
moveFile($fullPathToNewFolder & "/lib" & $omni_max_object_name_tilde_symbol & $max_object_extension, fullPathToBuiltExternal)
#cd back to the original folder where omni file is
setCurrentDir(omniFileDir)
# ============== #
# CREATE MC FILE #
# ============== #
var fullPathToMCFile : string
if(mc):
fullPathToMCFile = $fullPathToNewFolder & "/" & $omni_max_object_name_tilde & ".txt"
writeFile(fullPathToMCFile, "max objectfile mc." & $omni_max_object_name_tilde_symbol & " mc.wrapper~ " & $omni_max_object_name_tilde_symbol)
# ======================= #
# COPY TO PACKAGES FOLDER #
# ======================= #
#Remove build dir
removeDir(fullPathToNewFolder & "/build")
#Remove tmp dir
when defined(Windows):
removeDir(fullPathToNewFolder & "/tmp")
#Create externals dir and move the external over there
let
fullPathToExternalsDir = fullPathToNewFolder & "/externals"
fullPathToMovedExternal = fullPathToNewFolder & "/externals/" & $external_name_and_extension
createDir(fullPathToExternalsDir)
moveFile(fullPathToBuiltExternal, fullPathToMovedExternal)
#Create init dir and copy the mc txt file
if(mc):
let
fullPathToInitDir = fullPathToNewFolder & "/init"
fullPathToMovedMCFile = fullPathToInitDir & "/" & $omni_max_object_name_tilde_symbol & ".txt"
createDir(fullPathToInitDir)
moveFile(fullPathToMCFile, fullPathToMovedMCFile)
#If removeBuildFiles, remove all sources and static libraries compiled
if removeBuildFiles:
let fullPathToOmniHeaderFile = fullPathToNewFolder & "/omni.h"
removeFile(fullPathToOmniHeaderFile)
removeFile(fullPathToCppFile)
removeFile(fullPathToOmniFile)
removeFile(fullPathToCMakeFile)
removeFile(fullPathToIOFile)
removeFile(fullPathToStaticLib)
#Or, create a src dir and move them all there
else:
let
fullPathToSrcDir = fullPathToNewFolder & "/src"
fullPathToOmniHeaderFile = fullPathToNewFolder & "/omni.h"
createDir(fullPathToSrcDir)
moveFile(fullPathToOmniHeaderFile, fullPathToSrcDir & "/omni.h")
moveFile(fullPathToCppFile, fullPathToSrcDir & "/" & $omni_max_object_name_tilde & ".cpp")
moveFile(fullPathToOmniFile, fullPathToSrcDir & "/" & $omniFileName & $omniFileExt)
moveFile(fullPathToCMakeFile, fullPathToSrcDir & "/CMakeLists.txt")
moveFile(fullPathToIOFile, fullPathToSrcDir & "/IO.txt")
moveFile(fullPathToStaticLib, fullPathToSrcDir & "/lib" & $omni_file_name & $static_lib_extension)
#Copy to outDir folder
let
fullPathToOutDir_tilde = $expanded_out_dir & "/" & $omni_max_object_name_tilde #omnisaw_tilde
fullPathToOutDir = $expanded_out_dir & "/" & $omni_file_name #OmniSaw
#Remove temp folder used for compilation only if it differs from outDir (otherwise, it's gonna delete the actual folder)
if fullPathToOutDir_tilde != fullPathToNewFolder:
#Remove previous folders if there were
removeDir(fullPathToOutDir_tilde)
removeDir(fullPathToOutDir)
#Copy new one
copyDir(fullPathToNewFolder, fullPathToOutDir_tilde)
#Rename the folder to omni_name (instead of omni_name_tilde)
moveDir(fullPathToOutDir_tilde, fullPathToOutDir)
#Remove temp folder used for compilation
removeDir(fullPathToNewFolder)
printDone("The '" & $omni_max_object_name_tilde_symbol & "' object has been correctly built and installed in \"" & $expanded_out_dir & "\".")
return 0
proc omnimax(files : seq[string], outDir : string = "", maxPath : string = "", architecture : string = "native", mc : bool = true, removeBuildFiles : bool = true) : int =
#no files provided, print --version
if files.len == 0:
echo version_flag
return 0
for omniFile in files:
#Get full extended path
let omniFileFullPath = omniFile.normalizedPath().expandTilde().absolutePath()
#If it's a file or list of files, compile it / them
if omniFileFullPath.fileExists():
if files.len == 1:
return omnimax_single_file(false, omniFileFullPath, outDir, maxPath, architecture, mc, removeBuildFiles)
else:
if omnimax_single_file(true, omniFileFullPath, outDir, maxPath, architecture, mc, removeBuildFiles) > 0:
return 1
#If it's a dir, compile all .omni/.oi files in it
elif omniFileFullPath.dirExists():
for kind, dirFile in walkDir(omniFileFullPath):
if kind == pcFile:
let
dirFileFullPath = dirFile.normalizedPath().expandTilde().absolutePath()
dirFileExt = dirFileFullPath.splitFile().ext
if dirFileExt == ".omni" or dirFileExt == ".oi":
if omnimax_single_file(true, dirFileFullPath, outDir, maxPath, architecture, mc, removeBuildFiles) > 0:
return 1
else:
printError($omniFileFullPath & " does not exist.")
return 1
return 0
#Workaround to pass custom version
clCfg.version = version_flag
#Remove --help-syntax
clCfg.helpSyntax = ""
#Arguments string
let arguments = "Arguments:\n Omni file(s) or folder."
#Ignore clValType
clCfg.hTabCols = @[ clOptKeys, #[clValType,]# clDflVal, clDescrip ]
#Dispatch the omnimax function as the CLI one
dispatch(
omnimax,
#Remove "Usage: ..."
noHdr = true,
#Custom options printing
usage = version_flag & "\n\n" & arguments & "\n\nOptions:\n$options",
short = {
"version" : 'v',
"mc" : 'm',
"maxPath" : 'p'
},
help = {
"help" : "CLIGEN-NOHELP",
"version" : "CLIGEN-NOHELP",
"outDir" : "Output directory. Defaults to the Max 8 Packages' path: \"" & $default_packages_path & "\".",
"maxPath" : "Path to the max-api folder. Defaults to the one in OmniMax's dependencies: \"" & $default_max_api_path & ".\"" ,
"architecture" : "Build architecture.",
"mc" : "Build with mc support.",
"removeBuildFiles" : "Remove source files used for compilation from outDir."
}
)