Skip to content

Commit b40a085

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

File tree

3 files changed

+127
-54
lines changed

3 files changed

+127
-54
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

script/build.ts

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,46 @@ 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+
return value === undefined ? undefined : value === "true"
18+
}
19+
20+
function toString(value: string | undefined): string | undefined {
21+
return value === undefined || value === "" ? undefined : value
22+
}
23+
24+
function parseOptions(): Options {
25+
return {
26+
zmq_shared: toBool(process.env.npm_config_zmq_shared) ?? false,
27+
zmq_draft: toBool(process.env.npm_config_zmq_draft) ?? false,
28+
zmq_version:
29+
toString(process.env.npm_config_zmq_version) ??
30+
"5657b4586f24ec433930e8ece02ddba7afcf0fe0",
31+
zmq_build_type:
32+
toString(process.env.npm_config_zmq_build_type) ?? "Release",
33+
arch: toString(process.env.npm_config_arch) ?? process.arch,
34+
macosx_deployment_target:
35+
toString(process.env.npm_config_macosx_deployment_target) ?? "10.15",
36+
}
37+
}
38+
739
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`
40+
const opts = parseOptions()
41+
42+
if (opts.zmq_shared) {
43+
return
44+
}
45+
46+
const src_url = `https://github.com/zeromq/libzmq/archive/${opts.zmq_version}.tar.gz`
1247

1348
const libzmq_build_prefix = `${root}/build/libzmq-staging`
1449
const libzmq_install_prefix = `${root}/build/libzmq`
@@ -17,29 +52,25 @@ function main() {
1752
process.platform === "win32" ? ".lib" : ".a"
1853
}`
1954

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"
55+
const src_dir = `libzmq-${opts.zmq_version}`
56+
const tarball = `libzmq-${opts.zmq_version}.tar.gz`
2557

2658
let build_options: string = ""
2759

2860
// https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
2961
if (process.platform === "win32") {
30-
if (CMAKE_BUILD_TYPE !== "Debug") {
62+
if (opts.zmq_build_type !== "Debug") {
3163
build_options += " -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL"
3264
} else {
3365
build_options += " -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebugDLL"
3466
}
3567
}
3668

37-
build_options += archCMakeOptions()
69+
build_options += archCMakeOptions(opts)
3870

3971
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}`
72+
process.env.MACOSX_DEPLOYMENT_TARGET = opts.macosx_deployment_target
73+
build_options += ` -DCMAKE_OSX_DEPLOYMENT_TARGET=${opts.macosx_deployment_target}`
4374
}
4475

4576
mkdir("-p", libzmq_build_prefix)
@@ -65,24 +96,24 @@ function main() {
6596
exec(`tar xzf "${tarball}"`, execOptions)
6697
}
6798

68-
if (process.env.ZMQ_DRAFT === "true") {
99+
if (opts.zmq_draft) {
69100
console.log("Enabling draft support")
70101
build_options += " -DENABLE_DRAFTS=ON"
71102
}
72103

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

75106
// ClangFormat include causes issues but is not required to build.
76107
const clang_format_file = `${src_dir}/builds/cmake/Modules/ClangFormat.cmake`
77108
if (existsSync(clang_format_file)) {
78109
writeFileSync(clang_format_file, "")
79110
}
80111

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`
112+
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`
82113
console.log(cmake_configure)
83114
exec(cmake_configure, execOptions)
84115

85-
const cmake_build = `cmake --build ./build --config ${CMAKE_BUILD_TYPE} --target install --parallel`
116+
const cmake_build = `cmake --build ./build --config ${opts.zmq_build_type} --target install --parallel`
86117
console.log(cmake_build)
87118
exec(cmake_build, execOptions)
88119

@@ -95,9 +126,8 @@ function main() {
95126

96127
main()
97128

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()
129+
function archCMakeOptions(opts: Options) {
130+
const arch = opts.arch.toLowerCase()
101131

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

0 commit comments

Comments
 (0)