Skip to content

Commit 8dffd53

Browse files
committed
feat(builders): add platform/arch arguments and use socket-lib parseArgs
Changes: - Update node-smol-builder to accept --platform= and --arch= arguments for cross-compilation - Replace custom parseArgs functions with @socketsecurity/lib/argv/parse in both builders - Update publish-socketbin.yml workflow to pass platform/arch to smol builder - Add IS_WINDOWS constant and use TARGET_PLATFORM/TARGET_ARCH throughout smol builder Benefits: - Consistent argument parsing across all builders - Support for cross-compilation in smol builder (matching SEA builder) - Reduced code duplication with shared parseArgs utility - Better validation and error handling from socket-lib parseArgs
1 parent 6871d5a commit 8dffd53

File tree

3 files changed

+54
-69
lines changed

3 files changed

+54
-69
lines changed

.github/workflows/publish-socketbin.yml

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -129,36 +129,29 @@ jobs:
129129
restore-keys: |
130130
node-binary-${{ matrix.platform }}-${{ matrix.arch }}-
131131
132-
- name: Build binary
133-
id: build-binary
132+
- name: Build smol Node.js binary
133+
id: build-smol
134134
continue-on-error: true
135-
env:
136-
# Enable ccache for C++ compilation (Linux/macOS)
137-
CC: ${{ matrix.os != 'windows' && 'ccache gcc' || '' }}
138-
CXX: ${{ matrix.os != 'windows' && 'ccache g++' || '' }}
139-
# Use mold linker for faster linking (Linux only)
140-
LDFLAGS: ${{ matrix.os == 'linux' && '-fuse-ld=mold' || '' }}
141-
# Use ninja build system if available
142-
GYP_GENERATORS: ninja
143135
run: |
144-
pnpm run build --sea -- \
136+
echo "Building smol Node.js binary..."
137+
pnpm --filter @socketsecurity/node-smol-builder run build -- \
145138
--platform=${{ matrix.platform }} \
146139
--arch=${{ matrix.arch }}
147140
148-
- name: Build binary (fallback with clean rebuild)
149-
if: steps.build-binary.outcome == 'failure'
150-
env:
151-
# Enable ccache for C++ compilation (Linux/macOS)
152-
CC: ${{ matrix.os != 'windows' && 'ccache gcc' || '' }}
153-
CXX: ${{ matrix.os != 'windows' && 'ccache g++' || '' }}
154-
# Use mold linker for faster linking (Linux only)
155-
LDFLAGS: ${{ matrix.os == 'linux' && '-fuse-ld=mold' || '' }}
156-
# Use ninja build system if available
157-
GYP_GENERATORS: ninja
141+
- name: Build smol binary (fallback with clean rebuild)
142+
if: steps.build-smol.outcome == 'failure'
158143
run: |
159-
echo "Initial build failed, attempting clean rebuild with Node.js..."
160-
node packages/node-smol-builder/scripts/build.mjs --clean
161-
pnpm run build --sea -- \
144+
echo "Initial smol build failed, attempting clean rebuild..."
145+
pnpm --filter @socketsecurity/node-smol-builder run build -- \
146+
--platform=${{ matrix.platform }} \
147+
--arch=${{ matrix.arch }} \
148+
--clean
149+
150+
- name: Build SEA binary
151+
id: build-sea
152+
run: |
153+
echo "Building SEA binary..."
154+
pnpm --filter @socketsecurity/node-sea-builder run build -- \
162155
--platform=${{ matrix.platform }} \
163156
--arch=${{ matrix.arch }}
164157

packages/node-sea-builder/scripts/build.mjs

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import os from 'node:os'
3030
import path from 'node:path'
3131
import url from 'node:url'
3232

33+
import { parseArgs } from '@socketsecurity/lib/argv/parse'
3334
import { WIN32 } from '@socketsecurity/lib/constants/platform'
3435
import { safeDelete } from '@socketsecurity/lib/fs'
3536
import { logger } from '@socketsecurity/lib/logger'
@@ -628,45 +629,20 @@ if (typeof require !== 'undefined' && (!require.resolve || !require.resolve.path
628629
}
629630
}
630631

631-
/**
632-
* Parse command-line arguments.
633-
*/
634-
function parseArgs() {
635-
const args = process.argv.slice(2)
636-
const options = {}
637-
638-
for (const arg of args) {
639-
if (arg.startsWith('--platform=')) {
640-
const platform = arg.split('=')[1]
641-
if (platform) {
642-
options.platform = platform
643-
}
644-
} else if (arg.startsWith('--arch=')) {
645-
const arch = arg.split('=')[1]
646-
if (arch) {
647-
options.arch = arch
648-
}
649-
} else if (arg.startsWith('--node-version=')) {
650-
const nodeVersion = arg.split('=')[1]
651-
if (nodeVersion) {
652-
options.nodeVersion = nodeVersion
653-
}
654-
} else if (arg.startsWith('--output-dir=')) {
655-
const outputDir = arg.split('=')[1]
656-
if (outputDir) {
657-
options.outputDir = outputDir
658-
}
659-
}
660-
}
661-
662-
return options
663-
}
664-
665632
/**
666633
* Main build function.
667634
*/
668635
async function main() {
669-
const options = parseArgs()
636+
// Parse command-line arguments.
637+
const { values: options } = parseArgs({
638+
options: {
639+
arch: { type: 'string' },
640+
'node-version': { type: 'string' },
641+
'output-dir': { type: 'string' },
642+
platform: { type: 'string' },
643+
},
644+
strict: false,
645+
})
670646

671647
logger.log('Socket CLI Self-Executable Builder')
672648
logger.log('====================================')

packages/node-smol-builder/scripts/build.mjs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { dirname, join } from 'node:path'
6161
import { fileURLToPath } from 'node:url'
6262
import { brotliCompressSync, constants as zlibConstants } from 'node:zlib'
6363

64+
import { parseArgs } from '@socketsecurity/lib/argv/parse'
6465
import { whichBinSync } from '@socketsecurity/lib/bin'
6566
import { WIN32 } from '@socketsecurity/lib/constants/platform'
6667
import { logger } from '@socketsecurity/lib/logger'
@@ -147,12 +148,26 @@ async function execCapture(command, options = {}) {
147148
}
148149

149150
// Parse arguments.
150-
const args = process.argv.slice(2)
151-
const CLEAN_BUILD = args.includes('--clean')
152-
const RUN_VERIFY = args.includes('--verify')
153-
const RUN_TESTS = args.includes('--test')
154-
const RUN_FULL_TESTS = args.includes('--test-full')
155-
const AUTO_YES = args.includes('--yes') || args.includes('-y')
151+
const { values } = parseArgs({
152+
options: {
153+
arch: { type: 'string' },
154+
clean: { type: 'boolean' },
155+
platform: { type: 'string' },
156+
test: { type: 'boolean' },
157+
'test-full': { type: 'boolean' },
158+
verify: { type: 'boolean' },
159+
yes: { type: 'boolean', short: 'y' },
160+
},
161+
strict: false,
162+
})
163+
164+
const TARGET_PLATFORM = values.platform || platform()
165+
const TARGET_ARCH = values.arch || process.arch
166+
const CLEAN_BUILD = !!values.clean
167+
const RUN_VERIFY = !!values.verify
168+
const RUN_TESTS = !!values.test
169+
const RUN_FULL_TESTS = !!values['test-full'] || !!values.testFull
170+
const AUTO_YES = !!values.yes
156171

157172
// Configuration
158173
const NODE_VERSION = 'v24.10.0'
@@ -304,8 +319,9 @@ async function copySocketSecurityBootstrap() {
304319
}
305320

306321
const CPU_COUNT = cpus().length
307-
const IS_MACOS = platform() === 'darwin'
308-
const ARCH = process.arch
322+
const IS_MACOS = TARGET_PLATFORM === 'darwin'
323+
const IS_WINDOWS = TARGET_PLATFORM === 'win32'
324+
const ARCH = TARGET_ARCH
309325

310326
/**
311327
* Check if Node.js source has uncommitted changes.
@@ -963,7 +979,7 @@ async function main() {
963979

964980
// Check if we can use cached build (skip if --clean).
965981
if (!CLEAN_BUILD) {
966-
const distributionOutputBinary = join(BUILD_DIR, 'out', 'Distribution', platform === 'win32' ? 'node.exe' : 'node')
982+
const distributionOutputBinary = join(BUILD_DIR, 'out', 'Distribution', IS_WINDOWS ? 'node.exe' : 'node')
967983
const distBinary = join(ROOT_DIR, 'dist', 'socket-smol')
968984

969985
// Collect all source files that affect the build.
@@ -1770,7 +1786,7 @@ async function main() {
17701786
'.pkg-cache',
17711787
'v3.5',
17721788
)
1773-
const targetName = `built-${NODE_VERSION}-${platform()}-${ARCH}${IS_MACOS && ARCH === 'arm64' ? '-signed' : ''}`
1789+
const targetName = `built-${NODE_VERSION}-${TARGET_PLATFORM}-${ARCH}${IS_MACOS && ARCH === 'arm64' ? '-signed' : ''}`
17741790
const targetPath = join(pkgCacheDir, targetName)
17751791

17761792
printHeader('Installing to pkg Cache')

0 commit comments

Comments
 (0)