Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

msvc: fix make var for windows deps #1324

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions foreign_cc/configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions foreign_cc/make.bzl
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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():
Expand Down
9 changes: 6 additions & 3 deletions foreign_cc/private/configure_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 12 additions & 7 deletions foreign_cc/private/make_env_vars.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions foreign_cc/private/make_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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,
Expand Down