xref: /aosp_15_r20/external/pigweed/third_party/stm32cube/BUILD.gn (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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/linker_script.gni")
18import("$dir_pw_build/target_types.gni")
19import("$dir_pw_third_party/stm32cube/stm32cube.gni")
20
21if (dir_pw_third_party_stm32cube == "") {
22  group("linker_script_template") {
23  }
24  group("core_init_template") {
25  }
26  group("cmsis_init_template") {
27  }
28  group("hal_config_template") {
29  }
30  group("stm32cube_headers") {
31  }
32  group("stm32cube") {
33  }
34} else {
35  stm32cube_builder_script =
36      "$dir_pw_stm32cube_build/py/pw_stm32cube_build/__main__.py"
37
38  rebased_dir_pw_third_party_stm32cube =
39      rebase_path(dir_pw_third_party_stm32cube)
40
41  find_files_args = [
42    "find_files",
43    rebased_dir_pw_third_party_stm32cube,
44    pw_third_party_stm32cube_PRODUCT,
45  ]
46  if (pw_third_party_stm32cube_CORE_INIT ==
47      "$dir_pw_third_party/stm32cube:core_init_template") {
48    find_files_args += [ "--init" ]
49  }
50
51  # This script finds the files relavent for the current product.
52  files = exec_script(stm32cube_builder_script,
53                      find_files_args,
54                      "scope",
55                      [ "$rebased_dir_pw_third_party_stm32cube/files.txt" ])
56
57  if (pw_third_party_stm32cube_CORE_INIT ==
58      "$dir_pw_third_party/stm32cube:core_init_template") {
59    assert(files.gcc_linker != "" || files.iar_linker != "",
60           "No linker file found")
61
62    gcc_linker = files.gcc_linker
63    if (gcc_linker == "") {
64      gcc_linker = "$target_gen_dir/linker.ld"
65      gcc_linker_str = exec_script(stm32cube_builder_script,
66                                   [
67                                     "icf_to_ld",
68                                     files.iar_linker,
69                                   ],
70                                   "string",
71                                   [ files.iar_linker ])
72      write_file(gcc_linker, gcc_linker_str)
73    }
74
75    startup_file = "$target_gen_dir/startup.s"
76    startup_file_str = exec_script(stm32cube_builder_script,
77                                   [
78                                     "inject_init",
79                                     files.startup,
80                                   ],
81                                   "string",
82                                   [ files.startup ])
83    write_file(startup_file, startup_file_str)
84
85    pw_linker_script("linker_script_template") {
86      linker_script = gcc_linker
87    }
88
89    pw_source_set("core_init_template") {
90      deps = [ ":linker_script_template" ]
91      sources = [ startup_file ]
92    }
93  }
94
95  pw_source_set("hal_timebase_template") {
96    deps = [ ":stm32cube_headers" ]
97    sources = [ "$dir_pw_third_party_stm32cube/hal_driver/Src/${files.family}_hal_timebase_tim_template.c" ]
98  }
99
100  pw_source_set("cmsis_init_template") {
101    deps = [ ":stm32cube_headers" ]
102    sources = [ "$dir_pw_third_party_stm32cube/cmsis_device/Source/Templates/system_${files.family}.c" ]
103  }
104
105  # Generate a stub config header that points to the correct template.
106  write_file("$target_gen_dir/template_config/${files.family}_hal_conf.h",
107             "#include \"${files.family}_hal_conf_template.h\"")
108  config("hal_config_template_includes") {
109    include_dirs = [ "$target_gen_dir/template_config" ]
110  }
111  pw_source_set("hal_config_template") {
112    public_configs = [ ":hal_config_template_includes" ]
113
114    # This is to make sure GN properly detects changes to these files. The
115    # generated file shouldn't change, but the file it redirects to might.
116    public = [ "$target_gen_dir/template_config/${files.family}_hal_conf.h" ]
117    inputs = [ "$dir_pw_third_party_stm32cube/hal_driver/Inc/${files.family}_hal_conf_template.h" ]
118  }
119
120  config("header_flags") {
121    cflags = [ "-Wno-unused-parameter" ]
122    cflags_c = [
123      "-Wno-redundant-decls",
124      "-Wno-sign-compare",
125      "-Wno-undef",
126      "-Wno-implicit-function-declaration",
127      "-Wno-switch-enum",
128    ]
129
130    # TODO: b/301262374 - Provide a better way to detect the compiler type.
131    if (get_path_info(pw_toolchain_SCOPE.cc, "file") == "clang") {
132      cflags += [ "-Wno-deprecated-volatile" ]
133      cflags_c += [ "-Wno-parentheses-equality" ]
134    } else {
135      cflags_c += [
136        "-Wno-old-style-declaration",
137        "-Wno-maybe-uninitialized",
138      ]
139      cflags_cc = [ "-Wno-volatile" ]
140    }
141
142    defines = [
143      "USE_HAL_DRIVER",
144      files.product_define,
145      "STM32CUBE_HEADER=\"${files.family}.h\"",
146      "__ARMCC_VERSION=0",  # workaround for bug at stm32l552xx.h:1303
147    ]
148    visibility = [ ":*" ]
149  }
150
151  config("sources_flags") {
152    if (get_path_info(pw_toolchain_SCOPE.cc, "file") == "clang") {
153      cflags_c = [ "-Wno-unused-but-set-variable" ]
154    }
155
156    visibility = [ ":*" ]
157  }
158
159  config("public_include_paths") {
160    include_dirs = files.include_dirs
161    include_dirs += [ "public" ]
162    visibility = [ ":*" ]
163  }
164
165  # Only libraries that implement parts of the stm32cube hal should depend on
166  # this. If you just want to depend on the hal, depend on stm32cube directly.
167  pw_source_set("stm32cube_headers") {
168    public_configs = [
169      ":header_flags",
170      ":public_include_paths",
171    ]
172    public = [
173      "public/stm32cube/init.h",
174      "public/stm32cube/stm32cube.h",
175    ]
176    public += files.headers
177    public_deps = [ pw_third_party_stm32cube_CONFIG ]
178    visibility = [ ":*" ]
179    if (pw_third_party_stm32cube_CORE_INIT != "") {
180      visibility += [ pw_third_party_stm32cube_CORE_INIT ]
181    }
182  }
183
184  pw_source_set("stm32cube") {
185    configs = [ ":sources_flags" ]
186    public_deps = [ ":stm32cube_headers" ]
187    sources = files.sources
188    deps = [
189      pw_third_party_stm32cube_CMSIS_INIT,
190      pw_third_party_stm32cube_TIMEBASE,
191    ]
192    if (pw_third_party_stm32cube_CORE_INIT != "") {
193      deps += [ pw_third_party_stm32cube_CORE_INIT ]
194    }
195  }
196}
197