Files
cell/scripts/compile.ce

88 lines
2.3 KiB
Plaintext

// cell compile
var os = use('os')
var io = use('cellfs')
var project_name = cell.config.project.name
if (!project_name) {
log.console("Error: project.name not found in cell.toml")
$_.stop()
}
log.console("Building project: " + project_name)
// 2. Prepare output directory
if (!io.exists('.cell')) {
io.mkdir('.cell')
}
// 3. Find source files
var files = io.enumerate('.', true)
var objects = []
for (var i = 0; i < files.length; i++) {
var file = files[i]
if (file.endsWith('.c')) {
var path_no_ext = file.substring(0, file.length - 2)
var safe_path = path_no_ext.replace(/\//g, '_').replace(/\\/g, '_')
if (safe_path.startsWith('._')) safe_path = safe_path.substring(2)
var use_name = 'js_local_' + safe_path + '_use'
var obj_file = '.cell/build/' + file + '.o'
var obj_dir = io.realdir(obj_file)
var last_slash = file.lastIndexOf('/')
if (last_slash != -1) {
var dir = '.cell/build/' + file.substring(0, last_slash)
if (!io.exists(dir)) {
obj_file = '.cell/build/' + safe_path + '.o'
}
} else {
obj_file = '.cell/build/' + file + '.o'
}
objects.push(obj_file)
log.console("Compiling " + file + " -> " + obj_file)
log.console(`export name is ${use_name}`)
// Compile command
// cc -fPIC -c <file> -O3 -DCELL_USE_NAME=<name> -o <obj_file>
var cmd = 'cc -fPIC -c ' + file + ' -O3 -DCELL_USE_NAME=' + use_name + ' -o ' + obj_file
var ret = os.system(cmd)
if (ret != 0) {
log.console("Compilation failed for " + file)
$_.stop()
}
}
}
// 4. Link shared library
var lib_ext = '.so'
var link_flags = '-shared'
if (os.platform() == 'macOS') {
lib_ext = '.dylib'
link_flags = '-shared'
} else if (os.platform() == 'Windows') {
lib_ext = '.dll'
}
// Ensure .cell/local exists
if (!io.exists('.cell/local')) {
io.mkdir('.cell/local')
}
var lib_name = '.cell/local/local' + lib_ext
log.console("Linking " + lib_name)
var link_cmd = 'cc ' + link_flags + ' ' + objects.join(' ') + ' -lcell -o ' + lib_name
var ret = os.system(link_cmd)
if (ret != 0) {
log.console("Linking failed")
$_.stop()
}
log.console("Build complete: " + lib_name)
$_.stop()