buildroot/package/nodejs/nodejs.mk
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

40 lines
1.3 KiB
Makefile

################################################################################
#
# nodejs
#
################################################################################
# _VERSION, _SOURCE and _SITE must be kept empty to avoid downloading anything
NODEJS_COMMON_VERSION = 20.9.0
NODEJS_COMMON_SOURCE = node-v$(NODEJS_COMMON_VERSION).tar.xz
NODEJS_COMMON_SITE = http://nodejs.org/dist/v$(NODEJS_COMMON_VERSION)
NODEJS_LICENSE = MIT (core code); MIT, Apache and BSD family licenses (Bundled components)
NODEJS_LICENSE_FILES = LICENSE
NODEJS_CPE_ID_VENDOR = nodejs
NODEJS_CPE_ID_PRODUCT = node.js
NODEJS_BIN_ENV = $(TARGET_CONFIGURE_OPTS) \
LDFLAGS="$(NODEJS_LDFLAGS)" \
LD="$(TARGET_CXX)" \
npm_config_arch=$(NODEJS_CPU) \
npm_config_target_arch=$(NODEJS_CPU) \
npm_config_build_from_source=true \
npm_config_nodedir=$(STAGING_DIR)/usr \
npm_config_prefix=$(TARGET_DIR)/usr \
npm_config_cache=$(BUILD_DIR)/.npm-cache
# Define various packaging tools for other packages to use
NPM = $(NODEJS_BIN_ENV) $(HOST_DIR)/bin/npm
ifeq ($(BR2_PACKAGE_HOST_NODEJS_COREPACK),y)
COREPACK = $(NODEJS_BIN_ENV) $(HOST_DIR)/bin/corepack
PNPM = $(NODEJS_BIN_ENV) $(HOST_DIR)/bin/pnpm
YARN = $(NODEJS_BIN_ENV) $(HOST_DIR)/bin/yarn
endif
NODEJS_DEPENDENCIES = nodejs-src
$(eval $(generic-package))
$(eval $(host-virtual-package))
include $(sort $(wildcard package/nodejs/*/*.mk))