• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 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    Args:
21      ctx: The rule context.
22      cc_toolchain: CcToolchainInfo instance.
23      action_name: Name of the action.
24    Returns:
25      string containing flags separated by a space.
26    """
27
28    # Get default cc flags from toolchain's make_variables.
29    legacy_cc_flags = cc_common.legacy_cc_flags_make_variable_do_not_use(
30        cc_toolchain = cc_toolchain,
31    )
32
33    # Determine the sysroot flag.
34    sysroot_cc_flags = _from_sysroot(cc_toolchain)
35
36    # Flags from feature config.
37    feature_config_cc_flags = _from_features(ctx, cc_toolchain, action_name)
38
39    # Combine the different sources, but only add the sysroot flag if nothing
40    # else adds sysroot.
41    # If added, it must appear before the feature config flags.
42    cc_flags = []
43    if legacy_cc_flags:
44        cc_flags.append(legacy_cc_flags)
45    if sysroot_cc_flags and not _contains_sysroot(feature_config_cc_flags):
46        cc_flags.append(sysroot_cc_flags)
47    cc_flags.extend(feature_config_cc_flags)
48
49    return " ".join(cc_flags)
50
51def _contains_sysroot(flags):
52    for flag in flags:
53        if "--sysroot=" in flag:
54            return True
55    return False
56
57def _from_sysroot(cc_toolchain):
58    sysroot = cc_toolchain.sysroot
59    if sysroot:
60        return "--sysroot=%s" % sysroot
61    else:
62        return None
63
64def _from_features(ctx, cc_toolchain, action_name):
65    feature_configuration = cc_common.configure_features(
66        ctx = ctx,
67        cc_toolchain = cc_toolchain,
68        requested_features = ctx.features,
69        unsupported_features = ctx.disabled_features,
70    )
71
72    variables = cc_common.empty_variables()
73
74    cc_flags = cc_common.get_memory_inefficient_command_line(
75        feature_configuration = feature_configuration,
76        action_name = action_name,
77        variables = variables,
78    )
79    return cc_flags
80