Gyp语法规则参考 & 工具的使用

时间:2015-09-22 18:49:38   收藏:0   阅读:361

转自: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大致有如下的特点:

详细设计

设计准则:

样例

{
  ‘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和构建规则。

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

项目(targets)

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

配置(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提供了自定义处理输入输出的功能,其中的每一项都有下面的字段:

构建系统会比较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提供了自定义构建的功能,每一项都有下面的字段:

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

{
  ‘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提供了简单的文件拷贝功能,每一项都有下面的字段:

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}。例如:

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命令行

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

原文:http://www.cnblogs.com/x_wukong/p/4829598.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!