diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index f055208..bf47e2b 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -1,4 +1,7 @@ --- +buildifier: + version: latest + warnings: "all" platforms: ubuntu1604: run_targets: diff --git a/WORKSPACE b/WORKSPACE index 365eb83..87a57de 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -4,25 +4,29 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "bazel_federation", - url = "https://github.com/bazelbuild/bazel-federation/archive/f0e5eda7f0cbfe67f126ef4dacb18c89039b0506.zip", # 2019-09-30 sha256 = "33222ab7bcc430f1ff1db8788c2e0118b749319dd572476c4fd02322d7d15792", strip_prefix = "bazel-federation-f0e5eda7f0cbfe67f126ef4dacb18c89039b0506", type = "zip", + url = "https://github.com/bazelbuild/bazel-federation/archive/f0e5eda7f0cbfe67f126ef4dacb18c89039b0506.zip", # 2019-09-30 ) load("@bazel_federation//:repositories.bzl", "rules_cc_deps") + rules_cc_deps() load("@bazel_federation//setup:rules_cc.bzl", "rules_cc_setup") + rules_cc_setup() # # Dependencies for development of rules_cc itself. # load("//:internal_deps.bzl", "rules_cc_internal_deps") + rules_cc_internal_deps() load("//:internal_setup.bzl", "rules_cc_internal_setup") + rules_cc_internal_setup() http_archive( @@ -34,3 +38,19 @@ http_archive( "https://github.com/google/googletest/archive/release-1.10.0.tar.gz", ], ) + +http_archive( + name = "rules_proto", + sha256 = "602e7161d9195e50246177e7c55b2f39950a9cf7366f74ed5f22fd45750cd208", + strip_prefix = "rules_proto-97d8af4dc474595af3900dd85cb3a29ad28cc313", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/97d8af4dc474595af3900dd85cb3a29ad28cc313.tar.gz", + "https://github.com/bazelbuild/rules_proto/archive/97d8af4dc474595af3900dd85cb3a29ad28cc313.tar.gz", + ], +) + +load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains") + +rules_proto_dependencies() + +rules_proto_toolchains() diff --git a/cc/BUILD b/cc/BUILD index 21ee541..06b253f 100644 --- a/cc/BUILD +++ b/cc/BUILD @@ -55,3 +55,9 @@ filegroup( "//distro:__pkg__", ], ) + +cc_toolchain_alias(name = "current_cc_toolchain") + +cc_host_toolchain_alias(name = "current_cc_host_toolchain") + +cc_libc_top_alias(name = "current_libc_top") diff --git a/cc/private/toolchain/cc_toolchain_config_lib.bzl b/cc/cc_toolchain_config_lib.bzl similarity index 100% rename from cc/private/toolchain/cc_toolchain_config_lib.bzl rename to cc/cc_toolchain_config_lib.bzl diff --git a/cc/defs.bzl b/cc/defs.bzl index 8de01bd..a768a5c 100644 --- a/cc/defs.bzl +++ b/cc/defs.bzl @@ -14,8 +14,8 @@ """Starlark rules for building C++ projects.""" -load("@rules_cc//cc/private/rules_impl:cc_flags_supplier.bzl", _cc_flags_supplier = "cc_flags_supplier") -load("@rules_cc//cc/private/rules_impl:compiler_flag.bzl", _compiler_flag = "compiler_flag") +load("//cc/private/rules_impl:cc_flags_supplier.bzl", _cc_flags_supplier = "cc_flags_supplier") +load("//cc/private/rules_impl:compiler_flag.bzl", _compiler_flag = "compiler_flag") _MIGRATION_TAG = "__CC_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__" @@ -34,6 +34,8 @@ def cc_binary(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.cc_binary(**_add_tags(attrs)) def cc_test(**attrs): @@ -44,6 +46,8 @@ def cc_test(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.cc_test(**_add_tags(attrs)) def cc_library(**attrs): @@ -54,6 +58,8 @@ def cc_library(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.cc_library(**_add_tags(attrs)) def cc_import(**attrs): @@ -64,6 +70,8 @@ def cc_import(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.cc_import(**_add_tags(attrs)) def cc_proto_library(**attrs): @@ -74,6 +82,8 @@ def cc_proto_library(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.cc_proto_library(**_add_tags(attrs)) def fdo_prefetch_hints(**attrs): @@ -84,6 +94,8 @@ def fdo_prefetch_hints(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.fdo_prefetch_hints(**_add_tags(attrs)) def fdo_profile(**attrs): @@ -94,6 +106,8 @@ def fdo_profile(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.fdo_profile(**_add_tags(attrs)) def cc_toolchain(**attrs): @@ -104,6 +118,8 @@ def cc_toolchain(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.cc_toolchain(**_add_tags(attrs)) def cc_toolchain_suite(**attrs): @@ -114,6 +130,8 @@ def cc_toolchain_suite(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.cc_toolchain_suite(**_add_tags(attrs)) def objc_library(**attrs): @@ -124,6 +142,8 @@ def objc_library(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.objc_library(**_add_tags(attrs)) def objc_import(**attrs): @@ -134,6 +154,8 @@ def objc_import(**attrs): Args: **attrs: Rule attributes """ + + # buildifier: disable=native-cc native.objc_import(**_add_tags(attrs)) def cc_flags_supplier(**attrs): diff --git a/cc/find_cc_toolchain.bzl b/cc/find_cc_toolchain.bzl index 5784fa7..0fac527 100644 --- a/cc/find_cc_toolchain.bzl +++ b/cc/find_cc_toolchain.bzl @@ -21,12 +21,17 @@ Returns the current `CcToolchainInfo`. `--cpu`, `--compiler`). For that to work the rule needs to declare an `_cc_toolchain` attribute, e.g. -foo = rule( - implementation = _foo_impl, - attrs = { - "_cc_toolchain": attr.label(default = Label("@rules_cc//cc/private/toolchain:current_cc_toolchain")), - }, -) + foo = rule( + implementation = _foo_impl, + attrs = { + "_cc_toolchain": attr.label( + default = Label( + "@rules_cc//cc/:current_cc_toolchain", # copybara-use-repo-external-label + ), + ), + }, + ) + * When https://github.com/bazelbuild/bazel/issues/7260 **is** flipped, current C++ toolchain is selected using the toolchain resolution mechanism (`--platforms`). For that to work the rule needs to declare a dependency on @@ -34,14 +39,15 @@ foo = rule( foo = rule( implementation = _foo_impl, - toolchains = ["@rules_cc//cc:toolchain_type"], + toolchains = [ + "@rules_cc//cc:toolchain_type", # copybara-use-repo-external-label + ], ) -We advise to depend on both `_cc_toolchain` attr and -`@rules_cc//cc:toolchain_type` for the duration of the migration. After +We advise to depend on both `_cc_toolchain` attr and on the toolchain type for +the duration of the migration. After https://github.com/bazelbuild/bazel/issues/7260 is flipped (and support for old -Bazel version is not needed), it's enough to only keep the -`@rules_cc//cc:toolchain_type`. +Bazel version is not needed), it's enough to only keep the toolchain type. """ def find_cc_toolchain(ctx): @@ -57,13 +63,24 @@ Returns the current `CcToolchainInfo`. # Check the incompatible flag for toolchain resolution. if hasattr(cc_common, "is_cc_toolchain_resolution_enabled_do_not_use") and cc_common.is_cc_toolchain_resolution_enabled_do_not_use(ctx = ctx): - if "@rules_cc//cc:toolchain_type" in ctx.toolchains: - return ctx.toolchains["@rules_cc//cc:toolchain_type"] - fail("In order to use find_cc_toolchain, you must include the '@rules_cc//cc:toolchain_type' in the toolchains argument to your rule.") + if "//cc:toolchain_type" in ctx.toolchains: + return ctx.toolchains["//cc:toolchain_type"] + fail("In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.") # Fall back to the legacy implicit attribute lookup. if hasattr(ctx.attr, "_cc_toolchain"): return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo] # We didn't find anything. - fail("In order to use find_ccc_toolchain, you must define the '_cc_toolchain' attribute on your rule or aspect.") + fail("In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.") + +def find_cpp_toolchain(ctx): + """Deprecated, use `find_cc_toolchain` instead. + + Args: + ctx: See `find_cc_toolchain`. + + Returns: + A CcToolchainInfo. + """ + return find_cc_toolchain(ctx) diff --git a/cc/private/rules_impl/cc_flags_supplier.bzl b/cc/private/rules_impl/cc_flags_supplier.bzl index 3711369..103b3da 100644 --- a/cc/private/rules_impl/cc_flags_supplier.bzl +++ b/cc/private/rules_impl/cc_flags_supplier.bzl @@ -14,11 +14,11 @@ """Rule that provides the CC_FLAGS Make variable.""" load("//cc:action_names.bzl", "CC_FLAGS_MAKE_VARIABLE_ACTION_NAME") +load("//cc:find_cc_toolchain.bzl", "find_cc_toolchain") load("//cc/private/rules_impl:cc_flags_supplier_lib.bzl", "build_cc_flags") -load("//cc:toolchain_utils.bzl", "find_cpp_toolchain") def _cc_flags_supplier_impl(ctx): - cc_toolchain = find_cpp_toolchain(ctx) + cc_toolchain = find_cc_toolchain(ctx) cc_flags = build_cc_flags(ctx, cc_toolchain, CC_FLAGS_MAKE_VARIABLE_ACTION_NAME) variables = platform_common.TemplateVariableInfo({ "CC_FLAGS": cc_flags, @@ -28,7 +28,7 @@ def _cc_flags_supplier_impl(ctx): cc_flags_supplier = rule( implementation = _cc_flags_supplier_impl, attrs = { - "_cc_toolchain": attr.label(default = Label("@rules_cc//cc/private/toolchain:current_cc_toolchain")), + "_cc_toolchain": attr.label(default = Label("@rules_cc//cc:current_cc_toolchain")), }, toolchains = ["@rules_cc//cc:toolchain_type"], fragments = ["cpp"], diff --git a/cc/private/rules_impl/cc_flags_supplier_lib.bzl b/cc/private/rules_impl/cc_flags_supplier_lib.bzl index 042ecef..4b0782a 100644 --- a/cc/private/rules_impl/cc_flags_supplier_lib.bzl +++ b/cc/private/rules_impl/cc_flags_supplier_lib.bzl @@ -15,7 +15,15 @@ # This should match the logic in CcCommon.computeCcFlags: def build_cc_flags(ctx, cc_toolchain, action_name): - """Determine the value for CC_FLAGS based on the given toolchain.""" + """Determine the value for CC_FLAGS based on the given toolchain. + + Args: + ctx: The rule context. + cc_toolchain: CcToolchainInfo instance. + action_name: Name of the action. + Returns: + string containing flags separated by a space. + """ # Get default cc flags from toolchain's make_variables. legacy_cc_flags = cc_common.legacy_cc_flags_make_variable_do_not_use( diff --git a/cc/private/rules_impl/compiler_flag.bzl b/cc/private/rules_impl/compiler_flag.bzl index b399922..ccc9d45 100644 --- a/cc/private/rules_impl/compiler_flag.bzl +++ b/cc/private/rules_impl/compiler_flag.bzl @@ -14,16 +14,16 @@ """Rule that allows select() to differentiate between compilers.""" -load("@rules_cc//cc:toolchain_utils.bzl", "find_cpp_toolchain") +load("//cc:find_cc_toolchain.bzl", "find_cc_toolchain") def _compiler_flag_impl(ctx): - toolchain = find_cpp_toolchain(ctx) + toolchain = find_cc_toolchain(ctx) return [config_common.FeatureFlagInfo(value = toolchain.compiler)] compiler_flag = rule( implementation = _compiler_flag_impl, attrs = { - "_cc_toolchain": attr.label(default = Label("@rules_cc//cc/private/toolchain:current_cc_toolchain")), + "_cc_toolchain": attr.label(default = Label("//cc:current_cc_toolchain")), }, - toolchains = ["@rules_cc//cc:toolchain_type"], + toolchains = ["//cc:toolchain_type"], ) diff --git a/cc/private/toolchain/BUILD b/cc/private/toolchain/BUILD index 9860828..f267fc0 100644 --- a/cc/private/toolchain/BUILD +++ b/cc/private/toolchain/BUILD @@ -51,12 +51,6 @@ constraint_value( constraint_setting = ":cc_compiler", ) -cc_toolchain_alias(name = "current_cc_toolchain") - -cc_host_toolchain_alias(name = "current_cc_host_toolchain") - -cc_libc_top_alias(name = "current_libc_top") - cc_library( name = "malloc", ) @@ -86,17 +80,7 @@ alias( filegroup( name = "srcs", - srcs = glob(["**"]) + [ - "//cc/private/toolchain/runfiles:srcs", - ], -) - -filegroup( - name = "embedded_tools", - srcs = glob(["**"]) + [ - "//cc:action_names.bzl", - "//cc/private/toolchain/runfiles:embedded_tools", - ], + srcs = glob(["**"]) ) filegroup( diff --git a/cc/private/toolchain/BUILD.empty b/cc/private/toolchain/BUILD.empty index fd95fc4..a873d0c 100644 --- a/cc/private/toolchain/BUILD.empty +++ b/cc/private/toolchain/BUILD.empty @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain", "cc_toolchain_suite") + package(default_visibility = ["//visibility:public"]) load(":cc_toolchain_config.bzl", "cc_toolchain_config") @@ -28,15 +30,13 @@ filegroup( cc_toolchain_suite( name = "toolchain", toolchains = { - "local|local": ":local", "local": ":local", + "local|local": ":local", }, ) cc_toolchain( name = "local", - toolchain_identifier = "local", - toolchain_config = ":local_config", all_files = ":empty", ar_files = ":empty", as_files = ":empty", @@ -45,6 +45,8 @@ cc_toolchain( linker_files = ":empty", objcopy_files = ":empty", strip_files = ":empty", + toolchain_config = ":local_config", + toolchain_identifier = "local", ) cc_toolchain_config(name = "local_config") diff --git a/cc/private/toolchain/BUILD.static.freebsd b/cc/private/toolchain/BUILD.static.freebsd index c1fa44e..d8a7b2d 100644 --- a/cc/private/toolchain/BUILD.static.freebsd +++ b/cc/private/toolchain/BUILD.static.freebsd @@ -16,8 +16,8 @@ package(default_visibility = ["//visibility:public"]) +load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain", "cc_toolchain_suite") load(":cc_toolchain_config.bzl", "cc_toolchain_config") -load("@rules_cc//cc:defs.bzl", "cc_toolchain_suite", "cc_toolchain", "cc_library") cc_library( name = "malloc", @@ -32,17 +32,15 @@ filegroup( cc_toolchain_suite( name = "toolchain", toolchains = { - "armeabi-v7a|compiler": ":cc-compiler-armeabi-v7a", - "freebsd|compiler": ":cc-compiler-freebsd", "armeabi-v7a": ":cc-compiler-armeabi-v7a", + "armeabi-v7a|compiler": ":cc-compiler-armeabi-v7a", "freebsd": ":cc-compiler-freebsd", + "freebsd|compiler": ":cc-compiler-freebsd", }, ) cc_toolchain( name = "cc-compiler-freebsd", - toolchain_identifier = "local_freebsd", - toolchain_config = ":local_freebsd", all_files = ":empty", ar_files = ":empty", as_files = ":empty", @@ -52,6 +50,8 @@ cc_toolchain( objcopy_files = ":empty", strip_files = ":empty", supports_param_files = 0, + toolchain_config = ":local_freebsd", + toolchain_identifier = "local_freebsd", ) cc_toolchain_config( @@ -75,8 +75,6 @@ toolchain( cc_toolchain( name = "cc-compiler-armeabi-v7a", - toolchain_identifier = "stub_armeabi-v7a", - toolchain_config = ":stub_armeabi-v7a", all_files = ":empty", ar_files = ":empty", as_files = ":empty", @@ -86,6 +84,8 @@ cc_toolchain( objcopy_files = ":empty", strip_files = ":empty", supports_param_files = 0, + toolchain_config = ":stub_armeabi-v7a", + toolchain_identifier = "stub_armeabi-v7a", ) cc_toolchain_config( diff --git a/cc/private/toolchain/BUILD.toolchains.tpl b/cc/private/toolchain/BUILD.toolchains.tpl index 05ea101..3fee112 100644 --- a/cc/private/toolchain/BUILD.toolchains.tpl +++ b/cc/private/toolchain/BUILD.toolchains.tpl @@ -1,4 +1,5 @@ load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS") + toolchain( name = "cc-toolchain-%{name}", exec_compatible_with = HOST_CONSTRAINTS, diff --git a/cc/private/toolchain/armeabi_cc_toolchain_config.bzl b/cc/private/toolchain/armeabi_cc_toolchain_config.bzl index 3d2aa74..66c5752 100644 --- a/cc/private/toolchain/armeabi_cc_toolchain_config.bzl +++ b/cc/private/toolchain/armeabi_cc_toolchain_config.bzl @@ -15,7 +15,7 @@ """A Starlark cc_toolchain configuration rule""" load( - "@rules_cc//cc/private/toolchain:cc_toolchain_config_lib.bzl", + "@rules_cc//cc:cc_toolchain_config_lib.bzl", "feature", "tool_path", ) diff --git a/cc/private/toolchain/cc_configure.bzl b/cc/private/toolchain/cc_configure.bzl index 1c25484..9500efc 100644 --- a/cc/private/toolchain/cc_configure.bzl +++ b/cc/private/toolchain/cc_configure.bzl @@ -13,15 +13,15 @@ # limitations under the License. """Rules for configuring the C++ toolchain (experimental).""" -load("@rules_cc//cc/private/toolchain:windows_cc_configure.bzl", "configure_windows_toolchain") -load("@rules_cc//cc/private/toolchain:osx_cc_configure.bzl", "configure_osx_toolchain") -load("@rules_cc//cc/private/toolchain:unix_cc_configure.bzl", "configure_unix_toolchain") +load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator") load( - "@rules_cc//cc/private/toolchain:lib_cc_configure.bzl", + ":lib_cc_configure.bzl", "get_cpu_value", "resolve_labels", ) -load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator") +load(":osx_cc_configure.bzl", "configure_osx_toolchain") +load(":unix_cc_configure.bzl", "configure_unix_toolchain") +load(":windows_cc_configure.bzl", "configure_windows_toolchain") def _generate_cpp_only_build_file(repository_ctx, cpu_value, paths): repository_ctx.template( diff --git a/cc/private/toolchain/cc_toolchain_config.bzl b/cc/private/toolchain/cc_toolchain_config.bzl index 5a895fd..265fce6 100644 --- a/cc/private/toolchain/cc_toolchain_config.bzl +++ b/cc/private/toolchain/cc_toolchain_config.bzl @@ -14,16 +14,6 @@ """A Starlark cc_toolchain configuration rule""" -load( - "@rules_cc//cc/private/toolchain:cc_toolchain_config_lib.bzl", - "action_config", - "feature", - "flag_group", - "flag_set", - "tool", - "tool_path", - "with_feature_set", -) load( "@rules_cc//cc:action_names.bzl", _ASSEMBLE_ACTION_NAME = "ASSEMBLE_ACTION_NAME", @@ -40,6 +30,16 @@ load( _LTO_BACKEND_ACTION_NAME = "LTO_BACKEND_ACTION_NAME", _PREPROCESS_ASSEMBLE_ACTION_NAME = "PREPROCESS_ASSEMBLE_ACTION_NAME", ) +load( + "@rules_cc//cc:cc_toolchain_config_lib.bzl", + "action_config", + "feature", + "flag_group", + "flag_set", + "tool", + "tool_path", + "with_feature_set", +) all_compile_actions = [ _C_COMPILE_ACTION_NAME, @@ -209,6 +209,7 @@ def _impl(ctx): builtin_sysroot = None + objcopy_embed_data_action = None if (ctx.attr.cpu == "darwin" or ctx.attr.cpu == "freebsd" or ctx.attr.cpu == "local"): @@ -323,6 +324,7 @@ def _impl(ctx): ], ) + default_link_flags_feature = None if (ctx.attr.cpu == "local"): default_link_flags_feature = feature( name = "default_link_flags", @@ -416,6 +418,7 @@ def _impl(ctx): ], ) + unfiltered_compile_flags_feature = None if (ctx.attr.cpu == "darwin" or ctx.attr.cpu == "freebsd"): unfiltered_compile_flags_feature = feature( @@ -509,6 +512,7 @@ def _impl(ctx): supports_pic_feature = feature(name = "supports_pic", enabled = True) + default_compile_flags_feature = None if (ctx.attr.cpu == "darwin"): default_compile_flags_feature = feature( name = "default_compile_flags", @@ -939,6 +943,7 @@ def _impl(ctx): dbg_feature = feature(name = "dbg") + user_compile_flags_feature = None if (ctx.attr.cpu == "darwin" or ctx.attr.cpu == "freebsd" or ctx.attr.cpu == "local"): @@ -994,6 +999,7 @@ def _impl(ctx): ], ) + sysroot_feature = None if (ctx.attr.cpu == "darwin" or ctx.attr.cpu == "freebsd" or ctx.attr.cpu == "local"): @@ -1131,6 +1137,7 @@ def _impl(ctx): fastbuild_feature = feature(name = "fastbuild") + features = None if (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64"): features = [ default_compile_flags_feature, @@ -1475,8 +1482,8 @@ def _impl(ctx): cc_toolchain_config = rule( implementation = _impl, attrs = { - "cpu": attr.string(mandatory = True), "compiler": attr.string(), + "cpu": attr.string(mandatory = True), "disable_static_cc_toolchains": attr.bool(), }, provides = [CcToolchainConfigInfo], diff --git a/cc/private/toolchain/freebsd_cc_toolchain_config.bzl b/cc/private/toolchain/freebsd_cc_toolchain_config.bzl index 4b55cbb..3521d92 100644 --- a/cc/private/toolchain/freebsd_cc_toolchain_config.bzl +++ b/cc/private/toolchain/freebsd_cc_toolchain_config.bzl @@ -14,8 +14,9 @@ """A Starlark cc_toolchain configuration rule for freebsd.""" +load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES") load( - "@rules_cc//cc/private/toolchain:cc_toolchain_config_lib.bzl", + "@rules_cc//cc:cc_toolchain_config_lib.bzl", "action_config", "feature", "flag_group", @@ -24,7 +25,6 @@ load( "tool_path", "with_feature_set", ) -load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES") all_compile_actions = [ ACTION_NAMES.c_compile, diff --git a/cc/private/toolchain/lib_cc_configure.bzl b/cc/private/toolchain/lib_cc_configure.bzl index fbce23b..1ca6637 100644 --- a/cc/private/toolchain/lib_cc_configure.bzl +++ b/cc/private/toolchain/lib_cc_configure.bzl @@ -115,25 +115,53 @@ def auto_configure_warning(msg): """Output warning message during auto configuration.""" yellow = "\033[1;33m" no_color = "\033[0m" + + # buildifier: disable=print print("\n%sAuto-Configuration Warning:%s %s\n" % (yellow, no_color, msg)) def get_env_var(repository_ctx, name, default = None, enable_warning = True): - """Find an environment variable in system path. Doesn't %-escape the value!""" + """Find an environment variable in system path. Doesn't %-escape the value! + + Args: + repository_ctx: The repository context. + name: Name of the environment variable. + default: Default value to be used when such environment variable is not present. + enable_warning: Show warning if the variable is not present. + Returns: + value of the environment variable or default. + """ + if name in repository_ctx.os.environ: return repository_ctx.os.environ[name] if default != None: if enable_warning: auto_configure_warning("'%s' environment variable is not set, using '%s' as default" % (name, default)) return default - auto_configure_fail("'%s' environment variable is not set" % name) + return auto_configure_fail("'%s' environment variable is not set" % name) def which(repository_ctx, cmd, default = None): - """A wrapper around repository_ctx.which() to provide a fallback value. Doesn't %-escape the value!""" + """A wrapper around repository_ctx.which() to provide a fallback value. Doesn't %-escape the value! + + Args: + repository_ctx: The repository context. + cmd: name of the executable to resolve. + default: Value to be returned when such executable couldn't be found. + Returns: + absolute path to the cmd or default when not found. + """ result = repository_ctx.which(cmd) return default if result == None else str(result) def which_cmd(repository_ctx, cmd, default = None): - """Find cmd in PATH using repository_ctx.which() and fail if cannot find it. Doesn't %-escape the cmd!""" + """Find cmd in PATH using repository_ctx.which() and fail if cannot find it. Doesn't %-escape the cmd! + + Args: + repository_ctx: The repository context. + cmd: name of the executable to resolve. + default: Value to be returned when such executable couldn't be found. + Returns: + absolute path to the cmd or default when not found. + """ result = repository_ctx.which(cmd) if result != None: return str(result) @@ -149,7 +177,16 @@ def execute( command, environment = None, expect_failure = False): - """Execute a command, return stdout if succeed and throw an error if it fails. Doesn't %-escape the result!""" + """Execute a command, return stdout if succeed and throw an error if it fails. Doesn't %-escape the result! + + Args: + repository_ctx: The repository context. + command: command to execute. + environment: dictionary with environment variables to set for the command. + expect_failure: True if the command is expected to fail. + Returns: + stdout of the executed command. + """ if environment: result = repository_ctx.execute(command, environment = environment) else: @@ -178,7 +215,13 @@ def execute( return stripped_stdout def get_cpu_value(repository_ctx): - """Compute the cpu_value based on the OS name. Doesn't %-escape the result!""" + """Compute the cpu_value based on the OS name. Doesn't %-escape the result! + + Args: + repository_ctx: The repository context. + Returns: + One of (darwin, freebsd, x64_windows, ppc, s390x, arm, aarch64, k8, piii) + """ os_name = repository_ctx.os.name.lower() if os_name.startswith("mac os"): return "darwin" diff --git a/cc/private/toolchain/osx_cc_configure.bzl b/cc/private/toolchain/osx_cc_configure.bzl index 716d5b2..db41831 100644 --- a/cc/private/toolchain/osx_cc_configure.bzl +++ b/cc/private/toolchain/osx_cc_configure.bzl @@ -16,13 +16,13 @@ load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator") load( - "@rules_cc//cc/private/toolchain:lib_cc_configure.bzl", + ":lib_cc_configure.bzl", "escape_string", "resolve_labels", "write_builtin_include_directory_paths", ) load( - "@rules_cc//cc/private/toolchain:unix_cc_configure.bzl", + ":unix_cc_configure.bzl", "configure_unix_toolchain", "find_cc", "get_env", @@ -51,7 +51,12 @@ def _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains) return include_dirs def configure_osx_toolchain(repository_ctx, overriden_tools): - """Configure C++ toolchain on macOS.""" + """Configure C++ toolchain on macOS. + + Args: + repository_ctx: The repository context. + overriden_tools: dictionary of overriden tools. + """ paths = resolve_labels(repository_ctx, [ "@rules_cc//cc/private/toolchain:osx_cc_wrapper.sh.tpl", "@bazel_tools//tools/objc:libtool.sh", diff --git a/cc/private/toolchain/runfiles/BUILD b/cc/private/toolchain/runfiles/BUILD deleted file mode 100644 index cb4a9d5..0000000 --- a/cc/private/toolchain/runfiles/BUILD +++ /dev/null @@ -1,84 +0,0 @@ -package(default_visibility = ["//visibility:private"]) - -load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") - -filegroup( - name = "srcs", - srcs = glob( - ["**"], - exclude = [ - ".*", - "*~", - ], - ), - visibility = ["//cc/private/toolchain:__pkg__"], -) - -filegroup( - name = "embedded_tools", - srcs = [ - "BUILD.tools", - ":srcs_for_embedded_tools", - ], - visibility = ["//cc/private/toolchain:__pkg__"], -) - -# Rewrite the include path for runfiles.h in runfiles_src.cc, and create -# "generated_runfiles.{h,cc}". These files are renamed to "runfiles.{h,cc}" as -# part of creating the embedded tools of Bazel. -# -# We cannot just check in runfiles_src.{h,cc} as runfiles.{h,cc}, because it'd -# cause a header check failure on Windows when building targets in the Bazel -# source tree, if those targets depend on @rules_cc//cc/private/toolchain/runfiles, -# because due to lack of sandboxing they would accidentally pick up runfiles.h -# from @rules_cc//cc/private/toolchain/runfiles. -genrule( - name = "srcs_for_embedded_tools", - srcs = [ - "runfiles_src.cc", - "runfiles_src.h", - ], - outs = [ - "generated_runfiles.cc", - "generated_runfiles.h", - ], - # Keep this transformation logic in sync with the - # //scripts/bootstrap/compile.sh - cmd = ("sed " + - " 's|^#include.*/runfiles_src.h.*|#include \"tools/cpp/runfiles/runfiles.h\"|' " + - " $(location runfiles_src.cc) > $(location generated_runfiles.cc) && " + - "cp $(location runfiles_src.h) $(location generated_runfiles.h)"), -) - -cc_library( - name = "runfiles", - testonly = 1, - srcs = ["runfiles_src.cc"], - hdrs = ["runfiles_src.h"], -) - -cc_test( - name = "runfiles_test", - srcs = ["runfiles_test.cc"], - visibility = ["//visibility:public"], - deps = [ - ":runfiles", - "@com_google_googletest//:gtest_main", - ], -) - -test_suite( - name = "windows_tests", - tags = [ - "-no_windows", - "-slow", - ], -) - -test_suite( - name = "all_windows_tests", - tests = [ - ":windows_tests", - ], - visibility = ["//tools:__pkg__"], -) diff --git a/cc/private/toolchain/runfiles/BUILD.tools b/cc/private/toolchain/runfiles/BUILD.tools deleted file mode 100644 index ef9518c..0000000 --- a/cc/private/toolchain/runfiles/BUILD.tools +++ /dev/null @@ -1,8 +0,0 @@ -# This package will host the C++ runfiles library when it's finally released. - -cc_library( - name = "runfiles", - srcs = ["runfiles.cc"], - hdrs = ["runfiles.h"], - visibility = ["//visibility:public"], -) diff --git a/cc/private/toolchain/unix_cc_configure.bzl b/cc/private/toolchain/unix_cc_configure.bzl index a62afd9..856486e 100644 --- a/cc/private/toolchain/unix_cc_configure.bzl +++ b/cc/private/toolchain/unix_cc_configure.bzl @@ -15,7 +15,7 @@ """Configuring the C++ toolchain on Unix platforms.""" load( - "@rules_cc//cc/private/toolchain:lib_cc_configure.bzl", + ":lib_cc_configure.bzl", "auto_configure_fail", "auto_configure_warning", "auto_configure_warning_maybe", @@ -123,7 +123,16 @@ def _cxx_inc_convert(path): return path def get_escaped_cxx_inc_directories(repository_ctx, cc, lang_flag, additional_flags = []): - """Compute the list of default %-escaped C++ include directories.""" + """Compute the list of default %-escaped C++ include directories. + + Args: + repository_ctx: The repository context. + cc: path to the C compiler. + lang_flag: value for the language flag (c, c++). + additional_flags: additional flags to pass to cc. + Returns: + a list of escaped system include directories. + """ result = repository_ctx.execute([cc, "-E", lang_flag, "-", "-v"] + additional_flags) index1 = result.stderr.find(_INC_DIR_MARKER_BEGIN) if index1 == -1: @@ -243,8 +252,14 @@ def _get_no_canonical_prefixes_opt(repository_ctx, cc): ) return opt -def get_env(repository_ctx): - """Convert the environment in a list of export if in Homebrew. Doesn't %-escape the result!""" +def _get_env(repository_ctx): + """Convert the environment in a list of export if in Homebrew. Doesn't %-escape the result! + + Args: + repository_ctx: The repository context. + Returns: + empty string or a list of exports in case we're running with homebrew. Don't ask me why. + """ env = repository_ctx.os.environ if "HOMEBREW_RUBY_PATH" in env: return "\n".join([ @@ -304,7 +319,13 @@ def find_cc(repository_ctx, overriden_tools): return _find_generic(repository_ctx, "gcc", "CC", overriden_tools) def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): - """Configure C++ toolchain on Unix platforms.""" + """Configure C++ toolchain on Unix platforms. + + Args: + repository_ctx: The repository context. + cpu_value: current cpu name. + overriden_tools: overriden tools. + """ paths = resolve_labels(repository_ctx, [ "@rules_cc//cc/private/toolchain:BUILD.tpl", "@rules_cc//cc/private/toolchain:armeabi_cc_toolchain_config.bzl", @@ -411,16 +432,10 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): "BUILD", paths["@rules_cc//cc/private/toolchain:BUILD.tpl"], { - "%{cc_toolchain_identifier}": cc_toolchain_identifier, - "%{name}": cpu_value, - "%{supports_param_files}": "0" if darwin else "1", - "%{cc_compiler_deps}": get_starlark_list([":builtin_include_directory_paths"] + ( - [":cc_wrapper"] if darwin else [] - )), - "%{compiler}": escape_string(get_env_var( + "%{abi_libc_version}": escape_string(get_env_var( repository_ctx, - "BAZEL_COMPILER", - "compiler", + "ABI_LIBC_VERSION", + "local", False, )), "%{abi_version}": escape_string(get_env_var( @@ -429,40 +444,10 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): "local", False, )), - "%{abi_libc_version}": escape_string(get_env_var( - repository_ctx, - "ABI_LIBC_VERSION", - "local", - False, + "%{cc_compiler_deps}": get_starlark_list([":builtin_include_directory_paths"] + ( + [":cc_wrapper"] if darwin else [] )), - "%{host_system_name}": escape_string(get_env_var( - repository_ctx, - "BAZEL_HOST_SYSTEM", - "local", - False, - )), - "%{target_libc}": "macosx" if darwin else escape_string(get_env_var( - repository_ctx, - "BAZEL_TARGET_LIBC", - "local", - False, - )), - "%{target_cpu}": escape_string(get_env_var( - repository_ctx, - "BAZEL_TARGET_CPU", - cpu_value, - False, - )), - "%{target_system_name}": escape_string(get_env_var( - repository_ctx, - "BAZEL_TARGET_SYSTEM", - "local", - False, - )), - "%{tool_paths}": ",\n ".join( - ['"%s": "%s"' % (k, v) for k, v in tool_paths.items()], - ), - "%{cxx_builtin_include_directories}": get_starlark_list(builtin_include_directories), + "%{cc_toolchain_identifier}": cc_toolchain_identifier, "%{compile_flags}": get_starlark_list( [ # Security hardening requires optimization. @@ -488,7 +473,23 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): "-fno-omit-frame-pointer", ], ), + "%{compiler}": escape_string(get_env_var( + repository_ctx, + "BAZEL_COMPILER", + "compiler", + False, + )), + "%{coverage_compile_flags}": coverage_compile_flags, + "%{coverage_link_flags}": coverage_link_flags, + "%{cxx_builtin_include_directories}": get_starlark_list(builtin_include_directories), "%{cxx_flags}": get_starlark_list(cxx_opts + _escaped_cplus_include_paths(repository_ctx)), + "%{dbg_compile_flags}": get_starlark_list(["-g"]), + "%{host_system_name}": escape_string(get_env_var( + repository_ctx, + "BAZEL_HOST_SYSTEM", + "local", + False, + )), "%{link_flags}": get_starlark_list(( ["-fuse-ld=" + gold_linker_path] if gold_linker_path else [] ) + _add_linker_option_if_supported( @@ -518,6 +519,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): ) ) + link_opts), "%{link_libs}": get_starlark_list(link_libs), + "%{name}": cpu_value, "%{opt_compile_flags}": get_starlark_list( [ # No debug symbols. @@ -552,6 +554,29 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): "-gc-sections", ), ), + "%{supports_param_files}": "0" if darwin else "1", + "%{supports_start_end_lib}": "True" if gold_linker_path else "False", + "%{target_cpu}": escape_string(get_env_var( + repository_ctx, + "BAZEL_TARGET_CPU", + cpu_value, + False, + )), + "%{target_libc}": "macosx" if darwin else escape_string(get_env_var( + repository_ctx, + "BAZEL_TARGET_LIBC", + "local", + False, + )), + "%{target_system_name}": escape_string(get_env_var( + repository_ctx, + "BAZEL_TARGET_SYSTEM", + "local", + False, + )), + "%{tool_paths}": ",\n ".join( + ['"%s": "%s"' % (k, v) for k, v in tool_paths.items()], + ), "%{unfiltered_compile_flags}": get_starlark_list( _get_no_canonical_prefixes_opt(repository_ctx, cc) + [ # Make C++ compilation deterministic. Use linkstamping instead of these @@ -562,9 +587,5 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): "-D__TIME__=\\\"redacted\\\"", ], ), - "%{dbg_compile_flags}": get_starlark_list(["-g"]), - "%{coverage_compile_flags}": coverage_compile_flags, - "%{coverage_link_flags}": coverage_link_flags, - "%{supports_start_end_lib}": "True" if gold_linker_path else "False", }, ) diff --git a/cc/private/toolchain/unix_cc_toolchain_config.bzl b/cc/private/toolchain/unix_cc_toolchain_config.bzl index ff634bf..3d5ae2f 100644 --- a/cc/private/toolchain/unix_cc_toolchain_config.bzl +++ b/cc/private/toolchain/unix_cc_toolchain_config.bzl @@ -14,8 +14,9 @@ """A Starlark cc_toolchain configuration rule""" +load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES") load( - "@rules_cc//cc/private/toolchain:cc_toolchain_config_lib.bzl", + "@rules_cc//cc:cc_toolchain_config_lib.bzl", "feature", "feature_set", "flag_group", @@ -24,7 +25,6 @@ load( "variable_with_value", "with_feature_set", ) -load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES") all_compile_actions = [ ACTION_NAMES.c_compile, @@ -1171,27 +1171,27 @@ def _impl(ctx): cc_toolchain_config = rule( implementation = _impl, attrs = { - "cpu": attr.string(mandatory = True), - "compiler": attr.string(mandatory = True), - "toolchain_identifier": attr.string(mandatory = True), - "host_system_name": attr.string(mandatory = True), - "target_system_name": attr.string(mandatory = True), - "target_libc": attr.string(mandatory = True), - "abi_version": attr.string(mandatory = True), "abi_libc_version": attr.string(mandatory = True), - "cxx_builtin_include_directories": attr.string_list(), - "tool_paths": attr.string_dict(), + "abi_version": attr.string(mandatory = True), "compile_flags": attr.string_list(), - "dbg_compile_flags": attr.string_list(), - "opt_compile_flags": attr.string_list(), - "cxx_flags": attr.string_list(), - "link_flags": attr.string_list(), - "link_libs": attr.string_list(), - "opt_link_flags": attr.string_list(), - "unfiltered_compile_flags": attr.string_list(), + "compiler": attr.string(mandatory = True), "coverage_compile_flags": attr.string_list(), "coverage_link_flags": attr.string_list(), + "cpu": attr.string(mandatory = True), + "cxx_builtin_include_directories": attr.string_list(), + "cxx_flags": attr.string_list(), + "dbg_compile_flags": attr.string_list(), + "host_system_name": attr.string(mandatory = True), + "link_flags": attr.string_list(), + "link_libs": attr.string_list(), + "opt_compile_flags": attr.string_list(), + "opt_link_flags": attr.string_list(), "supports_start_end_lib": attr.bool(), + "target_libc": attr.string(mandatory = True), + "target_system_name": attr.string(mandatory = True), + "tool_paths": attr.string_dict(), + "toolchain_identifier": attr.string(mandatory = True), + "unfiltered_compile_flags": attr.string_list(), }, provides = [CcToolchainConfigInfo], ) diff --git a/cc/private/toolchain/windows_cc_configure.bzl b/cc/private/toolchain/windows_cc_configure.bzl index ccea3de..5483272 100644 --- a/cc/private/toolchain/windows_cc_configure.bzl +++ b/cc/private/toolchain/windows_cc_configure.bzl @@ -15,7 +15,7 @@ """Configuring the C++ toolchain on Windows.""" load( - "@rules_cc//cc/private/toolchain:lib_cc_configure.bzl", + ":lib_cc_configure.bzl", "auto_configure_fail", "auto_configure_warning", "auto_configure_warning_maybe", @@ -99,7 +99,7 @@ def _add_system_root(repository_ctx, env): env["PATH"] = env["PATH"] + ";" + _get_system_root(repository_ctx) + "\\system32" return env -def find_vc_path(repository_ctx): +def _find_vc_path(repository_ctx): """Find Visual C++ build tools install path. Doesn't %-escape the result.""" # 1. Check if BAZEL_VC or BAZEL_VS is already set by user. @@ -360,7 +360,7 @@ def _get_winsdk_full_version(repository_ctx): """Return the value of BAZEL_WINSDK_FULL_VERSION if defined, otherwise an empty string.""" return repository_ctx.os.environ.get("BAZEL_WINSDK_FULL_VERSION", default = "") -def find_msvc_tool(repository_ctx, vc_path, tool): +def _find_msvc_tool(repository_ctx, vc_path, tool): """Find the exact path of a specific build tool in MSVC. Doesn't %-escape the result.""" tool_path = None if _is_vs_2017_or_2019(vc_path): @@ -384,7 +384,7 @@ def _find_missing_vc_tools(repository_ctx, vc_path): missing_tools.append("VCVARSALL.BAT") for tool in ["cl.exe", "link.exe", "lib.exe", "ml64.exe"]: - if not find_msvc_tool(repository_ctx, vc_path, tool): + if not _find_msvc_tool(repository_ctx, vc_path, tool): missing_tools.append(tool) return missing_tools @@ -397,7 +397,7 @@ def _is_support_debug_fastlink(repository_ctx, linker): result = execute(repository_ctx, [linker], expect_failure = True) return result.find("/DEBUG[:{FASTLINK|FULL|NONE}]") != -1 -def find_llvm_path(repository_ctx): +def _find_llvm_path(repository_ctx): """Find LLVM install path.""" # 1. Check if BAZEL_LLVM is already set by user. @@ -443,7 +443,7 @@ def find_llvm_path(repository_ctx): auto_configure_warning_maybe(repository_ctx, "LLVM installation found at %s" % llvm_dir) return llvm_dir -def find_llvm_tool(repository_ctx, llvm_path, tool): +def _find_llvm_tool(repository_ctx, llvm_path, tool): """Find the exact path of a specific build tool in LLVM. Doesn't %-escape the result.""" tool_path = llvm_path + "\\bin\\" + tool @@ -460,7 +460,7 @@ def _find_missing_llvm_tools(repository_ctx, llvm_path): """Check if any required tool is missing under given LLVM path.""" missing_tools = [] for tool in ["clang-cl.exe", "lld-link.exe", "llvm-lib.exe"]: - if not find_llvm_tool(repository_ctx, llvm_path, tool): + if not _find_llvm_tool(repository_ctx, llvm_path, tool): missing_tools.append(tool) return missing_tools @@ -481,17 +481,17 @@ def _get_msys_mingw_vars(repository_ctx): msys_mingw_vars = { "%{cxx_builtin_include_directories}": inc_dir_msys, "%{mingw_cxx_builtin_include_directories}": inc_dir_mingw, - "%{tool_paths}": tool_paths, + "%{mingw_tool_bin_path}": tool_bin_path_mingw, "%{mingw_tool_paths}": tool_paths_mingw, "%{tool_bin_path}": tool_bin_path, - "%{mingw_tool_bin_path}": tool_bin_path_mingw, + "%{tool_paths}": tool_paths, } return msys_mingw_vars def _get_msvc_vars(repository_ctx, paths): """Get the variables we need to populate the MSVC toolchains.""" msvc_vars = dict() - vc_path = find_vc_path(repository_ctx) + vc_path = _find_vc_path(repository_ctx) missing_tools = None if not vc_path: repository_ctx.template( @@ -518,17 +518,17 @@ def _get_msvc_vars(repository_ctx, paths): if not vc_path or missing_tools: write_builtin_include_directory_paths(repository_ctx, "msvc", [], file_suffix = "_msvc") msvc_vars = { - "%{msvc_env_tmp}": "msvc_not_found", - "%{msvc_env_path}": "msvc_not_found", - "%{msvc_env_include}": "msvc_not_found", - "%{msvc_env_lib}": "msvc_not_found", - "%{msvc_cl_path}": "vc_installation_error.bat", - "%{msvc_ml_path}": "vc_installation_error.bat", - "%{msvc_link_path}": "vc_installation_error.bat", - "%{msvc_lib_path}": "vc_installation_error.bat", "%{dbg_mode_debug_flag}": "/DEBUG", "%{fastbuild_mode_debug_flag}": "/DEBUG", + "%{msvc_cl_path}": "vc_installation_error.bat", "%{msvc_cxx_builtin_include_directories}": "", + "%{msvc_env_include}": "msvc_not_found", + "%{msvc_env_lib}": "msvc_not_found", + "%{msvc_env_path}": "msvc_not_found", + "%{msvc_env_tmp}": "msvc_not_found", + "%{msvc_lib_path}": "vc_installation_error.bat", + "%{msvc_link_path}": "vc_installation_error.bat", + "%{msvc_ml_path}": "vc_installation_error.bat", } return msvc_vars @@ -540,23 +540,23 @@ def _get_msvc_vars(repository_ctx, paths): llvm_path = "" if _use_clang_cl(repository_ctx): - llvm_path = find_llvm_path(repository_ctx) + llvm_path = _find_llvm_path(repository_ctx) if not llvm_path: auto_configure_fail("\nUSE_CLANG_CL is set to 1, but Bazel cannot find Clang installation on your system.\n" + "Please install Clang via http://releases.llvm.org/download.html\n") - cl_path = find_llvm_tool(repository_ctx, llvm_path, "clang-cl.exe") - link_path = find_llvm_tool(repository_ctx, llvm_path, "lld-link.exe") + cl_path = _find_llvm_tool(repository_ctx, llvm_path, "clang-cl.exe") + link_path = _find_llvm_tool(repository_ctx, llvm_path, "lld-link.exe") if not link_path: - link_path = find_msvc_tool(repository_ctx, vc_path, "link.exe") - lib_path = find_llvm_tool(repository_ctx, llvm_path, "llvm-lib.exe") + link_path = _find_msvc_tool(repository_ctx, vc_path, "link.exe") + lib_path = _find_llvm_tool(repository_ctx, llvm_path, "llvm-lib.exe") if not lib_path: - lib_path = find_msvc_tool(repository_ctx, vc_path, "lib.exe") + lib_path = _find_msvc_tool(repository_ctx, vc_path, "lib.exe") else: - cl_path = find_msvc_tool(repository_ctx, vc_path, "cl.exe") - link_path = find_msvc_tool(repository_ctx, vc_path, "link.exe") - lib_path = find_msvc_tool(repository_ctx, vc_path, "lib.exe") + cl_path = _find_msvc_tool(repository_ctx, vc_path, "cl.exe") + link_path = _find_msvc_tool(repository_ctx, vc_path, "link.exe") + lib_path = _find_msvc_tool(repository_ctx, vc_path, "lib.exe") - msvc_ml_path = find_msvc_tool(repository_ctx, vc_path, "ml64.exe") + msvc_ml_path = _find_msvc_tool(repository_ctx, vc_path, "ml64.exe") escaped_cxx_include_directories = [] for path in escaped_include_paths.split(";"): @@ -574,23 +574,23 @@ def _get_msvc_vars(repository_ctx, paths): write_builtin_include_directory_paths(repository_ctx, "msvc", escaped_cxx_include_directories, file_suffix = "_msvc") msvc_vars = { - "%{msvc_env_tmp}": escaped_tmp_dir, - "%{msvc_env_path}": escaped_paths, - "%{msvc_env_include}": escaped_include_paths, - "%{msvc_env_lib}": escaped_lib_paths, - "%{msvc_cl_path}": cl_path, - "%{msvc_ml_path}": msvc_ml_path, - "%{msvc_link_path}": link_path, - "%{msvc_lib_path}": lib_path, "%{dbg_mode_debug_flag}": "/DEBUG:FULL" if support_debug_fastlink else "/DEBUG", "%{fastbuild_mode_debug_flag}": "/DEBUG:FASTLINK" if support_debug_fastlink else "/DEBUG", + "%{msvc_cl_path}": cl_path, "%{msvc_cxx_builtin_include_directories}": " " + ",\n ".join(escaped_cxx_include_directories), + "%{msvc_env_include}": escaped_include_paths, + "%{msvc_env_lib}": escaped_lib_paths, + "%{msvc_env_path}": escaped_paths, + "%{msvc_env_tmp}": escaped_tmp_dir, + "%{msvc_lib_path}": lib_path, + "%{msvc_link_path}": link_path, + "%{msvc_ml_path}": msvc_ml_path, } return msvc_vars def _get_clang_cl_vars(repository_ctx, paths, msvc_vars): """Get the variables we need to populate the clang-cl toolchains.""" - llvm_path = find_llvm_path(repository_ctx) + llvm_path = _find_llvm_path(repository_ctx) error_script = None if msvc_vars["%{msvc_cl_path}"] == "vc_installation_error.bat": error_script = "vc_installation_error.bat" @@ -621,23 +621,23 @@ def _get_clang_cl_vars(repository_ctx, paths, msvc_vars): if error_script: write_builtin_include_directory_paths(repository_ctx, "clang-cl", [], file_suffix = "_clangcl") clang_cl_vars = { - "%{clang_cl_env_tmp}": "clang_cl_not_found", - "%{clang_cl_env_path}": "clang_cl_not_found", + "%{clang_cl_cl_path}": error_script, + "%{clang_cl_cxx_builtin_include_directories}": "", + "%{clang_cl_dbg_mode_debug_flag}": "/DEBUG", "%{clang_cl_env_include}": "clang_cl_not_found", "%{clang_cl_env_lib}": "clang_cl_not_found", - "%{clang_cl_cl_path}": error_script, - "%{clang_cl_link_path}": error_script, - "%{clang_cl_lib_path}": error_script, - "%{clang_cl_ml_path}": error_script, - "%{clang_cl_dbg_mode_debug_flag}": "/DEBUG", + "%{clang_cl_env_path}": "clang_cl_not_found", + "%{clang_cl_env_tmp}": "clang_cl_not_found", "%{clang_cl_fastbuild_mode_debug_flag}": "/DEBUG", - "%{clang_cl_cxx_builtin_include_directories}": "", + "%{clang_cl_lib_path}": error_script, + "%{clang_cl_link_path}": error_script, + "%{clang_cl_ml_path}": error_script, } return clang_cl_vars - clang_cl_path = find_llvm_tool(repository_ctx, llvm_path, "clang-cl.exe") - lld_link_path = find_llvm_tool(repository_ctx, llvm_path, "lld-link.exe") - llvm_lib_path = find_llvm_tool(repository_ctx, llvm_path, "llvm-lib.exe") + clang_cl_path = _find_llvm_tool(repository_ctx, llvm_path, "clang-cl.exe") + lld_link_path = _find_llvm_tool(repository_ctx, llvm_path, "lld-link.exe") + llvm_lib_path = _find_llvm_tool(repository_ctx, llvm_path, "llvm-lib.exe") clang_version = _get_clang_version(repository_ctx, clang_cl_path) clang_dir = llvm_path + "\\lib\\clang\\" + clang_version @@ -647,23 +647,27 @@ def _get_clang_cl_vars(repository_ctx, paths, msvc_vars): clang_cl_include_directories = msvc_vars["%{msvc_cxx_builtin_include_directories}"] + (",\n \"%s\"" % clang_include_path) write_builtin_include_directory_paths(repository_ctx, "clang-cl", [clang_cl_include_directories], file_suffix = "_clangcl") clang_cl_vars = { - "%{clang_cl_env_tmp}": msvc_vars["%{msvc_env_tmp}"], - "%{clang_cl_env_path}": msvc_vars["%{msvc_env_path}"], - "%{clang_cl_env_include}": msvc_vars["%{msvc_env_include}"] + ";" + clang_include_path, - "%{clang_cl_env_lib}": msvc_vars["%{msvc_env_lib}"] + ";" + clang_lib_path, - "%{clang_cl_cxx_builtin_include_directories}": clang_cl_include_directories, "%{clang_cl_cl_path}": clang_cl_path, - "%{clang_cl_link_path}": lld_link_path, - "%{clang_cl_lib_path}": llvm_lib_path, - "%{clang_cl_ml_path}": msvc_vars["%{msvc_ml_path}"], + "%{clang_cl_cxx_builtin_include_directories}": clang_cl_include_directories, # LLVM's lld-link.exe doesn't support /DEBUG:FASTLINK. "%{clang_cl_dbg_mode_debug_flag}": "/DEBUG", + "%{clang_cl_env_include}": msvc_vars["%{msvc_env_include}"] + ";" + clang_include_path, + "%{clang_cl_env_lib}": msvc_vars["%{msvc_env_lib}"] + ";" + clang_lib_path, + "%{clang_cl_env_path}": msvc_vars["%{msvc_env_path}"], + "%{clang_cl_env_tmp}": msvc_vars["%{msvc_env_tmp}"], "%{clang_cl_fastbuild_mode_debug_flag}": "/DEBUG", + "%{clang_cl_lib_path}": llvm_lib_path, + "%{clang_cl_link_path}": lld_link_path, + "%{clang_cl_ml_path}": msvc_vars["%{msvc_ml_path}"], } return clang_cl_vars def configure_windows_toolchain(repository_ctx): - """Configure C++ toolchain on Windows.""" + """Configure C++ toolchain on Windows. + + Args: + repository_ctx: The repository context. + """ paths = resolve_labels(repository_ctx, [ "@rules_cc//cc/private/toolchain:BUILD.windows.tpl", "@rules_cc//cc/private/toolchain:windows_cc_toolchain_config.bzl", diff --git a/cc/private/toolchain/windows_cc_toolchain_config.bzl b/cc/private/toolchain/windows_cc_toolchain_config.bzl index 6c02b5c..f057b8d 100644 --- a/cc/private/toolchain/windows_cc_toolchain_config.bzl +++ b/cc/private/toolchain/windows_cc_toolchain_config.bzl @@ -14,8 +14,9 @@ """A Starlark cc_toolchain configuration rule for Windows""" +load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES") load( - "@rules_cc//cc/private/toolchain:cc_toolchain_config_lib.bzl", + "@rules_cc//cc:cc_toolchain_config_lib.bzl", "action_config", "artifact_name_pattern", "env_entry", @@ -29,7 +30,6 @@ load( "variable_with_value", "with_feature_set", ) -load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES") all_compile_actions = [ ACTION_NAMES.c_compile, @@ -1315,28 +1315,28 @@ def _impl(ctx): cc_toolchain_config = rule( implementation = _impl, attrs = { - "cpu": attr.string(mandatory = True), - "compiler": attr.string(), - "toolchain_identifier": attr.string(), - "host_system_name": attr.string(), - "target_system_name": attr.string(), - "target_libc": attr.string(), - "abi_version": attr.string(), "abi_libc_version": attr.string(), - "tool_paths": attr.string_dict(), + "abi_version": attr.string(), + "compiler": attr.string(), + "cpu": attr.string(mandatory = True), "cxx_builtin_include_directories": attr.string_list(), + "dbg_mode_debug_flag": attr.string(), "default_link_flags": attr.string_list(default = []), - "msvc_env_tmp": attr.string(default = "msvc_not_found"), - "msvc_env_path": attr.string(default = "msvc_not_found"), + "fastbuild_mode_debug_flag": attr.string(), + "host_system_name": attr.string(), + "msvc_cl_path": attr.string(default = "vc_installation_error.bat"), "msvc_env_include": attr.string(default = "msvc_not_found"), "msvc_env_lib": attr.string(default = "msvc_not_found"), - "msvc_cl_path": attr.string(default = "vc_installation_error.bat"), - "msvc_ml_path": attr.string(default = "vc_installation_error.bat"), - "msvc_link_path": attr.string(default = "vc_installation_error.bat"), + "msvc_env_path": attr.string(default = "msvc_not_found"), + "msvc_env_tmp": attr.string(default = "msvc_not_found"), "msvc_lib_path": attr.string(default = "vc_installation_error.bat"), - "dbg_mode_debug_flag": attr.string(), - "fastbuild_mode_debug_flag": attr.string(), + "msvc_link_path": attr.string(default = "vc_installation_error.bat"), + "msvc_ml_path": attr.string(default = "vc_installation_error.bat"), + "target_libc": attr.string(), + "target_system_name": attr.string(), "tool_bin_path": attr.string(default = "not_found"), + "tool_paths": attr.string_dict(), + "toolchain_identifier": attr.string(), }, provides = [CcToolchainConfigInfo], ) diff --git a/cc/toolchain_utils.bzl b/cc/toolchain_utils.bzl index dc97993..bec575e 100644 --- a/cc/toolchain_utils.bzl +++ b/cc/toolchain_utils.bzl @@ -14,35 +14,18 @@ # limitations under the License. """ -Finds the c++ toolchain. - -Returns the toolchain if enabled, and falls back to a toolchain constructed from -the CppConfiguration. +Deprecated, use find_cc_toolchain.bzl """ -def find_cpp_toolchain(ctx): - """ - Finds the c++ toolchain. +load(":find_cc_toolchain.bzl", "find_cc_toolchain") - If the c++ toolchain is in use, returns it. Otherwise, returns a c++ - toolchain derived from legacy toolchain selection. +def find_cpp_toolchain(ctx): + """Deprecated, use `find_cc_toolchain` instead. Args: - ctx: The rule context for which to find a toolchain. + ctx: See `find_cc_toolchain`. Returns: - A CcToolchainProvider. + A CcToolchainInfo. """ - - # Check the incompatible flag for toolchain resolution. - if hasattr(cc_common, "is_cc_toolchain_resolution_enabled_do_not_use") and cc_common.is_cc_toolchain_resolution_enabled_do_not_use(ctx = ctx): - if "@rules_cc//cc:toolchain_type" in ctx.toolchains: - return ctx.toolchains["@rules_cc//cc:toolchain_type"] - fail("In order to use find_cpp_toolchain, you must include the '@rules_cc//cc:toolchain_type' in the toolchains argument to your rule.") - - # Fall back to the legacy implicit attribute lookup. - if hasattr(ctx.attr, "_cc_toolchain"): - return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo] - - # We didn't find anything. - fail("In order to use find_cpp_toolchain, you must define the '_cc_toolchain' attribute on your rule or aspect.") + return find_cc_toolchain(ctx) diff --git a/distro/BUILD b/distro/BUILD index 2e07f40..757e2ec 100644 --- a/distro/BUILD +++ b/distro/BUILD @@ -1,11 +1,11 @@ +load("@rules_pkg//:pkg.bzl", "pkg_tar") +load("@rules_pkg//releasing:defs.bzl", "print_rel_notes") +load("//cc:version.bzl", "version") + package( default_visibility = ["//visibility:private"], ) -load("@rules_cc//cc:version.bzl", "version") -load("@rules_pkg//:pkg.bzl", "pkg_tar") -load("@rules_pkg//releasing:defs.bzl", "print_rel_notes") - # Build the artifact to put on the github release page. pkg_tar( name = "rules_cc-%s" % version, diff --git a/examples/my_c_archive/BUILD b/examples/my_c_archive/BUILD index 6b215bb..59bcd56 100644 --- a/examples/my_c_archive/BUILD +++ b/examples/my_c_archive/BUILD @@ -13,11 +13,12 @@ # limitations under the License. # Example showing how to create a custom Starlark rule that rules_cc can depend on -licenses(["notice"]) -load("//examples/my_c_compile:my_c_compile.bzl", "my_c_compile") +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") load("//examples/my_c_archive:my_c_archive.bzl", "my_c_archive") -load("@rules_cc//cc:defs.bzl", "cc_binary") +load("//examples/my_c_compile:my_c_compile.bzl", "my_c_compile") + +licenses(["notice"]) cc_binary( name = "main", diff --git a/examples/my_c_archive/my_c_archive.bzl b/examples/my_c_archive/my_c_archive.bzl index baf6d25..bde0e5e 100644 --- a/examples/my_c_archive/my_c_archive.bzl +++ b/examples/my_c_archive/my_c_archive.bzl @@ -14,8 +14,8 @@ """Example showing how to create a rule that rules_cc can depend on.""" +load("@rules_cc//cc:action_names.bzl", "CPP_LINK_STATIC_LIBRARY_ACTION_NAME") load("@rules_cc//cc:toolchain_utils.bzl", "find_cpp_toolchain") -load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "CPP_LINK_STATIC_LIBRARY_ACTION_NAME") load("//examples/my_c_compile:my_c_compile.bzl", "MyCCompileInfo") def _my_c_archive_impl(ctx): diff --git a/examples/my_c_compile/my_c_compile.bzl b/examples/my_c_compile/my_c_compile.bzl index 5dacfbc..2b06751 100644 --- a/examples/my_c_compile/my_c_compile.bzl +++ b/examples/my_c_compile/my_c_compile.bzl @@ -14,8 +14,8 @@ """Example showing how to create a rule that just compiles C sources.""" +load("@rules_cc//cc:action_names.bzl", "C_COMPILE_ACTION_NAME") load("@rules_cc//cc:toolchain_utils.bzl", "find_cpp_toolchain") -load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "C_COMPILE_ACTION_NAME") MyCCompileInfo = provider(doc = "", fields = ["object"]) diff --git a/internal_deps.bzl b/internal_deps.bzl index fda3634..9df5826 100644 --- a/internal_deps.bzl +++ b/internal_deps.bzl @@ -14,15 +14,16 @@ """Dependencies that are needed for rules_cc tests and tools.""" -load("@bazel_federation//:repositories.bzl", "bazel_skylib", "protobuf", "rules_go", "rules_pkg") +load("@bazel_federation//:repositories.bzl", "bazel_skylib", "protobuf", "rules_go", "rules_pkg", "rules_python") load("@bazel_federation//:third_party_repositories.bzl", "abseil_py", "py_mock", "six", "zlib") def rules_cc_internal_deps(): - """Fetches all required dependencies for rules_cc tests and tools.""" + """Fetches all dependencies for rules_cc tests and tools.""" bazel_skylib() protobuf() rules_go() rules_pkg() + rules_python() abseil_py() py_mock() diff --git a/internal_setup.bzl b/internal_setup.bzl index beee607..48b269d 100644 --- a/internal_setup.bzl +++ b/internal_setup.bzl @@ -14,15 +14,19 @@ """Setup for rules_cc tests and tools.""" +load("@bazel_federation//setup:rules_pkg.bzl", "rules_pkg_setup") +load("@bazel_federation//setup:rules_python.bzl", "rules_python_setup") + # TODO(fweikert): Add setup.bzl file for skylib to the federation and load it instead of workspace.bzl load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") # TODO(fweikert): Also load rules_go's setup.bzl file from the federation once it exists load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") -load("@bazel_federation//setup:rules_pkg.bzl", "rules_pkg_setup") def rules_cc_internal_setup(): + """Setup of dependencies of tests and development-only tools used in rules_cc repository.""" bazel_skylib_workspace() go_rules_dependencies() go_register_toolchains() rules_pkg_setup() + rules_python_setup() diff --git a/renovate.json b/renovate.json index f45d8f1..35d60f6 100644 --- a/renovate.json +++ b/renovate.json @@ -1,5 +1,5 @@ { - "extends": [ - "config:base" - ] + "extends": [ + "config:base", + ], } diff --git a/third_party/BUILD b/third_party/BUILD index ded53b6..0c41157 100644 --- a/third_party/BUILD +++ b/third_party/BUILD @@ -1 +1 @@ -# Intentionally empty, only there to make //third_party a package. \ No newline at end of file +# Intentionally empty, only there to make //third_party a package. diff --git a/third_party/com/github/bazelbuild/bazel/src/main/protobuf/BUILD b/third_party/com/github/bazelbuild/bazel/src/main/protobuf/BUILD index b96bbb9..77f3a7a 100644 --- a/third_party/com/github/bazelbuild/bazel/src/main/protobuf/BUILD +++ b/third_party/com/github/bazelbuild/bazel/src/main/protobuf/BUILD @@ -2,6 +2,7 @@ licenses(["notice"]) # Apache 2.0 load("@com_google_protobuf//:protobuf.bzl", "py_proto_library") load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") +load("@rules_proto//proto:defs.bzl", "proto_library") py_proto_library( name = "crosstool_config_py_pb2", diff --git a/third_party/six.BUILD b/third_party/six.BUILD index a1b2f7b..19433c2 100644 --- a/third_party/six.BUILD +++ b/third_party/six.BUILD @@ -2,6 +2,8 @@ # Six provides simple utilities for wrapping over differences between Python 2 # and Python 3. +load("@rules_python//python:defs.bzl", "py_library") + licenses(["notice"]) # MIT exports_files(["LICENSE"]) diff --git a/tools/migration/BUILD b/tools/migration/BUILD index 887bc06..da0af7e 100644 --- a/tools/migration/BUILD +++ b/tools/migration/BUILD @@ -12,11 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -package(default_visibility = ["//visibility:public"]) - # Go rules load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") +# Python rules +load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") + +package(default_visibility = ["//visibility:public"]) + licenses(["notice"]) # Apache 2.0 py_binary( diff --git a/tools/runfiles/BUILD b/tools/runfiles/BUILD new file mode 100644 index 0000000..c68d138 --- /dev/null +++ b/tools/runfiles/BUILD @@ -0,0 +1,20 @@ +load("//cc:defs.bzl", "cc_library", "cc_test") + +licenses(["notice"]) + +cc_library( + name = "runfiles", + testonly = 1, + srcs = ["runfiles_src.cc"], + hdrs = ["runfiles_src.h"], + visibility = ["//visibility:public"], +) + +cc_test( + name = "runfiles_test", + srcs = ["runfiles_test.cc"], + deps = [ + ":runfiles", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/cc/private/toolchain/runfiles/runfiles_src.cc b/tools/runfiles/runfiles_src.cc similarity index 99% rename from cc/private/toolchain/runfiles/runfiles_src.cc rename to tools/runfiles/runfiles_src.cc index e1fb7f4..7c70797 100644 --- a/cc/private/toolchain/runfiles/runfiles_src.cc +++ b/tools/runfiles/runfiles_src.cc @@ -14,7 +14,7 @@ // The "srcs_for_embedded_tools" rule in the same package sets the line below to // include runfiles.h from the correct path. Do not modify the line below. -#include "cc/private/toolchain/runfiles/runfiles_src.h" +#include "tools/runfiles/runfiles_src.h" #ifdef _WIN32 #include diff --git a/cc/private/toolchain/runfiles/runfiles_src.h b/tools/runfiles/runfiles_src.h similarity index 100% rename from cc/private/toolchain/runfiles/runfiles_src.h rename to tools/runfiles/runfiles_src.h diff --git a/cc/private/toolchain/runfiles/runfiles_test.cc b/tools/runfiles/runfiles_test.cc similarity index 99% rename from cc/private/toolchain/runfiles/runfiles_test.cc rename to tools/runfiles/runfiles_test.cc index 36ef21f..238dcbc 100644 --- a/cc/private/toolchain/runfiles/runfiles_test.cc +++ b/tools/runfiles/runfiles_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "cc/private/toolchain/runfiles/runfiles_src.h" +#include "tools/runfiles/runfiles_src.h" #ifdef _WIN32 #include #endif // _WIN32