UNPKG

oqs.js

Version:

![N-API](https://img.shields.io/badge/node--api-stable-brightgreen)

55 lines (44 loc) 1.85 kB
const { execSync } = require('child_process'); const os = require('os'); const path = require('path'); const fs = require('fs'); const spawnSync = require('child_process').spawnSync; const LIBOQS_REPO = 'https://github.com/open-quantum-safe/liboqs.git'; const LIBOQS_DIR = path.join(__dirname, '..', 'liboqs_build'); const LIBOQS_STATIC = path.join(LIBOQS_DIR, 'build', 'lib', 'liboqs.a'); const BUILD_DIR = path.join(LIBOQS_DIR, 'build'); function checkBin(bin) { const which = spawnSync('which', [bin]); return which.status === 0; } if (!['darwin', 'linux'].includes(os.platform())) { console.error('\n[oqs.js] Error: liboqs build is only supported on macOS or Linux at this time.\n'); process.exit(1); } for (const tool of ['git', 'cmake', 'make']) { if (!checkBin(tool)) { console.error(`[oqs.js] Error: required command '${tool}' not found in PATH.`); process.exit(1); } } // Early-out if already built if (fs.existsSync(LIBOQS_STATIC)) { console.log('[oqs.js] liboqs already built.'); process.exit(0); } // Clone if not present if (!fs.existsSync(LIBOQS_DIR)) { console.log(`[oqs.js] Cloning liboqs into ${LIBOQS_DIR} ...`); execSync(`git clone --depth 1 ${LIBOQS_REPO} "${LIBOQS_DIR}"`, { stdio: 'inherit' }); } if (!fs.existsSync(BUILD_DIR)) fs.mkdirSync(BUILD_DIR, {recursive: true}); console.log('[oqs.js] Configuring liboqs with CMake ...'); execSync(`cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release ..`, { cwd: BUILD_DIR, stdio: 'inherit' }); console.log('[oqs.js] Building liboqs (this may take a few minutes) ...'); execSync('make -j' + os.cpus().length, { cwd: BUILD_DIR, stdio: 'inherit' }); if (fs.existsSync(LIBOQS_STATIC)) { console.log('[oqs.js] liboqs built successfully.\n'); } else { console.error('[oqs.js] ERROR: liboqs.a did not build as expected.\n'); process.exit(1); }