1. 程式人生 > >view gn's targets name list

view gn's targets name list

前言

chromium的子工程太多了(5000+),在vs2017中找子工程時,資料夾的過濾器太多了,不好找(如果沒路子,基本找不到)。
發現可以用gn的命令列將子工程的名稱和位置列出來,這樣具體的工程資訊出來後,再找子工程容易多了。

這實驗還有個目的,可以作為向chromium工程中加入自己測試工程的前置實驗。
如果想測試一些具體的chromium的介面,在自己的測試工程,只測試一個或幾個知識點,簡潔很多。

實驗

gn產生工程,autoninja編譯工程。
gn是一定知道chromium所有子工程名稱的。

看看gn的哪個命令可以列出所有子工程名稱

gn --help > d:\my_tmp\gn_help.log

可以看出 gn ls 命令可以列出所有子工程名稱


Commands (type "gn help <command>" for more help):
  analyze: Analyze which targets are affected by a list of files.
  args: Display or configure arguments declared by the build.
  check: Check header dependencies.
  clean: Cleans the output directory.
  desc: Show lots of insightful information about a target or config.
  format: Format .gn file.
  gen: Generate ninja files.
  help: Does what you think.
  ls: List matching targets.
  path: Find paths between two targets.
  refs: Find stuff referencing a target or file.

Target declarations (type "gn help <function>" for more help):
  action: Declare a target that runs a script a single time.
  action_foreach: Declare a target that runs a script over a set of files.
  bundle_data: [iOS/macOS] Declare a target without output.
  copy: Declare a target that copies files.
  create_bundle: [iOS/macOS] Build an iOS or macOS bundle.
  executable: Declare an executable target.
  group: Declare a named group of targets.
  loadable_module: Declare a loadable module target.
  shared_library: Declare a shared library target.
  source_set: Declare a source set target.
  static_library: Declare a static library target.
  target: Declare an target with the given programmatic type.

Buildfile functions (type "gn help <function>" for more help):
  assert: Assert an expression is true at generation time.
  config: Defines a configuration object.
  declare_args: Declare build arguments.
  defined: Returns whether an identifier is defined.
  exec_script: Synchronously run a script and return the output.
  foreach: Iterate over a list.
  forward_variables_from: Copies variables from a different scope.
  get_label_info: Get an attribute from a target's label.
  get_path_info: Extract parts of a file or directory name.
  get_target_outputs: [file list] Get the list of outputs from a target.
  getenv: Get an environment variable.
  import: Import a file into the current scope.
  not_needed: Mark variables from scope as not needed.
  pool: Defines a pool object.
  print: Prints to the console.
  process_file_template: Do template expansion over a list of files.
  read_file: Read a file into a variable.
  rebase_path: Rebase a file or directory to another location.
  set_default_toolchain: Sets the default toolchain name.
  set_defaults: Set default values for a target type.
  set_sources_assignment_filter: Set a pattern to filter source files.
  split_list: Splits a list into N different sub-lists.
  template: Define a template rule.
  tool: Specify arguments to a toolchain tool.
  toolchain: Defines a toolchain.
  write_file: Write a file to disk.

Built-in predefined variables (type "gn help <variable>" for more help):
  current_cpu: [string] The processor architecture of the current toolchain.
  current_os: [string] The operating system of the current toolchain.
  current_toolchain: [string] Label of the current toolchain.
  default_toolchain: [string] Label of the default toolchain.
  host_cpu: [string] The processor architecture that GN is running on.
  host_os: [string] The operating system that GN is running on.
  invoker: [string] The invoking scope inside a template.
  python_path: [string] Absolute path of Python.
  root_build_dir: [string] Directory where build commands are run.
  root_gen_dir: [string] Directory for the toolchain's generated files.
  root_out_dir: [string] Root directory for toolchain output files.
  target_cpu: [string] The desired cpu architecture for the build.
  target_gen_dir: [string] Directory for a target's generated files.
  target_name: [string] The name of the current target.
  target_os: [string] The desired operating system for the build.
  target_out_dir: [string] Directory for target output files.

Variables you set in targets (type "gn help <variable>" for more help):
  all_dependent_configs: [label list] Configs to be forced on dependents.
  allow_circular_includes_from: [label list] Permit includes from deps.
  arflags: [string list] Arguments passed to static_library archiver.
  args: [string list] Arguments passed to an action.
  asmflags: [string list] Flags passed to the assembler.
  assert_no_deps: [label pattern list] Ensure no deps on these targets.
  bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.
  bundle_deps_filter: [label list] A list of labels that are filtered out.
  bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle
  bundle_plugins_dir: Expansion of {{bundle_plugins_dir}} in create_bundle.
  bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.
  bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.
  cflags: [string list] Flags passed to all C compiler variants.
  cflags_c: [string list] Flags passed to the C compiler.
  cflags_cc: [string list] Flags passed to the C++ compiler.
  cflags_objc: [string list] Flags passed to the Objective C compiler.
  cflags_objcc: [string list] Flags passed to the Objective C++ compiler.
  check_includes: [boolean] Controls whether a target's files are checked.
  code_signing_args: [string list] Arguments passed to code signing script.
  code_signing_outputs: [file list] Output files for code signing step.
  code_signing_script: [file name] Script for code signing.
  code_signing_sources: [file list] Sources for code signing step.
  complete_static_lib: [boolean] Links all deps into a static library.
  configs: [label list] Configs applying to this target or config.
  data: [file list] Runtime data file dependencies.
  data_deps: [label list] Non-linked dependencies.
  defines: [string list] C preprocessor defines.
  depfile: [string] File name for input dependencies for actions.
  deps: [label list] Private linked dependencies.
  friend: [label pattern list] Allow targets to include private headers.
  include_dirs: [directory list] Additional include directories.
  inputs: [file list] Additional compile-time dependencies.
  ldflags: [string list] Flags passed to the linker.
  lib_dirs: [directory list] Additional library directories.
  libs: [string list] Additional libraries to link.
  output_dir: [directory] Directory to put output file in.
  output_extension: [string] Value to use for the output's file extension.
  output_name: [string] Name for the output file other than the default.
  output_prefix_override: [boolean] Don't use prefix for output name.
  outputs: [file list] Output files for actions and copy targets.
  partial_info_plist: [filename] Path plist from asset catalog compiler.
  pool: [string] Label of the pool used by the action.
  precompiled_header: [string] Header file to precompile.
  precompiled_header_type: [string] "gcc" or "msvc".
  precompiled_source: [file name] Source file to precompile.
  product_type: [string] Product type for Xcode projects.
  public: [file list] Declare public header files for a target.
  public_configs: [label list] Configs applied to dependents.
  public_deps: [label list] Declare public dependencies.
  response_file_contents: [string list] Contents of .rsp file for actions.
  script: [file name] Script file for actions.
  sources: [file list] Source files for a target.
  testonly: [boolean] Declares a target must only be used for testing.
  visibility: [label list] A list of labels that can depend on a target.
  write_runtime_deps: Writes the target's runtime_deps to the given path.
  xcode_extra_attributes: [scope] Extra attributes for Xcode projects.
  test_application_name: [string] Test application name for unit or ui test target.

Other help topics:
  all: Print all the help at once
  buildargs: How build arguments work.
  dotfile: Info about the toplevel .gn file.
  execution: Build graph and execution overview.
  grammar: Language and grammar for GN build files.
  input_conversion: Processing input from exec_script and read_file.
  label_pattern: Matching more than one label.
  labels: About labels.
  ninja_rules: How Ninja build rules are named.
  nogncheck: Annotating includes for checking.
  runtime_deps: How runtime dependency computation works.
  source_expansion: Map sources to outputs for scripts.
  switches: Show available command-line switches.

用gn列出所有子工程名稱

cd /d Z:\chromium\src\
gn --ide=vs args out\my_x86_d
gn ls out\my_x86_d > d:\my_tmp\gn_list_target.log

//:blink_tests
//:chrome_official_builder
//:chrome_official_builder_no_unittests
//:chromium_builder_asan
//:chromium_builder_perf
//:gn_all
...
//base/test:run_all_unittests
//base/test:test_child_process
//base/test:test_config
...
//v8/tools:v8_testrunner
//v8/tools/gcmole:v8_run_gcmole
//v8/tools/jsfunfuzz:v8_jsfunfuzz

可以看出子工程在工程資料夾過濾器中的位置的規則如下
e.g. //v8/tools/jsfunfuzz:v8_jsfunfuzz
e.g. //chrome:chrome
// 工程過濾器的名稱:工程名稱
驗證一下,是否是這個規則
剛開始看chromium時,找子工程都是關鍵字搜尋,有了這個方法,可以準確定位到子工程在vs2017中的位置。

主工程chrome在chromium解決方案中的過濾器資料夾靠前,就那主工程為例,在vs2017解決方案中翻一下,看看是否可以清晰快速的找個chrome子工程。
在這裡插入圖片描述

只編譯一個子工程相關的內容

我找到一個測試工程 //base/test:test_child_process
在這裡插入圖片描述
這裡面沒幾句程式碼,真是個測試用的子工程。還是純控制檯的程式,沒用到一點chromium的基礎程式碼庫。
將這個工程修改後, 隨便寫2句測試程式碼如下:

// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Simple testing command, used to exercise child process launcher calls.
//
// Usage:
//        echo_test_helper [-x exit_code] arg0 arg1 arg2...
//        Prints arg0..n to stdout with space delimiters between args,
//        returning "exit_code" if -x is specified.
//
//        echo_test_helper -e env_var
//        Prints the environmental variable |env_var| to stdout.
int main(int argc, char** argv) {
	printf("hello - chromium test\n");
	system("pause");
	return EXIT_SUCCESS;

	/*
  if (strcmp(argv[1], "-e") == 0) {
    if (argc != 3) {
      return 1;
    }

    const char* env = getenv(argv[2]);
    if (env != NULL) {
      printf("%s", env);
    }
  } else {
    int return_code = 0;
    int start_idx = 1;

    if (strcmp(argv[1], "-x") == 0) {
      return_code = atoi(argv[2]);
      start_idx = 3;
    }

    for (int i = start_idx; i < argc; ++i) {
      printf((i < argc - 1 ? "%s " : "%s"), argv[i]);
    }

    return return_code;
  }
	*/
}

編譯指定的子工程

Z:\chromium\src>autoninja -C out\my_x86_d test_child_process
ninja.exe -C out\my_x86_d test_child_process -l 16
ninja: Entering directory `out\my_x86_d'
ninja: no work to do.
    Longest build steps:
           0.2 weighted s to build test_child_process.exe, test_child_process.exe.pdb (0.2 s CPU time)
           0.2 weighted s to build obj/base/test/test_child_process/test_child_process.obj (0.2 s CPU time)
    Time by build-step type:
           0.2 s weighted time to generate 1 PEFile (linking) files (0.2 s CPU time)
           0.2 s weighted time to generate 1 .obj files (0.2 s CPU time)
    0.4 s weighted time (0.4 s CPU time, 1.0x parallelism)
    2 build steps completed, average of 5.33/s
Z:\chromium\src>

執行編譯好的子工程

Z:\chromium\src>.\out\my_x86_d\test_child_process.exe
hello - chromium test
請按任意鍵繼續. . .