kumquat-buildroot/package/nodejs
Giulio Benetti 359eb5072d 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 4cbc2af604
(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 <giulio.benetti@benettiengineering.com>
[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 <yann.morin.1998@free.fr>
2023-11-07 22:34:57 +01:00
..
nodejs-bin package/nodejs/nodejs-bin: new provider of host-nodejs 2023-10-06 17:31:14 +02:00
nodejs-src package/nodejs/nodejs-src: fix arch check on additional modules 2023-11-07 22:31:43 +01:00
Config.in package/nodejs: bump version to 20.9.0 2023-11-03 20:59:28 +01:00
Config.in.host package/nodejs/nodejs-bin: new provider of host-nodejs 2023-10-06 17:31:14 +02:00
nodejs.hash package/nodejs: fix legal info 2023-11-06 22:05:25 +01:00
nodejs.mk package/nodejs: fix build of modules with native code 2023-11-07 22:34:57 +01:00