• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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/pi_pico.gni")
16import("//build_overrides/pigweed.gni")
17
18import("$dir_pw_arduino_build/arduino.gni")
19import("$dir_pw_build/host_tool.gni")
20import("$dir_pw_build/python.gni")
21import("$dir_pw_docgen/docs.gni")
22import("$dir_pw_perf_test/perf_test.gni")
23import("$dir_pw_rpc/config.gni")
24import("$dir_pw_rust/rust.gni")
25import("$dir_pw_third_party/mcuxpresso/mcuxpresso.gni")
26import("$dir_pw_toolchain/c_optimization.gni")
27import("$dir_pw_toolchain/generate_toolchain.gni")
28import("$dir_pw_toolchain/non_c_toolchain.gni")
29import("$dir_pw_unit_test/test.gni")
30
31# Main build file for upstream Pigweed.
32
33declare_args() {
34  # The default C++ optimization level for building upstream Pigweed targets.
35  #
36  # Must be one of "debug", "size_optimized", or "speed_optimized".
37  pw_DEFAULT_C_OPTIMIZATION_LEVEL = "debug"
38
39  # The C++ optimization levels for which to generate targets.
40  #
41  # Supported levels are "debug", "size_optimized", or "speed_optimized".
42  pw_C_OPTIMIZATION_LEVELS = [
43    "debug",
44    "size_optimized",
45  ]
46
47  # List of application image GN targets specific to the Pigweed target.
48  pw_TARGET_APPLICATIONS = []
49}
50
51# This toolchain is used to force some dependencies to not be parsed by the
52# default toolchain. This is desirable because the default toolchain generates
53# build steps for all parsed targets, not just desired dependencies.
54if (current_toolchain == default_toolchain) {
55  pw_non_c_toolchain("non_default_toolchain") {
56  }
57}
58
59# List any optimization levels in pw_C_OPTIMIZATION_LEVELS that are not in
60# pw_toolchain_SUPPORTED_C_OPTIMIZATION_LEVELS. This is accomplished by adding
61# all supported levels to the selected levels, then removing all supported
62# levels. The remaining list contains only the unsupported levels.
63_unknown_optimization_levels =
64    pw_C_OPTIMIZATION_LEVELS + pw_toolchain_SUPPORTED_C_OPTIMIZATION_LEVELS -
65    pw_toolchain_SUPPORTED_C_OPTIMIZATION_LEVELS
66
67assert(_unknown_optimization_levels == [],
68       "pw_C_OPTIMIZATION_LEVELS includes unsupported optimization levels: " +
69           "$_unknown_optimization_levels")
70
71# Assert that the selected pw_DEFAULT_C_OPTIMIZATION_LEVEL is in the
72# pw_toolchain_SUPPORTED_C_OPTIMIZATION_LEVELS list. This is done by adding then
73# removing all instances of the selected levels from the supported levels list.
74# If the resulting list did not change, then no supported levels were removed,
75# indicating that pw_DEFAULT_C_OPTIMIZATION_LEVEL is not a supported value.
76assert(pw_toolchain_SUPPORTED_C_OPTIMIZATION_LEVELS +
77           [ pw_DEFAULT_C_OPTIMIZATION_LEVEL ] -
78           [ pw_DEFAULT_C_OPTIMIZATION_LEVEL ] !=
79           pw_toolchain_SUPPORTED_C_OPTIMIZATION_LEVELS,
80       "pw_DEFAULT_C_OPTIMIZATION_LEVEL (\"$pw_DEFAULT_C_OPTIMIZATION_LEVEL" +
81           "\") must be one of the supported values: " +
82           "$pw_toolchain_SUPPORTED_C_OPTIMIZATION_LEVELS")
83
84# Enumerate all of the different targets that upstream Pigweed will build by
85# default. Downstream projects should not depend on this target; this target is
86# exclusively to facilitate easy upstream development and testing.
87group("default") {
88  deps = [
89    ":docs",
90    ":host",
91    ":pi_pico",
92    ":python.lint",
93    ":python.tests",
94    ":static_analysis",
95    ":stm32f429i",
96    ":warn_if_modules_out_of_date",
97  ]
98}
99
100# Verify that this BUILD.gn file is only used by Pigweed itself.
101assert(get_path_info("//", "abspath") == get_path_info(".", "abspath"),
102       "Pigweed's top-level BUILD.gn may only be used when building upstream " +
103           "Pigweed. To pull all Pigweed code into your build, import " +
104           "\$dir_pigweed/modules.gni and create a top-level pw_test_group " +
105           "that depends on the tests in pw_module_tests. See " +
106           "https://pigweed.dev/build_system.html for details.")
107
108_update_or_check_modules_lists = {
109  script = "$dir_pw_build/py/pw_build/generate_modules_lists.py"
110  args = [
111    rebase_path(".", root_build_dir),
112    rebase_path("PIGWEED_MODULES", root_build_dir),
113    rebase_path("$dir_pw_build/generated_pigweed_modules_lists.gni",
114                root_build_dir),
115  ]
116  inputs = [
117    "$dir_pw_build/generated_pigweed_modules_lists.gni",
118    "PIGWEED_MODULES",
119  ]
120}
121
122# There are races if the module check and module file update are run at the same
123# time. Force them to be serialized to prevent races. As long as the generated
124# module file is up to date, they'll pass.
125pool("module_check_pool") {
126  depth = 1
127}
128
129# Warns if PIGWEED_MODULES is not up-to-date and sorted.
130action("warn_if_modules_out_of_date") {
131  forward_variables_from(_update_or_check_modules_lists, "*")
132  outputs = [ "$target_gen_dir/$target_name.passed" ]
133  args += [
134            "--mode=WARN",
135            "--stamp",
136          ] + rebase_path(outputs, root_build_dir)
137  pool = ":module_check_pool"
138}
139
140# Fails if PIGWEED_MODULES is not up-to-date and sorted.
141action("check_modules") {
142  forward_variables_from(_update_or_check_modules_lists, "*")
143  outputs = [ "$target_gen_dir/$target_name.ALWAYS_RERUN" ]  # Never created
144  args += [ "--mode=CHECK" ]
145  pool = ":module_check_pool"
146}
147
148# Run this command after adding an item to PIGWEED_MODULES to update the
149# generated .gni with Pigweed modules lists.
150action("update_modules") {
151  forward_variables_from(_update_or_check_modules_lists, "*")
152  outputs = [ "$target_gen_dir/$target_name.ALWAYS_RERUN" ]  # Never created
153  args += [ "--mode=UPDATE" ]
154  pool = ":module_check_pool"
155}
156
157group("pw_system_demo") {
158  deps = [ "$dir_pw_system:system_examples(:non_default_toolchain)" ]
159}
160
161group("pi_pico") {
162  if (PICO_SRC_DIR != "") {
163    deps = [ ":pw_module_tests(targets/rp2040)" ]
164  }
165}
166
167_internal_toolchains = "$dir_pigweed/targets/host/pigweed_internal"
168
169# This template generates a group that builds pigweed_default with a particular
170# toolchain.
171template("_build_pigweed_default_at_all_optimization_levels") {
172  _toolchain_prefix = invoker.toolchain_prefix
173
174  group(target_name) {
175    deps = [
176      ":pigweed_default(${_toolchain_prefix}$pw_DEFAULT_C_OPTIMIZATION_LEVEL)",
177    ]
178  }
179
180  foreach(optimization, pw_C_OPTIMIZATION_LEVELS) {
181    group(target_name + "_$optimization") {
182      deps = [ ":pigweed_default($_toolchain_prefix$optimization)" ]
183    }
184  }
185}
186
187# Select a default toolchain based on host OS.
188if (host_os == "linux") {
189  _default_toolchain_prefix = "$_internal_toolchains:pw_strict_host_clang_"
190} else if (host_os == "mac") {
191  _default_toolchain_prefix = "$_internal_toolchains:pw_strict_host_clang_"
192} else if (host_os == "win") {
193  _default_toolchain_prefix = "$_internal_toolchains:pw_strict_host_gcc_"
194} else {
195  assert(false, "Please define a host config for your system: $host_os")
196}
197
198# Below are a list of GN targets you can build to force Pigweed to build for a
199# specific Pigweed target.
200_build_pigweed_default_at_all_optimization_levels("host") {
201  toolchain_prefix = _default_toolchain_prefix
202}
203
204_build_pigweed_default_at_all_optimization_levels("host_clang") {
205  toolchain_prefix = "$_internal_toolchains:pw_strict_host_clang_"
206}
207
208# GCC is only supported for Windows. Pigweed doesn't yet provide a Windows
209# clang toolchain, and Pigweed does not provide gcc toolchains for macOS and
210# Linux.
211if (host_os == "win") {
212  _build_pigweed_default_at_all_optimization_levels("host_gcc") {
213    toolchain_prefix = "$_internal_toolchains:pw_strict_host_gcc_"
214  }
215}
216
217if (pw_third_party_mcuxpresso_SDK != "") {
218  _build_pigweed_default_at_all_optimization_levels("mimxrt595") {
219    toolchain_prefix = "$dir_pigweed/targets/mimxrt595_evk:mimxrt595_evk_"
220  }
221}
222
223_build_pigweed_default_at_all_optimization_levels("stm32f429i") {
224  toolchain_prefix = "$dir_pigweed/targets/stm32f429i_disc1:stm32f429i_disc1_"
225}
226
227if (pw_arduino_build_CORE_PATH != "") {
228  _build_pigweed_default_at_all_optimization_levels("arduino") {
229    toolchain_prefix = "$dir_pigweed/targets/arduino:arduino_"
230  }
231}
232
233_build_pigweed_default_at_all_optimization_levels("qemu_gcc") {
234  toolchain_prefix =
235      "$dir_pigweed/targets/lm3s6965evb_qemu:lm3s6965evb_qemu_gcc_"
236}
237
238# TODO(b/244604080): Inline string tests are too big to fit in the QEMU firmware
239# except under a size-optimized build. For now, only build size-optimized.
240#
241# _build_pigweed_default_at_all_optimization_levels("qemu_clang") {
242#   toolchain_prefix =
243#       "$dir_pigweed/targets/lm3s6965evb_qemu:lm3s6965evb_qemu_clang_"
244# }
245group("qemu_clang_size_optimized") {
246  deps = [ ":pigweed_default($dir_pigweed/targets/lm3s6965evb_qemu:lm3s6965evb_qemu_clang_size_optimized)" ]
247}
248group("qemu_clang") {
249  deps = [ ":qemu_clang_size_optimized" ]
250}
251
252# Run clang-tidy on pigweed_default with pw_strict_host_clang_debug toolchain options.
253# Make sure to invoke gn clean out when any relevant .clang-tidy
254# file is updated.
255group("static_analysis") {
256  # Static analysis is only supported on Linux and macOS using clang-tidy.
257  if (host_os != "win") {
258    _toolchain = "$_internal_toolchains:pw_strict_host_clang_debug"
259    deps = [ ":pigweed_default($_toolchain.static_analysis)" ]
260  }
261}
262
263group("docs") {
264  deps = [ "$dir_pigweed/docs($dir_pigweed/targets/docs)" ]
265}
266
267# Tests that are run as host actions, such as tests of the build system.
268#
269# These are distinguished from `integration_tests` in that they are short
270# unit tests of specific functionality that should be tested in the default
271# build.
272group("action_tests") {
273  _default_tc = _default_toolchain_prefix + pw_DEFAULT_C_OPTIMIZATION_LEVEL
274  deps = [ "$dir_pw_unit_test:test_group_metadata_test.action($_default_tc)" ]
275}
276
277# Tests larger than unit tests, typically run in a specific configuration.
278group("integration_tests") {
279  _default_tc = _default_toolchain_prefix + pw_DEFAULT_C_OPTIMIZATION_LEVEL
280  deps = [
281    "$dir_pw_cli/py:process_integration_test.action($_default_tc)",
282    "$dir_pw_rpc:cpp_client_server_integration_test($_default_tc)",
283    "$dir_pw_rpc/py:python_client_cpp_server_test.action($_default_tc)",
284    "$dir_pw_unit_test/py:rpc_service_test.action($_default_tc)",
285  ]
286}
287
288# Build-only target for fuzzers.
289group("fuzzers") {
290  deps = []
291
292  # TODO(b/274437709): The client_fuzzer encounters build errors on macos. Limit
293  # it to Linux hosts for now.
294  if (host_os == "linux") {
295    _default_tc = _default_toolchain_prefix + pw_DEFAULT_C_OPTIMIZATION_LEVEL
296    deps += [ "$dir_pw_rpc/fuzz:client_fuzzer($_default_tc)" ]
297  }
298
299  if (host_os != "win") {
300    # Coverage-guided fuzzing is only supported on Linux and MacOS using clang.
301    deps += [
302      "$dir_pw_bluetooth_hci:fuzzers($dir_pigweed/targets/host:host_clang_fuzz)",
303      "$dir_pw_fuzzer:fuzzers($dir_pigweed/targets/host:host_clang_fuzz)",
304      "$dir_pw_protobuf:fuzzers($dir_pigweed/targets/host:host_clang_fuzz)",
305      "$dir_pw_random:fuzzers($dir_pigweed/targets/host:host_clang_fuzz)",
306      "$dir_pw_tokenizer:fuzzers($dir_pigweed/targets/host:host_clang_fuzz)",
307    ]
308  }
309}
310
311group("asan") {
312  if (host_os != "win") {
313    deps = [ ":pw_module_tests.run($dir_pigweed/targets/host:host_clang_asan)" ]
314  }
315}
316
317# TODO(b/234876100): msan will not work until the C++ standard library included
318# in the sysroot has a variant built with msan.
319group("msan") {
320  # TODO(b/259695498): msan doesn't work on macOS yet.
321  if (host_os != "win" && host_os != "mac" && host_os != "linux") {
322    deps = [ ":pw_module_tests.run($dir_pigweed/targets/host:host_clang_msan)" ]
323  }
324}
325
326group("tsan") {
327  if (host_os != "win") {
328    deps = [ ":pw_module_tests.run($dir_pigweed/targets/host:host_clang_tsan)" ]
329  }
330}
331
332group("ubsan") {
333  # TODO(b/259695498): ubsan doesn't work on macOS yet.
334  if (host_os != "win" && host_os != "mac") {
335    deps =
336        [ ":pw_module_tests.run($dir_pigweed/targets/host:host_clang_ubsan)" ]
337  }
338}
339
340group("ubsan_heuristic") {
341  # TODO(b/259695498): ubsan_heuristic doesn't work on macOS yet.
342  if (host_os != "win" && host_os != "mac") {
343    deps = [ ":pw_module_tests.run($dir_pigweed/targets/host:host_clang_ubsan_heuristic)" ]
344  }
345}
346
347group("runtime_sanitizers") {
348  if (host_os != "win") {
349    deps = [
350      ":asan",
351      ":tsan",
352      ":ubsan",
353
354      # TODO(b/234876100): msan will not work until the C++ standard library
355      # included in the sysroot has a variant built with msan.
356      # ":msan",
357
358      # No ubsan_heuristic, which may have false positives.
359    ]
360  }
361}
362
363pw_python_group("python") {
364  python_deps = [
365    "$dir_pw_env_setup:python($pw_build_PYTHON_TOOLCHAIN)",
366    "$dir_pw_env_setup:target_support_packages($pw_build_PYTHON_TOOLCHAIN)",
367  ]
368}
369
370group("pigweed_pypi_distribution") {
371  deps = [ "$dir_pw_env_setup:pypi_pigweed_python_source_tree($pw_build_PYTHON_TOOLCHAIN)" ]
372}
373
374# Build host-only tooling.
375group("host_tools") {
376  deps = [
377    "$dir_pw_target_runner/go:simple_client(:non_default_toolchain)",
378    "$dir_pw_target_runner/go:simple_server(:non_default_toolchain)",
379  ]
380
381  if (pw_rust_ENABLE_EXPERIMENTAL_BUILD) {
382    deps += [ "$dir_pw_rust/examples/host_executable:hello($dir_pigweed/targets/host:host_clang_debug)" ]
383  }
384}
385
386# By default, Pigweed will build this target when invoking ninja.
387group("pigweed_default") {
388  deps = []
389
390  # Prevent the default toolchain from parsing any other BUILD.gn files.
391  if (current_toolchain != default_toolchain) {
392    deps = [ ":apps" ]
393    if (pw_unit_test_AUTOMATIC_RUNNER == "") {
394      # Without a test runner defined, build the tests but don't run them.
395      deps += [ ":pw_module_tests" ]
396    } else {
397      # With a test runner, depend on the run targets so they run with the
398      # build.
399      deps += [ ":pw_module_tests.run" ]
400    }
401
402    # Add performance tests to the automatic build
403    deps += [ ":pw_perf_tests" ]
404
405    # Add action tests to the automatic build
406    deps += [ ":action_tests" ]
407
408    # Trace examples currently only support running on non-windows host
409    if (defined(pw_toolchain_SCOPE.is_host_toolchain) &&
410        pw_toolchain_SCOPE.is_host_toolchain && host_os != "win") {
411      deps += [
412        "$dir_pw_trace:trace_example_basic",
413        "$dir_pw_trace_tokenized:trace_tokenized_example_basic",
414        "$dir_pw_trace_tokenized:trace_tokenized_example_filter",
415        "$dir_pw_trace_tokenized:trace_tokenized_example_rpc",
416        "$dir_pw_trace_tokenized:trace_tokenized_example_trigger",
417      ]
418    }
419  }
420}
421
422group("cpp14_compatibility") {
423  _cpp14_toolchain = "$_internal_toolchains:pw_strict_host_clang_debug_cpp14"
424  deps = [
425    ":cpp14_modules($_cpp14_toolchain)",
426    ":cpp14_tests.run($_cpp14_toolchain)",
427  ]
428}
429
430# Build Pigweed with -std=c++20 to ensure compatibility. Compile with
431# optimizations since the compiler tends to catch more errors with optimizations
432# enabled than without.
433group("cpp20_compatibility") {
434  _cpp20_tc = "$_internal_toolchains:pw_strict_host_clang_size_optimized_cpp20"
435  deps = [ ":pigweed_default($_cpp20_tc)" ]
436}
437
438group("build_with_pw_minimal_cpp_stdlib") {
439  _toolchain = "$_internal_toolchains:pw_strict_host_clang_size_optimized_minimal_cpp_stdlib"
440
441  # This list of supported modules is incomplete.
442  deps = [
443    "$dir_pw_status($_toolchain)",
444    "$dir_pw_tokenizer($_toolchain)",
445  ]
446}
447
448# The default toolchain is not used for compiling C/C++ code.
449if (current_toolchain != default_toolchain) {
450  group("apps") {
451    # Application images built for all targets.
452    deps = [ "$dir_pw_hdlc/rpc_example" ]
453
454    # Add target-specific images.
455    deps += pw_TARGET_APPLICATIONS
456
457    # Add host-only targets to the build.
458    if (defined(pw_toolchain_SCOPE.is_host_toolchain) &&
459        pw_toolchain_SCOPE.is_host_toolchain) {
460      deps += [ "$dir_pw_tool" ]
461
462      # TODO(b/240982565): Build integration tests on Windows and macOS when
463      #     SocketStream supports those platforms.
464      if (host_os == "linux") {
465        # Build the integration test binaries, but don't run them by default.
466        deps += [
467          "$dir_pw_rpc:client_integration_test",
468          "$dir_pw_rpc:test_rpc_server",
469          "$dir_pw_unit_test:test_rpc_server",
470        ]
471      }
472    }
473  }
474
475  # All Pigweed modules that can be built using gn. Not built by default.
476  group("pw_modules") {
477    deps = pw_modules
478  }
479
480  # Targets for all module unit test groups.
481  pw_test_group("pw_module_tests") {
482    group_deps = pw_module_tests
483  }
484
485  group("pw_perf_tests") {
486    deps = [
487      "$dir_pw_checksum:perf_tests",
488      "$dir_pw_perf_test:perf_test_tests_test",
489      "$dir_pw_protobuf:perf_tests",
490    ]
491  }
492
493  # Modules that support C++14.
494  # TODO(hepler): pw_kvs is supposed to compile as C++14, but does not.
495  group("cpp14_modules") {
496    public_deps = [
497      dir_pw_polyfill,
498      dir_pw_preprocessor,
499      dir_pw_tokenizer,
500      dir_pw_varint,
501    ]
502  }
503
504  # Tests that support C++14.
505  pw_test_group("cpp14_tests") {
506    group_deps = [
507      "$dir_pw_polyfill:tests",
508      "$dir_pw_span:tests",
509    ]
510    tests = [
511      "$dir_pw_tokenizer:simple_tokenize_test",
512      "$dir_pw_containers:to_array_test",
513      "$dir_pw_string:string_test",
514    ]
515  }
516}
517