blob: a179fbf5313cb1417697e535980de15b41639187 [file] [log] [blame]
jcater0377b0d2019-01-07 09:21:52 -08001# Copyright 2018 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Library of functions that provide the CC_FLAGS Make variable."""
15
16# This should match the logic in CcCommon.computeCcFlags:
17def build_cc_flags(ctx, cc_toolchain, action_name):
18 """Determine the value for CC_FLAGS based on the given toolchain."""
19
20 # Get default cc flags from toolchain's make_variables.
21 legacy_cc_flags = cc_common.legacy_cc_flags_make_variable_do_not_use(
22 cc_toolchain = cc_toolchain,
23 )
24
25 # Determine the sysroot flag.
26 sysroot_cc_flags = _from_sysroot(cc_toolchain)
27
28 # Flags from feature config.
29 feature_config_cc_flags = _from_features(ctx, cc_toolchain, action_name)
30
31 # Combine the different sources, but only add the sysroot flag if nothing
32 # else adds sysroot.
33 # If added, it must appear before the feature config flags.
34 cc_flags = []
35 if legacy_cc_flags:
36 cc_flags.append(legacy_cc_flags)
37 if sysroot_cc_flags and not _contains_sysroot(feature_config_cc_flags):
38 cc_flags.append(sysroot_cc_flags)
39 cc_flags.extend(feature_config_cc_flags)
40
41 return " ".join(cc_flags)
42
43def _contains_sysroot(flags):
44 for flag in flags:
45 if "--sysroot=" in flag:
46 return True
47 return False
48
49def _from_sysroot(cc_toolchain):
50 sysroot = cc_toolchain.sysroot
51 if sysroot:
52 return "--sysroot=%s" % sysroot
53 else:
54 return None
55
56def _from_features(ctx, cc_toolchain, action_name):
57 feature_configuration = cc_common.configure_features(
58 cc_toolchain = cc_toolchain,
59 requested_features = ctx.features,
60 unsupported_features = ctx.disabled_features,
61 )
62
63 variables = cc_common.empty_variables()
64
65 cc_flags = cc_common.get_memory_inefficient_command_line(
66 feature_configuration = feature_configuration,
67 action_name = action_name,
68 variables = variables,
69 )
70 return cc_flags