Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 67 additions & 7 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,86 @@
"<!@(node -p \"require('./scripts/lib.js').includePath\")",
"<!@(node -p \"require('node-addon-api').include\")"
],
"libraries": [
"<!@(node -p \"require('./scripts/lib.js').libPath\")"
],
"sources": [
"src/convert.cpp",
"src/main.cpp"
],
"defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
"cflags": [
"-ffunction-sections",
"-fdata-sections",
"-fvisibility=hidden",
"-Os",
"-flto",
"-fno-rtti"
],
"cflags_cc": [
"-ffunction-sections",
"-fdata-sections",
"-fvisibility=hidden",
"-Os",
"-flto",
"-fno-rtti"
],
"ldflags": [
"-flto",
"-Wl,--gc-sections",
"-Wl,--exclude-libs,ALL"
],
"xcode_settings": {
"MACOSX_DEPLOYMENT_TARGET": "10.10",
"OTHER_LDFLAGS": [
"-Wl,-S",
"-Wl,-dead_strip",
"-Wl,-rpath,@loader_path",
"-Wl,-rpath,<!@(node -p \"require('./scripts/lib.js').libDir\")"
],
"DEPLOYMENT_POSTPROCESSING": "YES",
"STRIP_INSTALLED_PRODUCT": "YES",
"GCC_SYMBOLS_PRIVATE_EXTERN": "YES",
"GCC_OPTIMIZATION_LEVEL": "s",
"LLVM_LTO": "YES"
},
"conditions": [
["OS == 'mac'", {
"libraries": ["-lddwaf"],
"library_dirs": ["<!@(node -p \"require('./scripts/lib.js').libDir\")"],
"ldflags": [
"-Wl,-rpath,@loader_path",
"-Wl,-rpath,<!@(node -p \"require('./scripts/lib.js').libDir\")"
]
}],
["OS == 'linux'", {
'ldflags': ['-Wl,--rpath=\$$ORIGIN']
"libraries": ["-lddwaf"],
"library_dirs": ["<!@(node -p \"require('./scripts/lib.js').libDir\")"],
'ldflags': ['-Wl,--rpath=\$$ORIGIN', '-Wl,--strip-all', '-Wl,--gc-sections', '-Wl,--exclude-libs,ALL']
}],
["OS == 'win'", {
"libraries": ["Ws2_32.lib"],
"libraries": ["<!@(node -p \"require('./scripts/lib.js').libPath\")", "Ws2_32.lib"],
"cflags": [
"/WX"
]
"/WX",
"/O1",
"/GL",
"/Gy",
"/GR-"
],
"ldflags": [
"/LTCG",
"/OPT:REF",
"/OPT:ICF"
],
"msvs_settings": {
"VCCLCompilerTool": {
"Optimization": 1,
"FavorSizeOrSpeed": 2,
"WholeProgramOptimization": "true"
},
"VCLinkerTool": {
"OptimizeReferences": 2,
"EnableCOMDATFolding": 2,
"LinkTimeCodeGeneration": 1
}
}
}]
]
}]
Expand Down
25 changes: 19 additions & 6 deletions scripts/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ const pkg = require('../package.json')
const platform = process.env.PLATFORM || os.platform()
const arch = process.env.ARCH || os.arch()

const libNames = {
darwin: 'libddwaf.a',
win32: 'ddwaf_static.lib',
// Runtime filenames (what the addon will dlopen at runtime)
const runtimeLibNames = {
darwin: 'libddwaf.dylib',
win32: 'ddwaf.dll',
linux: 'libddwaf.so'
}

// Link-time filenames (what the linker should link against)
const linkLibNames = {
darwin: runtimeLibNames.darwin,
win32: 'ddwaf.lib',
linux: runtimeLibNames.linux
}

const dirNames = {
darwin: {
arm64: 'darwin-arm64',
Expand All @@ -40,11 +48,16 @@ function getDirName () {
}

const dirname = getDirName()
const libName = libNames[platform]
const runtimeLibName = runtimeLibNames[platform]
const linkLibName = linkLibNames[platform]
const basename = path.join(__dirname, '..', 'libddwaf', dirname)

module.exports = {
includePath: path.join(basename, 'include').split('\\').join('\\\\'),
libPath: path.join(basename, 'lib', libName).split('\\').join('\\\\'),
libName
// Used by the linker during build
libPath: path.join(basename, 'lib', linkLibName).split('\\').join('\\\\'),
libDir: path.join(basename, 'lib').split('\\').join('\\\\'),
libName: platform === 'darwin' ? 'ddwaf' : (platform === 'win32' ? 'ddwaf' : 'ddwaf'),
// Runtime library filename (for packaging/copying alongside the addon)
libFile: runtimeLibName
}
18 changes: 15 additions & 3 deletions scripts/postbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@
const fs = require('fs')
const path = require('path')
const os = require('os')
const { libPath, libName } = require('./lib')
const { libPath, libName, libDir, libFile } = require('./lib')

const platform = os.platform()
const arch = process.env.ARCH || os.arch()
const libc = process.env.LIBC || ''

const prebuildDir = path.join(__dirname, '..', 'prebuilds', `${platform}${libc}-${arch}`)
const runtimeLibPath = path.join(libDir, libFile)

if (platform === 'linux' && fs.existsSync(prebuildDir)) {
fs.copyFileSync(libPath, path.join(prebuildDir, libName))
if (fs.existsSync(prebuildDir)) {
// Copy the runtime library next to the addon so rpath/@loader_path or loader can find it
// - linux: libddwaf.so
// - darwin: libddwaf.dylib
// - win32: ddwaf.dll
if (fs.existsSync(runtimeLibPath)) {
fs.copyFileSync(runtimeLibPath, path.join(prebuildDir, libFile))
}

// Ensure link-time library is also available on linux (same as runtime) for completeness
if (platform === 'linux' && fs.existsSync(libPath)) {
fs.copyFileSync(libPath, path.join(prebuildDir, libName))
}
}