From b0935ee201e602db6020b4873d9346cd1325e763 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Mon, 21 Aug 2023 00:26:27 -0700 Subject: [PATCH] fix: preserve path override behavior for old rules Signed-off-by: Sam Gammon --- internal/native_image/rules.bzl | 80 ++++++++++++++++++++++++++++++--- tools/bazel/macos.bazelrc | 3 +- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/internal/native_image/rules.bzl b/internal/native_image/rules.bzl index 8e2511da..563a4bdb 100644 --- a/internal/native_image/rules.bzl +++ b/internal/native_image/rules.bzl @@ -111,11 +111,78 @@ def _graal_binary_implementation(ctx): action_name = CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME, ) - tool_paths = [c_compiler_path, ld_executable_path, ld_static_lib_path, ld_dynamic_lib_path] - path_set = {} - for tool_path in tool_paths: - tool_dir, _, _ = tool_path.rpartition("/") - path_set[tool_dir] = None + env = {} + use_shell_env = True + if ctx.attr._legacy_rule: + tool_paths = [c_compiler_path, ld_executable_path, ld_static_lib_path, ld_dynamic_lib_path] + path_set = {} + for tool_path in tool_paths: + tool_dir, _, _ = tool_path.rpartition("/") + path_set[tool_dir] = None + + paths = sorted(path_set.keys()) + use_shell_env = False + if ctx.configuration.host_path_separator == ":": + # HACK: ":" is a proxy for a UNIX-like host. + # The tools returned above may be bash scripts that reference commands + # in directories we might not otherwise include. For example, + # on macOS, wrapped_ar calls dirname. + if "/bin" not in path_set: + paths.append("/bin") + if "/usr/bin" not in path_set: + paths.append("/usr/bin") + + # fix: make sure to include VS install dir on windows + if "VSINSTALLDIR" in ctx.configuration.default_shell_env: + vs_install = ctx.configuration.default_shell_env["VSINSTALLDIR"] + if vs_install != None and len(vs_install) > 0: + env["VSINSTALLDIR"] = vs_install + + # fix: make sure to include SDKROOT on macos + if "SDKROOT" in ctx.configuration.default_shell_env: + sdkroot = ctx.configuration.default_shell_env["SDKROOT"] + if sdkroot != None and len(sdkroot) > 0: + env["SDKROOT"] = sdkroot + + # fix: make sure to include SDKROOT on macos + if "DEVELOPER_DIR" in ctx.configuration.default_shell_env: + devdir = ctx.configuration.default_shell_env["DEVELOPER_DIR"] + if devdir != None and len(devdir) > 0: + env["DEVELOPER_DIR"] = devdir + + # seal paths with hack above + env["PATH"] = ctx.configuration.host_path_separator.join(paths) + paths = sorted(path_set.keys()) + if ctx.configuration.host_path_separator == ":": + # HACK: ":" is a proxy for a UNIX-like host. + # The tools returned above may be bash scripts that reference commands + # in directories we might not otherwise include. For example, + # on macOS, wrapped_ar calls dirname. + if "/bin" not in path_set: + paths.append("/bin") + if "/usr/bin" not in path_set: + paths.append("/usr/bin") + + # fix: make sure to include VS install dir on windows + if "VSINSTALLDIR" in ctx.configuration.default_shell_env: + vs_install = ctx.configuration.default_shell_env["VSINSTALLDIR"] + if vs_install != None and len(vs_install) > 0: + env["VSINSTALLDIR"] = vs_install + + # fix: make sure to include SDKROOT on macos + if "SDKROOT" in ctx.configuration.default_shell_env: + sdkroot = ctx.configuration.default_shell_env["SDKROOT"] + if sdkroot != None and len(sdkroot) > 0: + env["SDKROOT"] = sdkroot + + # fix: make sure to include SDKROOT on macos + if "DEVELOPER_DIR" in ctx.configuration.default_shell_env: + devdir = ctx.configuration.default_shell_env["DEVELOPER_DIR"] + if devdir != None and len(devdir) > 0: + env["DEVELOPER_DIR"] = devdir + + # seal paths with hack above + env["PATH"] = ctx.configuration.host_path_separator.join(paths) # seal paths with hack above out_bin_name = ctx.attr.default_executable_name.replace("%target%", ctx.attr.name) @@ -171,7 +238,8 @@ def _graal_binary_implementation(ctx): "arguments": [args], "executable": graal, "mnemonic": "NativeImage", - "use_default_shell_env": True, + "use_default_shell_env": use_shell_env, + "env": env, # "toolchain": Label(_NATIVE_IMAGE_TOOLCHAIN_TYPE), } diff --git a/tools/bazel/macos.bazelrc b/tools/bazel/macos.bazelrc index b2fc9b31..35e96b00 100644 --- a/tools/bazel/macos.bazelrc +++ b/tools/bazel/macos.bazelrc @@ -1,2 +1 @@ -build:mac --action_env=DEVELOPER_DIR -build:mac --action_env=SDKROOT +build:mac --spawn_strategy=worker,sandboxed,local