diff options
author | Haibo Huang <hhb@google.com> | 2020-09-22 17:50:42 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-09-22 17:50:42 +0000 |
commit | dc3aaf3620582ce933b6f23fe1b93584bc60fce0 (patch) | |
tree | 196bf451ec7a656bcbaaeffce735fe19e5c7b66f /configuration/bazel/build_defs.bzl | |
parent | 1b13f367e02e2e90a59a7e6597bf47f64a1cf105 (diff) | |
parent | efde60e7146303998a704e8604e90d27ca3f6241 (diff) | |
download | google-fruit-dc3aaf3620582ce933b6f23fe1b93584bc60fce0.tar.gz |
Upgrade google-fruit to 928458857f4b85a0016c2d724486343b4660cb46 am: 873eb760e8 am: efde60e714
Original change: https://android-review.googlesource.com/c/platform/external/google-fruit/+/1433213
Change-Id: I58ec27630c20751b61ffab4fb455ba9056dc22e5
Diffstat (limited to 'configuration/bazel/build_defs.bzl')
-rw-r--r-- | configuration/bazel/build_defs.bzl | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/configuration/bazel/build_defs.bzl b/configuration/bazel/build_defs.bzl new file mode 100644 index 0000000..28c3b17 --- /dev/null +++ b/configuration/bazel/build_defs.bzl @@ -0,0 +1,103 @@ +load("@rules_cc//cc:action_names.bzl", "C_COMPILE_ACTION_NAME") +load("@rules_cc//cc:toolchain_utils.bzl", "find_cpp_toolchain") + +def _generate_fruit_config_impl(ctx): + cc_toolchain = find_cpp_toolchain(ctx) + + feature_configuration = cc_common.configure_features( + ctx = ctx, + cc_toolchain = cc_toolchain, + requested_features = ctx.features, + unsupported_features = ctx.disabled_features, + ) + c_compiler_path = cc_common.get_tool_for_action( + feature_configuration = feature_configuration, + action_name = C_COMPILE_ACTION_NAME, + ) + + check_output_files = [] + for check_source in ctx.files.check_sources: + check_name = check_source.path[:-len(".cpp")].split('/')[-1].split('\\')[-1] + + output_file = ctx.actions.declare_file(check_name + ".o") + + c_compile_variables = cc_common.create_compile_variables( + feature_configuration = feature_configuration, + cc_toolchain = cc_toolchain, + user_compile_flags = ctx.fragments.cpp.copts + ctx.fragments.cpp.conlyopts, + source_file = check_source.path, + output_file = output_file.path, + ) + command_line = cc_common.get_memory_inefficient_command_line( + feature_configuration = feature_configuration, + action_name = C_COMPILE_ACTION_NAME, + variables = c_compile_variables, + ) + env = cc_common.get_environment_variables( + feature_configuration = feature_configuration, + action_name = C_COMPILE_ACTION_NAME, + variables = c_compile_variables, + ) + + check_define = 'FRUIT_HAS_%s' % check_name.upper() + check_output_file = ctx.actions.declare_file(check_name + ".h") + + ctx.actions.run_shell( + command = '"$@" &>/dev/null && echo "#define %s 1" >"%s" || echo "#define %s 0" >"%s"; touch "%s"' % ( + check_define, check_output_file.path, check_define, check_output_file.path, output_file.path + ), + arguments = [c_compiler_path] + command_line, + env = env, + inputs = depset( + [check_source], + transitive = [cc_toolchain.all_files], + ), + outputs = [output_file, check_output_file], + ) + check_output_files.append(check_output_file) + + merged_output_file = ctx.actions.declare_file("fruit/impl/fruit-config-base.h") + ctx.actions.run_shell( + command = '\n'.join([ + '(', + 'echo "#ifndef FRUIT_CONFIG_BASE_H"', + 'echo "#define FRUIT_CONFIG_BASE_H"', + 'echo "#define FRUIT_USES_BOOST 1"', + 'cat %s' % ' '.join([check_output_file.path for check_output_file in check_output_files]), + 'echo "#endif"', + ')>%s' % merged_output_file.path + ]), + inputs = check_output_files, + outputs = [merged_output_file], + ) + + compilation_context, compilation_outputs = cc_common.compile( + actions = ctx.actions, + feature_configuration = feature_configuration, + cc_toolchain = cc_toolchain, + public_hdrs = [merged_output_file], + name = "%s_link" % ctx.label.name, + ) + + linking_context, linking_outputs = cc_common.create_linking_context_from_compilation_outputs( + actions = ctx.actions, + feature_configuration = feature_configuration, + compilation_outputs = compilation_outputs, + cc_toolchain = cc_toolchain, + name = "%s_link" % ctx.label.name, + ) + + return [ + DefaultInfo(files = depset([merged_output_file]), runfiles = ctx.runfiles(files = [merged_output_file])), + CcInfo(compilation_context=compilation_context, linking_context=linking_context), + ] + +generate_fruit_config = rule( + implementation = _generate_fruit_config_impl, + attrs = { + "check_sources": attr.label_list(allow_files = True), + "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), + }, + toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], + fragments = ["cpp"], +)
\ No newline at end of file |