Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 20 | inherit python3native |
| 21 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 22 | DEPENDS:prepend = "nodejs-native nodejs-oe-cache-native " |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 23 | RDEPENDS:${PN}:append:class-target = " nodejs" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 24 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 25 | EXTRA_OENPM = "" |
| 26 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 27 | NPM_INSTALL_DEV ?= "0" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 29 | NPM_NODEDIR ?= "${RECIPE_SYSROOT_NATIVE}${prefix_native}" |
| 30 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 31 | def 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 | |
| 44 | NPM_ARCH ?= "${@npm_target_arch_map(d.getVar("TARGET_ARCH"))}" |
| 45 | |
| 46 | NPM_PACKAGE = "${WORKDIR}/npm-package" |
| 47 | NPM_CACHE = "${WORKDIR}/npm-cache" |
| 48 | NPM_BUILD = "${WORKDIR}/npm-build" |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 49 | NPM_REGISTRY = "${WORKDIR}/npm-registry" |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 50 | |
| 51 | def 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 Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 57 | configs.append(("funds", False)) |
| 58 | configs.append(("audit", False)) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 59 | # Configure the cache directory |
| 60 | configs.append(("cache", d.getVar("NPM_CACHE"))) |
| 61 | return configs |
| 62 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 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. |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 66 | def npm_pack(env, srcdir, workdir): |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 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) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 93 | |
| 94 | python 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 Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 110 | import stat |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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 |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 116 | from oe.npm_registry import NpmRegistry |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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)) |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 122 | registry = NpmRegistry(d.getVar('NPM_REGISTRY'), d.getVar('NPM_CACHE')) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 123 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 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) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 163 | has_shrinkwrap_file = True |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 164 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 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) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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) |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 190 | (tarball, pkg) = npm_pack(env, destdir, tmpdir) |
| 191 | _npm_cache_add(tarball, pkg) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 192 | # Add its signature to the cached shrinkwrap |
| 193 | dep = _npmsw_dependency_dict(cached_shrinkwrap, deptree) |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 194 | dep["version"] = pkg['version'] |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 204 | |
| 205 | if has_shrinkwrap_file: |
| 206 | foreach_dependencies(orig_shrinkwrap, _count_dependency, dev) |
| 207 | foreach_dependencies(orig_shrinkwrap, _cache_dependency, dev) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 208 | |
| 209 | # Configure the main package |
| 210 | with tempfile.TemporaryDirectory() as tmpdir: |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 211 | (tarball, _) = npm_pack(env, d.getVar("S"), tmpdir) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 222 | if has_shrinkwrap_file: |
| 223 | _update_manifest("dependencies") |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 224 | |
| 225 | if dev: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 226 | if has_shrinkwrap_file: |
| 227 | _update_manifest("devDependencies") |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 228 | |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 229 | os.chmod(cached_manifest_file, os.stat(cached_manifest_file).st_mode | stat.S_IWUSR) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 230 | with open(cached_manifest_file, "w") as f: |
| 231 | json.dump(cached_manifest, f, indent=2) |
| 232 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 233 | if has_shrinkwrap_file: |
| 234 | with open(cached_shrinkwrap_file, "w") as f: |
| 235 | json.dump(cached_shrinkwrap, f, indent=2) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | python 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 256 | with tempfile.TemporaryDirectory() as tmpdir: |
| 257 | args = [] |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 258 | configs = npm_global_configs(d) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 259 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 260 | if bb.utils.to_boolean(d.getVar("NPM_INSTALL_DEV"), False): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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")) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 275 | configs.append(("nodedir", d.getVar("NPM_NODEDIR"))) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 276 | configs.append(("python", d.getVar("PYTHON"))) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 277 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 278 | env = NpmEnvironment(d, configs) |
| 279 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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 |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 285 | (tarball, _) = npm_pack(env, d.getVar("NPM_PACKAGE"), tmpdir) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 286 | cmd = "npm install %s %s" % (shlex.quote(tarball), d.getVar("EXTRA_OENPM")) |
| 287 | env.run(cmd, args=args) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | npm_do_install() { |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 291 | # Step three: final install |
| 292 | # |
| 293 | # The previous installation have to be filtered to remove some extra files. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 294 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 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 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 333 | } |
| 334 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 335 | FILES:${PN} += " \ |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 336 | ${bindir} \ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 337 | ${nonarch_libdir} \ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 338 | " |
| 339 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 340 | EXPORT_FUNCTIONS do_configure do_compile do_install |