From bcd0ec409f383a26602d266c5f50523b28ee88a9 Mon Sep 17 00:00:00 2001 From: John Sun <79071267+jsun-splunk@users.noreply.github.com> Date: Tue, 19 Nov 2024 23:19:37 +1100 Subject: [PATCH] msvc: fix make var for windows deps (#1324) --- foreign_cc/configure.bzl | 5 +++++ foreign_cc/make.bzl | 5 +++++ foreign_cc/private/configure_script.bzl | 9 ++++++--- foreign_cc/private/make_env_vars.bzl | 19 ++++++++++++------- foreign_cc/private/make_script.bzl | 7 ++++--- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/foreign_cc/configure.bzl b/foreign_cc/configure.bzl index f001abf1..881d9443 100644 --- a/foreign_cc/configure.bzl +++ b/foreign_cc/configure.bzl @@ -2,6 +2,7 @@ build tool """ +load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") load( "//foreign_cc/private:cc_toolchain_util.bzl", "get_flags_info", @@ -82,6 +83,9 @@ def _create_configure_script(configureParameters): if xcompile_options: configure_options.extend(xcompile_options) + cc_toolchain = find_cpp_toolchain(ctx) + is_msvc = cc_toolchain.compiler == "msvc-cl" + configure = create_configure_script( workspace_name = ctx.workspace_name, tools = tools, @@ -108,6 +112,7 @@ def _create_configure_script(configureParameters): make_args = args, executable_ldflags_vars = ctx.attr.executable_ldflags_vars, shared_ldflags_vars = ctx.attr.shared_ldflags_vars, + is_msvc = is_msvc, ) return define_install_prefix + configure diff --git a/foreign_cc/make.bzl b/foreign_cc/make.bzl index 187f2ccc..48bf19af 100644 --- a/foreign_cc/make.bzl +++ b/foreign_cc/make.bzl @@ -1,5 +1,6 @@ """A rule for building projects using the [GNU Make](https://www.gnu.org/software/make/) build tool""" +load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") load( "//foreign_cc/private:cc_toolchain_util.bzl", "get_flags_info", @@ -66,6 +67,9 @@ def _create_make_script(configureParameters): install_prefix = ctx.attr.install_prefix, )) + cc_toolchain = find_cpp_toolchain(ctx) + is_msvc = cc_toolchain.compiler == "msvc-cl" + return create_make_script( workspace_name = ctx.workspace_name, tools = tools, @@ -81,6 +85,7 @@ def _create_make_script(configureParameters): make_install_prefix = ctx.attr.install_prefix, executable_ldflags_vars = ctx.attr.executable_ldflags_vars, shared_ldflags_vars = ctx.attr.shared_ldflags_vars, + is_msvc = is_msvc, ) def _attrs(): diff --git a/foreign_cc/private/configure_script.bzl b/foreign_cc/private/configure_script.bzl index ac385d71..9ef6bd69 100644 --- a/foreign_cc/private/configure_script.bzl +++ b/foreign_cc/private/configure_script.bzl @@ -28,7 +28,8 @@ def create_configure_script( make_targets, make_args, executable_ldflags_vars, - shared_ldflags_vars): + shared_ldflags_vars, + is_msvc): ext_build_dirs = inputs.ext_build_dirs script = pkgconfig_script(ext_build_dirs) @@ -73,15 +74,17 @@ def create_configure_script( ).lstrip()) script.append("##mkdirs## $$BUILD_TMPDIR$$/$$INSTALL_PREFIX$$") + + make_commands = [] script.append("{env_vars} {prefix}\"{configure}\" {prefix_flag}$$BUILD_TMPDIR$$/$$INSTALL_PREFIX$$ {user_options}".format( - env_vars = get_make_env_vars(workspace_name, tools, flags, env_vars, deps, inputs), + env_vars = get_make_env_vars(workspace_name, tools, flags, env_vars, deps, inputs, is_msvc, make_commands), prefix = configure_prefix, configure = configure_path, prefix_flag = prefix_flag, user_options = " ".join(user_options), )) - ldflags_make_vars = get_ldflags_make_vars(executable_ldflags_vars, shared_ldflags_vars, workspace_name, flags, env_vars, deps, inputs) + ldflags_make_vars = get_ldflags_make_vars(executable_ldflags_vars, shared_ldflags_vars, workspace_name, flags, env_vars, deps, inputs, is_msvc) make_commands = [] for target in make_targets: diff --git a/foreign_cc/private/make_env_vars.bzl b/foreign_cc/private/make_env_vars.bzl index d2d1912c..19140128 100644 --- a/foreign_cc/private/make_env_vars.bzl +++ b/foreign_cc/private/make_env_vars.bzl @@ -11,9 +11,10 @@ def get_make_env_vars( user_vars, deps, inputs, - make_commands = []): + is_msvc, + make_commands): vars = _get_make_variables(workspace_name, tools, flags, user_vars, make_commands) - deps_flags = _define_deps_flags(deps, inputs) + deps_flags = _define_deps_flags(deps, inputs, is_msvc) # For cross-compilation. if "RANLIB" not in vars.keys(): @@ -42,27 +43,31 @@ def get_ldflags_make_vars( flags, user_vars, deps, - inputs): + inputs, + is_msvc): vars = _get_ldflags_vars(executable_ldflags_vars, shared_ldflags_vars, flags, user_vars) - deps_flags = _define_deps_flags(deps, inputs) + deps_flags = _define_deps_flags(deps, inputs, is_msvc) for key in vars.keys(): vars[key] = vars[key] + deps_flags.libs return " ".join(["{}=\"{}\"" .format(key, _join_flags_list(workspace_name, vars[key])) for key in vars]) -def _define_deps_flags(deps, inputs): +def _define_deps_flags(deps, inputs, is_msvc): # It is very important to keep the order for the linker => put them into list lib_dirs = [] + # msvc compiler uses -LIBPATH instead of -L + lib_flag = "-LIBPATH:" if is_msvc else "-L" + # Here go libraries built with Bazel gen_dirs_set = {} for lib in inputs.libs: dir_ = lib.dirname if not gen_dirs_set.get(dir_): gen_dirs_set[dir_] = 1 - lib_dirs.append("-L$$EXT_BUILD_ROOT$$/" + dir_) + lib_dirs.append(lib_flag + "$$EXT_BUILD_ROOT$$/" + dir_) include_dirs_set = {} for include_dir in inputs.include_dirs: @@ -89,7 +94,7 @@ def _define_deps_flags(deps, inputs): dir_name = artifact.gen_dir.basename include_dirs.append("-I$$EXT_BUILD_DEPS$$/{}/{}".format(dir_name, artifact.include_dir_name)) - lib_dirs.append("-L$$EXT_BUILD_DEPS$$/{}/{}".format(dir_name, artifact.lib_dir_name)) + lib_dirs.append(lib_flag + "$$EXT_BUILD_DEPS$$/{}/{}".format(dir_name, artifact.lib_dir_name)) return struct( libs = lib_dirs, diff --git a/foreign_cc/private/make_script.bzl b/foreign_cc/private/make_script.bzl index 5c86e8da..0dd6126f 100644 --- a/foreign_cc/private/make_script.bzl +++ b/foreign_cc/private/make_script.bzl @@ -17,7 +17,8 @@ def create_make_script( make_args, make_install_prefix, executable_ldflags_vars, - shared_ldflags_vars): + shared_ldflags_vars, + is_msvc): ext_build_dirs = inputs.ext_build_dirs script = pkgconfig_script(ext_build_dirs) @@ -26,7 +27,7 @@ def create_make_script( script.append("##enable_tracing##") - ldflags_make_vars = get_ldflags_make_vars(executable_ldflags_vars, shared_ldflags_vars, workspace_name, flags, env_vars, deps, inputs) + ldflags_make_vars = get_ldflags_make_vars(executable_ldflags_vars, shared_ldflags_vars, workspace_name, flags, env_vars, deps, inputs, is_msvc) make_commands = [] for target in make_targets: @@ -39,7 +40,7 @@ def create_make_script( install_prefix = make_install_prefix, )) - configure_vars = get_make_env_vars(workspace_name, tools, flags, env_vars, deps, inputs, make_commands) + configure_vars = get_make_env_vars(workspace_name, tools, flags, env_vars, deps, inputs, is_msvc, make_commands) script.extend(["{env_vars} {command}".format( env_vars = configure_vars,