Skip to content

Commit

Permalink
Change rules_foreign_cc_dependencies so that by default the built
Browse files Browse the repository at this point in the history
pkgconfig toolchain is not registered, as a few extra bazel flags are
required on Windows.
  • Loading branch information
jheaff1 committed Nov 4, 2022
1 parent 4192919 commit d111d89
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 10 deletions.
2 changes: 0 additions & 2 deletions .bazelci/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ tasks:
- "-//cmake_with_data/..."
build_targets: *windows_targets
test_targets: *windows_targets
test_flags:
- "--enable_runfiles"
rbe_ubuntu1604_flags:
name: Flags
platform: rbe_ubuntu1604
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ workspace(name = "rules_foreign_cc")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

rules_foreign_cc_dependencies()
rules_foreign_cc_dependencies(register_built_pkgconfig_toolchain=True)

local_repository(
name = "rules_foreign_cc_examples",
Expand Down
8 changes: 8 additions & 0 deletions examples/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# Required for Windows
build --enable_runfiles

# These are required otherwise paths are too long
startup --windows_enable_symlinks
build --action_env=MSYS=winsymlinks:nativestrict
test --action_env=MSYS=winsymlinks:nativestrict
3 changes: 1 addition & 2 deletions examples/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ local_repository(

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

# Don't use preinstalled tools to ensure builds are as hermetic as possible
rules_foreign_cc_dependencies(register_preinstalled_tools = False)
rules_foreign_cc_dependencies(register_built_pkgconfig_toolchain=True)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

Expand Down
2 changes: 1 addition & 1 deletion examples/third_party/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local_repository(

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

rules_foreign_cc_dependencies()
rules_foreign_cc_dependencies(register_built_pkgconfig_toolchain=True)

local_repository(
name = "rules_foreign_cc_examples",
Expand Down
12 changes: 11 additions & 1 deletion foreign_cc/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def rules_foreign_cc_dependencies(
pkgconfig_version = "0.29.2",
register_preinstalled_tools = True,
register_built_tools = True,
register_toolchains = True):
register_toolchains = True,
register_built_pkgconfig_toolchain = False):
"""Call this function from the WORKSPACE file to initialize rules_foreign_cc \
dependencies and let neccesary code generation happen \
(Code generation is needed to support different variants of the C++ Starlark API.).
Expand Down Expand Up @@ -47,6 +48,14 @@ def rules_foreign_cc_dependencies(
register_built_tools: If true, toolchains that build the tools from source are registered
register_toolchains: If true, registers the toolchains via native.register_toolchains. Used by bzlmod
register_built_pkgconfig_toolchain: If true, the built pkgconfig toolchain will be registered. On Windows it may be preferrable to set this to False, as
this requires the --enable_runfiles bazel option. Also note that building pkgconfig from source under bazel results in paths that are more
than 256 characters long, which will not work on Windows unless the following options are added to the .bazelrc and symlinks are enabled in Windows.
startup --windows_enable_symlinks -> This is required to enable symlinking to avoid long runfile paths
build --action_env=MSYS=winsymlinks:nativestrict -> This is required to enable symlinking to avoid long runfile paths
startup --output_user_root=C:/b -> This is required to keep paths as short as possible
"""

register_framework_toolchains(register_toolchains = register_toolchains)
Expand All @@ -64,6 +73,7 @@ def rules_foreign_cc_dependencies(
ninja_version = ninja_version,
pkgconfig_version = pkgconfig_version,
register_toolchains = register_toolchains,
register_built_pkgconfig_toolchain = register_built_pkgconfig_toolchain,
)

if register_preinstalled_tools:
Expand Down
10 changes: 9 additions & 1 deletion foreign_cc/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ def runnable_binary(name, binary, foreign_cc_target, match_binary_name = False,

tags = kwargs.pop("tags", [])

# filegroups cannot select on constraint_values in before Bazel 5.1. Add this config_setting as a workaround. See https://github.com/bazelbuild/bazel/issues/13047
native.config_setting(
name = "windows_config_setting",
constraint_values = [
"@platforms//os:windows",
],
)

native.filegroup(
name = name + "_fg",
srcs = [foreign_cc_target],
tags = tags + ["manual"],
output_group = select({
"@platforms//os:windows": binary + ".exe",
":windows_config_setting": binary + ".exe",
"//conditions:default": binary,
}),
)
Expand Down
6 changes: 4 additions & 2 deletions toolchains/built_toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ _CMAKE_SRCS = {
}

# buildifier: disable=unnamed-macro
def built_toolchains(cmake_version, make_version, ninja_version, pkgconfig_version, register_toolchains):
def built_toolchains(cmake_version, make_version, ninja_version, pkgconfig_version, register_toolchains, register_built_pkgconfig_toolchain):
"""Register toolchains for built tools that will be built from source"""
_cmake_toolchain(cmake_version, register_toolchains)
_make_toolchain(make_version, register_toolchains)
_ninja_toolchain(ninja_version, register_toolchains)
_pkgconfig_toolchain(pkgconfig_version, register_toolchains)

if register_built_pkgconfig_toolchain:
_pkgconfig_toolchain(pkgconfig_version, register_toolchains)

def _cmake_toolchain(version, register_toolchains):
if register_toolchains:
Expand Down

0 comments on commit d111d89

Please sign in to comment.