You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. node-gyp
  2. =========
  3. ## Node.js native addon build tool
  4. `node-gyp` is a cross-platform command-line tool written in Node.js for compiling
  5. native addon modules for Node.js. It bundles the [gyp](https://gyp.gsrc.io)
  6. project used by the Chromium team and takes away the pain of dealing with the
  7. various differences in build platforms. It is the replacement to the `node-waf`
  8. program which is removed for node `v0.8`. If you have a native addon for node that
  9. still has a `wscript` file, then you should definitely add a `binding.gyp` file
  10. to support the latest versions of node.
  11. Multiple target versions of node are supported (i.e. `0.8`, ..., `4`, `5`, `6`,
  12. etc.), regardless of what version of node is actually installed on your system
  13. (`node-gyp` downloads the necessary development files or headers for the target version).
  14. ## Features
  15. * Easy to use, consistent interface
  16. * Same commands to build your module on every platform
  17. * Supports multiple target versions of Node
  18. Installation
  19. ------------
  20. You can install with `npm`:
  21. ``` bash
  22. $ npm install -g node-gyp
  23. ```
  24. You will also need to install:
  25. ### On Unix
  26. * `python` (`v2.7` recommended, `v3.x.x` is __*not*__ supported)
  27. * `make`
  28. * A proper C/C++ compiler toolchain, like [GCC](https://gcc.gnu.org)
  29. ### On macOS
  30. * `python` (`v2.7` recommended, `v3.x.x` is __*not*__ supported) (already installed on macOS)
  31. * [Xcode](https://developer.apple.com/xcode/download/)
  32. * You also need to install the `Command Line Tools` via Xcode. You can find this under the menu `Xcode -> Preferences -> Locations` (or by running `xcode-select --install` in your Terminal)
  33. * This step will install `gcc` and the related toolchain containing `make`
  34. ### On Windows
  35. #### Option 1
  36. 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).
  37. #### Option 2
  38. Install tools and configuration manually:
  39. * Install Visual C++ Build Environment: [Visual Studio Build Tools](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools)
  40. (using "Visual C++ build tools" workload) or [Visual Studio 2017 Community](https://visualstudio.microsoft.com/pl/thank-you-downloading-visual-studio/?sku=Community)
  41. (using the "Desktop development with C++" workload)
  42. * 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.)
  43. * Launch cmd, `npm config set msvs_version 2017`
  44. 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.
  45. If you have multiple Python versions installed, you can identify which Python
  46. version `node-gyp` uses by setting the '--python' variable:
  47. ``` bash
  48. $ node-gyp --python /path/to/python2.7
  49. ```
  50. If `node-gyp` is called by way of `npm` *and* you have multiple versions of
  51. Python installed, then you can set `npm`'s 'python' config key to the appropriate
  52. value:
  53. ``` bash
  54. $ npm config set python /path/to/executable/python2.7
  55. ```
  56. How to Use
  57. ----------
  58. To compile your native addon, first go to its root directory:
  59. ``` bash
  60. $ cd my_node_addon
  61. ```
  62. The next step is to generate the appropriate project build files for the current
  63. platform. Use `configure` for that:
  64. ``` bash
  65. $ node-gyp configure
  66. ```
  67. Auto-detection fails for Visual C++ Build Tools 2015, so `--msvs_version=2015`
  68. needs to be added (not needed when run by npm as configured above):
  69. ``` bash
  70. $ node-gyp configure --msvs_version=2015
  71. ```
  72. __Note__: The `configure` step looks for the `binding.gyp` file in the current
  73. directory to process. See below for instructions on creating the `binding.gyp` file.
  74. Now you will have either a `Makefile` (on Unix platforms) or a `vcxproj` file
  75. (on Windows) in the `build/` directory. Next invoke the `build` command:
  76. ``` bash
  77. $ node-gyp build
  78. ```
  79. Now you have your compiled `.node` bindings file! The compiled bindings end up
  80. in `build/Debug/` or `build/Release/`, depending on the build mode. At this point
  81. you can require the `.node` file with Node and run your tests!
  82. __Note:__ To create a _Debug_ build of the bindings file, pass the `--debug` (or
  83. `-d`) switch when running either the `configure`, `build` or `rebuild` command.
  84. The "binding.gyp" file
  85. ----------------------
  86. Previously when node had `node-waf` you had to write a `wscript` file. The
  87. replacement for that is the `binding.gyp` file, which describes the configuration
  88. to build your module in a JSON-like format. This file gets placed in the root of
  89. your package, alongside the `package.json` file.
  90. A barebones `gyp` file appropriate for building a node addon looks like:
  91. ``` python
  92. {
  93. "targets": [
  94. {
  95. "target_name": "binding",
  96. "sources": [ "src/binding.cc" ]
  97. }
  98. ]
  99. }
  100. ```
  101. Some additional resources for addons and writing `gyp` files:
  102. * ["Going Native" a nodeschool.io tutorial](http://nodeschool.io/#goingnative)
  103. * ["Hello World" node addon example](https://github.com/nodejs/node/tree/master/test/addons/hello-world)
  104. * [gyp user documentation](https://gyp.gsrc.io/docs/UserDocumentation.md)
  105. * [gyp input format reference](https://gyp.gsrc.io/docs/InputFormatReference.md)
  106. * [*"binding.gyp" files out in the wild* wiki page](https://github.com/nodejs/node-gyp/wiki/%22binding.gyp%22-files-out-in-the-wild)
  107. Commands
  108. --------
  109. `node-gyp` responds to the following commands:
  110. | **Command** | **Description**
  111. |:--------------|:---------------------------------------------------------------
  112. | `help` | Shows the help dialog
  113. | `build` | Invokes `make`/`msbuild.exe` and builds the native addon
  114. | `clean` | Removes the `build` directory if it exists
  115. | `configure` | Generates project build files for the current platform
  116. | `rebuild` | Runs `clean`, `configure` and `build` all in a row
  117. | `install` | Installs node header files for the given version
  118. | `list` | Lists the currently installed node header versions
  119. | `remove` | Removes the node header files for the given version
  120. Command Options
  121. --------
  122. `node-gyp` accepts the following command options:
  123. | **Command** | **Description**
  124. |:----------------------------------|:------------------------------------------
  125. | `-j n`, `--jobs n` | Run make in parallel
  126. | `--target=v6.2.1` | Node version to build for (default=process.version)
  127. | `--silly`, `--loglevel=silly` | Log all progress to console
  128. | `--verbose`, `--loglevel=verbose` | Log most progress to console
  129. | `--silent`, `--loglevel=silent` | Don't log anything to console
  130. | `debug`, `--debug` | Make Debug build (default=Release)
  131. | `--release`, `--no-debug` | Make Release build
  132. | `-C $dir`, `--directory=$dir` | Run command in different directory
  133. | `--make=$make` | Override make command (e.g. gmake)
  134. | `--thin=yes` | Enable thin static libraries
  135. | `--arch=$arch` | Set target architecture (e.g. ia32)
  136. | `--tarball=$path` | Get headers from a local tarball
  137. | `--devdir=$path` | SDK download directory (default=~/.node-gyp)
  138. | `--ensure` | Don't reinstall headers if already present
  139. | `--dist-url=$url` | Download header tarball from custom URL
  140. | `--proxy=$url` | Set HTTP proxy for downloading header tarball
  141. | `--cafile=$cafile` | Override default CA chain (to download tarball)
  142. | `--nodedir=$path` | Set the path to the node source code
  143. | `--python=$path` | Set path to the python (2) binary
  144. | `--msvs_version=$version` | Set Visual Studio version (win)
  145. | `--solution=$solution` | Set Visual Studio Solution version (win)
  146. Configuration
  147. --------
  148. __`node-gyp` responds to environment variables or `npm` configuration__
  149. 1. Environment variables take the form `npm_config_OPTION_NAME` for any of the
  150. options listed above (dashes in option names should be replaced by underscores).
  151. These work also when `node-gyp` is invoked directly:
  152. `$ export npm_config_devdir=/tmp/.gyp`
  153. or on Windows
  154. `> set npm_config_devdir=c:\temp\.gyp`
  155. 2. As `npm` configuration, variables take the form `OPTION_NAME`.
  156. This way only works when `node-gyp` is executed by `npm`:
  157. `$ npm config set [--global] devdir /tmp/.gyp`
  158. `$ npm i buffertools`
  159. License
  160. -------
  161. (The MIT License)
  162. Copyright (c) 2012 Nathan Rajlich <nathan@tootallnate.net>
  163. Permission is hereby granted, free of charge, to any person obtaining
  164. a copy of this software and associated documentation files (the
  165. 'Software'), to deal in the Software without restriction, including
  166. without limitation the rights to use, copy, modify, merge, publish,
  167. distribute, sublicense, and/or sell copies of the Software, and to
  168. permit persons to whom the Software is furnished to do so, subject to
  169. the following conditions:
  170. The above copyright notice and this permission notice shall be
  171. included in all copies or substantial portions of the Software.
  172. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  173. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  174. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  175. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  176. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  177. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  178. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  179. [python-v2.7.10]: https://www.python.org/downloads/release/python-2710/
  180. [msvc2013]: https://www.microsoft.com/en-gb/download/details.aspx?id=44914
  181. [win7sdk]: https://www.microsoft.com/en-us/download/details.aspx?id=8279
  182. [compiler update for the Windows SDK 7.1]: https://www.microsoft.com/en-us/download/details.aspx?id=4422