Files
cell/scripts/compile.ce
2025-11-30 16:37:36 -06:00

110 lines
3.2 KiB
Plaintext

var os = use('os')
var io = use('cellfs')
// 2. Prepare output directory
if (!io.exists('.cell')) {
io.mkdir('.cell')
}
var shop = use('shop')
var config = shop.load_config()
// 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') || file.endsWith('.cpp')) {
var path_no_ext = file.substring(0, file.lastIndexOf('.'))
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)
var needs_compile = true
if (io.exists(obj_file)) {
try {
var src_stat = io.stat(file)
var obj_stat = io.stat(obj_file)
if (obj_stat.modtime >= src_stat.modtime) {
needs_compile = false
}
} catch (e) {
needs_compile = true
}
}
if (!needs_compile) {
log.console("Skipping " + file + " (up to date)")
} else {
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
if (config.compilation)
cmd += ` ${config.compilation.CFLAGS} `
if (config.compilation[os.platform()]?.CFLAGS)
cmd += ` ${config.compilation[os.platform()].CFLAGS} `
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)
if (config.compilation)
link_flags += ` ${config.compilation.LDFLAGS} `
if (config.compilation[os.platform()])
link_flags += ` ${config.compilation[os.platform()].LDFLAGS} `
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()