120 lines
3.6 KiB
Plaintext
120 lines
3.6 KiB
Plaintext
var os = use('os')
|
|
var fd = use('fd')
|
|
|
|
if (!fd.is_dir('.cell'))
|
|
fd.mkdir('.cell')
|
|
|
|
var shop = use('shop')
|
|
var config = shop.load_config()
|
|
|
|
function get_flags(config, platform, key) {
|
|
var flags = ''
|
|
if (config.compilation && config.compilation[key]) {
|
|
flags += config.compilation[key]
|
|
}
|
|
if (config.compilation && config.compilation[platform] && config.compilation[platform][key]) {
|
|
if (flags != '') flags += ' '
|
|
flags += config.compilation[platform][key]
|
|
}
|
|
return flags
|
|
}
|
|
|
|
var files = fd.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 last_slash = file.lastIndexOf('/')
|
|
if (last_slash != -1) {
|
|
var dir = '.cell/build/' + file.substring(0, last_slash)
|
|
if (!fd.is_dir(dir))
|
|
obj_file = '.cell/build/' + safe_path + '.o'
|
|
} else {
|
|
obj_file = '.cell/build/' + file + '.o'
|
|
}
|
|
|
|
objects.push(obj_file)
|
|
|
|
var needs_compile = true
|
|
if (fd.is_file(obj_file)) {
|
|
try {
|
|
var src_stat = fd.stat(file)
|
|
var obj_stat = fd.stat(obj_file)
|
|
var src_time = src_stat.modtime ?? src_stat.mtime
|
|
var obj_time = obj_stat.modtime ?? obj_stat.mtime
|
|
if (src_time != null && obj_time != null && obj_time >= src_time) {
|
|
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
|
|
var cflags = get_flags(config, os.platform(), 'CFLAGS')
|
|
if (cflags != '') cmd += ' ' + cflags
|
|
|
|
var ret = os.system(cmd)
|
|
if (ret != 0) {
|
|
log.console("Compilation failed for " + file)
|
|
$_.stop()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check if there are any object files to link
|
|
if (objects.length == 0) {
|
|
log.console("No object files found, skipping linking.")
|
|
log.console("Build complete: no shared library created")
|
|
$_.stop()
|
|
return
|
|
}
|
|
|
|
// 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 (!fd.is_dir('.cell/local'))
|
|
fd.mkdir('.cell/local')
|
|
|
|
var lib_name = '.cell/local/local' + lib_ext
|
|
log.console("Linking " + lib_name)
|
|
|
|
var ldflags = get_flags(config, os.platform(), 'LDFLAGS')
|
|
if (ldflags != '') link_flags += ' ' + ldflags
|
|
|
|
var link_cmd = 'cc ' + link_flags + ' ' + objects.join(' ') + ' -lcell -lc -lc++ -o ' + lib_name
|
|
var ret = os.system(link_cmd)
|
|
|
|
if (ret != 0) {
|
|
log.console("Linking failed")
|
|
$_.stop()
|
|
}
|
|
|
|
log.console("Build complete: " + lib_name)
|
|
|
|
$_.stop() |