Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v5.x Python compat fixes for README #1911

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,33 @@ matrix:

- name: "Node.js 6 & Python 3.7 on Linux"
python: 3.7
env: NODE_GYP_FORCE_PYTHON=python3 EXPERIMENTAL_NODE_GYP_PYTHON3=1
env: NODE_GYP_FORCE_PYTHON=python3
before_install: nvm install 6
- name: "Node.js 8 & Python 3.7 on Linux"
python: 3.7
env: NODE_GYP_FORCE_PYTHON=python3 EXPERIMENTAL_NODE_GYP_PYTHON3=1
env: NODE_GYP_FORCE_PYTHON=python3
before_install: nvm install 8
- name: "Node.js 10 & Python 3.7 on Linux"
python: 3.7
env: NODE_GYP_FORCE_PYTHON=python3 EXPERIMENTAL_NODE_GYP_PYTHON3=1
env: NODE_GYP_FORCE_PYTHON=python3
before_install: nvm install 10
- name: "Node.js 12 & Python 3.5 on Linux"
python: 3.5
env: NODE_GYP_FORCE_PYTHON=python3 EXPERIMENTAL_NODE_GYP_PYTHON3=1
env: NODE_GYP_FORCE_PYTHON=python3
before_install: nvm install 12
- name: "Node.js 12 & Python 3.6 on Linux"
python: 3.6
env: NODE_GYP_FORCE_PYTHON=python3 EXPERIMENTAL_NODE_GYP_PYTHON3=1
env: NODE_GYP_FORCE_PYTHON=python3
before_install: nvm install 12
- name: "Node.js 12 & Python 3.7 on Linux"
python: 3.7
env: NODE_GYP_FORCE_PYTHON=python3 EXPERIMENTAL_NODE_GYP_PYTHON3=1
env: NODE_GYP_FORCE_PYTHON=python3
before_install: nvm install 12
- name: "Python 3.7 on macOS"
os: osx
#osx_image: xcode11
language: shell # 'language: python' is not yet supported on macOS
env: NODE_GYP_FORCE_PYTHON=python3 EXPERIMENTAL_NODE_GYP_PYTHON3=1
env: NODE_GYP_FORCE_PYTHON=python3
before_install: HOMEBREW_NO_AUTO_UPDATE=1 brew install npm
- name: "Node.js 12 & Python 3.7 on Windows"
os: windows
Expand All @@ -66,7 +66,6 @@ matrix:
env: >-
PATH=/c/Python37:/c/Python37/Scripts:$PATH
NODE_GYP_FORCE_PYTHON=/c/Python37/python.exe
EXPERIMENTAL_NODE_GYP_PYTHON3=1
before_install: choco install python

install:
Expand All @@ -79,6 +78,7 @@ before_script:
# exit-zero treats all errors as warnings. Two space indentation is OK. The GitHub editor is 127 chars wide
- flake8 . --count --exit-zero --ignore=E111,E114,W503 --max-complexity=10 --max-line-length=127 --statistics
- npm install
- npm list
script:
- node -e 'require("npmlog").level="verbose"; require("./lib/find-python")(null,()=>{})'
- npm test
Expand Down
38 changes: 27 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# `node-gyp` - Node.js native addon build tool

`node-gyp` is a cross-platform command-line tool written in Node.js for compiling
native addon modules for Node.js. It bundles the [gyp](https://gyp.gsrc.io)
project used by the Chromium team and takes away the pain of dealing with the
various differences in build platforms.
`node-gyp` is a cross-platform command-line tool written in Node.js for
compiling native addon modules for Node.js. It contains a fork of the
[gyp](https://gyp.gsrc.io) project that was previously used by the Chromium
team and takes away the pain of dealing with the various differences in build
platforms.

Note that `node-gyp` is _not_ used to build Node.js itself.

Multiple target versions of Node.js are supported (i.e. `0.8`, ..., `4`, `5`, `6`,
etc.), regardless of what version of Node.js is actually installed on your system
Expand All @@ -25,20 +28,26 @@ $ npm install -g node-gyp

You will also need to install:

NOTE: node-gyp is compatible with Python v2.7, v3.5, v3.6, or v3.7. If the
Python to use is not explicitly configured (see "Configuring Python Dependency"
below) it will attempt to find a compatible Python executable.

### On Unix

* `python` (`v2.7` recommended, `v3.x.x` is __*not*__ supported)
* Python v2.7, v3.5, v3.6, or v3.7
* `make`
* A proper C/C++ compiler toolchain, like [GCC](https://gcc.gnu.org)

### On macOS

* `python` (`v2.7` recommended, `v3.x.x` is __*not*__ supported) (already installed on macOS)
* Python v2.7, v3.5, v3.6, or v3.7
* [Xcode](https://developer.apple.com/xcode/download/)
* You also need to install the `XCode Command Line Tools` by running `xcode-select --install`. Alternatively, if you already have the full Xcode installed, you can find them under the menu `Xcode -> Open Developer Tool -> More Developer Tools...`. This step will install `clang`, `clang++`, and `make`.

### On Windows

Install the current version of Python from the [Microsoft Store package](https://docs.python.org/3/using/windows.html#the-microsoft-store-package).

#### Option 1

Install all the required tools and configurations using Microsoft's [windows-build-tools](https://github.com/felixrieseberg/windows-build-tools) using `npm install --global --production windows-build-tools` from an elevated PowerShell or CMD.exe (run as Administrator).
Expand All @@ -49,7 +58,6 @@ Install tools and configuration manually:
* Install Visual C++ Build Environment: [Visual Studio Build Tools](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools)
(using "Visual C++ build tools" workload) or [Visual Studio 2017 Community](https://visualstudio.microsoft.com/pl/thank-you-downloading-visual-studio/?sku=Community)
(using the "Desktop development with C++" workload)
* Install [Python 2.7](https://www.python.org/downloads/) (`v3.x.x` is not supported), and run `npm config set python python2.7` (or see below for further instructions on specifying the proper Python version and path.)
* Launch cmd, `npm config set msvs_version 2017`

If the above steps didn't work for you, please visit [Microsoft's Node.js Guidelines for Windows](https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules) for additional tips.
Expand All @@ -62,17 +70,25 @@ If you have multiple Python versions installed, you can identify which Python
version `node-gyp` uses by setting the `--python` variable:

``` bash
$ node-gyp <command> --python /path/to/executable/python2.7
$ node-gyp <command> --python /path/to/executable/python
```

If `node-gyp` is called by way of `npm`, *and* you have multiple versions of
Python installed, then you can set `npm`'s 'python' config key to the appropriate
value:

``` bash
$ npm config set python /path/to/executable/python2.7
$ npm config set python /path/to/executable/python
```

If the `PYTHON` environment variable is set to the path of a Python executable,
it will be used if it is a compatible Python.

If the `NODE_GYP_FORCE_PYTHON` environment variable is set to the path of a
Python executable, it will be used instead of any of the other configured or
builtin Python search paths. If its not a compatible Python, no further
searching will be done.

## How to Use

To compile your native addon, first go to its root directory:
Expand Down Expand Up @@ -119,7 +135,7 @@ JSON-like format. This file gets placed in the root of your package, alongside

A barebones `gyp` file appropriate for building a Node.js addon could look like:

``` python
```python
{
"targets": [
{
Expand Down Expand Up @@ -179,7 +195,7 @@ Some additional resources for addons and writing `gyp` files:
| `--proxy=$url` | Set HTTP proxy for downloading header tarball
| `--cafile=$cafile` | Override default CA chain (to download tarball)
| `--nodedir=$path` | Set the path to the node source code
| `--python=$path` | Set path to the Python 2 binary
| `--python=$path` | Set path to the Python binary
| `--msvs_version=$version` | Set Visual Studio version (Windows only)
| `--solution=$solution` | Set Visual Studio Solution version (Windows only)

Expand Down
3 changes: 3 additions & 0 deletions gyp/pylib/gyp/MSVSNew.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def __cmp__(self, other):
# Sort by name then guid (so things are in order on vs2008).
return cmp((self.name, self.get_guid()), (other.name, other.get_guid()))

def __lt__(self, other):
return self.__cmp__(other) < 0


class MSVSFolder(MSVSSolutionEntry):
"""Folder in a Visual Studio project or solution."""
Expand Down
10 changes: 7 additions & 3 deletions lib/find-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ PythonFinder.prototype = {
log: logWithPrefix(log, 'find Python'),
argsExecutable: ['-c', 'import sys; print(sys.executable);'],
argsVersion: ['-c', 'import sys; print("%s.%s.%s" % sys.version_info[:3]);'],
semverRange: process.env.EXPERIMENTAL_NODE_GYP_PYTHON3 ? '2.7.x || >=3.5.0'
: '>=2.6.0 <3.0.0',
semverRange: '2.7.x || >=3.5.0',

// These can be overridden for testing:
execFile: cp.execFile,
Expand Down Expand Up @@ -97,6 +96,11 @@ PythonFinder.prototype = {
before: () => { this.addLog('checking if "python2" can be used') },
check: this.checkCommand,
arg: 'python2'
},
{
before: () => { this.addLog('checking if "python3" can be used') },
check: this.checkCommand,
arg: 'python3'
}
]

Expand Down Expand Up @@ -286,7 +290,7 @@ PythonFinder.prototype = {
// X
const info = [
'**********************************************************',
'You need to install the latest version of Python 2.7.',
'You need to install the latest version of Python.',
'Node-gyp should be able to find and use Python. If not,',
'you can try one of the following options:',
`- Use the switch --python="${pathExample}"`,
Expand Down
19 changes: 13 additions & 6 deletions lib/find-visualstudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,22 @@ VisualStudioFinder.prototype = {
// Helper - process toolset information
getToolset: function getToolset (info, versionYear) {
const pkg = 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64'
const express = 'Microsoft.VisualStudio.WDExpress'

if (info.packages.indexOf(pkg) !== -1) {
this.log.silly('- found VC.Tools.x86.x64')
if (versionYear === 2017) {
return 'v141'
}
if (versionYear === 2019) {
return 'v142'
}
} else if (info.packages.indexOf(express) !== -1) {
this.log.silly('- found Visual Studio Express (looking for toolset)')
} else {
return null
}

if (versionYear === 2017) {
return 'v141'
} else if (versionYear === 2019) {
return 'v142'
}
this.log.silly('- invalid versionYear:', versionYear)
return null
},

Expand Down
Loading