Skip to content

Commit

Permalink
Build apr for MSVC on Windows (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
jheaff1 authored Aug 5, 2021
1 parent 3b72ab3 commit f01fd35
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/third_party/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ test_suite(
name = "windows_tests",
tags = ["manual"],
tests = [
"//apr:apr_build_test",
"//curl:curl_test_suite",
"//openssl:openssl_test_suite",
# TODO: Add more windows tests
Expand Down
33 changes: 31 additions & 2 deletions examples/third_party/apr/BUILD.apr.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make", "make_variant")

LIB_NAME = "apr"

filegroup(
name = "all_srcs",
srcs = glob(["**"]),
)

configure_make(
alias(
name = "apr",
actual = select({
"@rules_foreign_cc_examples//:msvc_compiler": "apr_msvc",
"//conditions:default": "apr_default",
}),
visibility = ["//visibility:public"],
)

make_variant(
name = "apr_msvc",
args = [
"-F Makefile.win",
"ARCH=\"x64 Release\"",
],
lib_name = LIB_NAME,
lib_source = ":all_srcs",
out_static_libs = ["apr-1.lib"],
targets = [
"buildall",
"install",
],
toolchain = "@rules_foreign_cc//toolchains:preinstalled_nmake_toolchain",
visibility = ["//visibility:public"],
)

configure_make(
name = "apr_default",
configure_in_place = True,
configure_options = [
"--disable-shared",
Expand All @@ -15,6 +43,7 @@ configure_make(
"@platforms//os:macos": {"AR": ""},
"//conditions:default": {},
}),
lib_name = LIB_NAME,
lib_source = ":all_srcs",
out_static_libs = ["libapr-1.a"],
visibility = ["//visibility:public"],
Expand Down
4 changes: 3 additions & 1 deletion examples/third_party/apr/apr_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ def apr_repositories():
patches = [
# https://bz.apache.org/bugzilla/show_bug.cgi?id=50146
Label("//apr:macos_iovec.patch"),
#https://bz.apache.org/bugzilla/show_bug.cgi?id=64753
# https://bz.apache.org/bugzilla/show_bug.cgi?id=64753
Label("//apr:macos_pid_t.patch"),
# https://apachelounge.com/viewtopic.php?t=8260
Label("//apr:windows_winnt.patch"),
],
sha256 = "48e9dbf45ae3fdc7b491259ffb6ccf7d63049ffacbc1c0977cced095e4c2d5a2",
strip_prefix = "apr-1.7.0",
Expand Down
11 changes: 11 additions & 0 deletions examples/third_party/apr/windows_winnt.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- include/apr.hw 2019-03-21 00:41:37.000000000 +0100
+++ include/apr.hw 2019-03-21 00:41:37.000000000 +0100
@@ -86,7 +86,7 @@

/* Restrict the server to a subset of Windows XP header files by default
*/
-#define _WIN32_WINNT 0x0501
+#define _WIN32_WINNT 0x0600
#endif
#ifndef NOUSER
#define NOUSER
15 changes: 0 additions & 15 deletions foreign_cc/built_tools/private/built_tools_framework.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""A module defining a common framework for "built_tools" rules"""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("//foreign_cc/private:detect_root.bzl", "detect_root")
load("//foreign_cc/private:framework.bzl", "get_env_prelude", "wrap_outputs")
load("//foreign_cc/private/framework:helpers.bzl", "convert_shell_script", "shebang")
load("//foreign_cc/private/framework:platform.bzl", "os_name")

# Common attributes for all built_tool rules
FOREIGN_CC_BUILT_TOOLS_ATTRS = {
Expand Down Expand Up @@ -60,24 +58,11 @@ def built_tool_rule_impl(ctx, script_lines, out_dir, mnemonic):

cc_toolchain = find_cpp_toolchain(ctx)

path_prepend_cmd = ""
if "win" in os_name(ctx):
# 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)

# Change prefix of linker path from Windows style to Unix style, required by MSYS. E.g. change "C:" to "/c"
if linker_path[0].isalpha() and linker_path[1] == ":":
linker_path = linker_path.replace(linker_path[0:2], "/" + linker_path[0].lower())

# MSYS requires pahts containing whitespace to be wrapped in quotation marks
path_prepend_cmd = "export PATH=\"" + linker_path + "\":$PATH"

script = env_prelude + [
"##script_prelude##",
"export EXT_BUILD_ROOT=##pwd##",
"export INSTALLDIR=$$EXT_BUILD_ROOT$$/{}".format(out_dir.path),
"export BUILD_TMPDIR=$$INSTALLDIR$$.build_tmpdir",
path_prepend_cmd,
"##mkdirs## $$BUILD_TMPDIR$$",
"##copy_dir_contents_to_dir## ./{} $$BUILD_TMPDIR$$".format(root),
"cd $$BUILD_TMPDIR$$",
Expand Down
17 changes: 14 additions & 3 deletions foreign_cc/private/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ def get_env_prelude(ctx, lib_name, data_dependencies, target_root):
cc_env = _correct_path_variable(get_env_vars(ctx))
env.update(cc_env)

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)
env.update({"PATH": _normalize_path(linker_path) + ":" + env.get("PATH")})

# Add all user defined variables
user_vars = expand_locations(ctx, ctx.attr.env, data_dependencies)
env.update(user_vars)
Expand Down Expand Up @@ -592,13 +598,18 @@ def _print_env():
"##echo## \"__________________________\"",
]

def _normalize_path(path):
# Change Windows style paths to Unix style. E.g. change "C:" to "/c"
if path[0].isalpha() and path[1] == ":":
path = path.replace(path[0:2], "/" + path[0].lower())

return path.replace("\\", "/").replace(";", ":")

def _correct_path_variable(env):
value = env.get("PATH", "")
if not value:
return env
value = env.get("PATH", "").replace("C:\\", "/c/")
value = value.replace("\\", "/")
value = value.replace(";", ":")
value = _normalize_path(env.get("PATH", ""))
env["PATH"] = "$PATH:" + value
return env

Expand Down

0 comments on commit f01fd35

Please sign in to comment.