From 359eb5072d832aaab9328cbc84e57f0fb093f998 Mon Sep 17 00:00:00 2001 From: Giulio Benetti Date: Tue, 31 Oct 2023 10:01:14 +0100 Subject: [PATCH] package/nodejs: fix build of modules with native code When installing node modules with native code, said code needs to be built for the target; npm will delegate that to an internal copy of gyp, node_gyp, which uses a python script to generate the build configuration matching the target. That script is generated by the nodejs build system (at configure time), and eventually installed to staging alongside the headers. Currently, we tell node_gyp to use the script in the nodejs build directory, with the npm_config_nodedir environment variable: npm_config_nodedir=$(BUILD_DIR)/nodejs-$(NODEJS_VERSION) However, that is no longer working since commit 4cbc2af604e7 (package/nodejs: rename to nodejs-src and convert to virtual package), for two reasons: - the version variable NODEJS_VERSION was renamed to NODEJS_COMMON_VERSION; - the actual build directory is that of nodejs-src, not nodejs. When installing node modules with NODEJS_SRC_MODULES_LIST, we happen to still be running in the nodejs-src package, so it kinda makes sense that we use the file in nodejs-src build directory. But the NPM macro can also be called from other packages, in which case it looks uglier to have to look into nosejs-src build dir from another package context. Looking at the documentation for node-gyp [0], we can see that nodedir is meant to point to the path of the node source code; | `--nodedir=$path` | Set the path to the node source code However, that is only valid when building natively; here we are more similar to the "Third Party Node.js Runtimes", where the runtime nodejs is not the one doing the build; in that case, the following looks more appropriate: [...] you should use `--dist-url` or `--nodedir` flags to specify the headers of the runtime to build for. Also when `--dist-url` or `--nodedir` flags are passed, node-gyp will use the `config.gypi` shipped in the headers distribution to generate build configurations [...] So, we are going that route, and point npm_config_nodedir to the staging dir, where the headers are to be found. This is valid when installing modules with NODEJS_SRC_MODULES_LIST, as nodejs is already installed in staging at that time, as the package infra guarantees that staging is installed before target; it alsop works for modules installed in later packages (as long as they have nodejs in their dependencies, of course). Fixes: https://bugs.busybox.net/show_bug.cgi?id=15826 [0] https://github.com/nodejs/node/blob/main/deps/npm/node_modules/node-gyp/README.md Signed-off-by: Giulio Benetti [yann.morin.1998@free.fr: - use STAGING_DIR/usr, not TARGET_DIR/usr - extend commit log to explain what and why ] Signed-off-by: Yann E. MORIN --- package/nodejs/nodejs.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/nodejs/nodejs.mk b/package/nodejs/nodejs.mk index 7c8345cbcf..9806a64d38 100644 --- a/package/nodejs/nodejs.mk +++ b/package/nodejs/nodejs.mk @@ -20,7 +20,7 @@ NODEJS_BIN_ENV = $(TARGET_CONFIGURE_OPTS) \ npm_config_arch=$(NODEJS_CPU) \ npm_config_target_arch=$(NODEJS_CPU) \ npm_config_build_from_source=true \ - npm_config_nodedir=$(BUILD_DIR)/nodejs-$(NODEJS_VERSION) \ + npm_config_nodedir=$(STAGING_DIR)/usr \ npm_config_prefix=$(TARGET_DIR)/usr \ npm_config_cache=$(BUILD_DIR)/.npm-cache