1 """A function to compile C/C++ code, like cc_library() but from Starlark."""
3 load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
5 def upb_use_cpp_toolchain():
6 return use_cpp_toolchain()
8 def cc_library_func(ctx, name, hdrs, srcs, copts, dep_ccinfos, includes = []):
9 """Like cc_library(), but callable from rules.
13 name: Unique name used to generate output files.
14 hdrs: Public headers that can be #included from other rules.
15 srcs: C/C++ source files.
16 copts: Additional options for cc compilation.
17 dep_ccinfos: CcInfo providers of dependencies we should build/link against.
18 includes: Additional include paths.
21 CcInfo provider for this compilation.
24 compilation_contexts = [info.compilation_context for info in dep_ccinfos]
25 linking_contexts = [info.linking_context for info in dep_ccinfos]
26 toolchain = find_cpp_toolchain(ctx)
27 feature_configuration = cc_common.configure_features(
29 cc_toolchain = toolchain,
30 requested_features = ctx.features,
31 unsupported_features = ctx.disabled_features,
34 (compilation_context, compilation_outputs) = cc_common.compile(
35 actions = ctx.actions,
36 feature_configuration = feature_configuration,
37 cc_toolchain = toolchain,
42 user_compile_flags = copts,
43 compilation_contexts = compilation_contexts,
46 # buildifier: disable=unused-variable
47 (linking_context, linking_outputs) = cc_common.create_linking_context_from_compilation_outputs(
48 actions = ctx.actions,
50 feature_configuration = feature_configuration,
51 cc_toolchain = toolchain,
52 compilation_outputs = compilation_outputs,
53 linking_contexts = linking_contexts,
54 disallow_dynamic_library = cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "targets_windows") or not cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "supports_dynamic_linker"),
58 compilation_context = compilation_context,
59 linking_context = linking_context,