| 1 | 'use strict'
|
|---|
| 2 | // Copyright (c) 2018 inspiredware
|
|---|
| 3 |
|
|---|
| 4 | var path = require('path')
|
|---|
| 5 | var pkg = require(path.resolve('package.json'))
|
|---|
| 6 |
|
|---|
| 7 | var versionArray = process.version
|
|---|
| 8 | .substr(1)
|
|---|
| 9 | .replace(/-.*$/, '')
|
|---|
| 10 | .split('.')
|
|---|
| 11 | .map(function (item) {
|
|---|
| 12 | return +item
|
|---|
| 13 | })
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | *
|
|---|
| 17 | * A set of utilities to assist developers of tools that build
|
|---|
| 18 | * [N-API](https://nodejs.org/api/n-api.html#n_api_n_api) native add-ons.
|
|---|
| 19 | *
|
|---|
| 20 | * The main repository can be found
|
|---|
| 21 | * [here](https://github.com/inspiredware/napi-build-utils#napi-build-utils).
|
|---|
| 22 | *
|
|---|
| 23 | * @module napi-build-utils
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Implements a consistent name of `napi` for N-API runtimes.
|
|---|
| 28 | *
|
|---|
| 29 | * @param {string} runtime The runtime string.
|
|---|
| 30 | * @returns {boolean}
|
|---|
| 31 | */
|
|---|
| 32 | exports.isNapiRuntime = function (runtime) {
|
|---|
| 33 | return runtime === 'napi'
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Determines whether the specified N-API version is supported
|
|---|
| 38 | * by both the currently running Node instance and the package.
|
|---|
| 39 | *
|
|---|
| 40 | * @param {string} napiVersion The N-API version to check.
|
|---|
| 41 | * @returns {boolean}
|
|---|
| 42 | */
|
|---|
| 43 | exports.isSupportedVersion = function (napiVersion) {
|
|---|
| 44 | var version = parseInt(napiVersion, 10)
|
|---|
| 45 | return version <= exports.getNapiVersion() && exports.packageSupportsVersion(version)
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Determines whether the specified N-API version is supported by the package.
|
|---|
| 50 | * The N-API version must be present in the `package.json`
|
|---|
| 51 | * `binary.napi_versions` array.
|
|---|
| 52 | *
|
|---|
| 53 | * @param {number} napiVersion The N-API version to check.
|
|---|
| 54 | * @returns {boolean}
|
|---|
| 55 | * @private
|
|---|
| 56 | */
|
|---|
| 57 | exports.packageSupportsVersion = function (napiVersion) {
|
|---|
| 58 | if (pkg.binary && pkg.binary.napi_versions &&
|
|---|
| 59 | pkg.binary.napi_versions instanceof Array) { // integer array
|
|---|
| 60 | for (var i = 0; i < pkg.binary.napi_versions.length; i++) {
|
|---|
| 61 | if (pkg.binary.napi_versions[i] === napiVersion) return true
|
|---|
| 62 | };
|
|---|
| 63 | };
|
|---|
| 64 | return false
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Issues a warning to the supplied log if the N-API version is not supported
|
|---|
| 69 | * by the current Node instance or if the N-API version is not supported
|
|---|
| 70 | * by the package.
|
|---|
| 71 | *
|
|---|
| 72 | * @param {string} napiVersion The N-API version to check.
|
|---|
| 73 | * @param {Object} log The log object to which the warnings are to be issued.
|
|---|
| 74 | * Must implement the `warn` method.
|
|---|
| 75 | */
|
|---|
| 76 | exports.logUnsupportedVersion = function (napiVersion, log) {
|
|---|
| 77 | if (!exports.isSupportedVersion(napiVersion)) {
|
|---|
| 78 | if (exports.packageSupportsVersion(napiVersion)) {
|
|---|
| 79 | log.warn('This Node instance does not support N-API version ' + napiVersion)
|
|---|
| 80 | } else {
|
|---|
| 81 | log.warn('This package does not support N-API version ' + napiVersion)
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | * Issues warnings to the supplied log for those N-API versions not supported
|
|---|
| 88 | * by the N-API runtime or the package.
|
|---|
| 89 | *
|
|---|
| 90 | * Note that this function is specific to the
|
|---|
| 91 | * [`prebuild`](https://github.com/prebuild/prebuild#prebuild) package.
|
|---|
| 92 | *
|
|---|
| 93 | * `target` is the list of targets to be built and is determined in one of
|
|---|
| 94 | * three ways from the command line arguments:
|
|---|
| 95 | * (1) `--target` specifies a specific target to build.
|
|---|
| 96 | * (2) `--all` specifies all N-API versions supported by the package.
|
|---|
| 97 | * (3) Neither of these specifies to build the single "best version available."
|
|---|
| 98 | *
|
|---|
| 99 | * `prebuild` is an array of objects in the form `{runtime: 'napi', target: '2'}`.
|
|---|
| 100 | * The array contains the list of N-API versions that are supported by both the
|
|---|
| 101 | * package being built and the currently running Node instance.
|
|---|
| 102 | *
|
|---|
| 103 | * The objective of this function is to issue a warning for those items that appear
|
|---|
| 104 | * in the `target` argument but not in the `prebuild` argument.
|
|---|
| 105 | * If a specific target is supported by the package (`packageSupportsVersion`) but
|
|---|
| 106 | * but note in `prebuild`, the assumption is that the target is not supported by
|
|---|
| 107 | * Node.
|
|---|
| 108 | *
|
|---|
| 109 | * @param {(Array<string>|string)} target The N-API version(s) to check. Target is
|
|---|
| 110 | * @param {Array<Object>} prebuild A config object created by the `prebuild` package.
|
|---|
| 111 | * @param {Object} log The log object to which the warnings are to be issued.
|
|---|
| 112 | * Must implement the `warn` method.
|
|---|
| 113 | * @private
|
|---|
| 114 | */
|
|---|
| 115 | exports.logMissingNapiVersions = function (target, prebuild, log) {
|
|---|
| 116 | if (exports.getNapiBuildVersions()) {
|
|---|
| 117 | var targets = [].concat(target)
|
|---|
| 118 | targets.forEach(function (napiVersion) {
|
|---|
| 119 | if (!prebuildExists(prebuild, napiVersion)) {
|
|---|
| 120 | if (exports.packageSupportsVersion(parseInt(napiVersion, 10))) {
|
|---|
| 121 | log.warn('This Node instance does not support N-API version ' + napiVersion)
|
|---|
| 122 | } else {
|
|---|
| 123 | log.warn('This package does not support N-API version ' + napiVersion)
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | })
|
|---|
| 127 | } else {
|
|---|
| 128 | log.error('Builds with runtime \'napi\' require a binary.napi_versions ' +
|
|---|
| 129 | 'property on the package.json file')
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Determines whether the specified N-API version exists in the prebuild
|
|---|
| 135 | * configuration object.
|
|---|
| 136 | *
|
|---|
| 137 | * Note that this function is specific to the `prebuild` and `prebuild-install`
|
|---|
| 138 | * packages.
|
|---|
| 139 | *
|
|---|
| 140 | * @param {Object} prebuild A config object created by the `prebuild` package.
|
|---|
| 141 | * @param {string} napiVersion The N-APi version to be checked.
|
|---|
| 142 | * @return {boolean}
|
|---|
| 143 | * @private
|
|---|
| 144 | */
|
|---|
| 145 | var prebuildExists = function (prebuild, napiVersion) {
|
|---|
| 146 | if (prebuild) {
|
|---|
| 147 | for (var i = 0; i < prebuild.length; i++) {
|
|---|
| 148 | if (prebuild[i].target === napiVersion) return true
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 | return false
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * Returns the best N-API version to build given the highest N-API
|
|---|
| 156 | * version supported by the current Node instance and the N-API versions
|
|---|
| 157 | * supported by the package, or undefined if a suitable N-API version
|
|---|
| 158 | * cannot be determined.
|
|---|
| 159 | *
|
|---|
| 160 | * The best build version is the greatest N-API version supported by
|
|---|
| 161 | * the package that is less than or equal to the highest N-API version
|
|---|
| 162 | * supported by the current Node instance.
|
|---|
| 163 | *
|
|---|
| 164 | * @returns {number|undefined}
|
|---|
| 165 | */
|
|---|
| 166 | exports.getBestNapiBuildVersion = function () {
|
|---|
| 167 | var bestNapiBuildVersion = 0
|
|---|
| 168 | var napiBuildVersions = exports.getNapiBuildVersions(pkg) // array of integer strings
|
|---|
| 169 | if (napiBuildVersions) {
|
|---|
| 170 | var ourNapiVersion = exports.getNapiVersion()
|
|---|
| 171 | napiBuildVersions.forEach(function (napiBuildVersionStr) {
|
|---|
| 172 | var napiBuildVersion = parseInt(napiBuildVersionStr, 10)
|
|---|
| 173 | if (napiBuildVersion > bestNapiBuildVersion &&
|
|---|
| 174 | napiBuildVersion <= ourNapiVersion) {
|
|---|
| 175 | bestNapiBuildVersion = napiBuildVersion
|
|---|
| 176 | }
|
|---|
| 177 | })
|
|---|
| 178 | }
|
|---|
| 179 | return bestNapiBuildVersion === 0 ? undefined : bestNapiBuildVersion
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * Returns an array of N-API versions supported by the package.
|
|---|
| 184 | *
|
|---|
| 185 | * @returns {Array<string>|undefined}
|
|---|
| 186 | */
|
|---|
| 187 | exports.getNapiBuildVersions = function () {
|
|---|
| 188 | var napiBuildVersions = []
|
|---|
| 189 | // remove duplicates, convert to text
|
|---|
| 190 | if (pkg.binary && pkg.binary.napi_versions) {
|
|---|
| 191 | pkg.binary.napi_versions.forEach(function (napiVersion) {
|
|---|
| 192 | var duplicated = napiBuildVersions.indexOf('' + napiVersion) !== -1
|
|---|
| 193 | if (!duplicated) {
|
|---|
| 194 | napiBuildVersions.push('' + napiVersion)
|
|---|
| 195 | }
|
|---|
| 196 | })
|
|---|
| 197 | }
|
|---|
| 198 | return napiBuildVersions.length ? napiBuildVersions : undefined
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /**
|
|---|
| 202 | * Returns the highest N-API version supported by the current node instance
|
|---|
| 203 | * or undefined if N-API is not supported.
|
|---|
| 204 | *
|
|---|
| 205 | * @returns {string|undefined}
|
|---|
| 206 | */
|
|---|
| 207 | exports.getNapiVersion = function () {
|
|---|
| 208 | var version = process.versions.napi // integer string, can be undefined
|
|---|
| 209 | if (!version) { // this code should never need to be updated
|
|---|
| 210 | if (versionArray[0] === 9 && versionArray[1] >= 3) version = '2' // 9.3.0+
|
|---|
| 211 | else if (versionArray[0] === 8) version = '1' // 8.0.0+
|
|---|
| 212 | }
|
|---|
| 213 | return version
|
|---|
| 214 | }
|
|---|