xref: /aosp_15_r20/external/google-fruit/configuration/bazel/build_defs.bzl (revision a65addddcf69f38db5b288d787b6b7571a57bb8f)
1*a65addddSAndroid Build Coastguard Workerload("@rules_cc//cc:action_names.bzl", "C_COMPILE_ACTION_NAME")
2*a65addddSAndroid Build Coastguard Workerload("@rules_cc//cc:toolchain_utils.bzl", "find_cpp_toolchain")
3*a65addddSAndroid Build Coastguard Worker
4*a65addddSAndroid Build Coastguard Workerdef _generate_fruit_config_impl(ctx):
5*a65addddSAndroid Build Coastguard Worker    cc_toolchain = find_cpp_toolchain(ctx)
6*a65addddSAndroid Build Coastguard Worker
7*a65addddSAndroid Build Coastguard Worker    feature_configuration = cc_common.configure_features(
8*a65addddSAndroid Build Coastguard Worker        ctx = ctx,
9*a65addddSAndroid Build Coastguard Worker        cc_toolchain = cc_toolchain,
10*a65addddSAndroid Build Coastguard Worker        requested_features = ctx.features,
11*a65addddSAndroid Build Coastguard Worker        unsupported_features = ctx.disabled_features,
12*a65addddSAndroid Build Coastguard Worker    )
13*a65addddSAndroid Build Coastguard Worker    c_compiler_path = cc_common.get_tool_for_action(
14*a65addddSAndroid Build Coastguard Worker        feature_configuration = feature_configuration,
15*a65addddSAndroid Build Coastguard Worker        action_name = C_COMPILE_ACTION_NAME,
16*a65addddSAndroid Build Coastguard Worker    )
17*a65addddSAndroid Build Coastguard Worker
18*a65addddSAndroid Build Coastguard Worker    check_output_files = []
19*a65addddSAndroid Build Coastguard Worker    for check_source in ctx.files.check_sources:
20*a65addddSAndroid Build Coastguard Worker        check_name = check_source.path[:-len(".cpp")].split('/')[-1].split('\\')[-1]
21*a65addddSAndroid Build Coastguard Worker
22*a65addddSAndroid Build Coastguard Worker        output_file = ctx.actions.declare_file(check_name + ".o")
23*a65addddSAndroid Build Coastguard Worker
24*a65addddSAndroid Build Coastguard Worker        c_compile_variables = cc_common.create_compile_variables(
25*a65addddSAndroid Build Coastguard Worker            feature_configuration = feature_configuration,
26*a65addddSAndroid Build Coastguard Worker            cc_toolchain = cc_toolchain,
27*a65addddSAndroid Build Coastguard Worker            user_compile_flags = ctx.fragments.cpp.copts + ctx.fragments.cpp.conlyopts,
28*a65addddSAndroid Build Coastguard Worker            source_file = check_source.path,
29*a65addddSAndroid Build Coastguard Worker            output_file = output_file.path,
30*a65addddSAndroid Build Coastguard Worker        )
31*a65addddSAndroid Build Coastguard Worker        command_line = cc_common.get_memory_inefficient_command_line(
32*a65addddSAndroid Build Coastguard Worker            feature_configuration = feature_configuration,
33*a65addddSAndroid Build Coastguard Worker            action_name = C_COMPILE_ACTION_NAME,
34*a65addddSAndroid Build Coastguard Worker            variables = c_compile_variables,
35*a65addddSAndroid Build Coastguard Worker        )
36*a65addddSAndroid Build Coastguard Worker        env = cc_common.get_environment_variables(
37*a65addddSAndroid Build Coastguard Worker            feature_configuration = feature_configuration,
38*a65addddSAndroid Build Coastguard Worker            action_name = C_COMPILE_ACTION_NAME,
39*a65addddSAndroid Build Coastguard Worker            variables = c_compile_variables,
40*a65addddSAndroid Build Coastguard Worker        )
41*a65addddSAndroid Build Coastguard Worker
42*a65addddSAndroid Build Coastguard Worker        check_define = 'FRUIT_HAS_%s' % check_name.upper()
43*a65addddSAndroid Build Coastguard Worker        check_output_file = ctx.actions.declare_file(check_name + ".h")
44*a65addddSAndroid Build Coastguard Worker
45*a65addddSAndroid Build Coastguard Worker        ctx.actions.run_shell(
46*a65addddSAndroid Build Coastguard Worker            command = '"$@" &>/dev/null && echo "#define %s 1" >"%s" || echo "#define %s 0" >"%s"; touch "%s"' % (
47*a65addddSAndroid Build Coastguard Worker                check_define, check_output_file.path, check_define, check_output_file.path, output_file.path
48*a65addddSAndroid Build Coastguard Worker            ),
49*a65addddSAndroid Build Coastguard Worker            arguments = [c_compiler_path] + command_line,
50*a65addddSAndroid Build Coastguard Worker            env = env,
51*a65addddSAndroid Build Coastguard Worker            inputs = depset(
52*a65addddSAndroid Build Coastguard Worker                [check_source],
53*a65addddSAndroid Build Coastguard Worker                transitive = [cc_toolchain.all_files],
54*a65addddSAndroid Build Coastguard Worker            ),
55*a65addddSAndroid Build Coastguard Worker            outputs = [output_file, check_output_file],
56*a65addddSAndroid Build Coastguard Worker        )
57*a65addddSAndroid Build Coastguard Worker        check_output_files.append(check_output_file)
58*a65addddSAndroid Build Coastguard Worker
59*a65addddSAndroid Build Coastguard Worker    merged_output_file = ctx.actions.declare_file("fruit/impl/fruit-config-base.h")
60*a65addddSAndroid Build Coastguard Worker    ctx.actions.run_shell(
61*a65addddSAndroid Build Coastguard Worker        command = '\n'.join([
62*a65addddSAndroid Build Coastguard Worker            '(',
63*a65addddSAndroid Build Coastguard Worker            'echo "#ifndef FRUIT_CONFIG_BASE_H"',
64*a65addddSAndroid Build Coastguard Worker            'echo "#define FRUIT_CONFIG_BASE_H"',
65*a65addddSAndroid Build Coastguard Worker            'echo "#define FRUIT_USES_BOOST 1"',
66*a65addddSAndroid Build Coastguard Worker            'cat %s' % ' '.join([check_output_file.path for check_output_file in check_output_files]),
67*a65addddSAndroid Build Coastguard Worker            'echo "#endif"',
68*a65addddSAndroid Build Coastguard Worker            ')>%s' % merged_output_file.path
69*a65addddSAndroid Build Coastguard Worker        ]),
70*a65addddSAndroid Build Coastguard Worker        inputs = check_output_files,
71*a65addddSAndroid Build Coastguard Worker        outputs = [merged_output_file],
72*a65addddSAndroid Build Coastguard Worker    )
73*a65addddSAndroid Build Coastguard Worker
74*a65addddSAndroid Build Coastguard Worker    compilation_context, compilation_outputs = cc_common.compile(
75*a65addddSAndroid Build Coastguard Worker        actions = ctx.actions,
76*a65addddSAndroid Build Coastguard Worker        feature_configuration = feature_configuration,
77*a65addddSAndroid Build Coastguard Worker        cc_toolchain = cc_toolchain,
78*a65addddSAndroid Build Coastguard Worker        public_hdrs = [merged_output_file],
79*a65addddSAndroid Build Coastguard Worker        name = "%s_link" % ctx.label.name,
80*a65addddSAndroid Build Coastguard Worker    )
81*a65addddSAndroid Build Coastguard Worker
82*a65addddSAndroid Build Coastguard Worker    linking_context, linking_outputs = cc_common.create_linking_context_from_compilation_outputs(
83*a65addddSAndroid Build Coastguard Worker        actions = ctx.actions,
84*a65addddSAndroid Build Coastguard Worker        feature_configuration = feature_configuration,
85*a65addddSAndroid Build Coastguard Worker        compilation_outputs = compilation_outputs,
86*a65addddSAndroid Build Coastguard Worker        cc_toolchain = cc_toolchain,
87*a65addddSAndroid Build Coastguard Worker        name = "%s_link" % ctx.label.name,
88*a65addddSAndroid Build Coastguard Worker    )
89*a65addddSAndroid Build Coastguard Worker
90*a65addddSAndroid Build Coastguard Worker    return [
91*a65addddSAndroid Build Coastguard Worker        DefaultInfo(files = depset([merged_output_file]), runfiles = ctx.runfiles(files = [merged_output_file])),
92*a65addddSAndroid Build Coastguard Worker        CcInfo(compilation_context=compilation_context, linking_context=linking_context),
93*a65addddSAndroid Build Coastguard Worker    ]
94*a65addddSAndroid Build Coastguard Worker
95*a65addddSAndroid Build Coastguard Workergenerate_fruit_config = rule(
96*a65addddSAndroid Build Coastguard Worker    implementation = _generate_fruit_config_impl,
97*a65addddSAndroid Build Coastguard Worker    attrs = {
98*a65addddSAndroid Build Coastguard Worker        "check_sources": attr.label_list(allow_files = True),
99*a65addddSAndroid Build Coastguard Worker        "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
100*a65addddSAndroid Build Coastguard Worker    },
101*a65addddSAndroid Build Coastguard Worker    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
102*a65addddSAndroid Build Coastguard Worker    fragments = ["cpp"],
103*a65addddSAndroid Build Coastguard Worker)