Skip to content

Commit

Permalink
Building from source now requires node-addon-api in dependencies
Browse files Browse the repository at this point in the history
If you want to build sharp from source against a globally-installed
libvips then you will now need to add both node-addon-api and
node-gyp to the dependencies section of your package.json file.

The binding.gyp file is "hidden" inside the src directory to
prevent various build and package manager tooling from assuming
that everyone is going to build from source every time.
  • Loading branch information
lovell committed Oct 8, 2023
1 parent 51e56f9 commit f7da2e5
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build
src/build
src/node_modules
node_modules
/coverage
npm/*/*
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Requires libvips v8.14.5

* Drop support for Node.js 14 and 16, now requires Node.js >= 18.17.0

* Prebuilt binaries distributed via npm registry and installed via package manager.

* Building from source requires dependency on `node-addon-api`.

* Remove `sharp.vendor`.

* Make `compression` option of `heif` mandatory to help reduce HEIF vs HEIC confusion.
Expand Down
8 changes: 7 additions & 1 deletion docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ This module will be compiled from source at `npm install` time when:
Building from source requires:

* C++11 compiler
* [node-addon-api](https://www.npmjs.com/package/node-addon-api)
* [node-gyp](https://github.com/nodejs/node-gyp#installation) and its dependencies

If `node-gyp` cannot be found, try adding it to `devDependencies`.
There is an install-time check for these dependencies.
If `node-addon-api` or `node-gyp` cannot be found, try adding them via:

```sh
npm install --save node-addon-api node-gyp
```

For cross-compiling, the `--platform`, `--arch` and `--libc` npm flags
(or the `npm_config_platform`, `npm_config_arch` and `npm_config_libc` environment variables)
Expand Down
17 changes: 13 additions & 4 deletions install/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@

'use strict';

const { useGlobalLibvips, globalLibvipsVersion, log, gypRebuild } = require('../lib/libvips');
const { useGlobalLibvips, globalLibvipsVersion, log, spawnRebuild } = require('../lib/libvips');

const buildFromSource = (msg) => {
log(msg);
log('Attempting to build from source via node-gyp');
try {
require('node-gyp');
require('node-addon-api');
log('Found node-addon-api');
} catch (err) {
log('You might need to install node-gyp');
log('Please add node-addon-api to your dependencies');
return;
}
try {
const gyp = require('node-gyp');
log(`Found node-gyp version ${gyp().version}`);
} catch (err) {
log('Please add node-gyp to your dependencies');
return;
}
log('See https://sharp.pixelplumbing.com/install#building-from-source');
const status = gypRebuild();
const status = spawnRebuild();
if (status !== 0) {
process.exit(status);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/libvips.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ const isRosetta = () => {
};

/* istanbul ignore next */
const gypRebuild = () =>
spawnSync('node-gyp rebuild', {
const spawnRebuild = () =>
spawnSync('node-gyp rebuild --directory=src', {
...spawnSyncOptions,
stdio: 'inherit'
}).status;
Expand Down Expand Up @@ -146,7 +146,7 @@ module.exports = {
buildSharpLibvipsLibDir,
runtimePlatformArch,
log,
gypRebuild,
spawnRebuild,
globalLibvipsVersion,
pkgConfigPath,
useGlobalLibvips
Expand Down
2 changes: 1 addition & 1 deletion lib/sharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => run
/* istanbul ignore next */
try {
// Check for local build
module.exports = require(`../build/Release/sharp-${runtimePlatform}.node`);
module.exports = require(`../src/build/Release/sharp-${runtimePlatform}.node`);
} catch (errLocal) {
try {
// Check for runtime package
Expand Down
2 changes: 1 addition & 1 deletion npm/from-local-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const platform = buildPlatformArch();
const dest = path.join(__dirname, platform);

// Use same config as prebuild to copy binary files
const release = path.join(__dirname, '..', 'build', 'Release');
const release = path.join(__dirname, '..', 'src', 'build', 'Release');
const prebuildrc = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '.prebuildrc'), 'utf8'));
const include = new RegExp(prebuildrc['include-regex'], 'i');
fs.cpSync(release, path.join(dest, 'lib'), {
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
],
"scripts": {
"install": "node install/check",
"clean": "rm -rf build/ .nyc_output/ coverage/ test/fixtures/output.*",
"clean": "rm -rf src/build/ .nyc_output/ coverage/ test/fixtures/output.*",
"test": "npm run test-lint && npm run test-unit && npm run test-licensing && npm run test-types",
"test-lint": "semistandard && cpplint",
"test-unit": "nyc --reporter=lcov --reporter=text --check-coverage --branches=100 mocha",
Expand All @@ -107,10 +107,9 @@
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"binding.gyp",
"install",
"lib",
"src"
"src/*.{cc,h,gyp}"
],
"repository": {
"type": "git",
Expand All @@ -137,7 +136,6 @@
"dependencies": {
"color": "^4.2.3",
"detect-libc": "^2.0.2",
"node-addon-api": "^7.0.0",
"semver": "^7.5.4"
},
"optionalDependencies": {
Expand Down Expand Up @@ -171,6 +169,7 @@
"jsdoc-to-markdown": "^8.0.0",
"license-checker": "^25.0.1",
"mocha": "^10.2.0",
"node-addon-api": "^7.0.0",
"nyc": "^15.1.0",
"prebuild": "^12.1.0",
"semistandard": "^17.0.0",
Expand Down
28 changes: 14 additions & 14 deletions binding.gyp → src/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

{
'variables': {
'vips_version': '<!(node -p "require(\'./lib/libvips\').minimumLibvipsVersion")',
'platform_and_arch': '<!(node -p "require(\'./lib/libvips\').buildPlatformArch()")',
'sharp_libvips_include_dir': '<!(node -p "require(\'./lib/libvips\').buildSharpLibvipsIncludeDir()")',
'sharp_libvips_cplusplus_dir': '<!(node -p "require(\'./lib/libvips\').buildSharpLibvipsCPlusPlusDir()")',
'sharp_libvips_lib_dir': '<!(node -p "require(\'./lib/libvips\').buildSharpLibvipsLibDir()")'
'vips_version': '<!(node -p "require(\'../lib/libvips\').minimumLibvipsVersion")',
'platform_and_arch': '<!(node -p "require(\'../lib/libvips\').buildPlatformArch()")',
'sharp_libvips_include_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsIncludeDir()")',
'sharp_libvips_cplusplus_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsCPlusPlusDir()")',
'sharp_libvips_lib_dir': '<!(node -p "require(\'../lib/libvips\').buildSharpLibvipsLibDir()")'
},
'targets': [{
'target_name': 'libvips-cpp',
Expand Down Expand Up @@ -86,22 +86,22 @@
'variables': {
'conditions': [
['OS != "win"', {
'pkg_config_path': '<!(node -p "require(\'./lib/libvips\').pkgConfigPath()")',
'use_global_libvips': '<!(node -p "Boolean(require(\'./lib/libvips\').useGlobalLibvips()).toString()")'
'pkg_config_path': '<!(node -p "require(\'../lib/libvips\').pkgConfigPath()")',
'use_global_libvips': '<!(node -p "Boolean(require(\'../lib/libvips\').useGlobalLibvips()).toString()")'
}, {
'pkg_config_path': '',
'use_global_libvips': ''
}]
]
},
'sources': [
'src/common.cc',
'src/metadata.cc',
'src/stats.cc',
'src/operations.cc',
'src/pipeline.cc',
'src/utilities.cc',
'src/sharp.cc'
'common.cc',
'metadata.cc',
'stats.cc',
'operations.cc',
'pipeline.cc',
'utilities.cc',
'sharp.cc'
],
'include_dirs': [
'<!(node -p "require(\'node-addon-api\').include_dir")',
Expand Down

0 comments on commit f7da2e5

Please sign in to comment.