/var/lang/lib/node_modules/npm/node_modules/node-gyp/gyp/docs
This explorer reads the filesystem of the server it runs on, so /workspace/user isn't present here. Browsing and the terminal still work against this server's own disk from /.
# User Documentation ## Introduction This document is intended to provide a user-level guide to GYP. Theemphasis here is on how to use GYP to accomplish specific tasks, not onthe complete technical language specification. (For that, see the[LanguageSpecification](LanguageSpecification.md).) The document below starts with some overviews to provide context: anoverview of the structure of a `.gyp` file itself, an overview of atypical executable-program target in a `.gyp` file, an an overview of atypical library target in a `.gyp` file. After the overviews, there are examples of `gyp` patterns for differentcommon use cases. ## Skeleton of a typical Chromium .gyp file Here is the skeleton of a typical `.gyp` file in the Chromium tree: ``` { 'variables': { . . . }, 'includes': [ '../build/common.gypi', ], 'target_defaults': { . . . }, 'targets': [ { 'target_name': 'target_1', . . . }, { 'target_name': 'target_2', . . . }, ], 'conditions': [ ['OS=="linux"', { 'targets': [ { 'target_name': 'linux_target_3', . . . }, ], }], ['OS=="win"', { 'targets': [ { 'target_name': 'windows_target_4', . . . }, ], }, { # OS != "win" 'targets': [ { 'target_name': 'non_windows_target_5', . . . }, }], ], }``` The entire file just contains a Python dictionary. (It's actually JSON,with two small Pythonic deviations: comments are introduced with `#`,and a `,` (comma)) is legal after the last element in a list ordictionary.) The top-level pieces in the `.gyp` file are as follows: `'variables'`: Definitions of variables that can be interpolated andused in various other parts of the file. `'includes'`: A list of of other files that will be included in thisfile. By convention, included files have the suffix `.gypi` (gypinclude). `'target_defaults'`: Settings that will apply to _all_ of the targetsdefined in this `.gyp` file. `'targets'`: The list of targets for which this `.gyp` file cangenerate builds. Each target is a dictionary that contains settingsdescribing all the information necessary to build the target. `'conditions'`: A list of condition specifications that can modify thecontents of the items in the global dictionary defined by this `.gyp`file based on the values of different variables. As implied by theabove example, the most common use of a `conditions` section in thetop-level dictionary is to add platform-specific targets to the`targets` list. ## Skeleton of a typical executable target in a .gyp file The most straightforward target is probably a simple executable program.Here is an example `executable` target that demonstrates the featuresthat should cover most simple uses of gyp: ``` { 'targets': [ { 'target_name': 'foo', 'type': 'executable', 'msvs_guid': '5ECEC9E5-8F23-47B6-93E0-C3B328B3BE65', 'dependencies': [ 'xyzzy', '../bar/bar.gyp:bar', ], 'defines': [ 'DEFINE_FOO', 'DEFINE_A_VALUE=value', ], 'include_dirs': [ '..', ], 'sources': [ 'file1.cc', 'file2.cc', ], 'conditions': [ ['OS=="linux"', { 'defines': [ 'LINUX_DEFINE', ], 'include_dirs': [ 'include/linux', ], }], ['OS=="win"', { 'defines': [ 'WINDOWS_SPECIFIC_DEFINE', ], }, { # OS != "win", 'defines': [ 'NON_WINDOWS_DEFINE', ], }] ], }, ], }``` The top-level settings in the target include: `'target_name'`: The name by which the target should be known, whichshould be unique across all `.gyp` files. This name will be used as theproject name in the generated Visual Studio solution, as the target namein the generated XCode configuration, and as the alias for building thistarget from the command line of the generated SCons configuration. `'type'`: Set to `executable`, logically enough. `'msvs_guid'`: THIS IS ONLY TRANSITIONAL. This is a hard-coded GUIDvalues that will be used in the generated Visual Studio solutionfile(s). This allows us to check in a `chrome.sln` file thatinteroperates with gyp-generated project files. Once everything inChromium is being generated by gyp, it will no longer be important thatthe GUIDs stay constant across invocations, and we'll likely get rid ofthese settings, `'dependencies'`: This lists other targets that this target depends on.The gyp-generated files will guarantee that the other targets are builtbefore this target. Any library targets in the `dependencies` list willbe linked with this target. The various settings (`defines`,`include_dirs`, etc.) listed in the `direct_dependent_settings` sectionsof the targets in this list will be applied to how _this_ target isbuilt and linked. See the more complete discussion of`direct_dependent_settings`, below. `'defines'`: The C preprocessor definitions that will be passed in oncompilation command lines (using `-D` or `/D` options). `'include_dirs'`: The directories in which included header files live.These will be passed in on compilation command lines (using `-I` or `/I`options). `'sources'`: The source files for this target. `'conditions'`: A block of conditions that will be evaluated to updatethe different settings in the target dictionary. ## Skeleton of a typical library target in a .gyp file The vast majority of targets are libraries. Here is an example of alibrary target including the additional features that should cover mostneeds of libraries: ``` { 'targets': [ { 'target_name': 'foo', 'type': '<(library)' 'msvs_guid': '5ECEC9E5-8F23-47B6-93E0-C3B328B3BE65', 'dependencies': [ 'xyzzy', '../bar/bar.gyp:bar', ], 'defines': [ 'DEFINE_FOO', 'DEFINE_A_VALUE=value', ], 'include_dirs': [ '..', ], 'direct_dependent_settings': { 'defines': [ 'DEFINE_FOO', 'DEFINE_ADDITIONAL', ], 'linkflags': [ ], }, 'export_dependent_settings': [ '../bar/bar.gyp:bar', ], 'sources': [ 'file1.cc', 'file2.cc', ], 'conditions': [ ['OS=="linux"', { 'defines': [ 'LINUX_DEFINE', ], 'include_dirs': [ 'include/linux', ], ], ['OS=="win"', { 'defines': [ 'WINDOWS_SPECIFIC_DEFINE', ], }, { # OS != "win", 'defines': [ 'NON_WINDOWS_DEFINE', ], }] ], ], }``` The possible entries in a library target are largely the same as thosethat can be specified for an executable target (`defines`,`include_dirs`, etc.). The differences include: `'type'`: This should almost always be set to '<(library)', which allowsthe user to define at gyp time whether libraries are to be built staticor shared. (On Linux, at least, linking with shared libraries savessignificant link time.) If it's necessary to pin down the type oflibrary to be built, the `type` can be set explicitly to`static_library` or `shared_library`. `'direct_dependent_settings'`: This defines the settings that will beapplied to other targets that _directly depend_ on this target--that is,that list _this_ target in their `'dependencies'` setting. This iswhere you list the `defines`, `include_dirs`, `cflags` and `linkflags`that other targets that compile or link against this target need tobuild consistently. `'export_dependent_settings'`: This lists the targets whose`direct_dependent_settings` should be "passed on" to other targets thatuse (depend on) this target. `TODO: expand on this description.` ## Use Cases These use cases are intended to cover the most common actions performedby developers using GYP. Note that these examples are _not_ fully-functioning, self-containedexamples (or else they'd be way too long). Each example mostly containsjust the keywords and settings relevant to the example, with perhaps afew extra keywords for context. The intent is to try to show thespecific pieces you need to pay attention to when doing something.[NOTE: if practical use shows that these examples are confusing withoutadditional context, please add what's necessary to clarify things.] ### Add new source files There are similar but slightly different patterns for adding aplatform-independent source file vs. adding a source file that onlybuilds on some of the supported platforms. #### Add a source file that builds on all platforms **Simplest possible case**: You are adding a file(s) that builds on allplatforms. Just add the file(s) to the `sources` list of the appropriate dictionaryin the `targets` list: ``` { 'targets': [ { 'target_name': 'my_target', 'type': 'executable', 'sources': [ '../other/file_1.cc', 'new_file.cc', 'subdir/file3.cc', ], }, ], },``` File path names are relative to the directory in which the `.gyp` file lives. Keep the list sorted alphabetically (unless there's a really, really,_really_ good reason not to). #### Add a platform-specific source file ##### Your platform-specific file is named `*_linux.{ext}`, `*_mac.{ext}`, `*_posix.{ext}` or `*_win.{ext}` The simplest way to add a platform-specific source file, assuming you'readding a completely new file and get to name it, is to use one of thefollowing standard suffixes: * `_linux` (e.g. `foo_linux.cc`) * `_mac` (e.g. `foo_mac.cc`) * `_posix` (e.g. `foo_posix.cc`) * `_win` (e.g. `foo_win.cc`) Simply add the file to the `sources` list of the appropriate dict withinthe `targets` list, like you would any other source file. ``` { 'targets': [ { 'target_name': 'foo', 'type': 'executable', 'sources': [ 'independent.cc', 'specific_win.cc', ], }, ], },``` The Chromium `.gyp` files all have appropriate `conditions` entries tofilter out the files that aren't appropriate for the current platform.In the above example, the `specific_win.cc` file will be removedautomatically from the source-list on non-Windows builds. ##### Your platform-specific file does not use an already-defined pattern If your platform-specific file does not contain a`*_{linux,mac,posix,win}` substring (or some other pattern that'salready in the `conditions` for the target), and you can't change thefile name, there are two patterns that can be used. **Preferred**: Add the file to the `sources` list of the appropriatedictionary within the `targets` list. Add an appropriate `conditions`section to exclude the specific files name: ``` { 'targets': [ { 'target_name': 'foo', 'type': 'executable', 'sources': [ 'linux_specific.cc', ], 'conditions': [ ['OS != "linux"', { 'sources!': [ # Linux-only; exclude on other platforms. 'linux_specific.cc', ] }[, ], }, ], },``` Despite the duplicate listing, the above is generally preferred becausethe `sources` list contains a useful global list of all sources on allplatforms with consistent sorting on all platforms. **Non-preferred**: In some situations, however, it might make sense tolist a platform-specific file only in a `conditions` section thatspecifically _includes_ it in the `sources` list: ``` { 'targets': [ { 'target_name': 'foo', 'type': 'executable', 'sources': [], ['OS == "linux"', { 'sources': [ # Only add to sources list on Linux. 'linux_specific.cc', ] }], }, ], },``` The above two examples end up generating equivalent builds, with thesmall exception that the `sources` lists will list the files indifferent orders. (The first example defines explicitly where`linux_specific.cc` appears in the list--perhaps in in themiddle--whereas the second example will always tack it on to the end ofthe list.) **Including or excluding files using patterns**: There are morecomplicated ways to construct a `sources` list based on patterns. See`TODO` below. ### Add a new executable An executable program is probably the most straightforward type oftarget, since all it typically needs is a list of source files, somecompiler/linker settings (probably varied by platform), and some librarytargets on which it depends and which must be used in the final link. #### Add an executable that builds on all platforms Add a dictionary defining the new executable target to the `targets`list in the appropriate `.gyp` file. Example: ``` { 'targets': [ { 'target_name': 'new_unit_tests', 'type': 'executable', 'defines': [ 'FOO', ], 'include_dirs': [ '..', ], 'dependencies': [ 'other_target_in_this_file', 'other_gyp2:target_in_other_gyp2', ], 'sources': [ 'new_additional_source.cc', 'new_unit_tests.cc', ], }, ], }``` #### Add a platform-specific executable Add a dictionary defining the new executable target to the `targets`list within an appropriate `conditions` block for the platform. The`conditions` block should be a sibling to the top-level `targets` list: ``` { 'targets': [ ], 'conditions': [ ['OS=="win"', { 'targets': [ { 'target_name': 'new_unit_tests', 'type': 'executable', 'defines': [ 'FOO', ], 'include_dirs': [ '..', ], 'dependencies': [ 'other_target_in_this_file', 'other_gyp2:target_in_other_gyp2', ], 'sources': [ 'new_additional_source.cc', 'new_unit_tests.cc', ], }, ], }], ], }``` ### Add settings to a target There are several different types of settings that can be defined forany given target. #### Add new preprocessor definitions (`-D` or `/D` flags) New preprocessor definitions are added by the `defines` setting: ``` { 'targets': [ { 'target_name': 'existing_target', 'defines': [ 'FOO', 'BAR=some_value', ], }, ], },``` These may be specified directly in a target's settings, as in the aboveexample, or in a `conditions` section. #### Add a new include directory (`-I` or `/I` flags) New include directories are added by the `include_dirs` setting: ``` { 'targets': [ { 'target_name': 'existing_target', 'include_dirs': [ '..', 'include', ], }, ], },``` These may be specified directly in a target's settings, as in the aboveexample, or in a `conditions` section. #### Add new compiler flags Specific compiler flags can be added with the `cflags` setting: ``` { 'targets': [ { 'target_name': 'existing_target', 'conditions': [ ['OS=="win"', { 'cflags': [ '/WX', ], }, { # OS != "win" 'cflags': [ '-Werror', ], }], ], }, ], },``` Because these flags will be specific to the actual compiler involved,they will almost always be only set within a `conditions` section. #### Add new linker flags Setting linker flags is OS-specific. On linux and most non-mac posixsystems, they can be added with the `ldflags` setting: ``` { 'targets': [ { 'target_name': 'existing_target', 'conditions': [ ['OS=="linux"', { 'ldflags': [ '-pthread', ], }], ], }, ], },``` Because these flags will be specific to the actual linker involved,they will almost always be only set within a `conditions` section. On OS X, linker settings are set via `xcode_settings`, on Windows via`msvs_settings`. #### Exclude settings on a platform Any given settings keyword (`defines`, `include_dirs`, etc.) has acorresponding form with a trailing `!` (exclamation point) to removevalues from a setting. One useful example of this is to remove theLinux `-Werror` flag from the global settings defined in`build/common.gypi`: ``` { 'targets': [ { 'target_name': 'third_party_target', 'conditions': [ ['OS=="linux"', { 'cflags!': [ '-Werror', ], }], ], }, ], },``` ### Cross-compiling GYP has some (relatively limited) support for cross-compiling. If the variable `GYP_CROSSCOMPILE` or one of the toolchain-relatedvariables (like `CC_host` or `CC_target`) is set, GYP will think thatyou wish to do a cross-compile. When cross-compiling, each target can be part of a "host" build, a"target" build, or both. By default, the target is assumed to be (only)part of the "target" build. The 'toolsets' property can be set on atarget to change the default. A target's dependencies are assumed to match the build type (so, if Adepends on B, by default that means that a target build of A depends ona target build of B). You can explicitly depend on targets acrosstoolchains by specifying "#host" or "#target" in the dependencies list.If GYP is not doing a cross-compile, the "#host" and "#target" will bestripped as needed, so nothing breaks. ### Add a new library TODO: write intro #### Add a library that builds on all platforms Add the a dictionary defining the new library target to the `targets`list in the appropriate `.gyp` file. Example: ``` { 'targets': [ { 'target_name': 'new_library', 'type': '<(library)', 'defines': [ 'FOO', 'BAR=some_value', ], 'include_dirs': [ '..', ], 'dependencies': [ 'other_target_in_this_file', 'other_gyp2:target_in_other_gyp2', ], 'direct_dependent_settings': { 'include_dirs': '.', }, 'export_dependent_settings': [ 'other_target_in_this_file', ], 'sources': [ 'new_additional_source.cc', 'new_library.cc', ], }, ], }``` The use of the `<(library)` variable above should be the default `type`setting for most library targets, as it allows the developer to choose,at `gyp` time, whether to build with static or shared libraries.(Building with shared libraries saves a _lot_ of link time on Linux.) It may be necessary to build a specific library as a fixed type. Is so,the `type` field can be hard-wired appropriately. For a static library: ``` 'type': 'static_library',``` For a shared library: ``` 'type': 'shared_library',``` #### Add a platform-specific library Add a dictionary defining the new library target to the `targets` listwithin a `conditions` block that's a sibling to the top-level `targets`list: ``` { 'targets': [ ], 'conditions': [ ['OS=="win"', { 'targets': [ { 'target_name': 'new_library', 'type': '<(library)', 'defines': [ 'FOO', 'BAR=some_value', ], 'include_dirs': [ '..', ], 'dependencies': [ 'other_target_in_this_file', 'other_gyp2:target_in_other_gyp2', ], 'direct_dependent_settings': { 'include_dirs': '.', }, 'export_dependent_settings': [ 'other_target_in_this_file', ], 'sources': [ 'new_additional_source.cc', 'new_library.cc', ], }, ], }], ], }``` ### Dependencies between targets GYP provides useful primitives for establishing dependencies betweentargets, which need to be configured in the following situations. #### Linking with another library target ``` { 'targets': [ { 'target_name': 'foo', 'dependencies': [ 'libbar', ], }, { 'target_name': 'libbar', 'type': '<(library)', 'sources': [ ], }, ], }``` Note that if the library target is in a different `.gyp` file, you haveto specify the path to other `.gyp` file, relative to this `.gyp` file'sdirectory: ``` { 'targets': [ { 'target_name': 'foo', 'dependencies': [ '../bar/bar.gyp:libbar', ], }, ], }``` Adding a library often involves updating multiple `.gyp` files, addingthe target to the appropriate `.gyp` file (possibly a newly-added `.gyp`file), and updating targets in the other `.gyp` files that depend on(link with) the new library. #### Compiling with necessary flags for a library target dependency We need to build a library (often a third-party library) with specificpreprocessor definitions or command-line flags, and need to ensure thattargets that depend on the library build with the same settings. Thissituation is handled by a `direct_dependent_settings` block: ``` { 'targets': [ { 'target_name': 'foo', 'type': 'executable', 'dependencies': [ 'libbar', ], }, { 'target_name': 'libbar', 'type': '<(library)', 'defines': [ 'LOCAL_DEFINE_FOR_LIBBAR', 'DEFINE_TO_USE_LIBBAR', ], 'include_dirs': [ '..', 'include/libbar', ], 'direct_dependent_settings': { 'defines': [ 'DEFINE_TO_USE_LIBBAR', ], 'include_dirs': [ 'include/libbar', ], }, }, ], }``` In the above example, the sources of the `foo` executable will becompiled with the options `-DDEFINE_TO_USE_LIBBAR -Iinclude/libbar`,because of those settings' being listed in the`direct_dependent_settings` block. Note that these settings will likely need to be replicated in thesettings for the library target itself, so that the library will buildwith the same options. This does not prevent the target from definingadditional options for its "internal" use when compiling its own sourcefiles. (In the above example, these are the `LOCAL_DEFINE_FOR_LIBBAR`define, and the `..` entry in the `include_dirs` list.) #### When a library depends on an additional library at final link time ``` { 'targets': [ { 'target_name': 'foo', 'type': 'executable', 'dependencies': [ 'libbar', ], }, { 'target_name': 'libbar', 'type': '<(library)', 'dependencies': [ 'libother' ], 'export_dependent_settings': [ 'libother' ], }, { 'target_name': 'libother', 'type': '<(library)', 'direct_dependent_settings': { 'defines': [ 'DEFINE_FOR_LIBOTHER', ], 'include_dirs': [ 'include/libother', ], }, }, ], }``` ### Support for Mac OS X bundles gyp supports building bundles on OS X (.app, .framework, .bundle, etc).Here is an example of this: ``` { 'target_name': 'test_app', 'product_name': 'Test App Gyp', 'type': 'executable', 'mac_bundle': 1, 'sources': [ 'main.m', 'TestAppAppDelegate.h', 'TestAppAppDelegate.m', ], 'mac_bundle_resources': [ 'TestApp/English.lproj/InfoPlist.strings', 'TestApp/English.lproj/MainMenu.xib', ], 'link_settings': { 'libraries': [ '$(SDKROOT)/System/Library/Frameworks/Cocoa.framework', ], }, 'xcode_settings': { 'INFOPLIST_FILE': 'TestApp/TestApp-Info.plist', }, },``` The `mac_bundle` key tells gyp that this target should be a bundle.`executable` targets get extension `.app` by default, `shared_library`targets get `.framework` – but you can change the bundle extensions bysetting `product_extension` if you want. Files listed in`mac_bundle_resources` will be copied to the bundle's `Resource` folderof the bundle. You can also set`process_outputs_as_mac_bundle_resources` to 1 in actions and rules tolet the output of actions and rules be added to that folder (similar to`process_outputs_as_sources`). If `product_name` is not set, the bundlewill be named after `target_name`as usual. ### Move files (refactoring) TODO ### Custom build steps TODO #### Adding an explicit build step to generate specific files TODO #### Adding a rule to handle files with a new suffix TODO ### Build flavors TODO