Skip to content

Commit 7c9cd91

Browse files
committed
build: improve cpp-std.js to detect Electron versions
1 parent 254fa40 commit 7c9cd91

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

README.md

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
- [Electron / NW.js](#electron--nwjs)
6363
- [NW.js (aka node-webkit)](#nwjs-aka-node-webkit)
6464
- [Electron (aka atom-shell)](#electron-aka-atom-shell)
65-
- [Electron \>= 11 / NW.js \>= 0.50](#electron--11--nwjs--050)
6665
- [Building on Linux](#building-on-linux)
6766
- [Building on macOS](#building-on-macos)
6867
- [Xcode \>= 10 | macOS \>= Mojave](#xcode--10--macos--mojave)
@@ -276,7 +275,7 @@ Features: AsynchDNS, IDN, IPv6, Largefile, NTLM, NTLM_WB, SSL, libz, brotli, TLS
276275

277276
If there is no prebuilt binary available that matches your system, or if the installation fails, then you will need an environment capable of compiling Node.js addons, which means:
278277
- [python 3.x](https://www.python.org/downloads/) installed
279-
- updated C++ compiler able to compile C++11, or if building Electron >= 11 / NW.js >= 0.50, C++17 (see the [Electron >= 11 / NW.js >= 0.50](#electron--11--nwjs--050) section below).
278+
- updated C++ compiler able to compile C++17 (C++20 for Electron >= v32).
280279

281280
If you don't want to use the prebuilt binary even if it works on your system, you can pass a flag when installing:
282281
> With `npm`
@@ -372,19 +371,6 @@ target_arch = x64
372371
dist_url = https://atom.io/download/atom-shell
373372
```
374373

375-
#### Electron >= 11 / NW.js >= 0.50
376-
377-
If you are building for Electron >= 11 or NW.js >= 0.50 you need to set the build process to use the C++17 std, you can do that by passing the variable `node_libcurl_cpp_std=c++17`. The way you do that depends if you are using `npm` or `yarn`:
378-
379-
> If using `npm`:
380-
```sh
381-
npm install node-libcurl --node_libcurl_cpp_std=c++17 <...other args...>
382-
```
383-
> If using `yarn`:
384-
```sh
385-
npm_config_node_libcurl_cpp_std=c++17 <...other args...> yarn add node-libcurl
386-
```
387-
388374
### Building on Linux
389375

390376
To build the addon on linux based systems you must have:

binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'curl_static_build%': 'false',
1212
'curl_config_bin%': 'node <(module_root_dir)/scripts/curl-config.js',
1313
'node_libcurl_no_setlocale%': 'false',
14-
'node_libcurl_cpp_std%': '<!(node <(module_root_dir)/scripts/cpp-std.js)',
14+
'node_libcurl_cpp_std%': '<!(node <(module_root_dir)/scripts/cpp-std.js <(node_root_dir))',
1515
'macos_universal_build%': 'false',
1616
},
1717
'targets': [

scripts/cpp-std.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,48 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7+
const isGreaterOrEqual = (a, b) => {
8+
return a.localeCompare(b, undefined, { numeric: true }) >= 0
9+
}
10+
711
if (process.env.NODE_LIBCURL_CPP_STD) {
812
console.log(process.env.NODE_LIBCURL_CPP_STD)
913
} else {
10-
if (process.versions.modules && parseInt(process.versions.modules) >= 88) {
14+
// e.g: '/Users/jcm/.electron-gyp/32.2.6'
15+
const nodeRootDir = process.argv[2]
16+
17+
// detect electron related env vars coming from
18+
// npm flags, e.g: npm_config_runtime
19+
// we could also use node-abi here!
20+
const isElectron =
21+
process.env['npm_config_runtime'] === 'electron' ||
22+
nodeRootDir.includes('electron-gyp')
23+
const electronVersion = isElectron
24+
? process.env['npm_config_target'] ?? nodeRootDir.split('/').pop()
25+
: null
26+
27+
if (isElectron) {
28+
if (isGreaterOrEqual(electronVersion, '32.0.0')) {
29+
console.log('c++20')
30+
} else if (isGreaterOrEqual(electronVersion, '13.0.0')) {
31+
console.log('c++17')
32+
} else {
33+
console.log('c++98')
34+
}
35+
}
36+
37+
// https://github.com/nodejs/node/blob/main/doc/abi_version_registry.json
38+
// https://github.com/nodejs/node/blob/main/doc/abi_version_registry.json
39+
// 129 === Node.js v23
40+
if (process.versions.modules && parseInt(process.versions.modules) >= 129) {
41+
console.log('c++20')
42+
} else if (
43+
process.versions.modules &&
44+
parseInt(process.versions.modules) >= 88
45+
) {
1146
// 88 === Node.js v15
1247
console.log('c++17')
1348
} else {
14-
console.log('c++11')
49+
console.log('c++98')
1550
}
1651
}

0 commit comments

Comments
 (0)