转自: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. vs2013中一: 无法查找或打开 PDB 文件解决办法 二:解决This function or variable may be unsafe

    一   链接地址: https://www.baidu.com/s?wd=%E6%97%A0%E6%B3%95%E6%9F%A5%E6%89%BE%E6%88%96%E6%89%93%E5%BC%80 ...

  2. Format

    strTimeZone := Format('%s ~ %s',[formatdatetime('yyyy-mm-dd',dtpStartTime.DateTime), formatdatetime( ...

  3. java基础-泛型3

    浏览以下内容前,请点击并阅读 声明 8 类型擦除 为实现泛型,java编译器进行如下操作进行类型擦除: 如果类型参数有限制则替换为限制的类型,如果没有则替换为Object类,变成普通的类,接口和方法. ...

  4. 使用代理下载Unity AssetStore上资源的方法

    Unity的AssetStore下载package的时候经常抽风,而且开了代理工具的全局代理依然无效. 检索网络后得知,这是因为它下载的时候不检测IE代理设置,而是取环境变量中HTTPS_proxy和 ...

  5. Nginx 正向代理

    目前现状:只有1个机器能上网(web),其他机器不能 方法:能上网的做一个代理web服务器中转,其他机器连接它即可.采用nginx Nginx配置如下: server{         resolve ...

  6. XVII Open Cup named after E.V. Pankratiev. GP of SPb

    A. Array Factory 将下标按前缀和排序,然后双指针,维护最大的右边界即可. #include<cstdio> #include<algorithm> using ...

  7. windows消息机制详解(转载)

    消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了.例如,单击鼠标.改变窗口尺寸.按下键盘上的一个键都会使Windows发送一个消息给应用程序.消息本身是作为一个记录传递给应用程序的 ...

  8. 一个flex buider 3 在eclipse下不能编译的问题解决

    今天处理一个遗留的项目:项目使用了flex作为界面,装好flex Builder 3 并添加插件到eclipse,eclipse使用3.7版本. 导入项目,编译,发现编译时候出现 Errors run ...

  9. Python之路【第一篇】python基础

    一.python开发 1.开发: 1)高级语言:python .Java .PHP. C#  Go ruby  c++  ===>字节码 2)低级语言:c .汇编 2.语言之间的对比: 1)py ...

  10. EXT 下拉框事件

    1. <ext:ComboBox ID="cbline" FieldLabel="平台部门来源" runat="server" Dis ...