xref: /aosp_15_r20/external/angle/build/util/process_version.gni (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1# Copyright 2014 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/util/lastchange.gni")
6
7# Runs the version processing script over the given template file to produce
8# an output file. This is used for generating various forms of files that
9# incorporate the product name and version.
10#
11# Unlike GYP, this will actually compile the resulting file, so you don't need
12# to add it separately to the sources, just depend on the target.
13#
14# In GYP this is a rule that runs once per ".ver" file. In GN this just
15# processes one file per invocation of the template so you may have to have
16# multiple targets.
17#
18# Parameters:
19#   sources (optional):
20#     List of file names to read. When converting a GYP target, this should
21#     list the 'source' (see above) as well as any extra_variable_files.
22#     The files will be passed to version.py in the order specified here.
23#
24#   output:
25#     File name of file to write. In GYP this is unspecified and it will
26#     make up a file name for you based on the input name, and tack on
27#     "_version.rc" to the end. But in GN you need to specify the full name.
28#
29#   template_file (optional):
30#     Template file to use (not a list). Most Windows users that want to use
31#     this to process a .rc template should use process_version_rc_template(),
32#     defined in //chrome/process_version_rc_template.gni, instead.
33#
34#   extra_args (optional):
35#     Extra arguments to pass to version.py. Any "-f <filename>" args should
36#     use sources instead.
37#
38#   process_only (optional, defaults to false)
39#     Set to generate only one action that processes the version file and
40#     doesn't attempt to link the result into a source set. This is for if
41#     you are processing the version as data only.
42#
43#   executable (optional, defaults to false)
44#     Sets the executable bit on the output file (POSIX only).
45#
46#   visibility (optional)
47#
48# Example:
49#   process_version("myversion") {
50#     sources = [
51#       "//chrome/VERSION"
52#       "myfile.h.in"
53#     ]
54#     output = "$target_gen_dir/myfile.h"
55#     extra_args = [ "-e", "FOO=42" ]
56#   }
57template("process_version") {
58  assert(defined(invoker.output), "Output must be defined for $target_name")
59
60  process_only = defined(invoker.process_only) && invoker.process_only
61
62  if (process_only) {
63    action_name = target_name
64  } else {
65    action_name = target_name + "_action"
66    source_set_name = target_name
67  }
68
69  action(action_name) {
70    script = "//build/util/version.py"
71
72    sources = [ "//build/util/android_chrome_version.py" ]
73
74    inputs = [ lastchange_file ]
75    if (defined(invoker.inputs)) {
76      inputs += invoker.inputs
77    }
78    if (defined(invoker.template_file)) {
79      inputs += [ invoker.template_file ]
80    }
81
82    outputs = [ invoker.output ]
83
84    args = []
85
86    if (is_official_build) {
87      args += [ "--official" ]
88    }
89
90    if (defined(invoker.sources)) {
91      inputs += invoker.sources
92      foreach(i, invoker.sources) {
93        args += [
94          "-f",
95          rebase_path(i, root_build_dir),
96        ]
97      }
98    }
99
100    if (defined(invoker.executable) && invoker.executable) {
101      args += [ "-x" ]
102    }
103
104    if (defined(invoker.extra_args)) {
105      args += invoker.extra_args
106    }
107    args += [
108      "-o",
109      rebase_path(invoker.output, root_build_dir),
110    ]
111    if (defined(invoker.template_file)) {
112      args += [ rebase_path(invoker.template_file, root_build_dir) ]
113    }
114
115    forward_variables_from(invoker, [ "deps" ])
116
117    if (process_only) {
118      # When processing only, visibility gets applied to this target.
119      forward_variables_from(invoker, [ "visibility" ])
120    } else {
121      # When linking the result, only the source set can depend on the action.
122      visibility = [ ":$source_set_name" ]
123    }
124  }
125
126  if (!process_only) {
127    source_set(source_set_name) {
128      forward_variables_from(invoker,
129                             [
130                               "visibility",
131                               "deps",
132                             ])
133      sources = get_target_outputs(":$action_name")
134      public_deps = [ ":$action_name" ]
135    }
136  }
137}
138