Rob Mohr | be98ead | 2019-11-14 07:16:40 -0800 | [diff] [blame] | 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 | |
Rob Mohr | af7bd88 | 2019-11-19 09:48:23 -0800 | [diff] [blame] | 15 | """Pigweed build environment for bazel.""" |
| 16 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 17 | # Standard compiler flags to reduce output binary size. |
| 18 | REDUCED_SIZE_COPTS = [ |
| 19 | "-fno-common", |
| 20 | "-fno-exceptions", |
| 21 | "-ffunction-sections", |
| 22 | "-fdata-sections", |
| 23 | "-fno-rtti", |
| 24 | ] |
Rob Mohr | be98ead | 2019-11-14 07:16:40 -0800 | [diff] [blame] | 25 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 26 | STRICT_WARNINGS_COPTS = [ |
| 27 | "-Wall", |
| 28 | "-Wextra", |
Rob Mohr | e6cbdf8 | 2019-11-18 14:37:48 -0800 | [diff] [blame] | 29 | "-Wnon-virtual-dtor", |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 30 | # Make all warnings errors, except for the exemptions below. |
| 31 | "-Werror", |
| 32 | "-Wno-error=cpp", # preprocessor #warning statement |
| 33 | "-Wno-error=deprecated-declarations", # [[deprecated]] attribute |
| 34 | ] |
Rob Mohr | be98ead | 2019-11-14 07:16:40 -0800 | [diff] [blame] | 35 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 36 | CPP17_COPTS = [ |
| 37 | "-std=c++17", |
| 38 | # Allow uses of the register keyword, which may appear in C headers. |
| 39 | "-Wno-register", |
| 40 | ] |
Rob Mohr | be98ead | 2019-11-14 07:16:40 -0800 | [diff] [blame] | 41 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 42 | INCLUDES_COPTS = [ |
Rob Mohr | ea9a66b | 2019-11-26 13:56:36 -0800 | [diff] [blame] | 43 | "-Ipw_bloat/public", |
Rob Mohr | 57211c2 | 2019-11-18 15:26:35 -0800 | [diff] [blame] | 44 | "-Ipw_dumb_io/public", |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 45 | "-Ipw_preprocessor/public", |
| 46 | "-Ipw_span/public", |
| 47 | "-Ipw_status/public", |
| 48 | "-Ipw_string/public", |
| 49 | "-Ipw_unit_test/public", |
Rob Mohr | 3895a12 | 2019-12-02 11:49:18 -0800 | [diff] [blame] | 50 | "-Ipw_varint/public", |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 51 | ] |
Rob Mohr | be98ead | 2019-11-14 07:16:40 -0800 | [diff] [blame] | 52 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 53 | PW_DEFAULT_COPTS = ( |
| 54 | REDUCED_SIZE_COPTS + |
| 55 | STRICT_WARNINGS_COPTS + |
| 56 | CPP17_COPTS + |
| 57 | INCLUDES_COPTS |
| 58 | ) |
Rob Mohr | be98ead | 2019-11-14 07:16:40 -0800 | [diff] [blame] | 59 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 60 | PW_DEFAULT_LINKOPTS = [] |
Rob Mohr | be98ead | 2019-11-14 07:16:40 -0800 | [diff] [blame] | 61 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 62 | def _add_defaults(kwargs): |
| 63 | kwargs.setdefault("copts", []) |
Rob Mohr | 05b0b11 | 2019-12-03 09:57:38 -0800 | [diff] [blame] | 64 | kwargs["copts"] = PW_DEFAULT_COPTS + kwargs["copts"] |
Rob Mohr | df2b13b | 2019-11-18 14:04:25 -0800 | [diff] [blame] | 65 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 66 | kwargs.setdefault("linkopts", []) |
Rob Mohr | 05b0b11 | 2019-12-03 09:57:38 -0800 | [diff] [blame] | 67 | kwargs["linkopts"] = PW_DEFAULT_LINKOPTS + kwargs["linkopts"] |
Rob Mohr | be98ead | 2019-11-14 07:16:40 -0800 | [diff] [blame] | 68 | |
Rob Mohr | df2b13b | 2019-11-18 14:04:25 -0800 | [diff] [blame] | 69 | kwargs.setdefault("features", []) |
| 70 | |
| 71 | # Crosstool--adding this line to features disables header modules, which |
| 72 | # don't work with -fno-rtti. Note: this is not a command-line argument, |
| 73 | # it's "minus use_header_modules". |
| 74 | kwargs["features"].append("-use_header_modules") |
| 75 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 76 | def pw_cc_binary(**kwargs): |
| 77 | _add_defaults(kwargs) |
| 78 | native.cc_binary(**kwargs) |
Rob Mohr | be98ead | 2019-11-14 07:16:40 -0800 | [diff] [blame] | 79 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 80 | def pw_cc_library(**kwargs): |
| 81 | _add_defaults(kwargs) |
| 82 | native.cc_library(**kwargs) |
Rob Mohr | be98ead | 2019-11-14 07:16:40 -0800 | [diff] [blame] | 83 | |
Rob Mohr | c70366e | 2019-11-18 11:09:39 -0800 | [diff] [blame] | 84 | def pw_cc_test(**kwargs): |
| 85 | _add_defaults(kwargs) |
| 86 | kwargs.setdefault("deps", []) |
| 87 | kwargs["deps"].append("//pw_unit_test:main") |
| 88 | native.cc_test(**kwargs) |