Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into toolchain_registration
Browse files Browse the repository at this point in the history
  • Loading branch information
jsharpe committed Jun 14, 2024
2 parents 7784be4 + d48f8b5 commit bf9f34b
Show file tree
Hide file tree
Showing 7 changed files with 770 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tools.meson(version = "1.1.1")

use_repo(tools, "meson_1.1.1_src")

bazel_dep(name = "platforms", version = "0.0.7")
bazel_dep(name = "platforms", version = "0.0.9")
bazel_dep(name = "rules_swift", version = "1.17.0", repo_name = "build_bazel_rules_swift")
bazel_dep(name = "rules_apple", version = "3.4.0", repo_name = "build_bazel_rules_apple")
bazel_dep(name = "apple_support", version = "1.14.0", repo_name = "build_bazel_apple_support")
Expand Down
65 changes: 64 additions & 1 deletion foreign_cc/built_tools/pkgconfig_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,74 @@ load(
"FOREIGN_CC_BUILT_TOOLS_ATTRS",
"FOREIGN_CC_BUILT_TOOLS_FRAGMENTS",
"FOREIGN_CC_BUILT_TOOLS_HOST_FRAGMENTS",
"absolutize",
"built_tool_rule_impl",
)
load(
"//foreign_cc/private:cc_toolchain_util.bzl",
"get_env_vars",
"get_flags_info",
"get_tools_info",
)
load("//foreign_cc/private/framework:platform.bzl", "os_name")
load("//toolchains/native_tools:tool_access.bzl", "get_make_data")

def _pkgconfig_tool_impl(ctx):
env = get_env_vars(ctx)
flags_info = get_flags_info(ctx)
tools_info = get_tools_info(ctx)

ar_path = tools_info.cxx_linker_static
frozen_arflags = flags_info.cxx_linker_static

cc_path = tools_info.cc
cflags = flags_info.cc + ["-Wno-int-conversion"] # Fix building with clang 15+
sysroot_cflags = [flag for flag in cflags if flag.startswith("--sysroot=")]
non_sysroot_cflags = [flag for flag in cflags if not flag.startswith("--sysroot=")]

ld_path = tools_info.cxx_linker_executable
ldflags = flags_info.cxx_linker_executable
sysroot_ldflags = [flag for flag in ldflags if flag.startswith("--sysroot=")]
non_sysroot_ldflags = [flag for flag in ldflags if not flag.startswith("--sysroot=")]

# Make's build script does not forward CFLAGS to all compiler and linker
# invocations, so we append --sysroot flags directly to CC and LD.
absolute_cc = absolutize(ctx.workspace_name, cc_path, True)
if sysroot_cflags:
absolute_cc += " " + _join_flags_list(ctx.workspace_name, sysroot_cflags)
absolute_ld = absolutize(ctx.workspace_name, ld_path, True)
if sysroot_ldflags:
absolute_ld += " " + _join_flags_list(ctx.workspace_name, sysroot_ldflags)

# If libtool is used as AR, the output file has to be prefixed with
# "-o". Since the make Makefile only uses ar-style invocations, the
# output file always comes first and we can append this argument to the
# flags list.
absolute_ar = absolutize(ctx.workspace_name, ar_path, True)

if os_name(ctx) == "macos":
absolute_ar = ""
non_sysroot_ldflags += ["-undefined", "error"]

arflags = [e for e in frozen_arflags]
if absolute_ar == "libtool" or absolute_ar.endswith("/libtool"):
arflags.append("-o")

make_data = get_make_data(ctx)

env.update({
"AR": absolute_ar,
"ARFLAGS": _join_flags_list(ctx.workspace_name, arflags),
"CC": absolute_cc,
"CFLAGS": _join_flags_list(ctx.workspace_name, non_sysroot_cflags),
"LD": absolute_ld,
"LDFLAGS": _join_flags_list(ctx.workspace_name, non_sysroot_ldflags),
"MAKE": make_data.path,
})

configure_env = " ".join(["%s=\"%s\"" % (key, value) for key, value in env.items()])
script = [
"./configure --with-internal-glib --prefix=$$INSTALLDIR$$",
"%s ./configure --with-internal-glib --prefix=$$INSTALLDIR$$" % configure_env,
"%s" % make_data.path,
"%s install" % make_data.path,
]
Expand Down Expand Up @@ -109,3 +169,6 @@ def pkgconfig_tool(name, srcs, **kwargs):
match_binary_name = True,
tags = tags,
)

def _join_flags_list(workspace_name, flags):
return " ".join([absolutize(workspace_name, flag) for flag in flags])
40 changes: 35 additions & 5 deletions foreign_cc/private/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ def get_env_prelude(ctx, lib_name, data_dependencies, target_root):
env = dict()

# Add all environment variables from the cc_toolchain
cc_env = _correct_path_variable(get_env_vars(ctx))
cc_toolchain = find_cpp_toolchain(ctx)
cc_env = _correct_path_variable(cc_toolchain, get_env_vars(ctx))
env.update(cc_env)

# This logic mirrors XcodeLocalEnvProvider#querySdkRoot in bazel itself
Expand All @@ -326,17 +327,25 @@ def get_env_prelude(ctx, lib_name, data_dependencies, target_root):
user_vars = expand_locations_and_make_variables(ctx, ctx.attr.env, "env", data_dependencies)
env.update(user_vars)

if cc_toolchain.compiler == "msvc-cl":
if "PATH" in user_vars and "$$EXT_BUILD_ROOT$$" in user_vars["PATH"]:
# Convert any $$EXT_BUILD_ROOT$$ in PATH to /${EXT_BUILD_ROOT/$(printf '\072')/}.
# This is because PATH needs to be in unix format for MSYS2.
user_vars["PATH"] = user_vars["PATH"].replace("$$EXT_BUILD_ROOT$$", "/$${EXT_BUILD_ROOT/$$$(printf '\072')/}")

# If user has defined a PATH variable (e.g. PATH, LD_LIBRARY_PATH, CPATH) prepend it to the existing variable
for user_var in user_vars:
is_existing_var = "PATH" in user_var or _is_msvc_var(user_var)
list_delimiter = ";" if _is_msvc_var(user_var) else ":"
if is_existing_var and cc_env.get(user_var):
env.update({user_var: user_vars.get(user_var) + list_delimiter + cc_env.get(user_var)})

cc_toolchain = find_cpp_toolchain(ctx)
if cc_toolchain.compiler == "msvc-cl":
# Prepend PATH environment variable with the path to the toolchain linker, which prevents MSYS using its linker (/usr/bin/link.exe) rather than the MSVC linker (both are named "link.exe")
linker_path = paths.dirname(cc_toolchain.ld_executable)
if linker_path[1] != ":":
linker_path = "${EXT_BUILD_ROOT/$(printf '\072')/}/" + linker_path

env.update({"PATH": _normalize_path(linker_path) + ":" + env.get("PATH")})

env_snippet.extend(["export {}=\"{}\"".format(key, escape_dquote_bash(val)) for key, val in env.items()])
Expand Down Expand Up @@ -662,12 +671,32 @@ def _print_env():

def _normalize_path(path):
# Change Windows style paths to Unix style.
if path[0].isalpha() and path[1] == ":":
if path[0].isalpha() and path[1] == ":" or path[0] == "$":
# Change "c:\foo;d:\bar" to "/c/foo:/d/bar
return "/" + path.replace("\\", "/").replace(":/", "/").replace(";", ":/")
return path.replace("\\", "/").replace(";", ":")

def _correct_path_variable(env):
def _correct_path_variable(toolchain, env):
if toolchain.compiler == "msvc-cl":
# Workaround for msvc toolchain to prefix relative paths with $EXT_BUILD_ROOT
corrected_env = dict()
for key, value in env.items():
corrected_env[key] = value
if _is_msvc_var(key) or key == "PATH":
if key == "PATH":
# '\072' is ':'. This is unsightly but we cannot use the ':' character
# because we do a search and replace later on. This is required because
# we need PATH to be all unix path (for MSYS2) where as other env (e.g.
# INCLUDE) needs windows path (for passing as arguments to compiler).
prefix = "${EXT_BUILD_ROOT/$(printf '\072')/}/"
else:
prefix = "$EXT_BUILD_ROOT/"

# external/path becomes $EXT_BUILD_ROOT/external/path
path_paths = [prefix + path if path and path[1] != ":" else path for path in value.split(";")]
corrected_env[key] = ";".join(path_paths)
env = corrected_env

value = env.get("PATH", "")
if not value:
return env
Expand Down Expand Up @@ -897,7 +926,8 @@ def get_foreign_cc_dep(dep):
# consider optimization here to do not iterate both collections
def _get_headers(compilation_info):
include_dirs = compilation_info.system_includes.to_list() + \
compilation_info.includes.to_list()
compilation_info.includes.to_list() + \
getattr(compilation_info, "external_includes", depset()).to_list()

# do not use quote includes, currently they do not contain
# library-specific information
Expand Down
20 changes: 19 additions & 1 deletion foreign_cc/private/framework/toolchains/windows_commands.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def script_extension():
return ".sh"

def pwd():
return "$(type -t cygpath > /dev/null && cygpath $(pwd) -w || pwd -W)"
return "$(type -t cygpath > /dev/null && cygpath $(pwd) -m || pwd -W)"

def echo(text):
return "echo \"{text}\"".format(text = text)
Expand All @@ -27,8 +27,26 @@ def env():
return "env"

def path(expression):
# In windows we cannot add vars like $$EXT_BUILD_DEPS$$ directly to PATH as PATH is
# required to be in unix format. This implementation also assumes that the vars
# like $$EXT_BUILD_DEPS$$ are exported to the ENV.
expression = _path_var_expansion(expression)
return "export PATH=\"{expression}:$PATH\"".format(expression = expression)

def _path_var_expansion(expression):
result = []
parts = expression.split("$$")
if len(parts) < 3:
return expression

for index, part in enumerate(parts):
if index % 2 == 0:
result.append(part)
else:
result.append("${" + part + "/:/}")

return "/" + "".join(result)

def touch(path):
return "touch " + path

Expand Down
35 changes: 35 additions & 0 deletions toolchains/cmake_versions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,20 @@ CMAKE_SRCS = {
"cmake-3.28.4",
"eb9c787e078848dc493f4f83f8a4bbec857cd1f38ab6425ce8d2776a9f6aa6fb",
],
"3.28.5": [
[
"https://github.com/Kitware/CMake/releases/download/v3.28.5/cmake-3.28.5.tar.gz",
],
"cmake-3.28.5",
"a75d3487ffe817e116a2bf668bf1434af7fe5864cbd4c7e3dcf33dd1a470d659",
],
"3.28.6": [
[
"https://github.com/Kitware/CMake/releases/download/v3.28.6/cmake-3.28.6.tar.gz",
],
"cmake-3.28.6",
"c39c733900affc4eb0e9688b4d1a45435a732105d9bf9cc1e75dd2b9b81a36bb",
],
"3.29.0": [
[
"https://github.com/Kitware/CMake/releases/download/v3.29.0/cmake-3.29.0.tar.gz",
Expand All @@ -552,6 +566,27 @@ CMAKE_SRCS = {
"cmake-3.29.2",
"36db4b6926aab741ba6e4b2ea2d99c9193222132308b4dc824d4123cb730352e",
],
"3.29.3": [
[
"https://github.com/Kitware/CMake/releases/download/v3.29.3/cmake-3.29.3.tar.gz",
],
"cmake-3.29.3",
"252aee1448d49caa04954fd5e27d189dd51570557313e7b281636716a238bccb",
],
"3.29.4": [
[
"https://github.com/Kitware/CMake/releases/download/v3.29.4/cmake-3.29.4.tar.gz",
],
"cmake-3.29.4",
"b1b48d7100bdff0b46e8c8f6a3c86476dbe872c8df39c42b8d104298b3d56a2c",
],
"3.29.5": [
[
"https://github.com/Kitware/CMake/releases/download/v3.29.5/cmake-3.29.5.tar.gz",
],
"cmake-3.29.5",
"dd63da7d763c0db455ca232f2c443f5234fe0b11f8bd6958a81d29cc987dfd6e",
],
"3.3.2": [
[
"https://github.com/Kitware/CMake/releases/download/v3.3.2/cmake-3.3.2.tar.gz",
Expand Down
Loading

0 comments on commit bf9f34b

Please sign in to comment.