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))。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章