UNPKG

libmaxminddb

Version:

Node.js bindings for libmaxminddb (to read MaxMind DB with the best performance - using memory-mapped file!)

77 lines (70 loc) 2.35 kB
const fs = require('fs'); const path = require('path'); const {pkgConfigPath, useGlobalLibmaxminddb, vendorPath} = require('./install/vars-build'); const {shellEsc, spawnSync} = require('./install/utils'); const os = process.platform === 'win32' ? 'win' : process.platform === 'darwin' ? 'mac' : 'linux'; const pkgConfig = (...args) => { return spawnSync(`PKG_CONFIG_PATH=${shellEsc(pkgConfigPath)} pkg-config ${args.map(shellEsc).join(' ')}`).trim(); }; const target = { target_name: 'maxminddb', sources: [ 'src/errors.cc', 'src/index.cc', 'src/mmdb_reader.cc', 'src/util/async_worker.cc', 'src/util/string_copy.cc', 'src/util/v8_helpers.cc', ], include_dirs: [path.dirname(require.resolve('nan'))], link_settings: { library_dirs: [], libraries: [], }, defines: [], configurations: { Debug: {}, Release: {}, }, }; if (useGlobalLibmaxminddb) { target.include_dirs.push(pkgConfig('--cflags-only-I', 'libmaxminddb').substr(2)); target.link_settings.libraries.push(pkgConfig('--libs', 'libmaxminddb')); } else { target.include_dirs.push(vendorPath); target.link_settings.library_dirs.push(vendorPath); if (os === 'win') { target.link_settings.libraries.push('maxminddb.lib', 'Ws2_32.lib'); } else { target.link_settings.libraries.push('-lmaxminddb'); } } if (os === 'linux') { let useCxx11Abi; if (useGlobalLibmaxminddb) { const lib = `${pkgConfig('--variable', 'libdir', 'libmaxminddb')}/libmaxminddb.so`; useCxx11Abi = spawnSync( `if readelf -Ws ${shellEsc(lib)} | c++filt | grep -qF __cxx11; then echo 1; else echo 0; fi` ).trim(); } else { useCxx11Abi = 1; } target.defines.push(`_GLIBCXX_USE_CXX11_ABI=${useCxx11Abi}`); } if (os === 'win') { target.configurations.Debug.msvs_settings = { VCCLCompilerTool: { RuntimeLibrary: '3', // /MDd }, }; target.configurations.Release.msvs_settings = { VCCLCompilerTool: { RuntimeLibrary: '2', // /MD }, }; } const binding = { __NOTE: 'This file was generated by binding.js', targets: [target], }; fs.writeFileSync(path.join(__dirname, 'binding.gyp'), JSON.stringify(binding, null, 2), 'utf8');