1 # Copyright (c) 2020 Project CHIP Authors
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Copyright 2015 The Chromium Authors. All rights reserved.
17 # Redistribution and use in source and binary forms, with or without
18 # modification, are permitted provided that the following conditions are
21 # * Redistributions of source code must retain the above copyright
22 # notice, this list of conditions and the following disclaimer.
23 # * Redistributions in binary form must reproduce the above
24 # copyright notice, this list of conditions and the following disclaimer
25 # in the documentation and/or other materials provided with the
27 # * Neither the name of Google Inc. nor the names of its
28 # contributors may be used to endorse or promote products derived from
29 # this software without specific prior written permission.
31 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 # Generates a header with preprocessor defines specified by the build file.
45 # In the GN template, specify build defines in the template as a list
46 # of strings that encode key/value pairs like this:
48 # defines = [ "ENABLE_FOO=1", "ENABLE_BAR=$enable_bar" ]
50 # The GN values "true" and "false" will be mapped to 0 and 1 for boolean
51 # #if defines to be expressed naturally. This means you can't directly make a
52 # define that generates C++ value of true or false for use in code. If you
53 # REALLY need this, you can also use the string "(true)" and "(false)" to
54 # prevent the rewriting.
56 # To check the value of the define in C code:
58 # #include "path/to/here/header_file.h"
64 # const char kSpamServerUrl[] = SPAM_SERVER_URL;
68 # defines [required, list of strings]
69 # Defines as described above.
71 # header [required, string]
72 # File name for generated header. By default, this will go in the
73 # generated file directory for this target, and you would include it
75 # #include "<path_to_this_BUILD_file>/<header>"
77 # header_dir [optional, string]
78 # Override the default location of the generated header. The string will
79 # be treated as a subdirectory of the root_gen_dir. For example:
80 # header_dir = "foo/bar"
81 # Then you can include the header as:
82 # #include "foo/bar/baz.h"
84 # deps, public_deps, testonly, visibility
89 # buildconfig_header("foo_buildconfig") {
90 # header = "foo_buildconfig.h"
93 # # This uses the GN build argument enable_doom_melon as the definition.
94 # "ENABLE_DOOM_MELON=$enable_doom_melon",
96 # # This force-enables the define
97 # "ENABLE_SPACE_LASER=true",
99 # # This will expand to the quoted C string when used in source code.
100 # "SPAM_SERVER_URL=\"http://www.example.com/\"",
104 import("//build_overrides/build.gni")
105 import("//build_overrides/chip.gni")
107 template("buildconfig_header") {
108 source_set_name = target_name
109 gen_target_name = "gen_${target_name}"
111 action(gen_target_name) {
112 script = "${chip_root}/build/chip/write_buildconfig_header.py"
114 if (defined(invoker.header_dir)) {
115 header_file = "${invoker.header_dir}/${invoker.header}"
117 # Compute the path from the root to this file.
118 header_file = rebase_path(".", "//") + "/${invoker.header}"
121 include_dir = "${root_gen_dir}/include"
123 outputs = [ "${include_dir}/${header_file}" ]
125 # Always write --defines to the file so it's not empty. Empty will confuse GN
126 # into thinking the response file isn't used.
127 response_file_contents = [ "--defines" ]
128 if (defined(invoker.defines)) {
129 response_file_contents += invoker.defines
134 header_file, # Not rebased, Python script puts it inside gen-dir.
136 get_label_info(":$target_name", "label_no_toolchain"),
138 rebase_path(include_dir, root_build_dir),
140 "{{response_file_name}}",
143 visibility = [ ":${source_set_name}" ]
146 source_set(source_set_name) {
147 sources = get_target_outputs(":${gen_target_name}")
149 deps = [ ":${gen_target_name}" ]
151 forward_variables_from(invoker,