xref: /aosp_15_r20/external/pigweed/pw_build/CMakeLists.txt (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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
15# IMPORTANT: The compilation flags in this file must be kept in sync with
16#            the GN flags //pw_build/BUILD.gn.
17
18include("$ENV{PW_ROOT}/pw_build/cc_blob_library.cmake")
19include("$ENV{PW_ROOT}/pw_build/pigweed.cmake")
20
21# Target that specifies the standard Pigweed build options.
22pw_add_library_generic(pw_build INTERFACE
23  PUBLIC_COMPILE_OPTIONS
24    -g
25    # Force the compiler use colorized output. This is required for Ninja.
26    $<$<CXX_COMPILER_ID:Clang>:-fcolor-diagnostics>
27    $<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
28  PUBLIC_DEPS
29    pw_build.reduced_size
30)
31if(ZEPHYR_PIGWEED_MODULE_DIR)
32  target_link_libraries(pw_build INTERFACE zephyr_interface)
33endif()
34
35# Declare top-level targets for tests.
36add_custom_target(pw_tests.default)
37add_custom_target(pw_run_tests.default)
38
39add_custom_target(pw_tests DEPENDS pw_tests.default)
40add_custom_target(pw_run_tests DEPENDS pw_run_tests.default)
41
42# Define the standard Pigweed compile options.
43pw_add_library_generic(pw_build.reduced_size INTERFACE
44  PUBLIC_COMPILE_OPTIONS
45    "-fno-common"
46    "-fno-exceptions"
47    "-ffunction-sections"
48    "-fdata-sections"
49    $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
50)
51
52# Define the standard Pigweed compile options.
53#
54# The pw_build.warnings library is used by upstream Pigweed targets to add
55# compiler warnings to the build.
56#
57# Toolchains may override these warnings by setting pw_build_WARNINGS:
58#
59#   set(pw_build_WARNINGS my_warnings CACHE STRING "" FORCE)
60#
61set(pw_build_WARNINGS pw_build.strict_warnings
62    CACHE STRING "Warnings libraries to use for Pigweed upstream code")
63
64pw_add_library_generic(pw_build.warnings INTERFACE
65  PUBLIC_DEPS
66    ${pw_build_WARNINGS}
67)
68
69# TODO(hepler): These Zephyr exceptions should be made by overriding
70#     pw_build_WARNINGS.
71if(ZEPHYR_PIGWEED_MODULE_DIR)
72  # -Wtype-limits is incompatible with Kconfig at times, disable it for Zephyr
73  # builds.
74  set(strict_warnings_cond "-Wno-type-limits")
75else()
76  # Only include these flags if we're not building with Zephyr.
77  set(strict_warnings_cond "-Wundef")
78endif()
79
80pw_add_library_generic(pw_build.strict_warnings INTERFACE
81  PUBLIC_COMPILE_OPTIONS
82    "-Wall"
83    "-Wextra"
84    "-Wimplicit-fallthrough"
85    ${strict_warnings_cond}
86    "-Wpointer-arith"
87
88    # Make all warnings errors, except for the exemptions below.
89    "-Werror"
90    "-Wno-error=cpp"  # preprocessor #warning statement
91    "-Wno-error=deprecated-declarations"  # [[deprecated]] attribute
92
93    $<$<COMPILE_LANGUAGE:CXX>:-Wnon-virtual-dtor>
94)
95
96if(NOT ZEPHYR_PIGWEED_MODULE_DIR)
97  # Only include these flags if we're not building with Zephyr.
98  set(extra_strict_warnings_cond "-Wredundant-decls")
99endif()
100
101pw_add_library_generic(pw_build.extra_strict_warnings INTERFACE
102  PUBLIC_COMPILE_OPTIONS
103    "-Wshadow"
104    ${extra_strict_warnings_cond}
105    $<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>
106)
107
108pw_add_library_generic(pw_build.pedantic_warnings INTERFACE
109  PUBLIC_COMPILE_OPTIONS
110    # Enable -Wpedantic, but disable a few warnings.
111    "-Wpedantic"
112
113    # Allow designated initializers, which were added in C++20 but widely
114    # supported prior and permitted by the Google style guide.
115    "-Wno-c++20-designator"
116
117    # Allow empty ... arguments in macros, which are permitted in C++20 but
118    # widely supported prior.
119    "-Wno-gnu-zero-variadic-macro-arguments"
120
121    # TODO: b/333712899 - Enable C23 extension warnings.
122    "-Wno-c23-extensions"
123
124    # TODO: b/335021928 - Enable C++ 20 extension warnings.
125    "-Wno-c++20-extensions"
126
127    # TODO: b/335328444 - Enable C++ 20 extension warnings.
128    "-Wno-deprecated-pragma"
129
130    # TODO: b/356904203 - Unblock Clang roll. Missing template argument
131    # warning introduced by Clang produces warnings in third party code.
132    # Remove this warning suppression after addressing the root cause.
133    "-Wno-missing-template-arg-list-after-template-kw"
134
135    # TODO: b/356689444 - Remove no-unknown-warning-option after Clang roll.
136    "-Wno-unknown-warning-option"
137)
138
139pw_add_library_generic(pw_build.cpp17 INTERFACE
140  PUBLIC_COMPILE_OPTIONS
141    $<$<COMPILE_LANGUAGE:CXX>:-std=c++17>
142    # Allow uses of the register keyword, which may appear in C headers.
143    $<$<COMPILE_LANGUAGE:CXX>:-Wno-register>
144)
145
146# Create an empty C++ source file and library for general use.
147file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/empty_file.cc" "")
148add_library(pw_build.empty OBJECT "${CMAKE_CURRENT_BINARY_DIR}/empty_file.cc" "")
149
150pw_add_test(pw_build.cc_blob_library_test
151  SOURCES
152    cc_blob_library_test.cc
153  PRIVATE_DEPS
154    pw_build.test_blob
155  GROUPS
156    modules
157    pw_build
158)
159
160pw_cc_blob_library(pw_build.test_blob
161  HEADER
162    pw_build/test_blob.h
163  NAMESPACE
164    test::ns
165  BLOB
166    SYMBOL_NAME kFirstBlob0123
167    PATH test_blob_0123.bin
168    ALIGNAS 512
169  BLOB
170    SYMBOL_NAME kSecondBlob0123
171    PATH test_blob_0123.bin
172)
173
174pw_add_library(pw_build.must_place INTERFACE
175  HEADERS
176    public/pw_build/must_place.ld.h
177  PUBLIC_INCLUDES
178    public
179)
180