summaryrefslogtreecommitdiffstats
path: root/meta/classes/npm.bbclass
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2022-08-10 14:35:29 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2022-08-12 15:27:17 +0100
commitfd1517e2b51a170f2427122c6b95396db251d827 (patch)
treedabfe3e631339c2fc99a9ee7febb0f9c128e325e /meta/classes/npm.bbclass
parent10317912ee319ccf7f83605d438b5cbf9663f296 (diff)
downloadpoky-fd1517e2b51a170f2427122c6b95396db251d827.tar.gz
classes: Update classes to match new bitbake class scope functionality
Move classes to classes-global or classes-recipe as appropriate to take advantage of new bitbake functionality to check class scope/usage. (From OE-Core rev: f5c128008365e141082c129417eb72d2751e8045) Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/classes/npm.bbclass')
-rw-r--r--meta/classes/npm.bbclass340
1 files changed, 0 insertions, 340 deletions
diff --git a/meta/classes/npm.bbclass b/meta/classes/npm.bbclass
deleted file mode 100644
index deea53c9ec..0000000000
--- a/meta/classes/npm.bbclass
+++ /dev/null
@@ -1,340 +0,0 @@
1# 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
20inherit python3native
21
22DEPENDS:prepend = "nodejs-native nodejs-oe-cache-native "
23RDEPENDS:${PN}:append:class-target = " nodejs"
24
25EXTRA_OENPM = ""
26
27NPM_INSTALL_DEV ?= "0"
28
29NPM_NODEDIR ?= "${RECIPE_SYSROOT_NATIVE}${prefix_native}"
30
31def 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"
49NPM_REGISTRY = "${WORKDIR}/npm-registry"
50
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"))
57 configs.append(("funds", False))
58 configs.append(("audit", False))
59 # Configure the cache directory
60 configs.append(("cache", d.getVar("NPM_CACHE")))
61 return configs
62
63## '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.
66def npm_pack(env, srcdir, workdir):
67 """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)
93
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
110 import stat
111 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
116 from oe.npm_registry import NpmRegistry
117
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))
122 registry = NpmRegistry(d.getVar('NPM_REGISTRY'), d.getVar('NPM_CACHE'))
123
124 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)
128
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
134 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
163 has_shrinkwrap_file = True
164
165 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)
174
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)
190 (tarball, pkg) = npm_pack(env, destdir, tmpdir)
191 _npm_cache_add(tarball, pkg)
192 # Add its signature to the cached shrinkwrap
193 dep = _npmsw_dependency_dict(cached_shrinkwrap, deptree)
194 dep["version"] = pkg['version']
195 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)
204
205 if has_shrinkwrap_file:
206 foreach_dependencies(orig_shrinkwrap, _count_dependency, dev)
207 foreach_dependencies(orig_shrinkwrap, _cache_dependency, dev)
208
209 # Configure the main package
210 with tempfile.TemporaryDirectory() as tmpdir:
211 (tarball, _) = npm_pack(env, d.getVar("S"), tmpdir)
212 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
222 if has_shrinkwrap_file:
223 _update_manifest("dependencies")
224
225 if dev:
226 if has_shrinkwrap_file:
227 _update_manifest("devDependencies")
228
229 os.chmod(cached_manifest_file, os.stat(cached_manifest_file).st_mode | stat.S_IWUSR)
230 with open(cached_manifest_file, "w") as f:
231 json.dump(cached_manifest, f, indent=2)
232
233 if has_shrinkwrap_file:
234 with open(cached_shrinkwrap_file, "w") as f:
235 json.dump(cached_shrinkwrap, f, indent=2)
236}
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
256 with tempfile.TemporaryDirectory() as tmpdir:
257 args = []
258 configs = npm_global_configs(d)
259
260 if bb.utils.to_boolean(d.getVar("NPM_INSTALL_DEV"), False):
261 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"))
275 configs.append(("nodedir", d.getVar("NPM_NODEDIR")))
276 configs.append(("python", d.getVar("PYTHON")))
277
278 env = NpmEnvironment(d, configs)
279
280 # 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
285 (tarball, _) = npm_pack(env, d.getVar("NPM_PACKAGE"), tmpdir)
286 cmd = "npm install %s %s" % (shlex.quote(tarball), d.getVar("EXTRA_OENPM"))
287 env.run(cmd, args=args)
288}
289
290npm_do_install() {
291 # Step three: final install
292 #
293 # The previous installation have to be filtered to remove some extra files.
294
295 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
333}
334
335FILES:${PN} += " \
336 ${bindir} \
337 ${nonarch_libdir} \
338"
339
340EXPORT_FUNCTIONS do_configure do_compile do_install