转自:http://www.cnblogs.com/nanvann/p/3913880.html

翻译自 https://code.google.com/p/gyp/wiki/GypLanguageSpecification

目的和背景

Google使用过很多处理平台无关的项目构建系统,比如SconsCMake。在实际使用中这些并不能满足需求。开发复杂的应用程序时,在Mac上Xcode更加适合,而Windows上Visual Studio更是无二之选。
gyp是为Chromium项目创建的项目生成工具,可以从平台无关的配置生成平台相关的Visual Studio、Xcode、Makefile的项目文件。这样一来我们就不需要花额外的时间处理每个平台不同的项目配置以及项目之间的依赖关系。

概述

Gyp大致有如下的特点:

  • 配置文件都是以.gyp结尾
  • 每个.gyp文件都描述了如何去构建项目
  • 每个.gyp文件都能生成若干个合适特定平台的项目文件:

    • 在Mac上,.gyp文件会生成Xcode项目文件,包含了如何构建项目。一个.gyp文件会生成的一个.xcodeproj
    • 在Windows上,.gyp文件会生成Visual Studio项目文件。一个.gyp文件会生成一个.sln,并为每个targets生成一个.vcproj
    • 在Linux上,.gyp文件会生成Makefile文件
  • .gyp文件的语法是Python数据格式(Json格式)
  • .gypi文件可以被.gyp文件包含,方便统一设置
  • 配置中数据是键-值对的形式

详细设计

设计准则:

  • 关键字一致性:所有的关键字都和平台项目的项目配置字段相同
  • 通过前缀表明配置属于的特定平台。比如:msvs_disabled_warnings,xcode_framework_dirs

样例

{
'target_defaults': {
'defines': [
'U_STATIC_IMPLEMENTATION',
['LOGFILE', 'foo.log',],
],
'include_dirs': [
'..',
],
},
'targets': [
{
'target_name': 'foo',
'type': 'static_library',
'sources': [
'foo/src/foo.cc',
'foo/src/foo_main.cc',
],
'include_dirs': [
'foo',
'foo/include',
],
'conditions': [
[ 'OS==mac', { 'sources': [ 'platform_test_mac.mm' ] } ]
],
'direct_dependent_settings': {
'defines': [
'UNIT_TEST',
],
'include_dirs': [
'foo',
'foo/include',
],
},
},
],
}

结构元素

.gyp文件中定义了一些targets和构建规则。

下面的关键字可以定义在最顶层:

  • conditions: 条件定义。见*****条件(conditions)*****
  • includes: 包含.gypi文件的列表
    {
    'targets':[
    {
    'target_name': 'Thread',
    'type': 'executable',
    'includes': [
    '../common.gypi',
    './thread.gypi',
    ]
    ...
    }
    ],
    }
  • target_defaults: 默认的项目配置,每个项目(targets)的配置都需要从这个配置继承
  • targets: 项目列表
    {
    'targets': [
    {
    'target_name': 'hello1',
    'product_extension': 'stuff',
    'type': 'executable',
    'sources': [
    'hello.c',
    ],
    },
    {
    'target_name': 'hello2',
    'target_extension': 'stuff',
    'type': 'executable',
    'sources': [
    'hello.c',
    ],
    }
    ]
    }
  • variables: 定义了键值对,可以被其他地方以<(varname)的方式引用
    {
    'variables': {
    'pi': 'import math; print math.pi',
    'third_letters': "<(other_letters)HIJK",
    'letters_list': 'ABCD',
    'other_letters': '<(letters_list)EFG',
    'check_included': '<(included_variable)',
    'check_lists': [
    '<(included_variable)',
    '<(third_letters)',
    ],
    'check_int': 5,
    'check_str_int': '6',
    'check_list_int': [
    7,
    '8',
    9,
    ],
    'not_int_1': ' 10',
    'not_int_2': '11 ',
    'not_int_3': '012',
    'not_int_4': '13.0',
    'not_int_5': '+14',
    'negative_int': '-15',
    'zero_int': '0',
    },
    ...
    }

项目(targets)

.gyp文件定义的一套构建项目的规则。targets中也可以包含includesconditionsvariables。下面的是一些targets中的专有字段:

  • target_name: 指定定义的target的名称
  • type: target的类型。支持executablestatic_libraryshared_library和 none。其中none类型也很有用,可以作为处理资源、文档等特别项目的类型。例如,在Windows中,none将作为工具集类型的项目存在
  • product_extension: 指定target生成目标的扩展名,不包含'.'
  • product_name: 指定tareget生成目标的文件名,与product_extension组成一个文件全名
  • dependencies: 指定target依赖的其他target
    {
    'targets': [
    {
    'target_name': 'a',
    'type': 'static_library',
    'dependencies': [
    'b/b.gyp:b',
    'c/c.gyp:*'
    ],
    'sources': [
    'a.c',
    ],
    },
    ],
    }
  • defines: 定义了预处理宏。类似于C/C++命令行编译中的-D或/D选项
    {
    'targets': [
    {
    'target_name': 'defines',
    'type': 'executable',
    'defines': [
    'FOO',
    'VALUE=1',
    'PAREN_VALUE=(1+2+3)',
    'HASH_VALUE="a#1"',
    ],
    'sources': [
    'defines.c',
    ],
    },
    ]
    }
  • include_dirs: 指定了包含文件的查找目录。类似于C/C++命令行编译中的-I或/I选项
    {
    'targets': [
    {
    'target_name': 'includes',
    'type': 'executable',
    'dependencies': [
    'subdir/subdir_includes.gyp:subdir_includes',
    ],
    'cflags': [
    '-Ishadow1',
    ],
    'include_dirs': [
    '.',
    'inc1',
    'shadow2',
    'subdir/inc2',
    ],
    'sources': [
    'includes.c',
    ],
    },
    ],
    }
  • sources: 列出了项目中的代码文件和一些项目相关的文件。sources!段中可以指定被排除的文件
  • configurations: 为targets定义的一套构建配置。见*****配置(configurations)*****
  • link_settings: 指定target需要链接的库。executableshared_library类型的target需要指定链接库。这个段内可以指定target中可包含的除了configurationstarget_nametype的所有配置。可以与all_dependent_settingdirect_dependent_setting做对比
  • direct_dependent_settings: 指定依赖本target的target设置。这个段内可以指定target中可包含的除了configurationstarget_nametype的所有配置。可以与all_dependent_settinglink_settings做对比
  • all_dependent_settings:
  • libraries: 指定target依赖的库,见link_settings>libraries
    ...
    'link_settings': {
    'libraries': [
    'libiconv.dylib',
    '<(ZLIB_DIR)contrib/minizip/libminizip.a',
    'libcurl.dylib',
    ],
    },
    ...
  • actions: 针对输入的文件,定义了一组自定义的构建动作。见*****动作(actions)*****
  • copies: 定义了一套拷贝动作。见*****拷贝(copies)*****
  • rules: 见*****规则(rules)*****
  • target_conditions: 类似于conditions,但是起左右的时间比conditions晚
  • msvs_precompiled_header: 指定预编译头文件。只能用于Visual Studio
  • msvs_precompiled_source: 指定预编译源文件。只能用于Visual Studio
  • msvs_prebuild: 生成之前事件。只能用于Visual Studio
  • msvs_postbuild: 生成之后事件。只能用于Visual Studio
    'msvs_postbuild': r'copy "$(OutDir)$(TargetName)" "C:\$(TargetName)"
  • msvs_props: 指定target的属性页文件(.vsprops) 。只能用于Visual Studio
  • msvs_cygwin_shell: 指定action运行在cygwin下。只能用于Visual Studio
  • msvs_cygwin_dirs: 指定cygwin的目录。只能用于Visual Studio
  • xcode_config_file: 在xcode中,指定target的配置文件(.xcconfig)。只能用于xcode
  • xcode_framework_dirs: 在xcode中,指定框架的目录。只能用于xcode

配置(configurations)

configurations段可以在targetstarget_defaults段中。configurations不能够重写由target_defaults中指定的项。

在有些时候我们需要为项目指定多个配置,比如DebugRelease。 下面的一段为Windows中的常规配置


...
'configurations': {
'Debug': {
'msvs_settings': {
'VCCLCompilerTool': {
# 多线程调试 DLL (/MDd)
'RuntimeLibrary': '3',
# 不优化 /Od
'Optimization': '0',
# 用于“编辑并继续”的程序数据库 (/ZI)
'DebugInformationFormat': '4',
},
'VCLinkerTool': {
'GenerateDebugInformation': 'true',
'GenerateMapFile': 'false',
# 'SubSystem': '1',
},
},
}, # Debug
'Release': {
'msvs_settings': {
'VCCLCompilerTool': {
# 多线程 DLL (/MD)
'RuntimeLibrary': '2',
# 完全优化 /Os
'Optimization': '2',
# 使用内部函数 /Oi
'EnableIntrinsicFunctions': 'true',
# 程序数据库 (/Zi)
'DebugInformationFormat': '3',
},
'VCLinkerTool': {
'GenerateDebugInformation': 'true',
'GenerateMapFile': 'false',
},
},
}, # Release
},
...

条件(conditions)

conditionstarget_conditons可以出现在.gyp文件的任何位置。conditions语句在加载.gyp文件后即进行处理,target_conditions语句在处理完所有依赖项后在处理。
在加载完全局和局部的数据后,使用python的eval()函数对条件字符串做处理。

动作(actions)

actions提供了自定义处理输入输出的功能,其中的每一项都有下面的字段:

  • action_name: action的名称,某些平台可能会忽略这个字段
  • inputs: 输入信息,作为增量构建时使用
  • outputs: 输出信息,作为增量构建时使用
  • action: 构建命令
  • message: 构建时显示的信息

构建系统会比较inputsoutputs中的文件是否是修改过,只有在修改过的情况下才会运行action
在Xcode中,是通过shell脚本实现的;在Visual Studio中通过FileConfiguration包含一个自定义的VCCustomBuildTool实现。

'sources': [
# libraries.cc is generated by the js2c action below.
'<(INTERMEDIATE_DIR)/libraries.cc',
],
'actions': [
{
'variables': {
'core_library_files': [
'src/runtime.js',
'src/v8natives.js',
'src/macros.py',
],
},
'action_name': 'js2c',
'inputs': [
'tools/js2c.py',
'<@(core_library_files)',
],
'outputs': [
'<(INTERMEDIATE_DIR)/libraries.cc',
'<(INTERMEDIATE_DIR)/libraries-empty.cc',
],
'action': ['python', 'tools/js2c.py', '<@(outputs)', 'CORE', '<@(corelibrary_files)'],
},
],

规则(rules)

rules提供了自定义构建的功能,每一项都有下面的字段:

  • rule_name: rule的名称,某些平台可能会忽略这个字段
  • extension: 在本target中,所有以此为扩展名的源文件在构建时都使用这个规则
  • inputs: 依赖的构建规则
  • outputs: 输出信息,作为增量构建时使用。可以访问下面的RULE_INPUT_*变量
  • action: 构建命令。可以访问下面的RULE_INPUT_*变量
  • message: 构建时显示的信息。可以访问下面的RULE_INPUT_*变量

下面的这个变量可以在outputsactionmessage中访问:

  • RULE_INPUT_PATH: 当前输入的全路径
  • RULE_INPUT_DIRNAME: 当前输入的目录
  • RULE_INPUT_NAME: 当前输入的名称
  • RULE_INPUT_ROOT: 当前输入的去掉扩展名
  • RULE_INPUT_EXT: 当前输入的扩展名
{
'targets': [
{
'target_name': 'program',
'type': 'executable',
'msvs_cygwin_shell': 0,
'sources': [
'main.c',
'prog1.in',
'prog2.in',
],
'rules': [
{
'rule_name': 'make_sources',
'extension': 'in',
'inputs': [
'make-sources.py',
],
'outputs': [
'<(INTERMEDIATE_DIR)/<(RULE_INPUT_ROOT).c',
'<(INTERMEDIATE_DIR)/<(RULE_INPUT_ROOT).h',
],
'action': [
'python', '<(_inputs)', '<(RULE_INPUT_NAME)', '<@(_outputs)',
],
'process_outputs_as_sources': 1,
},
],
},
],
}

rulesactions有些相似之处。

拷贝(copies)

copies提供了简单的文件拷贝功能,每一项都有下面的字段:

  • destination: 拷贝的目的地文件夹
  • files: 需要拷贝的文件列表

copies会在destination创建相同名称的文件。

{
'targets': [
{
'target_name': 'copies1',
'type': 'none',
'copies': [
{
'destination': 'copies-out',
'files': [
'file1',
],
},
],
},
{
'target_name': 'copies2',
'type': 'none',
'copies': [
{
'destination': '<(PRODUCT_DIR)/copies-out',
'files': [
'file2',
],
},
],
},
# 拷贝目录
{
'target_name': 'copies3',
'type': 'none',
'copies': [
{
'destination': '<(PRODUCT_DIR)/copies-out',
'files': [
'directory/',
],
},
],
},
],
}

代码文件的命名

平台相关的文件命名规则为*_win.{ext}*_mac.{ext}*_linux.{ext}*_posix.{ext}。例如:

  • _win: foo_win.cpp
  • _mac: foo_mac.cpp,foo_mac.mm
  • _linux: foo_linux.cpp
  • _posix: foo_posix.cpp

gyp的源代码

对于文档中一些未列出和有争议地方最可靠的是去看gyp的源代码和例子。gyp的测试代码很详细,可以作为学习的例子做参考。

使用IDE调试生成的Makefile项目

在Linux上调试一般都是使用gdb,但是对于不态熟悉的就不好直接上手。这里推荐QtCreator,当然Netbeans和Eclispe CDT也可以,但是不如QtCreator方便和高效。

通用配置 common.gypi

 {
'includes': [
'env.gypi'
],
'include_dirs': [
'.',
],
'configurations': {
'Debug': {
'defines': [
'DEBUG',
],
},
'Release': {
'defines': [
'NDEBUG',
],
},
}, # configurations
'conditions': [
['OS=="win"', {
'defines': [
'WIN32',
'UNICODE',
'_UNICODE',
'OS_WIN',
],
'msbuild_configuration_attributes': {
'IntermediateDirectory': '$(OutDir)__BuildTemp\\$(ProjectName)\\',
},
'msbuild_toolset': {
#'PlatformToolset': 'v100'
}, # msbuild_toolset
'msvs_settings': {
'VCCLCompilerTool': {
'WarningLevel': '3',
'DisableSpecificWarnings': ['4251','4996'],
'WarnAsError': 'true',
},
'VCLinkerTool': {
'AdditionalDependencies': [
'kernel32.lib',
'user32.lib',
],
'AdditionalLibraryDirectories': [],
},
}, # msvs_settings
'configurations': {
'Debug': {
'msvs_settings': {
'VCCLCompilerTool': {
# 多线程调试 DLL (/MDd)
'RuntimeLibrary': '3',
# 不优化 /Od
'Optimization': '0',
# 用于“编辑并继续”的程序数据库 (/ZI)
'DebugInformationFormat': '4',
},
'VCLinkerTool': {
'GenerateDebugInformation': 'true',
'GenerateMapFile': 'false',
# 'SubSystem': '1',
},
}, # msvs_settings
}, # Debug
'Release': {
'msvs_settings': {
'VCCLCompilerTool': {
# 多线程 DLL (/MD)
'RuntimeLibrary': '2',
# 完全优化 /Os
'Optimization': '2',
# 使用内部函数 /Oi
'EnableIntrinsicFunctions': 'true',
# 程序数据库 (/Zi)
'DebugInformationFormat': '3',
},
'VCLinkerTool': {
'GenerateDebugInformation': 'true',
'GenerateMapFile': 'false',
},
}, # msvs_settings
}, # Release
}, # configurations
}], # Windows
['OS=="mac"', {
'make_global_settings': [
['CC', '/usr/bin/clang'],
['CXX', '/usr/bin/clang++'],
], 'defines': [
'OS_POSIX',
'OS_MACOSX',
],
'xcode_settings': {
'ALWAYS_SEARCH_USER_PATHS': 'NO',
#'i386', 'x86_64'
'ARCHS': [ 'x86_64' ],
'MACOSX_DEPLOYMENT_TARGET': '10.6',
'CC': 'clang',
'GCC_VERSION': 'com.apple.compilers.llvm.clang.1_0',
'CLANG_CXX_LANGUAGE_STANDARD': 'c++0x',
# libstdc++, c++11, libc++
'CLANG_CXX_LIBRARY': 'libstdc++',
'GCC_ENABLE_OBJC_GC': 'unsupported',
#'LIBRARY_SEARCH_PATHS': [],
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'GCC_SYMBOLS_PRIVATE_EXTERN': 'NO',
'DEBUG_INFORMATION_FORMAT': 'dwarf-with-dsym',
#'DEPLOYMENT_POSTPROCESSING': 'YES',
'OTHER_CFLAGS': [
'-fno-eliminate-unused-debug-symbols',
'-mmacosx-version-min=10.6',
# compile use oc++
'-x objective-c++',
],
'WARNING_CFLAGS': ['-Wno-deprecated-declarations'],
'WARNING_CFLAGS!': ['-Wall', '-Wextra',],
'WARNING_CXXFLAGS': ['-Wstrict-aliasing', '-Wno-deprecated',],
}, # xcode_settings
'link_settings': {
'libraries': [
'$(SDKROOT)/System/Library/Frameworks/Foundation.framework',
],
},
'libraries': [],
'mac_framework_dirs': [],
'configurations': {
'Debug': {
'xcode_settings': {
'GCC_DEBUGGING_SYMBOLS': 'full',
'STRIP_INSTALLED_PRODUCT': 'YES',
'GCC_OPTIMIZATION_LEVEL': '0',
'OTHER_CFLAGS': ['-g',],
'OTHER_CXXFLAGS': ['-g',],
}, # xcode_settings
}, # Debug
'Release': {
'xcode_settings': {
'GCC_OPTIMIZATION_LEVEL': 's',
}, # xcode_settings
}, # Release
}, # configurations
}], # Mac
['OS=="linux"', {
'defines': [
'OS_POSIX',
'OS_LINUX',
],
'cflags': [
# Support 64-bit shared libs (also works fine for 32-bit).
'-fPIC',
'-std=c++11',
'-fstrict-aliasing',
'-Wall',
'-Wextra',
'-Wshadow',
'-Wconversion',
#'-Wpadded',
'-Wstrict-aliasing=2',
'-Wstrict-overflow=4',
],
'ldflags': [],
'configurations': {
'Debug': {
'cflags': [
'-g',
'-C',
],
},
'Release': {
'cflags': [
'-O2',
],
},
}, # configurations
}], # Linux
], # conditions
}

gyp命令行

  • --depth: Chromium历史遗留问题,需要设置为.
  • --generator-out: 设置生成项目的输出文件夹
  • --f: 设置生成项目的类型,不设置则根据平台决定生成的项目类型。可用的值有:msvsxcodemake
  • --G: 设置生成的其他标记参数,这个值和具体的平台有关。

    • msvs_version: 指定生成的Visual Studio项目的版本,不设置则会根据系统中安装的最新的Visual Stuio生成项目。可用的值有20052005e20082008e20102010e20122012e20132013e。例如: --Gmsvs_version=2010
  • --toplevel-dir: 设置源代码的跟目录,默认将取--depth指定的值
  • -D: 传入变量到.gyp,可以使用<(varname)这种方式读取。例如:-Dbuildtype=share_library
  • --no-circular-check:

写API文档是一件很蛋疼的事情,以后继续添加。

Gyp语法规则参考 & 工具的使用的更多相关文章

  1. markdown 语法规则

    markdown 语法规则 参考Markdown--入门指南 也可以参考这篇 Markdown: Basics (快速入门) 一级标题 二级标题 三级标题 列表 无序 1 2 3 有序 1 2 3 需 ...

  2. 2018-01-15 Antlr4: 修改语法规则更接近普通BNF格式

    经 @沈默 在上文Antlr4添加中文变量赋求值,括号,各种问题评论中指出, 语法规则描述依赖于Antlr4生成的语法分析器的默认分析方法, 比如运算符的左联系, 以及优先级处理等等. 于是将语法修改 ...

  3. XML 树结构,语法规则,元素,属性,验证及其解析

    XML 文档形成了一种树结构,它从"根部"开始,然后扩展到"枝叶". 一个 XML 文档实例 XML 文档使用简单的具有自我描述性的语法: <?xml v ...

  4. XML的树结构与语法规则

    ㈠概念 什么是 XML? ⑴XML 指可扩展标记语言(EXtensible Markup Language) ⑵XML 是一种标记语言,很类似 HTML ⑶XML 的设计宗旨是传输数据,而非显示数据 ...

  5. JSON 语法规则详解

    JSON 的语法规则十分简单,无论用何种方法总结都只有数条而已,它参考了 C 语言家族的一些习惯,学习起来并不会感到陌生. 回顾JSON 的五点语法 1)- 数组(Array)用方括号("[ ...

  6. PHPCMS标签:PC标签模板语法规则

    模板语法规则1.变量表示{$name} 被解析成 <?=$name?>,表示显示变量$name的值,其中的“name”由英文字母.数字和下划线组成首字母必须是英文字母或者下划线. 2.常量 ...

  7. Thrax-构建基于语法的语言模型工具

    安装 http://www.openfst.org/twiki/bin/view/GRM/ThraxQuickTour http://cslu.ogi.edu/~sproatr/Courses/Tex ...

  8. 良好的JavaScript编码风格(语法规则)

    编码风格 1.概述 "编程风格"(programming style)指的是编写代码的样式规则.不同的程序员,往往有不同的编程风格. 有人说,编译器的规范叫做"语法规则& ...

  9. Django模板语言(常用语法规则)

    Django模板语言 The Django template language 模板中常用的语法规则 {最新版本的Django语法可能有改变,不支持的操作可能支持了.[HTML教程 - 基本元素/标签 ...

随机推荐

  1. 【APICloud】利用sublimetext3编写apicloud

    下载sublime text 3 安装插件 使用模糊搜索apicloud有三个插件全部下载下来 安装海马玩模拟器,这是一个安卓的模拟器,进入官网下载后直接安装就可以了. 打开sublime text ...

  2. 我理想中的父母(The Ideal Parents In My Heart)

    Parents are the first teachers in children's life, and people all know the great importance exactly ...

  3. Spark性能优化-coalesce(n)

    有时用Spark 运行Job 的时候,输出可能会出现一些空或者小内容.这时重新将输出的Partition 进行重新调整,可以减少RDD中Patition的数目. 两种方式: 1. coalesce(n ...

  4. ubuntu14.04+nvidia driver+cuda8+cudnn5+tensorflow0.12

    文章在简书里面编辑的,复制过来貌似不太好看,还是到简书的页面看吧: http://www.jianshu.com/p/c89b97d052b7 1.安装环境简介: 硬件: cpu:i7 6700k g ...

  5. PLSQL配置新的oracle实例

    在Win环境下的Oracle中,D:\Oracle\product\10.1.0\db_1\NETWORK\ADMIN\tnsnames.ora文件很重要,它作用是:本地命名的配置.本地名可以用简单的 ...

  6. C#输出文字对齐,空格位数对齐

    Align String with Space This example shows how to align strings with spaces. The example formats tex ...

  7. <十二>JDBC_批量处理

    import java.sql.Connection;import java.sql.PreparedStatement;import org.junit.Test;import com.kk.jdb ...

  8. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  9. wamp2.5 局域网无法访问问题

    1.打开http.conf文件,在对应处修改为如下内容(通常经过步骤一之后就能访问了,若不行则再执行后面步骤) <Directory /> Options FollowSymLinks A ...

  10. PHP基础知识之魔术方法

    __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sle ...