gyp_chromium分析

gclient最后会执行hooks里的动作,而在chromium的代码配置文件中设置的hooks是运行src/build/gyp_chromium文件。下面分析这个文件的作用以及运行过程。

定义两个路径分别是script_dir和chromium_src,这里分别是脚本所在的路径和它的父目录。然后定义两个函数apply_gyp_environment和additional_include_files。

在程序运行开始就检查是否设置了环境变量SKIP_CHROMIUM_GYP_ENV,如果没有则调用函数apply_gyp_environment尝试读取chromium.gyp_env。

def apply_gyp_environment(file_path=None):
  """
  Reads in a *.gyp_env file and applies the valid keys to os.environ.
  """
  if not file_path or not os.path.exists(file_path):
    return
  file_contents = open(file_path).read()
  try:
    file_data = eval(file_contents, {'__builtins__': None}, None)
  except SyntaxError, e:
    e.filename = os.path.abspath(file_path)
    raise
  supported_vars = ( 'CHROMIUM_GYP_FILE',
                     'CHROMIUM_GYP_SYNTAX_CHECK',
                     'GYP_DEFINES',
                     'GYP_GENERATOR_FLAGS',
                     'GYP_GENERATOR_OUTPUT', )
  for var in supported_vars:
    val = file_data.get(var)
    if val:
      if var in os.environ:
        print 'INFO: Environment value for "%s" overrides value in %s.' % (
            var, os.path.abspath(file_path)
        )
      else:
        os.environ[var] = val

读取file_path的内容,赋值给file_data。eval函数求值file_data字符串的值,其结果file_data是dict。读取其中的内容,与环境定义的变量supported_vars比较,如果环境中存在的变量则保持不变,否则加入到环境。

如果没有指定gyp文件,则添加all.gyp路径到args中,args.append(os.path.join(script_dir, 'all.gyp')),并且args.extend(['-I' + i for i in additional_include_files(args)])把参数中以-I指定的路径加入到args中,在本例中args开始为空。

########## gyp_chromium ##############
def additional_include_files(args=[]):
  """
  Returns a list of additional (.gypi) files to include, without
  duplicating ones that are already specified on the command line.
  """
  # Determine the include files specified on the command line.
  # This doesn't cover all the different option formats you can use,
  # but it's mainly intended to avoid duplicating flags on the automatic
  # makefile regeneration which only uses this format.
  specified_includes = set()
  for arg in args:
    if arg.startswith('-I') and len(arg) > 2:
      specified_includes.add(os.path.realpath(arg[2:]))

  result = []
  def AddInclude(path):
    if os.path.realpath(path) not in specified_includes:
      result.append(path)

  # Always include common.gypi & features_override.gypi
  AddInclude(os.path.join(script_dir, 'common.gypi'))
  AddInclude(os.path.join(script_dir, 'features_override.gypi'))

  # Optionally add supplemental .gypi files if present.
  supplements = glob.glob(os.path.join(chrome_src, '*', 'supplement.gypi'))
  for supplement in supplements:
    AddInclude(supplement)

  return result
additional_include_files函数的作用是除了命令行指定的路径外,包含chrome_src目录下的所有supplement.gypi文件以及script_dir下的common.gypi和features_override.gypi。还有all.gyp文件的路径组成了args。最后调用sys.exit(gyp.main(args))。

你可能感兴趣的:(gyp_chromium分析)