1 # Copyright 2019 The Pigweed Authors
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
7 # https://www.apache.org/licenses/LICENSE-2.0
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
15 """Pigweed build environment for bazel."""
21 # Standard compiler flags to reduce output binary size.
22 REDUCED_SIZE_COPTS = [
25 "-ffunction-sections",
29 STRICT_WARNINGS_COPTS = [
32 # Make all warnings errors, except for the exemptions below.
34 "-Wno-error=cpp", # preprocessor #warning statement
35 "-Wno-error=deprecated-declarations", # [[deprecated]] attribute
42 # Allow uses of the register keyword, which may appear in C headers.
46 DISABLE_PENDING_WORKAROUND_COPTS = [
47 "-Wno-private-header",
53 STRICT_WARNINGS_COPTS +
54 DISABLE_PENDING_WORKAROUND_COPTS
58 "-Wno-unknown-warning-option",
61 PW_DEFAULT_LINKOPTS = []
63 def _add_defaults(kwargs):
64 """Adds default arguments suitable for both C and C++ code to kwargs."""
66 copts = kwargs.get("copts", []) + PW_DEFAULT_COPTS
67 kwargs["copts"] = select({
68 "//pw_build:kythe": copts + KYTHE_COPTS,
69 "//conditions:default": copts,
71 kwargs["linkopts"] = kwargs.get("linkopts", []) + PW_DEFAULT_LINKOPTS
73 # Set linkstatic to avoid building .so files.
74 kwargs["linkstatic"] = True
76 kwargs.setdefault("features", [])
78 # Crosstool--adding this line to features disables header modules, which
79 # don't work with -fno-rtti. Note: this is not a command-line argument,
80 # it's "minus use_header_modules".
81 kwargs["features"].append("-use_header_modules")
83 def _default_cc_and_c_kwargs(kwargs):
85 kwargs.setdefault("srcs", [])
87 cc = dict(kwargs.items())
88 cc["srcs"] = [src for src in kwargs["srcs"] if not src.endswith(".c")]
89 cc["copts"] = cc["copts"] + CPP17_COPTS
91 c_srcs = [src for src in kwargs["srcs"] if src.endswith(".c")]
94 c = dict(kwargs.items())
96 c["srcs"] = c_srcs + [src for src in kwargs["srcs"] if src.endswith(".h")]
98 cc["deps"] = cc.get("deps", []) + [":" + c["name"]]
103 def _add_cc_and_c_targets(target, kwargs):
104 cc_kwargs, c_kwargs = _default_cc_and_c_kwargs(kwargs)
107 native.cc_library(**c_kwargs)
111 def pw_cc_binary(**kwargs):
112 _add_cc_and_c_targets(native.cc_binary, kwargs)
114 def pw_cc_library(**kwargs):
115 _add_cc_and_c_targets(native.cc_library, kwargs)
117 def pw_cc_test(**kwargs):
118 kwargs["deps"] = kwargs.get("deps", []) + ["//pw_unit_test:main"]
119 _add_cc_and_c_targets(native.cc_test, kwargs)