From e5906ddfd42a74354981e535fb9e4dfc1e05905b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Tue, 28 Mar 2023 17:00:23 +0200 Subject: [PATCH 1/5] nodejs: fix cross compilation for armv7l The v8 library supports only limited number of build platforms based on the host architecture. The rule there is the bitness (the mixing of 32bit and 64bit architectures seems to be in most cases disallowed). Thus this adds usage of the emulator of the host platform and builds tools for that. Co-authored-by: Ivan Trubach --- .../nodejs/configure-emulator-node18.patch | 138 +++++++++++++ .../web/nodejs/configure-emulator.patch | 146 +++++++++++++ pkgs/development/web/nodejs/nodejs.nix | 191 +++++++++++------- pkgs/development/web/nodejs/v18.nix | 1 + pkgs/development/web/nodejs/v20.nix | 1 + pkgs/development/web/nodejs/v22.nix | 1 + 6 files changed, 405 insertions(+), 73 deletions(-) create mode 100644 pkgs/development/web/nodejs/configure-emulator-node18.patch create mode 100644 pkgs/development/web/nodejs/configure-emulator.patch diff --git a/pkgs/development/web/nodejs/configure-emulator-node18.patch b/pkgs/development/web/nodejs/configure-emulator-node18.patch new file mode 100644 index 000000000000..f7faddb7e005 --- /dev/null +++ b/pkgs/development/web/nodejs/configure-emulator-node18.patch @@ -0,0 +1,138 @@ +From 4b83f714c821d6d4d2306673ee3a87907cfec80e Mon Sep 17 00:00:00 2001 +From: Ivan Trubach +Date: Fri, 19 Jul 2024 10:45:13 +0300 +Subject: [PATCH] build: support setting an emulator from configure script +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +V8’s JIT infrastructure requires binaries such as mksnapshot to be run +during the build. However, these binaries must have the same bit-width +as the host platform (e.g. a x86_64 build platform targeting ARMv6 needs +to produce a 32-bit binary). + +To work around this issue, allow building the binaries for the host +platform and running them on the build platform with an emulator. + +Based on Buildroot’s nodejs-src 0001-add-qemu-wrapper-support.patch. +https://gitlab.com/buildroot.org/buildroot/-/blob/c1d5eada4d4db9eeaa1c44dd1dea95a67c8a70ca/package/nodejs/nodejs-src/0001-add-qemu-wrapper-support.patch + +Upstream: https://github.com/nodejs/node/pull/53899 +--- + common.gypi | 1 + + configure.py | 14 ++++++++++++++ + node.gyp | 3 +++ + tools/v8_gypfiles/v8.gyp | 4 ++++ + 4 files changed, 22 insertions(+) + +diff --git a/common.gypi b/common.gypi +index ec92c9df4c..6474495ab6 100644 +--- a/common.gypi ++++ b/common.gypi +@@ -13,6 +13,7 @@ + 'enable_pgo_generate%': '0', + 'enable_pgo_use%': '0', + 'python%': 'python', ++ 'emulator%': [], + + 'node_shared%': 'false', + 'force_dynamic_crt%': 0, +diff --git a/configure.py b/configure.py +index 82916748fd..10dc0becbb 100755 +--- a/configure.py ++++ b/configure.py +@@ -112,6 +112,12 @@ parser.add_argument('--dest-cpu', + choices=valid_arch, + help=f"CPU architecture to build for ({', '.join(valid_arch)})") + ++parser.add_argument('--emulator', ++ action='store', ++ dest='emulator', ++ default=None, ++ help='emulator command that can run executables built for the target system') ++ + parser.add_argument('--cross-compiling', + action='store_true', + dest='cross_compiling', +@@ -2160,6 +2166,14 @@ write('config.mk', do_not_edit + config_str) + gyp_args = ['--no-parallel', '-Dconfiguring_node=1'] + gyp_args += ['-Dbuild_type=' + config['BUILDTYPE']] + ++if options.emulator is not None: ++ if not options.cross_compiling: ++ # Note that emulator is a list so we have to quote the variable. ++ gyp_args += ['-Demulator=' + shlex.quote(options.emulator)] ++ else: ++ # TODO: perhaps use emulator for tests? ++ warn('The `--emulator` option has no effect when cross-compiling.') ++ + if options.use_ninja: + gyp_args += ['-f', 'ninja-' + flavor] + elif flavor == 'win' and sys.platform != 'msys': +diff --git a/node.gyp b/node.gyp +index 08cb3f38e8..515b305933 100644 +--- a/node.gyp ++++ b/node.gyp +@@ -332,6 +332,7 @@ + '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', + ], + 'action': [ ++ '<@(emulator)', + '<(node_mksnapshot_exec)', + '--build-snapshot', + '<(node_snapshot_main)', +@@ -351,6 +352,7 @@ + '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', + ], + 'action': [ ++ '<@(emulator)', + '<@(_inputs)', + '<@(_outputs)', + ], +@@ -1520,6 +1522,7 @@ + '<(PRODUCT_DIR)/<(node_core_target_name).def', + ], + 'action': [ ++ '<@(emulator)', + '<(PRODUCT_DIR)/gen_node_def.exe', + '<@(_inputs)', + '<@(_outputs)', +diff --git a/tools/v8_gypfiles/v8.gyp b/tools/v8_gypfiles/v8.gyp +index ba8b161f0f..d5c90dad50 100644 +--- a/tools/v8_gypfiles/v8.gyp ++++ b/tools/v8_gypfiles/v8.gyp +@@ -99,6 +99,7 @@ + '<@(torque_outputs_inc)', + ], + 'action': [ ++ '<@(emulator)', + '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)torque<(EXECUTABLE_SUFFIX)', + '-o', '<(SHARED_INTERMEDIATE_DIR)/torque-generated', + '-v8-root', '<(V8_ROOT)', +@@ -219,6 +220,7 @@ + 'action': [ + '<(python)', + '<(V8_ROOT)/tools/run.py', ++ '<@(emulator)', + '<@(_inputs)', + '<@(_outputs)', + ], +@@ -442,6 +444,7 @@ + }], + ], + 'action': [ ++ '<@(emulator)', + '>@(_inputs)', + '>@(mksnapshot_flags)', + ], +@@ -1577,6 +1580,7 @@ + 'action': [ + '<(python)', + '<(V8_ROOT)/tools/run.py', ++ '<@(emulator)', + '<@(_inputs)', + '<@(_outputs)', + ], +-- +2.44.1 + diff --git a/pkgs/development/web/nodejs/configure-emulator.patch b/pkgs/development/web/nodejs/configure-emulator.patch new file mode 100644 index 000000000000..d1462f374d7e --- /dev/null +++ b/pkgs/development/web/nodejs/configure-emulator.patch @@ -0,0 +1,146 @@ +From 999d918bc8fefec1752243743a47c0ce5380bcec Mon Sep 17 00:00:00 2001 +From: Ivan Trubach +Date: Wed, 17 Jul 2024 10:16:02 +0300 +Subject: [PATCH] build: support setting an emulator from configure script +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +V8’s JIT infrastructure requires binaries such as mksnapshot to be run +during the build. However, these binaries must have the same bit-width +as the host platform (e.g. a x86_64 build platform targeting ARMv6 needs +to produce a 32-bit binary). + +To work around this issue, allow building the binaries for the host +platform and running them on the build platform with an emulator. + +Based on Buildroot’s nodejs-src 0001-add-qemu-wrapper-support.patch. +https://gitlab.com/buildroot.org/buildroot/-/blob/c1d5eada4d4db9eeaa1c44dd1dea95a67c8a70ca/package/nodejs/nodejs-src/0001-add-qemu-wrapper-support.patch + +Upstream: https://github.com/nodejs/node/pull/53899 +--- + common.gypi | 1 + + configure.py | 14 ++++++++++++++ + node.gyp | 4 ++++ + tools/v8_gypfiles/v8.gyp | 4 ++++ + 4 files changed, 23 insertions(+) + +diff --git a/common.gypi b/common.gypi +index 154bbf2a0d..54d2afe3b3 100644 +--- a/common.gypi ++++ b/common.gypi +@@ -13,6 +13,7 @@ + 'enable_pgo_generate%': '0', + 'enable_pgo_use%': '0', + 'python%': 'python', ++ 'emulator%': [], + + 'node_shared%': 'false', + 'force_dynamic_crt%': 0, +diff --git a/configure.py b/configure.py +index f7e3310723..f7c7acdf4f 100755 +--- a/configure.py ++++ b/configure.py +@@ -112,6 +112,12 @@ parser.add_argument('--dest-cpu', + choices=valid_arch, + help=f"CPU architecture to build for ({', '.join(valid_arch)})") + ++parser.add_argument('--emulator', ++ action='store', ++ dest='emulator', ++ default=None, ++ help='emulator command that can run executables built for the target system') ++ + parser.add_argument('--cross-compiling', + action='store_true', + dest='cross_compiling', +@@ -2276,6 +2282,14 @@ if flavor == 'win' and python.lower().endswith('.exe'): + # will fail to run python scripts. + gyp_args += ['-Dpython=' + python] + ++if options.emulator is not None: ++ if not options.cross_compiling: ++ # Note that emulator is a list so we have to quote the variable. ++ gyp_args += ['-Demulator=' + shlex.quote(options.emulator)] ++ else: ++ # TODO: perhaps use emulator for tests? ++ warn('The `--emulator` option has no effect when cross-compiling.') ++ + if options.use_ninja: + gyp_args += ['-f', 'ninja-' + flavor] + elif flavor == 'win' and sys.platform != 'msys': +diff --git a/node.gyp b/node.gyp +index 9617596760..439c76aca6 100644 +--- a/node.gyp ++++ b/node.gyp +@@ -703,6 +703,7 @@ + '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', + ], + 'action': [ ++ '<@(emulator)', + '<(node_mksnapshot_exec)', + '--build-snapshot', + '<(node_snapshot_main)', +@@ -722,6 +723,7 @@ + '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', + ], + 'action': [ ++ '<@(emulator)', + '<@(_inputs)', + '<@(_outputs)', + ], +@@ -1010,6 +1012,7 @@ + '<(SHARED_INTERMEDIATE_DIR)/node_javascript.cc', + ], + 'action': [ ++ '<@(emulator)', + '<(node_js2c_exec)', + '<@(_outputs)', + 'lib', +@@ -1477,6 +1480,7 @@ + '<(PRODUCT_DIR)/<(node_core_target_name).def', + ], + 'action': [ ++ '<@(emulator)', + '<(PRODUCT_DIR)/gen_node_def.exe', + '<@(_inputs)', + '<@(_outputs)', +diff --git a/tools/v8_gypfiles/v8.gyp b/tools/v8_gypfiles/v8.gyp +index d65a5c268e..5cd6c36b86 100644 +--- a/tools/v8_gypfiles/v8.gyp ++++ b/tools/v8_gypfiles/v8.gyp +@@ -112,6 +112,7 @@ + '<@(torque_outputs_inc)', + ], + 'action': [ ++ '<@(emulator)', + '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)torque<(EXECUTABLE_SUFFIX)', + '-o', '<(SHARED_INTERMEDIATE_DIR)/torque-generated', + '-v8-root', '<(V8_ROOT)', +@@ -232,6 +233,7 @@ + 'action': [ + '<(python)', + '<(V8_ROOT)/tools/run.py', ++ '<@(emulator)', + '<@(_inputs)', + '<@(_outputs)', + ], +@@ -453,6 +455,7 @@ + }], + ], + 'action': [ ++ '<@(emulator)', + '>@(_inputs)', + '>@(mksnapshot_flags)', + ], +@@ -1842,6 +1845,7 @@ + 'action': [ + '<(python)', + '<(V8_ROOT)/tools/run.py', ++ '<@(emulator)', + '<@(_inputs)', + '<@(_outputs)', + ], +-- +2.44.1 + diff --git a/pkgs/development/web/nodejs/nodejs.nix b/pkgs/development/web/nodejs/nodejs.nix index c6ed6a116a37..b89fe4853091 100644 --- a/pkgs/development/web/nodejs/nodejs.nix +++ b/pkgs/development/web/nodejs/nodejs.nix @@ -1,4 +1,4 @@ -{ lib, stdenv, fetchurl, openssl, python, zlib, libuv, util-linux, http-parser, bash +{ lib, stdenv, fetchurl, openssl, python, zlib, libuv, http-parser, icu, bash , pkg-config, which, buildPackages , testers # for `.pkgs` attribute @@ -7,7 +7,6 @@ , writeScript, coreutils, gnugrep, jq, curl, common-updater-scripts, nix, runtimeShell , gnupg , darwin, xcbuild -, procps, icu , installShellFiles }: @@ -16,36 +15,95 @@ let inherit (darwin.apple_sdk.frameworks) CoreServices ApplicationServices; - isCross = stdenv.hostPlatform != stdenv.buildPlatform; - majorVersion = lib.versions.major version; minorVersion = lib.versions.minor version; pname = if enableNpm then "nodejs" else "nodejs-slim"; + canExecute = stdenv.buildPlatform.canExecute stdenv.hostPlatform; + emulator = stdenv.hostPlatform.emulator buildPackages; + + # See valid_os and valid_arch in configure.py. + destOS = + let + platform = stdenv.hostPlatform; + in + if platform.isiOS then + "ios" + else if platform.isAndroid then + "android" + else if platform.isWindows then + "win" + else if platform.isDarwin then + "mac" + else if platform.isLinux then + "linux" + else if platform.isOpenBSD then + "openbsd" + else if platform.isFreeBSD then + "freebsd" + else + throw "unsupported os ${platform.uname.system}"; + destCPU = + let + platform = stdenv.hostPlatform; + in + if platform.isAarch then + "arm" + lib.optionalString platform.is64bit "64" + else if platform.isMips32 then + "mips" + lib.optionalString platform.isLittleEndian "le" + else if platform.isMips64 && platform.isLittleEndian then + "mips64el" + else if platform.isPower then + "ppc" + lib.optionalString platform.is64bit "64" + else if platform.isx86_64 then + "x64" + else if platform.isx86_32 then + "ia32" + else if platform.isS390x then + "s390x" + else if platform.isRiscV64 then + "riscv64" + else if platform.isLoongArch64 then + "loong64" + else + throw "unsupported cpu ${platform.uname.processor}"; + destARMFPU = + let + platform = stdenv.hostPlatform; + in + if platform.isAarch32 && platform ? gcc.fpu then + lib.throwIfNot (builtins.elem platform.gcc.fpu [ + "vfp" + "vfpv3" + "vfpv3-d16" + "neon" + ]) "unsupported ARM FPU ${platform.gcc.fpu}" platform.gcc.fpu + else + null; + destARMFloatABI = + let + platform = stdenv.hostPlatform; + in + if platform.isAarch32 && platform ? gcc.float-abi then + lib.throwIfNot (builtins.elem platform.gcc.float-abi [ + "soft" + "softfp" + "hard" + ]) "unsupported ARM float ABI ${platform.gcc.float-abi}" platform.gcc.float-abi + else + null; + # TODO: also handle MIPS flags (mips_arch, mips_fpu, mips_float_abi). + useSharedHttpParser = !stdenv.isDarwin && lib.versionOlder "${majorVersion}.${minorVersion}" "11.4"; sharedLibDeps = { inherit openssl zlib libuv; } // (lib.optionalAttrs useSharedHttpParser { inherit http-parser; }); - sharedConfigureFlags = lib.concatMap (name: [ - "--shared-${name}" - "--shared-${name}-libpath=${lib.getLib sharedLibDeps.${name}}/lib" - /** Closure notes: we explicitly avoid specifying --shared-*-includes, - * as that would put the paths into bin/nodejs. - * Including pkg-config in build inputs would also have the same effect! - */ - ]) (builtins.attrNames sharedLibDeps) ++ [ - "--with-intl=system-icu" - "--openssl-use-def-ca-store" - ]; - copyLibHeaders = map (name: "${lib.getDev sharedLibDeps.${name}}/include/*") (builtins.attrNames sharedLibDeps); - extraConfigFlags = lib.optionals (!enableNpm) [ "--without-npm" ]; - package = stdenv.mkDerivation (finalAttrs: let /** the final package fixed point, after potential overrides */ @@ -67,8 +125,6 @@ let NIX_CFLAGS_COMPILE = "-D__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=101300"; }; - depsBuildBuild = [ buildPackages.stdenv.cc openssl libuv zlib icu ]; - # NB: technically, we do not need bash in build inputs since all scripts are # wrappers over the corresponding JS scripts. There are some packages though # that use bash wrappers, e.g. polaris-web. @@ -82,37 +138,46 @@ let setOutputFlags = false; moveToDev = false; - configureFlags = let - inherit (stdenv.hostPlatform) gcc isAarch32; - in sharedConfigureFlags ++ lib.optionals (lib.versionOlder version "19") [ - "--without-dtrace" - ] ++ (lib.optionals isCross [ - "--cross-compiling" - "--dest-cpu=${let platform = stdenv.hostPlatform; in - if platform.isAarch32 then "arm" - else if platform.isAarch64 then "arm64" - else if platform.isMips32 && platform.isLittleEndian then "mipsel" - else if platform.isMips32 && !platform.isLittleEndian then "mips" - else if platform.isMips64 && platform.isLittleEndian then "mips64el" - else if platform.isPower && platform.is32bit then "ppc" - else if platform.isPower && platform.is64bit then "ppc64" - else if platform.isx86_64 then "x86_64" - else if platform.isx86_32 then "x86" - else if platform.isS390 && platform.is64bit then "s390x" - else if platform.isRiscV && platform.is64bit then "riscv64" - else throw "unsupported cpu ${stdenv.hostPlatform.uname.processor}"}" - ]) ++ (lib.optionals (isCross && isAarch32 && lib.hasAttr "fpu" gcc) [ - "--with-arm-fpu=${gcc.fpu}" - ]) ++ (lib.optionals (isCross && isAarch32 && lib.hasAttr "float-abi" gcc) [ - "--with-arm-float-abi=${gcc.float-abi}" - ]) ++ extraConfigFlags; + configureFlags = + [ + "--no-cross-compiling" + "--dest-os=${destOS}" + "--dest-cpu=${destCPU}" + ] + ++ lib.optionals (destARMFPU != null) [ "--with-arm-fpu=${destARMFPU}" ] + ++ lib.optionals (destARMFloatABI != null) [ "--with-arm-float-abi=${destARMFloatABI}" ] + ++ lib.optionals (!canExecute) [ + # Node.js requires matching bitness between build and host platforms, e.g. + # for V8 startup snapshot builder (see tools/snapshot) and some other + # tools. We apply a patch that runs these tools using a host platform + # emulator and avoid cross-compiling altogether (from the build system’s + # perspective). + "--emulator=${emulator}" + ] + ++ lib.optionals (lib.versionOlder version "19") [ "--without-dtrace" ] + ++ lib.optionals (!enableNpm) [ "--without-npm" ] + ++ lib.concatMap (name: [ + "--shared-${name}" + "--shared-${name}-libpath=${lib.getLib sharedLibDeps.${name}}/lib" + /** + Closure notes: we explicitly avoid specifying --shared-*-includes, + as that would put the paths into bin/nodejs. + Including pkg-config in build inputs would also have the same effect! - configurePlatforms = []; + FIXME: the statement above is outdated, we have to include pkg-config + in build inputs for system-icu. + */ + ]) (builtins.attrNames sharedLibDeps) + ++ [ + "--with-intl=system-icu" + "--openssl-use-def-ca-store" + ]; + + configurePlatforms = [ ]; dontDisableStatic = true; configureScript = writeScript "nodejs-configure" '' - export CC_host="$CC_FOR_BUILD" CXX_host="$CXX_FOR_BUILD" exec ${python.executable} configure.py "$@" ''; @@ -140,9 +205,7 @@ let __darwinAllowLocalNetworking = true; # for tests - # TODO: what about tests when cross-compiling? - # Note that currently stdenv does not run check phase if build ≠ host. - doCheck = true; + doCheck = canExecute; # Some dependencies required for tools/doc/node_modules (and therefore # test-addons, jstest and others) target are not included in the tarball. @@ -213,12 +276,12 @@ let postInstall = '' HOST_PATH=$out/bin patchShebangs --host $out - ${lib.optionalString (stdenv.buildPlatform.canExecute stdenv.hostPlatform) '' - $out/bin/${self.meta.mainProgram} --completion-bash > ${self.meta.mainProgram}.bash - installShellCompletion ${self.meta.mainProgram}.bash + ${lib.optionalString canExecute '' + $out/bin/node --completion-bash > node.bash + installShellCompletion node.bash ''} - ${lib.optionalString (enableNpm) '' + ${lib.optionalString enableNpm '' mkdir -p $out/share/bash-completion/completions ln -s $out/lib/node_modules/npm/lib/utils/completion.sh \ $out/share/bash-completion/completions/npm @@ -231,24 +294,14 @@ let ''} # install the missing headers for node-gyp + # TODO: add dev output and use propagatedBuildInputs instead of copying headers. cp -r ${lib.concatStringsSep " " copyLibHeaders} $out/include/node # assemble a static v8 library and put it in the 'libv8' output mkdir -p $libv8/lib pushd out/Release/obj.target find . -path "./torque_*/**/*.o" -or -path "./v8*/**/*.o" | sort -u >files - ${if stdenv.buildPlatform.isGnu then '' - ar -cqs $libv8/lib/libv8.a @files - '' else '' - # llvm-ar supports response files, so take advantage of it if it’s available. - if [ "$(basename $(readlink -f $(command -v ar)))" = "llvm-ar" ]; then - ar -cqs $libv8/lib/libv8.a @files - else - cat files | while read -r file; do - ar -cqS $libv8/lib/libv8.a $file - done - fi - ''} + $AR -cqs $libv8/lib/libv8.a @files popd # copy v8 headers @@ -290,14 +343,6 @@ let platforms = platforms.linux ++ platforms.darwin; mainProgram = "node"; knownVulnerabilities = optional (versionOlder version "18") "This NodeJS release has reached its end of life. See https://nodejs.org/en/about/releases/."; - - # Node.js build system does not have separate host and target OS - # configurations (architectures are defined as host_arch and target_arch, - # but there is no such thing as host_os and target_os). - # - # We may be missing something here, but it doesn’t look like it is - # possible to cross-compile between different operating systems. - broken = stdenv.buildPlatform.parsed.kernel.name != stdenv.hostPlatform.parsed.kernel.name; }; passthru.python = python; # to ensure nodeEnv uses the same version diff --git a/pkgs/development/web/nodejs/v18.nix b/pkgs/development/web/nodejs/v18.nix index 1101fbdf725d..b69f92d51c3a 100644 --- a/pkgs/development/web/nodejs/v18.nix +++ b/pkgs/development/web/nodejs/v18.nix @@ -26,6 +26,7 @@ buildNodejs { version = "18.20.4"; sha256 = "sha256-p2x+oblq62ljoViAYmDICUtiRNZKaWUp0CBUe5qVyio="; patches = [ + ./configure-emulator-node18.patch ./disable-darwin-v8-system-instrumentation.patch ./bypass-darwin-xcrun-node16.patch ./revert-arm64-pointer-auth.patch diff --git a/pkgs/development/web/nodejs/v20.nix b/pkgs/development/web/nodejs/v20.nix index bf0e2f7feffb..ced82cb0d7ee 100644 --- a/pkgs/development/web/nodejs/v20.nix +++ b/pkgs/development/web/nodejs/v20.nix @@ -15,6 +15,7 @@ buildNodejs { version = "20.15.1"; sha256 = "sha256-/dU6VynZNmkaKhFRBG+0iXchy4sPyir5V4I6m0D+DDQ="; patches = [ + ./configure-emulator.patch ./disable-darwin-v8-system-instrumentation-node19.patch ./bypass-darwin-xcrun-node16.patch ./node-npm-build-npm-package-logic.patch diff --git a/pkgs/development/web/nodejs/v22.nix b/pkgs/development/web/nodejs/v22.nix index 44737d2ba8ec..58a34f8aff2a 100644 --- a/pkgs/development/web/nodejs/v22.nix +++ b/pkgs/development/web/nodejs/v22.nix @@ -15,6 +15,7 @@ buildNodejs { version = "22.4.1"; sha256 = "sha256-ZfyFf1qoJWqvyQCzRMARXJrq4loCVB/Vzg29Tf0cX7k="; patches = [ + ./configure-emulator.patch ./disable-darwin-v8-system-instrumentation-node19.patch ./bypass-darwin-xcrun-node16.patch ./node-npm-build-npm-package-logic.patch From d162d8e50de274431d97bcf28c70751481e2ca82 Mon Sep 17 00:00:00 2001 From: Ivan Trubach Date: Tue, 16 Jul 2024 15:14:51 +0300 Subject: [PATCH 2/5] nodejs: use ninja for build This change cleans up build inputs and adjusts the build to use ninja. --- .../nodejs/bypass-darwin-xcrun-node16.patch | 39 +++++++++++- pkgs/development/web/nodejs/nodejs.nix | 60 +++++++++++++++---- 2 files changed, 86 insertions(+), 13 deletions(-) diff --git a/pkgs/development/web/nodejs/bypass-darwin-xcrun-node16.patch b/pkgs/development/web/nodejs/bypass-darwin-xcrun-node16.patch index e86fcc4d2973..ba998c0510be 100644 --- a/pkgs/development/web/nodejs/bypass-darwin-xcrun-node16.patch +++ b/pkgs/development/web/nodejs/bypass-darwin-xcrun-node16.patch @@ -1,7 +1,42 @@ Avoids needing xcrun or xcodebuild in PATH for native package builds -diff --git a/deps/npm/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py b/deps/npm/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py -index a75d8ee..476440d 100644 +--- a/tools/gyp/pylib/gyp/xcode_emulation.py ++++ b/tools/gyp/pylib/gyp/xcode_emulation.py +@@ -522,7 +522,13 @@ class XcodeSettings: + # Since the CLT has no SDK paths anyway, returning None is the + # most sensible route and should still do the right thing. + try: +- return GetStdoutQuiet(["xcrun", "--sdk", sdk, infoitem]) ++ #return GetStdoutQuiet(["xcrun", "--sdk", sdk, infoitem]) ++ return { ++ "--show-sdk-platform-path": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform", ++ "--show-sdk-path": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk", ++ "--show-sdk-build-version": "19A547", ++ "--show-sdk-version": "10.15" ++ }[infoitem] + except GypError: + pass + +@@ -1499,7 +1505,8 @@ def XcodeVersion(): + version = "" + build = "" + try: +- version_list = GetStdoutQuiet(["xcodebuild", "-version"]).splitlines() ++ #version_list = GetStdoutQuiet(["xcodebuild", "-version"]).splitlines() ++ version_list = [] + # In some circumstances xcodebuild exits 0 but doesn't return + # the right results; for example, a user on 10.7 or 10.8 with + # a bogus path set via xcode-select +@@ -1510,7 +1517,8 @@ def XcodeVersion(): + version = version_list[0].split()[-1] # Last word on first line + build = version_list[-1].split()[-1] # Last word on last line + except GypError: # Xcode not installed so look for XCode Command Line Tools +- version = CLTVersion() # macOS Catalina returns 11.0.0.0.1.1567737322 ++ #version = CLTVersion() # macOS Catalina returns 11.0.0.0.1.1567737322 ++ version = "11.0.0.0.1.1567737322" + if not version: + raise GypError("No Xcode or CLT version detected!") + # Be careful to convert "4.2.3" to "0423" and "11.0.0" to "1100": --- a/deps/npm/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py +++ b/deps/npm/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py @@ -522,7 +522,13 @@ class XcodeSettings: diff --git a/pkgs/development/web/nodejs/nodejs.nix b/pkgs/development/web/nodejs/nodejs.nix index b89fe4853091..cda26e84ab30 100644 --- a/pkgs/development/web/nodejs/nodejs.nix +++ b/pkgs/development/web/nodejs/nodejs.nix @@ -1,12 +1,12 @@ { lib, stdenv, fetchurl, openssl, python, zlib, libuv, http-parser, icu, bash -, pkg-config, which, buildPackages +, ninja, pkgconf, unixtools, runCommand, buildPackages , testers # for `.pkgs` attribute , callPackage # Updater dependencies , writeScript, coreutils, gnugrep, jq, curl, common-updater-scripts, nix, runtimeShell , gnupg -, darwin, xcbuild +, darwin , installShellFiles }: @@ -104,6 +104,20 @@ let (name: "${lib.getDev sharedLibDeps.${name}}/include/*") (builtins.attrNames sharedLibDeps); + # Currently stdenv sets CC/LD/AR/etc environment variables to program names + # instead of absolute paths. If we add cctools to nativeBuildInputs, that + # would shadow stdenv’s bintools and potentially break other parts of the + # build. The correct behavior is to use absolute paths, and there is a PR for + # that, see https://github.com/NixOS/nixpkgs/pull/314920. As a temporary + # workaround, we use only a single program we need (and that is not part of + # the stdenv). + darwin-cctools-only-libtool = + # Would be nice to have onlyExe builder similar to onlyBin… + runCommand "darwin-cctools-only-libtool" { cctools = lib.getBin buildPackages.cctools; } '' + mkdir -p "$out/bin" + ln -s "$cctools/bin/libtool" "$out/bin/libtool" + ''; + package = stdenv.mkDerivation (finalAttrs: let /** the final package fixed point, after potential overrides */ @@ -128,11 +142,30 @@ let # NB: technically, we do not need bash in build inputs since all scripts are # wrappers over the corresponding JS scripts. There are some packages though # that use bash wrappers, e.g. polaris-web. - buildInputs = lib.optionals stdenv.isDarwin [ CoreServices ApplicationServices ] + buildInputs = lib.optionals stdenv.hostPlatform.isDarwin [ CoreServices ApplicationServices ] ++ [ zlib libuv openssl http-parser icu bash ]; - nativeBuildInputs = [ installShellFiles pkg-config python which ] - ++ lib.optionals stdenv.isDarwin [ xcbuild ]; + nativeBuildInputs = + [ + installShellFiles + ninja + pkgconf + python + ] + ++ lib.optionals stdenv.buildPlatform.isDarwin [ + # gyp checks `sysctl -n hw.memsize` if `sys.platform == "darwin"`. + unixtools.sysctl + ] + ++ lib.optionals stdenv.hostPlatform.isDarwin [ + # For gyp-mac-tool if `flavor == "mac"`. + darwin-cctools-only-libtool + ]; + + # We currently rely on Makefile and stdenv for build phases, so do not let + # ninja’s setup hook to override default stdenv phases. + dontUseNinjaBuild = true; + dontUseNinjaCheck = true; + dontUseNinjaInstall = true; outputs = [ "out" "libv8" ]; setOutputFlags = false; @@ -140,6 +173,9 @@ let configureFlags = [ + "--ninja" + "--with-intl=system-icu" + "--openssl-use-def-ca-store" "--no-cross-compiling" "--dest-os=${destOS}" "--dest-cpu=${destCPU}" @@ -167,11 +203,7 @@ let FIXME: the statement above is outdated, we have to include pkg-config in build inputs for system-icu. */ - ]) (builtins.attrNames sharedLibDeps) - ++ [ - "--with-intl=system-icu" - "--openssl-use-def-ca-store" - ]; + ]) (builtins.attrNames sharedLibDeps); configurePlatforms = [ ]; @@ -183,6 +215,12 @@ let enableParallelBuilding = true; + makeFlags = [ + # Tell ninja to avoid ANSI sequences, otherwise we don’t see build + # progress in Nix logs. + "TERM=dumb" + ]; + # Don't allow enabling content addressed conversion as `nodejs` # checksums it's image before conversion happens and image loading # breaks: @@ -299,7 +337,7 @@ let # assemble a static v8 library and put it in the 'libv8' output mkdir -p $libv8/lib - pushd out/Release/obj.target + pushd out/Release/obj find . -path "./torque_*/**/*.o" -or -path "./v8*/**/*.o" | sort -u >files $AR -cqs $libv8/lib/libv8.a @files popd From 3cf3f6ae23b0173acaa37bc3ba23031f9a60c0c6 Mon Sep 17 00:00:00 2001 From: Ivan Trubach Date: Wed, 17 Jul 2024 04:57:33 +0300 Subject: [PATCH 3/5] nodejs: allow building for vfpv2 --- .../web/nodejs/configure-armv6-vfpv2.patch | 15 +++++++++++++++ pkgs/development/web/nodejs/nodejs.nix | 1 + pkgs/development/web/nodejs/v18.nix | 1 + pkgs/development/web/nodejs/v20.nix | 1 + pkgs/development/web/nodejs/v22.nix | 1 + 5 files changed, 19 insertions(+) create mode 100644 pkgs/development/web/nodejs/configure-armv6-vfpv2.patch diff --git a/pkgs/development/web/nodejs/configure-armv6-vfpv2.patch b/pkgs/development/web/nodejs/configure-armv6-vfpv2.patch new file mode 100644 index 000000000000..0bb7c8160249 --- /dev/null +++ b/pkgs/development/web/nodejs/configure-armv6-vfpv2.patch @@ -0,0 +1,15 @@ +Allows ARM FPU to be set to vfpv2, e.g. for Raspberry Pi. + +See https://github.com/nodejs/node/issues/44357#issuecomment-1235821878 + +--- a/configure.py ++++ b/configure.py +@@ -50,7 +50,7 @@ + valid_arch = ('arm', 'arm64', 'ia32', 'mips', 'mipsel', 'mips64el', 'ppc', + 'ppc64', 'x64', 'x86', 'x86_64', 's390x', 'riscv64', 'loong64') + valid_arm_float_abi = ('soft', 'softfp', 'hard') +-valid_arm_fpu = ('vfp', 'vfpv3', 'vfpv3-d16', 'neon') ++valid_arm_fpu = ('vfp', 'vfpv2', 'vfpv3', 'vfpv3-d16', 'neon') + valid_mips_arch = ('loongson', 'r1', 'r2', 'r6', 'rx') + valid_mips_fpu = ('fp32', 'fp64', 'fpxx') + valid_mips_float_abi = ('soft', 'hard') diff --git a/pkgs/development/web/nodejs/nodejs.nix b/pkgs/development/web/nodejs/nodejs.nix index cda26e84ab30..dbb7bee53179 100644 --- a/pkgs/development/web/nodejs/nodejs.nix +++ b/pkgs/development/web/nodejs/nodejs.nix @@ -75,6 +75,7 @@ let if platform.isAarch32 && platform ? gcc.fpu then lib.throwIfNot (builtins.elem platform.gcc.fpu [ "vfp" + "vfpv2" "vfpv3" "vfpv3-d16" "neon" diff --git a/pkgs/development/web/nodejs/v18.nix b/pkgs/development/web/nodejs/v18.nix index b69f92d51c3a..e5343b17c0e0 100644 --- a/pkgs/development/web/nodejs/v18.nix +++ b/pkgs/development/web/nodejs/v18.nix @@ -27,6 +27,7 @@ buildNodejs { sha256 = "sha256-p2x+oblq62ljoViAYmDICUtiRNZKaWUp0CBUe5qVyio="; patches = [ ./configure-emulator-node18.patch + ./configure-armv6-vfpv2.patch ./disable-darwin-v8-system-instrumentation.patch ./bypass-darwin-xcrun-node16.patch ./revert-arm64-pointer-auth.patch diff --git a/pkgs/development/web/nodejs/v20.nix b/pkgs/development/web/nodejs/v20.nix index ced82cb0d7ee..e9a769d8911c 100644 --- a/pkgs/development/web/nodejs/v20.nix +++ b/pkgs/development/web/nodejs/v20.nix @@ -16,6 +16,7 @@ buildNodejs { sha256 = "sha256-/dU6VynZNmkaKhFRBG+0iXchy4sPyir5V4I6m0D+DDQ="; patches = [ ./configure-emulator.patch + ./configure-armv6-vfpv2.patch ./disable-darwin-v8-system-instrumentation-node19.patch ./bypass-darwin-xcrun-node16.patch ./node-npm-build-npm-package-logic.patch diff --git a/pkgs/development/web/nodejs/v22.nix b/pkgs/development/web/nodejs/v22.nix index 58a34f8aff2a..b51e0275af7a 100644 --- a/pkgs/development/web/nodejs/v22.nix +++ b/pkgs/development/web/nodejs/v22.nix @@ -16,6 +16,7 @@ buildNodejs { sha256 = "sha256-ZfyFf1qoJWqvyQCzRMARXJrq4loCVB/Vzg29Tf0cX7k="; patches = [ ./configure-emulator.patch + ./configure-armv6-vfpv2.patch ./disable-darwin-v8-system-instrumentation-node19.patch ./bypass-darwin-xcrun-node16.patch ./node-npm-build-npm-package-logic.patch From d7f46fbb5c40e7e9a165adf4f82669ae35c5b055 Mon Sep 17 00:00:00 2001 From: Ivan Trubach Date: Wed, 17 Jul 2024 20:14:57 +0300 Subject: [PATCH 4/5] nodejs: fix tests and disable parallel checking --- pkgs/development/web/nodejs/nodejs.nix | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pkgs/development/web/nodejs/nodejs.nix b/pkgs/development/web/nodejs/nodejs.nix index dbb7bee53179..93421c709c43 100644 --- a/pkgs/development/web/nodejs/nodejs.nix +++ b/pkgs/development/web/nodejs/nodejs.nix @@ -134,7 +134,14 @@ let strictDeps = true; - env = lib.optionalAttrs (stdenv.isDarwin && stdenv.isx86_64) { + env = { + # Tell ninja to avoid ANSI sequences, otherwise we don’t see build + # progress in Nix logs. + # + # Note: do not set TERM=dumb environment variable globally, it is used in + # test-ci-js test suite to skip tests that otherwise run fine. + NINJA = "TERM=dumb ninja"; + } // lib.optionalAttrs (stdenv.isDarwin && stdenv.isx86_64) { # Make sure libc++ uses `posix_memalign` instead of `aligned_alloc` on x86_64-darwin. # Otherwise, nodejs would require the 11.0 SDK and macOS 10.15+. NIX_CFLAGS_COMPILE = "-D__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=101300"; @@ -216,12 +223,6 @@ let enableParallelBuilding = true; - makeFlags = [ - # Tell ninja to avoid ANSI sequences, otherwise we don’t see build - # progress in Nix logs. - "TERM=dumb" - ]; - # Don't allow enabling content addressed conversion as `nodejs` # checksums it's image before conversion happens and image loading # breaks: @@ -246,6 +247,9 @@ let doCheck = canExecute; + # See https://github.com/nodejs/node/issues/22006 + enableParallelChecking = false; + # Some dependencies required for tools/doc/node_modules (and therefore # test-addons, jstest and others) target are not included in the tarball. # Run test targets that do not require network access. @@ -260,8 +264,6 @@ let # than a year (Node.js 18 will be EOL at 2025-04-30). Note that these # failures are specific to Nix sandbox on macOS and should not affect # actual functionality. - ] ++ lib.optionals (!stdenv.isDarwin) [ - # TODO: JS test suite is too flaky on Darwin; revisit at a later date. "test-ci-js" ]); From 31d238110c720ab467cbd7adb715ebca76f9e09d Mon Sep 17 00:00:00 2001 From: Ivan Trubach Date: Sat, 10 Aug 2024 10:32:22 +0300 Subject: [PATCH 5/5] nodejs: disable failing tests on x86_64-darwin --- pkgs/development/web/nodejs/nodejs.nix | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pkgs/development/web/nodejs/nodejs.nix b/pkgs/development/web/nodejs/nodejs.nix index 93421c709c43..a589757664e1 100644 --- a/pkgs/development/web/nodejs/nodejs.nix +++ b/pkgs/development/web/nodejs/nodejs.nix @@ -96,7 +96,7 @@ let null; # TODO: also handle MIPS flags (mips_arch, mips_fpu, mips_float_abi). - useSharedHttpParser = !stdenv.isDarwin && lib.versionOlder "${majorVersion}.${minorVersion}" "11.4"; + useSharedHttpParser = !stdenv.hostPlatform.isDarwin && lib.versionOlder "${majorVersion}.${minorVersion}" "11.4"; sharedLibDeps = { inherit openssl zlib libuv; } // (lib.optionalAttrs useSharedHttpParser { inherit http-parser; }); @@ -141,10 +141,10 @@ let # Note: do not set TERM=dumb environment variable globally, it is used in # test-ci-js test suite to skip tests that otherwise run fine. NINJA = "TERM=dumb ninja"; - } // lib.optionalAttrs (stdenv.isDarwin && stdenv.isx86_64) { + } // lib.optionalAttrs (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) { # Make sure libc++ uses `posix_memalign` instead of `aligned_alloc` on x86_64-darwin. # Otherwise, nodejs would require the 11.0 SDK and macOS 10.15+. - NIX_CFLAGS_COMPILE = "-D__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=101300"; + NIX_CFLAGS_COMPILE = "-D__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=101300 -Wno-macro-redefined"; }; # NB: technically, we do not need bash in build inputs since all scripts are @@ -286,7 +286,7 @@ let "test-tls-cli-max-version-1.3" "test-tls-client-auth" "test-tls-sni-option" - ] ++ lib.optionals stdenv.hostPlatform.isDarwin [ + ] ++ lib.optionals stdenv.buildPlatform.isDarwin [ # Disable tests that don’t work under macOS sandbox. "test-macos-app-sandbox" "test-os" @@ -311,6 +311,11 @@ let "test-runner-run" "test-runner-watch-mode" "test-watch-mode-files_watcher" + ] ++ lib.optionals (stdenv.buildPlatform.isDarwin && stdenv.buildPlatform.isx86_64) [ + # These tests fail on x86_64-darwin (even without sandbox). + # TODO: revisit at a later date. + "test-fs-readv" + "test-fs-readv-sync" ])}" ];