• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_build/cc_library.gni")
18
19# Note: In general, prefer to import target_types.gni rather than this file.
20# cc_executable.gni and cc_library.gni are both provided by target_types.gni.
21#
22# cc_library.gni is split out from cc_executable.gni because pw_executable
23# templates may need to create pw_source_set targets internally, and can't
24# import target_types.gni because it creates a circular import path.
25
26declare_args() {
27  # The name of the GN target type used to build Pigweed executables.
28  #
29  # If this is a custom template, the .gni file containing the template must
30  # be imported at the top of the target configuration file to make it globally
31  # available.
32  pw_build_EXECUTABLE_TARGET_TYPE = "executable"
33
34  # The path to the .gni file that defines pw_build_EXECUTABLE_TARGET_TYPE.
35  #
36  # If pw_build_EXECUTABLE_TARGET_TYPE is not the default of `executable`, this
37  # .gni file is imported to provide the template definition.
38  pw_build_EXECUTABLE_TARGET_TYPE_FILE = ""
39}
40
41if (pw_build_EXECUTABLE_TARGET_TYPE != "executable" &&
42    pw_build_EXECUTABLE_TARGET_TYPE_FILE != "") {
43  import(pw_build_EXECUTABLE_TARGET_TYPE_FILE)
44}
45
46_supported_toolchain_defaults = [
47  "configs",
48  "public_deps",
49]
50
51# Wrapper for Pigweed executable build targets which uses a globally-defined,
52# configurable target type.
53template("pw_executable") {
54  _pw_source_files = []
55
56  # Boilerplate for tracking target sources.  For more information see
57  # https://pigweed.dev/pw_build/#target-types
58  if (defined(invoker.sources)) {
59    foreach(path, invoker.sources) {
60      _pw_source_files += [ path ]
61    }
62  }
63  if (defined(invoker.public)) {
64    foreach(path, invoker.public) {
65      _pw_source_files += [ path ]
66    }
67  }
68
69  _executable_output_dir = "${target_out_dir}/bin"
70  if (defined(invoker.output_dir)) {
71    _executable_output_dir = invoker.output_dir
72  }
73
74  target(pw_build_EXECUTABLE_TARGET_TYPE, target_name) {
75    import("$dir_pw_build/defaults.gni")
76
77    forward_variables_from(invoker, "*", _supported_toolchain_defaults)
78
79    # Ensure that we don't overwrite metadata forwared from the invoker above.
80    if (defined(metadata)) {
81      metadata.pw_source_files = _pw_source_files
82    } else {
83      metadata = {
84        pw_source_files = _pw_source_files
85      }
86    }
87
88    if (!defined(configs)) {
89      configs = []
90    }
91    if (defined(pw_build_defaults.configs)) {
92      configs += pw_build_defaults.configs
93    }
94    if (defined(remove_configs)) {
95      if (remove_configs != [] && remove_configs[0] == "*") {
96        configs = []
97      } else {
98        configs += remove_configs  # Add configs in case they aren't already
99        configs -= remove_configs  # present, then remove them.
100      }
101    }
102    if (defined(invoker.configs)) {
103      configs += invoker.configs
104    }
105
106    public_deps = [ "$dir_pw_build:link_deps" ]
107    if (defined(pw_build_defaults.public_deps)) {
108      public_deps += pw_build_defaults.public_deps
109    }
110    if (defined(remove_public_deps)) {
111      if (remove_public_deps != [] && remove_public_deps[0] == "*") {
112        public_deps = []
113      } else {
114        public_deps += remove_public_deps
115        public_deps -= remove_public_deps
116      }
117    }
118    if (defined(invoker.public_deps)) {
119      public_deps += invoker.public_deps
120    }
121
122    output_dir = _executable_output_dir
123  }
124}
125