xref: /aosp_15_r20/external/angle/build/buildflag_header.gni (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker# Copyright 2015 The Chromium Authors
2*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
3*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file.
4*8975f5c5SAndroid Build Coastguard Worker
5*8975f5c5SAndroid Build Coastguard Worker# Generates a header with preprocessor defines specified by the build file.
6*8975f5c5SAndroid Build Coastguard Worker#
7*8975f5c5SAndroid Build Coastguard Worker# The flags are converted to function-style defines with mangled names and
8*8975f5c5SAndroid Build Coastguard Worker# code uses an accessor macro to access the values. This is to try to
9*8975f5c5SAndroid Build Coastguard Worker# minimize bugs where code checks whether something is defined or not, and
10*8975f5c5SAndroid Build Coastguard Worker# the proper header isn't included, meaning the answer will always be silently
11*8975f5c5SAndroid Build Coastguard Worker# false or might vary across the code base.
12*8975f5c5SAndroid Build Coastguard Worker#
13*8975f5c5SAndroid Build Coastguard Worker# In the GN template, specify build flags in the template as a list
14*8975f5c5SAndroid Build Coastguard Worker# of strings that encode key/value pairs like this:
15*8975f5c5SAndroid Build Coastguard Worker#
16*8975f5c5SAndroid Build Coastguard Worker#   flags = [ "ENABLE_FOO=1", "ENABLE_BAR=$enable_bar" ]
17*8975f5c5SAndroid Build Coastguard Worker#
18*8975f5c5SAndroid Build Coastguard Worker# The GN values "true" and "false" will be mapped to 0 and 1 for boolean
19*8975f5c5SAndroid Build Coastguard Worker# #if flags to be expressed naturally. This means you can't directly make a
20*8975f5c5SAndroid Build Coastguard Worker# define that generates C++ value of true or false for use in code. If you
21*8975f5c5SAndroid Build Coastguard Worker# REALLY need this, you can also use the string "(true)" and "(false)" to
22*8975f5c5SAndroid Build Coastguard Worker# prevent the rewriting.
23*8975f5c5SAndroid Build Coastguard Worker
24*8975f5c5SAndroid Build Coastguard Worker# To check the value of the flag in C code:
25*8975f5c5SAndroid Build Coastguard Worker#
26*8975f5c5SAndroid Build Coastguard Worker#   #include "path/to/here/header_file.h"
27*8975f5c5SAndroid Build Coastguard Worker#
28*8975f5c5SAndroid Build Coastguard Worker#   #if BUILDFLAG(ENABLE_FOO)
29*8975f5c5SAndroid Build Coastguard Worker#   ...
30*8975f5c5SAndroid Build Coastguard Worker#   #endif
31*8975f5c5SAndroid Build Coastguard Worker#
32*8975f5c5SAndroid Build Coastguard Worker#   const char kSpamServerUrl[] = BUILDFLAG(SPAM_SERVER_URL);
33*8975f5c5SAndroid Build Coastguard Worker#
34*8975f5c5SAndroid Build Coastguard Worker# There will be no #define called ENABLE_FOO so if you accidentally test for
35*8975f5c5SAndroid Build Coastguard Worker# that in an ifdef it will always be negative.
36*8975f5c5SAndroid Build Coastguard Worker#
37*8975f5c5SAndroid Build Coastguard Worker#
38*8975f5c5SAndroid Build Coastguard Worker# Template parameters
39*8975f5c5SAndroid Build Coastguard Worker#
40*8975f5c5SAndroid Build Coastguard Worker#   flags [required, list of strings]
41*8975f5c5SAndroid Build Coastguard Worker#       Flag values as described above.
42*8975f5c5SAndroid Build Coastguard Worker#
43*8975f5c5SAndroid Build Coastguard Worker#   header [required, string]
44*8975f5c5SAndroid Build Coastguard Worker#       File name for generated header. By default, this will go in the
45*8975f5c5SAndroid Build Coastguard Worker#       generated file directory for this target, and you would include it
46*8975f5c5SAndroid Build Coastguard Worker#       with:
47*8975f5c5SAndroid Build Coastguard Worker#         #include "<path_to_this_BUILD_file>/<header>"
48*8975f5c5SAndroid Build Coastguard Worker#
49*8975f5c5SAndroid Build Coastguard Worker#   header_dir [optional, string]
50*8975f5c5SAndroid Build Coastguard Worker#       Override the default location of the generated header. The string will
51*8975f5c5SAndroid Build Coastguard Worker#       be treated as a subdirectory of the root_gen_dir. For example:
52*8975f5c5SAndroid Build Coastguard Worker#         header_dir = "foo/bar"
53*8975f5c5SAndroid Build Coastguard Worker#       Then you can include the header as:
54*8975f5c5SAndroid Build Coastguard Worker#         #include "foo/bar/baz.h"
55*8975f5c5SAndroid Build Coastguard Worker#
56*8975f5c5SAndroid Build Coastguard Worker#   deps, public_deps, testonly, visibility
57*8975f5c5SAndroid Build Coastguard Worker#       Normal meaning.
58*8975f5c5SAndroid Build Coastguard Worker#
59*8975f5c5SAndroid Build Coastguard Worker#
60*8975f5c5SAndroid Build Coastguard Worker# Grit defines
61*8975f5c5SAndroid Build Coastguard Worker#
62*8975f5c5SAndroid Build Coastguard Worker# If one .grd file uses a flag, just add to the grit target:
63*8975f5c5SAndroid Build Coastguard Worker#
64*8975f5c5SAndroid Build Coastguard Worker#   defines = [
65*8975f5c5SAndroid Build Coastguard Worker#     "enable_doom_melon=$enable_doom_melon",
66*8975f5c5SAndroid Build Coastguard Worker#   ]
67*8975f5c5SAndroid Build Coastguard Worker#
68*8975f5c5SAndroid Build Coastguard Worker# If multiple .grd files use it, you'll want to put the defines in a .gni file
69*8975f5c5SAndroid Build Coastguard Worker# so it can be shared. Generally this .gni file should include all grit defines
70*8975f5c5SAndroid Build Coastguard Worker# for a given module (for some definition of "module"). Then do:
71*8975f5c5SAndroid Build Coastguard Worker#
72*8975f5c5SAndroid Build Coastguard Worker#   defines = ui_grit_defines
73*8975f5c5SAndroid Build Coastguard Worker#
74*8975f5c5SAndroid Build Coastguard Worker# If you forget to do this, the flag will be implicitly false in the .grd file
75*8975f5c5SAndroid Build Coastguard Worker# and those resources won't be compiled. You'll know because the resource
76*8975f5c5SAndroid Build Coastguard Worker# #define won't be generated and any code that uses it won't compile. If you
77*8975f5c5SAndroid Build Coastguard Worker# see a missing IDS_* string, this is probably the reason.
78*8975f5c5SAndroid Build Coastguard Worker#
79*8975f5c5SAndroid Build Coastguard Worker#
80*8975f5c5SAndroid Build Coastguard Worker# Example
81*8975f5c5SAndroid Build Coastguard Worker#
82*8975f5c5SAndroid Build Coastguard Worker#   buildflag_header("foo_buildflags") {
83*8975f5c5SAndroid Build Coastguard Worker#     header = "foo_buildflags.h"
84*8975f5c5SAndroid Build Coastguard Worker#
85*8975f5c5SAndroid Build Coastguard Worker#     flags = [
86*8975f5c5SAndroid Build Coastguard Worker#       # This uses the GN build flag enable_doom_melon as the definition.
87*8975f5c5SAndroid Build Coastguard Worker#       "ENABLE_DOOM_MELON=$enable_doom_melon",
88*8975f5c5SAndroid Build Coastguard Worker#
89*8975f5c5SAndroid Build Coastguard Worker#       # This force-enables the flag.
90*8975f5c5SAndroid Build Coastguard Worker#       "ENABLE_SPACE_LASER=true",
91*8975f5c5SAndroid Build Coastguard Worker#
92*8975f5c5SAndroid Build Coastguard Worker#       # This will expand to the quoted C string when used in source code.
93*8975f5c5SAndroid Build Coastguard Worker#       "SPAM_SERVER_URL=\"http://www.example.com/\"",
94*8975f5c5SAndroid Build Coastguard Worker#     ]
95*8975f5c5SAndroid Build Coastguard Worker#   }
96*8975f5c5SAndroid Build Coastguard Workertemplate("buildflag_header") {
97*8975f5c5SAndroid Build Coastguard Worker  action(target_name) {
98*8975f5c5SAndroid Build Coastguard Worker    script = "//build/write_buildflag_header.py"
99*8975f5c5SAndroid Build Coastguard Worker
100*8975f5c5SAndroid Build Coastguard Worker    if (defined(invoker.header_dir)) {
101*8975f5c5SAndroid Build Coastguard Worker      header_file = "${invoker.header_dir}/${invoker.header}"
102*8975f5c5SAndroid Build Coastguard Worker    } else {
103*8975f5c5SAndroid Build Coastguard Worker      # Compute the path from the root to this file.
104*8975f5c5SAndroid Build Coastguard Worker      header_file = rebase_path(".", "//") + "/${invoker.header}"
105*8975f5c5SAndroid Build Coastguard Worker    }
106*8975f5c5SAndroid Build Coastguard Worker
107*8975f5c5SAndroid Build Coastguard Worker    outputs = [ "$root_gen_dir/$header_file" ]
108*8975f5c5SAndroid Build Coastguard Worker
109*8975f5c5SAndroid Build Coastguard Worker    # Always write --flags to the file so it's not empty. Empty will confuse GN
110*8975f5c5SAndroid Build Coastguard Worker    # into thinking the response file isn't used.
111*8975f5c5SAndroid Build Coastguard Worker    response_file_contents = [ "--flags" ]
112*8975f5c5SAndroid Build Coastguard Worker    if (defined(invoker.flags)) {
113*8975f5c5SAndroid Build Coastguard Worker      response_file_contents += invoker.flags
114*8975f5c5SAndroid Build Coastguard Worker    }
115*8975f5c5SAndroid Build Coastguard Worker
116*8975f5c5SAndroid Build Coastguard Worker    args = [
117*8975f5c5SAndroid Build Coastguard Worker      "--output",
118*8975f5c5SAndroid Build Coastguard Worker      header_file,  # Not rebased, Python script puts it inside gen-dir.
119*8975f5c5SAndroid Build Coastguard Worker      "--rulename",
120*8975f5c5SAndroid Build Coastguard Worker      get_label_info(":$target_name", "label_no_toolchain"),
121*8975f5c5SAndroid Build Coastguard Worker      "--gen-dir",
122*8975f5c5SAndroid Build Coastguard Worker      rebase_path(root_gen_dir, root_build_dir),
123*8975f5c5SAndroid Build Coastguard Worker      "--definitions",
124*8975f5c5SAndroid Build Coastguard Worker      "{{response_file_name}}",
125*8975f5c5SAndroid Build Coastguard Worker    ]
126*8975f5c5SAndroid Build Coastguard Worker
127*8975f5c5SAndroid Build Coastguard Worker    forward_variables_from(invoker,
128*8975f5c5SAndroid Build Coastguard Worker                           [
129*8975f5c5SAndroid Build Coastguard Worker                             "deps",
130*8975f5c5SAndroid Build Coastguard Worker                             "public_deps",
131*8975f5c5SAndroid Build Coastguard Worker                             "testonly",
132*8975f5c5SAndroid Build Coastguard Worker                             "visibility",
133*8975f5c5SAndroid Build Coastguard Worker                           ])
134*8975f5c5SAndroid Build Coastguard Worker
135*8975f5c5SAndroid Build Coastguard Worker    public_deps = [ "//build:buildflag_header_h" ]
136*8975f5c5SAndroid Build Coastguard Worker  }
137*8975f5c5SAndroid Build Coastguard Worker}
138