• 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
15# IMPORTANT: The compilation flags in this file must be kept in sync with
16#            the GN flags //pw_build/BUILD.gn.
17
18# Target that specifies the standard Pigweed build options.
19add_library(pw_build INTERFACE)
20target_compile_options(pw_build INTERFACE "-g")
21target_link_libraries(pw_build
22  INTERFACE
23    pw_build.reduced_size
24    pw_build.cpp17
25)
26target_compile_options(pw_build
27  INTERFACE
28    # Force the compiler use colorized output. This is required for Ninja.
29    $<$<CXX_COMPILER_ID:Clang>:-fcolor-diagnostics>
30    $<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
31)
32if(ZEPHYR_PIGWEED_MODULE_DIR)
33  target_link_libraries(pw_build INTERFACE zephyr_interface)
34endif()
35
36# Declare top-level targets for tests.
37add_custom_target(pw_tests.default)
38add_custom_target(pw_run_tests.default)
39
40add_custom_target(pw_tests DEPENDS pw_tests.default)
41add_custom_target(pw_run_tests DEPENDS pw_run_tests.default)
42
43# Define the standard Pigweed compile options.
44add_library(pw_build.reduced_size INTERFACE)
45target_compile_options(pw_build.reduced_size
46  INTERFACE
47    "-fno-common"
48    "-fno-exceptions"
49    "-ffunction-sections"
50    "-fdata-sections"
51    $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
52)
53
54# Define the standard Pigweed compile options.
55#
56# The pw_build.warnings library is used by upstream Pigweed targets to add
57# compiler warnings to the build.
58#
59# Toolchains may override these warnings by setting pw_build_WARNINGS:
60#
61#   set(pw_build_WARNINGS my_warnings CACHE STRING "" FORCE)
62#
63set(pw_build_WARNINGS pw_build.strict_warnings
64    CACHE STRING "Warnings libraries to use for Pigweed upstream code")
65
66add_library(pw_build.warnings INTERFACE)
67target_link_libraries(pw_build.warnings INTERFACE ${pw_build_WARNINGS})
68
69# TODO(hepler): These Zephyr exceptions should be made by overriding
70#     pw_build_WARNINGS.
71add_library(pw_build.strict_warnings INTERFACE)
72if(NOT ZEPHYR_PIGWEED_MODULE_DIR)
73  # Only include these flags if we're not building with Zephyr.
74  set(strict_warnings_cond "-Wcast-qual" "-Wundef")
75endif()
76target_compile_options(pw_build.strict_warnings
77  INTERFACE
78    "-Wall"
79    "-Wextra"
80    "-Wimplicit-fallthrough"
81    ${strict_warnings_cond}
82    "-Wpointer-arith"
83
84    # Make all warnings errors, except for the exemptions below.
85    "-Werror"
86    "-Wno-error=cpp"  # preprocessor #warning statement
87    "-Wno-error=deprecated-declarations"  # [[deprecated]] attribute
88
89    $<$<COMPILE_LANGUAGE:CXX>:-Wnon-virtual-dtor>
90)
91
92add_library(pw_build.extra_strict_warnings INTERFACE)
93if(NOT ZEPHYR_PIGWEED_MODULE_DIR)
94  # Only include these flags if we're not building with Zephyr.
95  set(extra_strict_warnings_cond "-Wredundant-decls")
96endif()
97target_compile_options(pw_build.extra_strict_warnings
98  INTERFACE
99    "-Wshadow"
100    ${extra_strict_warnings_cond}
101    $<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>
102)
103
104add_library(pw_build.cpp17 INTERFACE)
105target_compile_options(pw_build.cpp17
106  INTERFACE
107    $<$<COMPILE_LANGUAGE:CXX>:-std=c++17>
108    # Allow uses of the register keyword, which may appear in C headers.
109    $<$<COMPILE_LANGUAGE:CXX>:-Wno-register>
110)
111
112# Create an empty source file and library for general use.
113file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/empty_file.c" "")
114add_library(pw_build.empty OBJECT "${CMAKE_CURRENT_BINARY_DIR}/empty_file.c" "")
115