Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / build / chip / buildconfig_header.gni
1 # Copyright (c) 2020 Project CHIP Authors
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Copyright 2015 The Chromium Authors. All rights reserved.
16 #
17 # Redistribution and use in source and binary forms, with or without
18 # modification, are permitted provided that the following conditions are
19 # met:
20 #
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
26 # distribution.
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.
30 #
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.
42
43 # Generates a header with preprocessor defines specified by the build file.
44 #
45 # In the GN template, specify build defines in the template as a list
46 # of strings that encode key/value pairs like this:
47 #
48 #   defines = [ "ENABLE_FOO=1", "ENABLE_BAR=$enable_bar" ]
49 #
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.
55
56 # To check the value of the define in C code:
57 #
58 #   #include "path/to/here/header_file.h"
59 #
60 #   #if ENABLE_FOO
61 #   ...
62 #   #endif
63 #
64 #   const char kSpamServerUrl[] = SPAM_SERVER_URL;
65 #
66 # Template parameters
67 #
68 #   defines [required, list of strings]
69 #       Defines as described above.
70 #
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
74 #       with:
75 #         #include "<path_to_this_BUILD_file>/<header>"
76 #
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"
83 #
84 #   deps, public_deps, testonly, visibility
85 #       Normal meaning.
86 #
87 # Example
88 #
89 #   buildconfig_header("foo_buildconfig") {
90 #     header = "foo_buildconfig.h"
91 #
92 #     defines = [
93 #       # This uses the GN build argument enable_doom_melon as the definition.
94 #       "ENABLE_DOOM_MELON=$enable_doom_melon",
95 #
96 #       # This force-enables the define
97 #       "ENABLE_SPACE_LASER=true",
98 #
99 #       # This will expand to the quoted C string when used in source code.
100 #       "SPAM_SERVER_URL=\"http://www.example.com/\"",
101 #     ]
102 #   }
103
104 import("//build_overrides/build.gni")
105 import("//build_overrides/chip.gni")
106
107 template("buildconfig_header") {
108   source_set_name = target_name
109   gen_target_name = "gen_${target_name}"
110
111   action(gen_target_name) {
112     script = "${chip_root}/build/chip/write_buildconfig_header.py"
113
114     if (defined(invoker.header_dir)) {
115       header_file = "${invoker.header_dir}/${invoker.header}"
116     } else {
117       # Compute the path from the root to this file.
118       header_file = rebase_path(".", "//") + "/${invoker.header}"
119     }
120
121     include_dir = "${root_gen_dir}/include"
122
123     outputs = [ "${include_dir}/${header_file}" ]
124
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
130     }
131
132     args = [
133       "--output",
134       header_file,  # Not rebased, Python script puts it inside gen-dir.
135       "--rulename",
136       get_label_info(":$target_name", "label_no_toolchain"),
137       "--gen-dir",
138       rebase_path(include_dir, root_build_dir),
139       "--definitions",
140       "{{response_file_name}}",
141     ]
142
143     visibility = [ ":${source_set_name}" ]
144   }
145
146   source_set(source_set_name) {
147     sources = get_target_outputs(":${gen_target_name}")
148
149     deps = [ ":${gen_target_name}" ]
150
151     forward_variables_from(invoker,
152                            [
153                              "deps",
154                              "public_deps",
155                              "testonly",
156                              "visibility",
157                            ])
158   }
159 }