• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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"""Pigweed build environment for bazel."""
16
17DEBUGGING = [
18    "-g",
19]
20
21# Standard compiler flags to reduce output binary size.
22REDUCED_SIZE_COPTS = [
23    "-fno-common",
24    "-fno-exceptions",
25    "-ffunction-sections",
26    "-fdata-sections",
27]
28
29STRICT_WARNINGS_COPTS = [
30    "-Wall",
31    "-Wextra",
32    # Make all warnings errors, except for the exemptions below.
33    "-Werror",
34    "-Wno-error=cpp",  # preprocessor #warning statement
35    "-Wno-error=deprecated-declarations",  # [[deprecated]] attribute
36]
37
38CPP17_COPTS = [
39    "-std=c++17",
40    "-fno-rtti",
41    "-Wnon-virtual-dtor",
42    # Allow uses of the register keyword, which may appear in C headers.
43    "-Wno-register",
44]
45
46DISABLE_PENDING_WORKAROUND_COPTS = [
47    "-Wno-private-header",
48]
49
50PW_DEFAULT_COPTS = (
51    DEBUGGING +
52    REDUCED_SIZE_COPTS +
53    STRICT_WARNINGS_COPTS +
54    DISABLE_PENDING_WORKAROUND_COPTS
55)
56
57KYTHE_COPTS = [
58    "-Wno-unknown-warning-option",
59]
60
61PW_DEFAULT_LINKOPTS = []
62
63def _add_defaults(kwargs):
64    """Adds default arguments suitable for both C and C++ code to kwargs."""
65
66    copts = kwargs.get("copts", []) + PW_DEFAULT_COPTS
67    kwargs["copts"] = select({
68        "//pw_build:kythe": copts + KYTHE_COPTS,
69        "//conditions:default": copts,
70    })
71    kwargs["linkopts"] = kwargs.get("linkopts", []) + PW_DEFAULT_LINKOPTS
72
73    # Set linkstatic to avoid building .so files.
74    kwargs["linkstatic"] = True
75
76    kwargs.setdefault("features", [])
77
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")
82
83def _default_cc_and_c_kwargs(kwargs):
84    _add_defaults(kwargs)
85    kwargs.setdefault("srcs", [])
86
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
90
91    c_srcs = [src for src in kwargs["srcs"] if src.endswith(".c")]
92
93    if c_srcs:
94        c = dict(kwargs.items())
95        c["name"] += "_c"
96        c["srcs"] = c_srcs + [src for src in kwargs["srcs"] if src.endswith(".h")]
97
98        cc["deps"] = cc.get("deps", []) + [":" + c["name"]]
99        return cc, c
100
101    return cc, None
102
103def _add_cc_and_c_targets(target, kwargs):
104    cc_kwargs, c_kwargs = _default_cc_and_c_kwargs(kwargs)
105
106    if c_kwargs:
107        native.cc_library(**c_kwargs)
108
109    target(**cc_kwargs)
110
111def pw_cc_binary(**kwargs):
112    _add_cc_and_c_targets(native.cc_binary, kwargs)
113
114def pw_cc_library(**kwargs):
115    _add_cc_and_c_targets(native.cc_library, kwargs)
116
117def pw_cc_test(**kwargs):
118    kwargs["deps"] = kwargs.get("deps", []) + ["//pw_unit_test:main"]
119    _add_cc_and_c_targets(native.cc_test, kwargs)
120