blob: deea53c9ec657548fef33602301062200ca504f4 [file] [log] [blame]
Andrew Geissler82c905d2020-04-13 13:39:40 -05001# Copyright (C) 2020 Savoir-Faire Linux
2#
3# SPDX-License-Identifier: GPL-2.0-only
4#
5# This bbclass builds and installs an npm package to the target. The package
6# sources files should be fetched in the calling recipe by using the SRC_URI
7# variable. The ${S} variable should be updated depending of your fetcher.
8#
9# Usage:
10# SRC_URI = "..."
11# inherit npm
12#
13# Optional variables:
14# NPM_ARCH:
15# Override the auto generated npm architecture.
16#
17# NPM_INSTALL_DEV:
18# Set to 1 to also install devDependencies.
19
Andrew Geisslerd1e89492021-02-12 15:35:20 -060020inherit python3native
21
Andrew Geissler615f2f12022-07-15 14:00:58 -050022DEPENDS:prepend = "nodejs-native nodejs-oe-cache-native "
Patrick Williams213cb262021-08-07 19:21:33 -050023RDEPENDS:${PN}:append:class-target = " nodejs"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050024
Andrew Geisslereff27472021-10-29 15:35:00 -050025EXTRA_OENPM = ""
26
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080027NPM_INSTALL_DEV ?= "0"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060028
Andrew Geisslereff27472021-10-29 15:35:00 -050029NPM_NODEDIR ?= "${RECIPE_SYSROOT_NATIVE}${prefix_native}"
30
Andrew Geissler82c905d2020-04-13 13:39:40 -050031def npm_target_arch_map(target_arch):
32 """Maps arch names to npm arch names"""
33 import re
34 if re.match("p(pc|owerpc)(|64)", target_arch):
35 return "ppc"
36 elif re.match("i.86$", target_arch):
37 return "ia32"
38 elif re.match("x86_64$", target_arch):
39 return "x64"
40 elif re.match("arm64$", target_arch):
41 return "arm"
42 return target_arch
43
44NPM_ARCH ?= "${@npm_target_arch_map(d.getVar("TARGET_ARCH"))}"
45
46NPM_PACKAGE = "${WORKDIR}/npm-package"
47NPM_CACHE = "${WORKDIR}/npm-cache"
48NPM_BUILD = "${WORKDIR}/npm-build"
Andrew Geissler615f2f12022-07-15 14:00:58 -050049NPM_REGISTRY = "${WORKDIR}/npm-registry"
Andrew Geissler82c905d2020-04-13 13:39:40 -050050
51def npm_global_configs(d):
52 """Get the npm global configuration"""
53 configs = []
54 # Ensure no network access is done
55 configs.append(("offline", "true"))
56 configs.append(("proxy", "http://invalid"))
Andrew Geissler615f2f12022-07-15 14:00:58 -050057 configs.append(("funds", False))
58 configs.append(("audit", False))
Andrew Geissler82c905d2020-04-13 13:39:40 -050059 # Configure the cache directory
60 configs.append(("cache", d.getVar("NPM_CACHE")))
61 return configs
62
Andrew Geissler615f2f12022-07-15 14:00:58 -050063## 'npm pack' runs 'prepare' and 'prepack' scripts. Support for
64## 'ignore-scripts' which prevents this behavior has been removed
65## from nodejs 16. Use simple 'tar' instead of.
Andrew Geissler82c905d2020-04-13 13:39:40 -050066def npm_pack(env, srcdir, workdir):
Andrew Geissler615f2f12022-07-15 14:00:58 -050067 """Emulate 'npm pack' on a specified directory"""
68 import subprocess
69 import os
70 import json
71
72 src = os.path.join(srcdir, 'package.json')
73 with open(src) as f:
74 j = json.load(f)
75
76 # base does not really matter and is for documentation purposes
77 # only. But the 'version' part must exist because other parts of
78 # the bbclass rely on it.
79 base = j['name'].split('/')[-1]
80 tarball = os.path.join(workdir, "%s-%s.tgz" % (base, j['version']));
81
82 # TODO: real 'npm pack' does not include directories while 'tar'
83 # does. But this does not seem to matter...
84 subprocess.run(['tar', 'czf', tarball,
85 '--exclude', './node-modules',
86 '--exclude-vcs',
87 '--transform', 's,^\./,package/,',
88 '--mtime', '1985-10-26T08:15:00.000Z',
89 '.'],
90 check = True, cwd = srcdir)
91
92 return (tarball, j)
Andrew Geissler82c905d2020-04-13 13:39:40 -050093
94python npm_do_configure() {
95 """
96 Step one: configure the npm cache and the main npm package
97
98 Every dependencies have been fetched and patched in the source directory.
99 They have to be packed (this remove unneeded files) and added to the npm
100 cache to be available for the next step.
101
102 The main package and its associated manifest file and shrinkwrap file have
103 to be configured to take into account these cached dependencies.
104 """
105 import base64
106 import copy
107 import json
108 import re
109 import shlex
Andrew Geisslerd5838332022-05-27 11:33:10 -0500110 import stat
Andrew Geissler82c905d2020-04-13 13:39:40 -0500111 import tempfile
112 from bb.fetch2.npm import NpmEnvironment
113 from bb.fetch2.npm import npm_unpack
114 from bb.fetch2.npmsw import foreach_dependencies
115 from bb.progress import OutOfProgressHandler
Andrew Geissler615f2f12022-07-15 14:00:58 -0500116 from oe.npm_registry import NpmRegistry
Andrew Geissler82c905d2020-04-13 13:39:40 -0500117
118 bb.utils.remove(d.getVar("NPM_CACHE"), recurse=True)
119 bb.utils.remove(d.getVar("NPM_PACKAGE"), recurse=True)
120
121 env = NpmEnvironment(d, configs=npm_global_configs(d))
Andrew Geissler615f2f12022-07-15 14:00:58 -0500122 registry = NpmRegistry(d.getVar('NPM_REGISTRY'), d.getVar('NPM_CACHE'))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500123
Andrew Geissler615f2f12022-07-15 14:00:58 -0500124 def _npm_cache_add(tarball, pkg):
125 """Add tarball to local registry and register it in the
126 cache"""
127 registry.add_pkg(tarball, pkg)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500128
129 def _npm_integrity(tarball):
130 """Return the npm integrity of a specified tarball"""
131 sha512 = bb.utils.sha512_file(tarball)
132 return "sha512-" + base64.b64encode(bytes.fromhex(sha512)).decode()
133
Andrew Geissler82c905d2020-04-13 13:39:40 -0500134 def _npmsw_dependency_dict(orig, deptree):
135 """
136 Return the sub dictionary in the 'orig' dictionary corresponding to the
137 'deptree' dependency tree. This function follows the shrinkwrap file
138 format.
139 """
140 ptr = orig
141 for dep in deptree:
142 if "dependencies" not in ptr:
143 ptr["dependencies"] = {}
144 ptr = ptr["dependencies"]
145 if dep not in ptr:
146 ptr[dep] = {}
147 ptr = ptr[dep]
148 return ptr
149
150 # Manage the manifest file and shrinkwrap files
151 orig_manifest_file = d.expand("${S}/package.json")
152 orig_shrinkwrap_file = d.expand("${S}/npm-shrinkwrap.json")
153 cached_manifest_file = d.expand("${NPM_PACKAGE}/package.json")
154 cached_shrinkwrap_file = d.expand("${NPM_PACKAGE}/npm-shrinkwrap.json")
155
156 with open(orig_manifest_file, "r") as f:
157 orig_manifest = json.load(f)
158
159 cached_manifest = copy.deepcopy(orig_manifest)
160 cached_manifest.pop("dependencies", None)
161 cached_manifest.pop("devDependencies", None)
162
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600163 has_shrinkwrap_file = True
Andrew Geissler82c905d2020-04-13 13:39:40 -0500164
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600165 try:
166 with open(orig_shrinkwrap_file, "r") as f:
167 orig_shrinkwrap = json.load(f)
168 except IOError:
169 has_shrinkwrap_file = False
170
171 if has_shrinkwrap_file:
172 cached_shrinkwrap = copy.deepcopy(orig_shrinkwrap)
173 cached_shrinkwrap.pop("dependencies", None)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500174
175 # Manage the dependencies
176 progress = OutOfProgressHandler(d, r"^(\d+)/(\d+)$")
177 progress_total = 1 # also count the main package
178 progress_done = 0
179
180 def _count_dependency(name, params, deptree):
181 nonlocal progress_total
182 progress_total += 1
183
184 def _cache_dependency(name, params, deptree):
185 destsubdirs = [os.path.join("node_modules", dep) for dep in deptree]
186 destsuffix = os.path.join(*destsubdirs)
187 with tempfile.TemporaryDirectory() as tmpdir:
188 # Add the dependency to the npm cache
189 destdir = os.path.join(d.getVar("S"), destsuffix)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500190 (tarball, pkg) = npm_pack(env, destdir, tmpdir)
191 _npm_cache_add(tarball, pkg)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500192 # Add its signature to the cached shrinkwrap
193 dep = _npmsw_dependency_dict(cached_shrinkwrap, deptree)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500194 dep["version"] = pkg['version']
Andrew Geissler82c905d2020-04-13 13:39:40 -0500195 dep["integrity"] = _npm_integrity(tarball)
196 if params.get("dev", False):
197 dep["dev"] = True
198 # Display progress
199 nonlocal progress_done
200 progress_done += 1
201 progress.write("%d/%d" % (progress_done, progress_total))
202
203 dev = bb.utils.to_boolean(d.getVar("NPM_INSTALL_DEV"), False)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600204
205 if has_shrinkwrap_file:
206 foreach_dependencies(orig_shrinkwrap, _count_dependency, dev)
207 foreach_dependencies(orig_shrinkwrap, _cache_dependency, dev)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500208
209 # Configure the main package
210 with tempfile.TemporaryDirectory() as tmpdir:
Andrew Geissler615f2f12022-07-15 14:00:58 -0500211 (tarball, _) = npm_pack(env, d.getVar("S"), tmpdir)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500212 npm_unpack(tarball, d.getVar("NPM_PACKAGE"), d)
213
214 # Configure the cached manifest file and cached shrinkwrap file
215 def _update_manifest(depkey):
216 for name in orig_manifest.get(depkey, {}):
217 version = cached_shrinkwrap["dependencies"][name]["version"]
218 if depkey not in cached_manifest:
219 cached_manifest[depkey] = {}
220 cached_manifest[depkey][name] = version
221
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600222 if has_shrinkwrap_file:
223 _update_manifest("dependencies")
Andrew Geissler82c905d2020-04-13 13:39:40 -0500224
225 if dev:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600226 if has_shrinkwrap_file:
227 _update_manifest("devDependencies")
Andrew Geissler82c905d2020-04-13 13:39:40 -0500228
Andrew Geisslerd5838332022-05-27 11:33:10 -0500229 os.chmod(cached_manifest_file, os.stat(cached_manifest_file).st_mode | stat.S_IWUSR)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500230 with open(cached_manifest_file, "w") as f:
231 json.dump(cached_manifest, f, indent=2)
232
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600233 if has_shrinkwrap_file:
234 with open(cached_shrinkwrap_file, "w") as f:
235 json.dump(cached_shrinkwrap, f, indent=2)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500236}
237
238python npm_do_compile() {
239 """
240 Step two: install the npm package
241
242 Use the configured main package and the cached dependencies to run the
243 installation process. The installation is done in a directory which is
244 not the destination directory yet.
245
246 A combination of 'npm pack' and 'npm install' is used to ensure that the
247 installed files are actual copies instead of symbolic links (which is the
248 default npm behavior).
249 """
250 import shlex
251 import tempfile
252 from bb.fetch2.npm import NpmEnvironment
253
254 bb.utils.remove(d.getVar("NPM_BUILD"), recurse=True)
255
Andrew Geissler82c905d2020-04-13 13:39:40 -0500256 with tempfile.TemporaryDirectory() as tmpdir:
257 args = []
Andrew Geisslereff27472021-10-29 15:35:00 -0500258 configs = npm_global_configs(d)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500259
Andrew Geisslereff27472021-10-29 15:35:00 -0500260 if bb.utils.to_boolean(d.getVar("NPM_INSTALL_DEV"), False):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500261 configs.append(("also", "development"))
262 else:
263 configs.append(("only", "production"))
264
265 # Report as many logs as possible for debugging purpose
266 configs.append(("loglevel", "silly"))
267
268 # Configure the installation to be done globally in the build directory
269 configs.append(("global", "true"))
270 configs.append(("prefix", d.getVar("NPM_BUILD")))
271
272 # Add node-gyp configuration
273 configs.append(("arch", d.getVar("NPM_ARCH")))
274 configs.append(("release", "true"))
Andrew Geisslereff27472021-10-29 15:35:00 -0500275 configs.append(("nodedir", d.getVar("NPM_NODEDIR")))
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600276 configs.append(("python", d.getVar("PYTHON")))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500277
Andrew Geisslereff27472021-10-29 15:35:00 -0500278 env = NpmEnvironment(d, configs)
279
Andrew Geissler82c905d2020-04-13 13:39:40 -0500280 # Add node-pre-gyp configuration
281 args.append(("target_arch", d.getVar("NPM_ARCH")))
282 args.append(("build-from-source", "true"))
283
284 # Pack and install the main package
Andrew Geissler615f2f12022-07-15 14:00:58 -0500285 (tarball, _) = npm_pack(env, d.getVar("NPM_PACKAGE"), tmpdir)
Andrew Geisslereff27472021-10-29 15:35:00 -0500286 cmd = "npm install %s %s" % (shlex.quote(tarball), d.getVar("EXTRA_OENPM"))
287 env.run(cmd, args=args)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500288}
289
290npm_do_install() {
Andrew Geissler82c905d2020-04-13 13:39:40 -0500291 # Step three: final install
292 #
293 # The previous installation have to be filtered to remove some extra files.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500294
Andrew Geissler82c905d2020-04-13 13:39:40 -0500295 rm -rf ${D}
296
297 # Copy the entire lib and bin directories
298 install -d ${D}/${nonarch_libdir}
299 cp --no-preserve=ownership --recursive ${NPM_BUILD}/lib/. ${D}/${nonarch_libdir}
300
301 if [ -d "${NPM_BUILD}/bin" ]
302 then
303 install -d ${D}/${bindir}
304 cp --no-preserve=ownership --recursive ${NPM_BUILD}/bin/. ${D}/${bindir}
305 fi
306
307 # If the package (or its dependencies) uses node-gyp to build native addons,
308 # object files, static libraries or other temporary files can be hidden in
309 # the lib directory. To reduce the package size and to avoid QA issues
310 # (staticdev with static library files) these files must be removed.
311 local GYP_REGEX=".*/build/Release/[^/]*.node"
312
313 # Remove any node-gyp directory in ${D} to remove temporary build files
314 for GYP_D_FILE in $(find ${D} -regex "${GYP_REGEX}")
315 do
316 local GYP_D_DIR=${GYP_D_FILE%/Release/*}
317
318 rm --recursive --force ${GYP_D_DIR}
319 done
320
321 # Copy only the node-gyp release files
322 for GYP_B_FILE in $(find ${NPM_BUILD} -regex "${GYP_REGEX}")
323 do
324 local GYP_D_FILE=${D}/${prefix}/${GYP_B_FILE#${NPM_BUILD}}
325
326 install -d ${GYP_D_FILE%/*}
327 install -m 755 ${GYP_B_FILE} ${GYP_D_FILE}
328 done
329
330 # Remove the shrinkwrap file which does not need to be packed
331 rm -f ${D}/${nonarch_libdir}/node_modules/*/npm-shrinkwrap.json
332 rm -f ${D}/${nonarch_libdir}/node_modules/@*/*/npm-shrinkwrap.json
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500333}
334
Patrick Williams213cb262021-08-07 19:21:33 -0500335FILES:${PN} += " \
Brad Bishop15ae2502019-06-18 21:44:24 -0400336 ${bindir} \
Andrew Geissler82c905d2020-04-13 13:39:40 -0500337 ${nonarch_libdir} \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500338"
339
Andrew Geissler82c905d2020-04-13 13:39:40 -0500340EXPORT_FUNCTIONS do_configure do_compile do_install