Skip to content

Commit e48f032

Browse files
committed
feat: support build options via npmrc
1 parent ea5f35c commit e48f032

File tree

4 files changed

+155
-71
lines changed

4 files changed

+155
-71
lines changed

.github/workflows/CI.yml

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ jobs:
2222
- x64
2323
cpp_arch:
2424
- x64
25-
ARCH:
25+
arch:
2626
- x64
27-
zmq_draft:
28-
- false
2927
docker:
3028
- ""
3129
docker_cmd:
@@ -35,30 +33,26 @@ jobs:
3533
- os: windows-2022
3634
node_version: 18
3735
node_arch: x86
38-
ARCH: x86
36+
arch: x86
3937
cpp_arch: amd64_x86
40-
zmq_draft: false
4138

4239
# - os: windows-2022
4340
# node_version: 18
4441
# node_arch: x64
45-
# ARCH: arm64
42+
# arch: arm64
4643
# cpp_arch: amd64_arm64
47-
# zmq_draft: false
4844

4945
- os: macos-13
5046
node_version: 18
5147
node_arch: x64
52-
ARCH: x86_64
48+
arch: x86_64
5349
cpp_arch: x64
54-
zmq_draft: false
5550

5651
- os: macos-14
5752
node_version: 18
5853
node_arch: arm64
59-
ARCH: arm64
54+
arch: arm64
6055
cpp_arch: amd64_arm64
61-
zmq_draft: false
6256

6357
# Alpine
6458
- os: ubuntu-22.04
@@ -69,14 +63,13 @@ jobs:
6963
build.prebuild
7064
node_version: 18
7165
node_arch: x64
72-
ARCH: x64
66+
arch: x64
7367
cpp_arch: x64
74-
zmq_draft: false
7568

7669
env:
77-
ZMQ_DRAFT: ${{ matrix.zmq_draft }}
78-
ZMQ_SHARED: false
79-
ARCH: ${{ matrix.ARCH }}
70+
npm_config_zmq_draft: false
71+
npm_config_zmq_shared: false
72+
npm_config_arch: ${{ matrix.arch }}
8073
steps:
8174
- uses: actions/checkout@v4
8275

@@ -88,10 +81,10 @@ jobs:
8881
./build/
8982
key:
9083
"cache-OS:${{ matrix.os }}-arch:${{ matrix.node_arch
91-
}}-ZMQ_DRAFT:${{ matrix.zmq_draft }}-Node:${{ matrix.node_version
92-
}}-${{ hashFiles('./package.json') }}"
84+
}}-ZMQ_DRAFT:${{ env.npm_config_zmq_draft }}-Node:${{
85+
matrix.node_version }}-${{ hashFiles('./package.json') }}"
9386
restore-keys: |
94-
"cache-OS:${{ matrix.os }}-arch:${{ matrix.node_arch }}-ZMQ_DRAFT:${{ matrix.zmq_draft }}-Node:${{ matrix.node_version }}-"
87+
"cache-OS:${{ matrix.os }}-arch:${{ matrix.node_arch }}-ZMQ_DRAFT:${{ env.npm_config_zmq_draft }}-Node:${{ matrix.node_version }}-"
9588
9689
- name: Setup Cpp
9790
if: ${{ !matrix.docker }}
@@ -115,12 +108,12 @@ jobs:
115108
architecture: ${{ matrix.node_arch }}
116109

117110
- name: Install Mac-OS x86_64 Dependencies
118-
if: ${{ contains(matrix.os, 'macos') && matrix.ARCH == 'x86_64' }}
111+
if: ${{ contains(matrix.os, 'macos') && matrix.arch == 'x86_64' }}
119112
run: |
120113
brew install libsodium gnutls
121114
122115
- name: Install Mac-OS arm64 Dependencies
123-
if: ${{ contains(matrix.os, 'macos') && matrix.ARCH == 'arm64' }}
116+
if: ${{ contains(matrix.os, 'macos') && matrix.arch == 'arm64' }}
124117
run: |
125118
brew uninstall libsodium --force --ignore-dependencies
126119
source ./script/macos-arm-deps.sh
@@ -155,7 +148,7 @@ jobs:
155148
if: "${{ contains(matrix.os, 'ubuntu') && !matrix.docker }}"
156149
run: pnpm run lint-test
157150

158-
- name: Test (Debug)
151+
- name: Test
159152
if: ${{ !matrix.docker }}
160153
uses: nick-fields/retry@v3
161154
with:

README.md

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,76 @@ source:
8181
- CMake 2.8+
8282
- curl
8383

84-
To install from source:
84+
To install from source, specify `build_from_source=true` in a `.npmrc` file
8585

86-
```sh
87-
npm install [email protected] --build-from-source
86+
```
87+
build_from_source=true
8888
```
8989

90-
If you want to link against a shared ZeroMQ library, you can build skip
91-
downloading `libzmq` and link with the installed library instead as follows:
90+
When building from source, you can also specify additional build options in a
91+
`.npmrc` file in your project:
9292

93-
```sh
94-
npm install [email protected] --zmq-shared
93+
<details>
94+
<summary>Available Build Options</summary>
95+
96+
#### Draft support
97+
98+
By default `libzmq` is built with support for `Draft` patterns (e.g.
99+
`server-client`, `radio-dish`, `scatter-gather`). If you want to build `libzmq`
100+
without support for `Draft`, you can specify the following in `.npmrc`:
101+
102+
```
103+
zmq_draft=false
95104
```
96105

97-
If you wish to use any DRAFT sockets then it is also necessary to compile the
98-
library from source:
106+
#### Shared library support
99107

100-
```sh
101-
npm install [email protected] --zmq-draft
108+
If you want to link against a shared ZeroMQ library installed on your system,
109+
you can build skip downloading `libzmq` and link with the installed library
110+
instead by specifying the following in `.npmrc`:
111+
112+
```ini
113+
zmq_shared=true
114+
```
115+
116+
#### Alternative libzmq version
117+
118+
You can specify an alternative version or Git revision of `libzmq` to build
119+
against by specifying the following in `.npmrc`:
120+
121+
```ini
122+
zmq_version="4.3.5"
102123
```
103124

125+
#### Debug build of libzmq
126+
127+
If you want to build `libzmq` with debug symbols, you can specify the following
128+
in `.npmrc`:
129+
130+
```ini
131+
zmq_build_type="Debug"
132+
```
133+
134+
#### Cross-compilation for different architectures
135+
136+
If you want to cross-compile for a different architecture, you can specify the
137+
following in `.npmrc`:
138+
139+
```ini
140+
arch="arm64"
141+
```
142+
143+
#### MacOS Deployment Target
144+
145+
If you want to specify the MacOS deployment target, you can specify the
146+
following in `.npmrc`:
147+
148+
```ini
149+
macos_deployment_target="10.15"
150+
```
151+
152+
</details>
153+
104154
## Examples
105155

106156
**Note:** These examples assume the reader is familiar with ZeroMQ. If you are

binding.gyp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,19 @@
1212
{
1313
'target_name': 'libzmq',
1414
'type': 'none',
15-
16-
'conditions': [
17-
["zmq_shared == 'false'", {
18-
'actions': [{
19-
'action_name': 'build_libzmq',
20-
'inputs': [],
21-
'conditions': [
22-
['OS != "win"', {
23-
'outputs': ['<(module_root_dir)/build/libzmq/lib/libzmq.a', '<(module_root_dir)/build/libzmq/include/zmq.h', '<(module_root_dir)/build/libzmq/include/zmq_utils.h'],
24-
}],
25-
['OS == "win"', {
26-
'outputs': ['<(module_root_dir)/build/libzmq/lib/libzmq.lib', '<(module_root_dir)/build/libzmq/include/zmq.h', '<(module_root_dir)/build/libzmq/include/zmq_utils.h'],
27-
}],
28-
],
29-
'action': ['node', '<(module_root_dir)/script/build.js'],
15+
'actions': [{
16+
'action_name': 'build_libzmq',
17+
'inputs': [],
18+
'conditions': [
19+
['OS != "win"', {
20+
'outputs': ['<(module_root_dir)/build/libzmq/lib/libzmq.a', '<(module_root_dir)/build/libzmq/include/zmq.h', '<(module_root_dir)/build/libzmq/include/zmq_utils.h'],
3021
}],
31-
}],
32-
],
22+
['OS == "win"', {
23+
'outputs': ['<(module_root_dir)/build/libzmq/lib/libzmq.lib', '<(module_root_dir)/build/libzmq/include/zmq.h', '<(module_root_dir)/build/libzmq/include/zmq_utils.h'],
24+
}],
25+
],
26+
'action': ['node', '<(module_root_dir)/script/build.js'],
27+
}],
3328
},
3429

3530
{

script/build.ts

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,62 @@ import {mkdir, cd, exec, find, mv} from "shelljs"
44

55
const root = dirname(__dirname)
66

7+
type Options = {
8+
zmq_shared: boolean
9+
zmq_version: string
10+
zmq_draft: boolean
11+
zmq_build_type: string
12+
arch: string
13+
macosx_deployment_target?: string
14+
}
15+
16+
function toBool(value: string | undefined): boolean | undefined {
17+
switch (value) {
18+
case "true":
19+
case "1":
20+
return true
21+
case "false":
22+
case "0":
23+
return false
24+
default:
25+
return undefined
26+
}
27+
}
28+
29+
function toString(value: string | undefined): string | undefined {
30+
switch (value) {
31+
case undefined:
32+
case "":
33+
return undefined
34+
default:
35+
return value
36+
}
37+
}
38+
39+
function parseOptions(): Options {
40+
return {
41+
zmq_shared: toBool(process.env.npm_config_zmq_shared) ?? false,
42+
zmq_draft: toBool(process.env.npm_config_zmq_draft) ?? false,
43+
zmq_version:
44+
toString(process.env.npm_config_zmq_version) ??
45+
"5657b4586f24ec433930e8ece02ddba7afcf0fe0",
46+
zmq_build_type:
47+
toString(process.env.npm_config_zmq_build_type) ?? "Release",
48+
arch: toString(process.env.npm_config_arch) ?? process.arch,
49+
macosx_deployment_target:
50+
toString(process.env.npm_config_macosx_deployment_target) ?? "10.15",
51+
}
52+
}
53+
754
function main() {
8-
const zmq_rev =
9-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/strict-boolean-expressions
10-
process.env.ZMQ_VERSION || "5657b4586f24ec433930e8ece02ddba7afcf0fe0"
11-
const src_url = `https://github.com/zeromq/libzmq/archive/${zmq_rev}.tar.gz`
55+
const opts = parseOptions()
56+
console.log("Building libzmq with options ", opts)
57+
58+
if (opts.zmq_shared) {
59+
return
60+
}
61+
62+
const src_url = `https://github.com/zeromq/libzmq/archive/${opts.zmq_version}.tar.gz`
1263

1364
const libzmq_build_prefix = `${root}/build/libzmq-staging`
1465
const libzmq_install_prefix = `${root}/build/libzmq`
@@ -17,29 +68,25 @@ function main() {
1768
process.platform === "win32" ? ".lib" : ".a"
1869
}`
1970

20-
const src_dir = `libzmq-${zmq_rev}`
21-
const tarball = `libzmq-${zmq_rev}.tar.gz`
22-
23-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/strict-boolean-expressions
24-
const CMAKE_BUILD_TYPE = process.env.CMAKE_BUILD_TYPE || "Release"
71+
const src_dir = `libzmq-${opts.zmq_version}`
72+
const tarball = `libzmq-${opts.zmq_version}.tar.gz`
2573

2674
let build_options: string = ""
2775

2876
// https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
2977
if (process.platform === "win32") {
30-
if (CMAKE_BUILD_TYPE !== "Debug") {
78+
if (opts.zmq_build_type !== "Debug") {
3179
build_options += " -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL"
3280
} else {
3381
build_options += " -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebugDLL"
3482
}
3583
}
3684

37-
build_options += archCMakeOptions()
85+
build_options += archCMakeOptions(opts)
3886

3987
if (process.platform === "darwin") {
40-
const MACOSX_DEPLOYMENT_TARGET = "10.15"
41-
process.env.MACOSX_DEPLOYMENT_TARGET = MACOSX_DEPLOYMENT_TARGET
42-
build_options += ` -DCMAKE_OSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET}`
88+
process.env.MACOSX_DEPLOYMENT_TARGET = opts.macosx_deployment_target
89+
build_options += ` -DCMAKE_OSX_DEPLOYMENT_TARGET=${opts.macosx_deployment_target}`
4390
}
4491

4592
mkdir("-p", libzmq_build_prefix)
@@ -65,24 +112,24 @@ function main() {
65112
exec(`tar xzf "${tarball}"`, execOptions)
66113
}
67114

68-
if (process.env.ZMQ_DRAFT === "true") {
115+
if (opts.zmq_draft) {
69116
console.log("Enabling draft support")
70117
build_options += " -DENABLE_DRAFTS=ON"
71118
}
72119

73-
console.log(`Building libzmq ${CMAKE_BUILD_TYPE}`)
120+
console.log(`Building libzmq ${opts.zmq_build_type}`)
74121

75122
// ClangFormat include causes issues but is not required to build.
76123
const clang_format_file = `${src_dir}/builds/cmake/Modules/ClangFormat.cmake`
77124
if (existsSync(clang_format_file)) {
78125
writeFileSync(clang_format_file, "")
79126
}
80127

81-
const cmake_configure = `cmake -S "${src_dir}" -B ./build ${build_options} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX="${libzmq_install_prefix}" -DCMAKE_INSTALL_LIBDIR=lib -DBUILD_STATIC=ON -DBUILD_TESTS=OFF -DBUILD_SHARED=OFF -DWITH_DOCS=OFF -DWITH_LIBSODIUM=OFF`
128+
const cmake_configure = `cmake -S "${src_dir}" -B ./build ${build_options} -DCMAKE_BUILD_TYPE=${opts.zmq_build_type} -DCMAKE_INSTALL_PREFIX="${libzmq_install_prefix}" -DCMAKE_INSTALL_LIBDIR=lib -DBUILD_STATIC=ON -DBUILD_TESTS=OFF -DBUILD_SHARED=OFF -DWITH_DOCS=OFF -DWITH_LIBSODIUM=OFF`
82129
console.log(cmake_configure)
83130
exec(cmake_configure, execOptions)
84131

85-
const cmake_build = `cmake --build ./build --config ${CMAKE_BUILD_TYPE} --target install --parallel`
132+
const cmake_build = `cmake --build ./build --config ${opts.zmq_build_type} --target install --parallel`
86133
console.log(cmake_build)
87134
exec(cmake_build, execOptions)
88135

@@ -95,9 +142,8 @@ function main() {
95142

96143
main()
97144

98-
function archCMakeOptions() {
99-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/strict-boolean-expressions
100-
const arch = (process.env.ARCH || process.arch).toLowerCase()
145+
function archCMakeOptions(opts: Options) {
146+
const arch = opts.arch.toLowerCase()
101147

102148
if (process.platform === "win32") {
103149
// CMAKE_GENERATOR_PLATFORM only supported on Windows

0 commit comments

Comments
 (0)