Skip to content

Commit

Permalink
Merge branch 'main' into jsun-configurable-ldflags
Browse files Browse the repository at this point in the history
  • Loading branch information
jsun-splunk authored Nov 3, 2024
2 parents 41e5e1a + 77d4483 commit 47078b0
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This is **not an officially supported Google product**

## Documentation

Documentation for all rules and providers are available at: https://bazelbuild.github.io/rules_foreign_cc/
Documentation for all rules and providers are available at: https://bazel-contrib.github.io/rules_foreign_cc/

## Bazel versions compatibility

Expand Down
2 changes: 0 additions & 2 deletions examples/third_party/curl/BUILD.curl.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ _CACHE_ENTRIES = {
"BUILD_CURL_EXE": "off",
"BUILD_SHARED_LIBS": "off",
"CMAKE_BUILD_TYPE": "RELEASE",
"CMAKE_PREFIX_PATH": "$$EXT_BUILD_DEPS/openssl",
"CMAKE_USE_OPENSSL": "on",
# TODO: ldap should likely be enabled
"CURL_DISABLE_LDAP": "on",
"OPENSSL_ROOT_DIR": "$$EXT_BUILD_DEPS/openssl",
}

_MACOS_CACHE_ENTRIES = dict(_CACHE_ENTRIES.items() + {
Expand Down
1 change: 0 additions & 1 deletion examples/third_party/libgit2/BUILD.libgit2.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ _CACHE_ENTRIES = {
"BUILD_EXAMPLES": "off",
"BUILD_FUZZERS": "off",
"BUILD_SHARED_LIBS": "off",
"CMAKE_PREFIX_PATH": "$$EXT_BUILD_DEPS/pcre;$$EXT_BUILD_DEPS/openssl;$$EXT_BUILD_DEPS/libssh2;$$EXT_BUILD_DEPS/zlib;$${CMAKE_PREFIX_PATH:-}",
#"EMBED_SSH_PATH": "$(location @libssh2//:libssh2)",
"USE_HTTPS": "on",
}
Expand Down
1 change: 0 additions & 1 deletion examples/third_party/libssh2/BUILD.libssh2.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ _CACHE_ENTRIES = {
"BUILD_SHARED_LIBS": "off",
"BUILD_TESTING": "off",
"CMAKE_FIND_DEBUG_MODE": "on",
"CMAKE_PREFIX_PATH": "$${CMAKE_PREFIX_PATH:-};$$EXT_BUILD_DEPS/openssl",
}

_LINUX_CACHE_ENTRIES = dict(_CACHE_ENTRIES.items() + {
Expand Down
5 changes: 4 additions & 1 deletion foreign_cc/built_tools/pkgconfig_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ def _pkgconfig_tool_impl(ctx):
"%s install" % make_data.path,
]

additional_tools = depset(transitive = [make_data.target.files])
if make_data.target:
additional_tools = depset(transitive = [make_data.target.files])
else:
additional_tools = depset()

return built_tool_rule_impl(
ctx,
Expand Down
16 changes: 10 additions & 6 deletions foreign_cc/built_tools/private/built_tools_framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,13 @@ def built_tool_rule_impl(ctx, script_lines, out_dir, mnemonic, additional_tools

root = detect_root(ctx.attr.srcs)
lib_name = ctx.attr.name
env_prelude = get_env_prelude(ctx, lib_name, [], "")
env_prelude = get_env_prelude(ctx, out_dir.path, [])

cc_toolchain = find_cpp_toolchain(ctx)

script = env_prelude + [
script = [
"##script_prelude##",
"export EXT_BUILD_ROOT=##pwd##",
"export INSTALLDIR=$$EXT_BUILD_ROOT$$/{}".format(out_dir.path),
"export BUILD_TMPDIR=$$INSTALLDIR$$.build_tmpdir",
] + env_prelude + [
"##rm_rf## $$INSTALLDIR$$",
"##rm_rf## $$BUILD_TMPDIR$$",
"##mkdirs## $$INSTALLDIR$$",
Expand All @@ -97,7 +95,13 @@ def built_tool_rule_impl(ctx, script_lines, out_dir, mnemonic, additional_tools
"",
])

wrapped_outputs = wrap_outputs(ctx, lib_name, mnemonic, script_text)
wrapped_outputs = wrap_outputs(
ctx,
lib_name = lib_name,
configure_name = mnemonic,
env_prelude = env_prelude,
script_text = script_text,
)

tools = depset(
[wrapped_outputs.wrapper_script_file, wrapped_outputs.script_file],
Expand Down
1 change: 1 addition & 0 deletions foreign_cc/cmake.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def _create_configure_script(configureParameters):
cmake_prefix = prefix,
include_dirs = inputs.include_dirs,
is_debug_mode = is_debug_mode(ctx),
ext_build_dirs = inputs.ext_build_dirs,
)
return configure_script

Expand Down
2 changes: 1 addition & 1 deletion foreign_cc/private/cc_toolchain_util.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def _convert_flags(compiler, flags):
list: The converted flags
"""
if compiler == "msvc-cl":
return [flag.replace("/", "-") if flag.startswith("/") else flag for flag in flags]
return [("-" + flag.removeprefix("/")) if flag.startswith("/") else flag for flag in flags]
return flags

def _add_if_needed(arr, add_arr):
Expand Down
11 changes: 8 additions & 3 deletions foreign_cc/private/cmake_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def create_cmake_script(
cmake_commands,
include_dirs = [],
cmake_prefix = None,
is_debug_mode = True):
is_debug_mode = True,
ext_build_dirs = []):
"""Constructs CMake script to be passed to cc_external_rule_impl.
Args:
Expand All @@ -81,12 +82,13 @@ def create_cmake_script(
include_dirs: Optional additional include directories. Defaults to [].
cmake_prefix: Optional prefix before the cmake command (without the trailing space).
is_debug_mode: If the compilation mode is `debug`. Defaults to True.
ext_build_dirs: A list of gen_dirs for each foreign_cc dep.
Returns:
list: Lines of bash which make up the build script
"""

merged_prefix_path = _merge_prefix_path(user_cache, include_dirs)
merged_prefix_path = _merge_prefix_path(user_cache, include_dirs, ext_build_dirs)

toolchain_dict = _fill_crossfile_from_toolchain(workspace_name, tools, flags)
params = None
Expand Down Expand Up @@ -171,9 +173,12 @@ def _wipe_empty_values(cache, keys_with_empty_values_in_user_cache):
cache.pop(key)

# From CMake documentation: ;-list of directories specifying installation prefixes to be searched...
def _merge_prefix_path(user_cache, include_dirs):
def _merge_prefix_path(user_cache, include_dirs, ext_build_dirs):
user_prefix = user_cache.get("CMAKE_PREFIX_PATH")
values = ["$$EXT_BUILD_DEPS$$"] + include_dirs
for ext_dir in ext_build_dirs:
values.append("$$EXT_BUILD_DEPS$$/{}".format(ext_dir.basename))

if user_prefix != None:
# remove it, it is gonna be merged specifically
user_cache.pop("CMAKE_PREFIX_PATH")
Expand Down
30 changes: 20 additions & 10 deletions foreign_cc/private/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,21 @@ dependencies.""",
def _is_msvc_var(var):
return var == "INCLUDE" or var == "LIB"

def get_env_prelude(ctx, lib_name, data_dependencies, target_root):
def get_env_prelude(ctx, installdir, data_dependencies):
"""Generate a bash snippet containing environment variable definitions
Args:
ctx (ctx): The rule's context object
lib_name (str): The name of the target being built
installdir (str): The path from the root target's directory in the build output
data_dependencies (list): A list of targets representing dependencies
target_root (str): The path from the root target's directory in the build output
Returns:
tuple: A list of environment variables to define in the build script and a dict
of environment variables
"""
env_snippet = [
"export EXT_BUILD_ROOT=##pwd##",
"export INSTALLDIR=$$EXT_BUILD_ROOT$$/" + target_root + "/" + lib_name,
"export INSTALLDIR=$$EXT_BUILD_ROOT$$/" + installdir,
"export BUILD_TMPDIR=$$INSTALLDIR$$.build_tmpdir",
"export EXT_BUILD_DEPS=$$INSTALLDIR$$.ext_build_deps",
]
Expand Down Expand Up @@ -447,7 +446,8 @@ def cc_external_rule_impl(ctx, attrs):
# Also add legacy dependencies while they're still available
data_dependencies += ctx.attr.tools_deps + ctx.attr.additional_tools

env_prelude = get_env_prelude(ctx, lib_name, data_dependencies, target_root)
installdir = target_root + "/" + lib_name
env_prelude = get_env_prelude(ctx, installdir, data_dependencies)

postfix_script = [attrs.postfix_script]
if not attrs.postfix_script:
Expand Down Expand Up @@ -491,7 +491,13 @@ def cc_external_rule_impl(ctx, attrs):
convert_shell_script(ctx, script_lines),
"",
])
wrapped_outputs = wrap_outputs(ctx, lib_name, attrs.configure_name, script_text)
wrapped_outputs = wrap_outputs(
ctx,
lib_name = lib_name,
configure_name = attrs.configure_name,
script_text = script_text,
env_prelude = env_prelude,
)

rule_outputs = outputs.declared_outputs + [installdir_copy.file]
cc_toolchain = find_cpp_toolchain(ctx)
Expand Down Expand Up @@ -595,7 +601,7 @@ WrappedOutputs = provider(
)

# buildifier: disable=function-docstring
def wrap_outputs(ctx, lib_name, configure_name, script_text, build_script_file = None):
def wrap_outputs(ctx, lib_name, configure_name, script_text, env_prelude, build_script_file = None):
extension = script_extension(ctx)
build_log_file = ctx.actions.declare_file("{}_foreign_cc/{}.log".format(lib_name, configure_name))
build_script_file = ctx.actions.declare_file("{}_foreign_cc/build_script{}".format(lib_name, extension))
Expand All @@ -610,34 +616,38 @@ def wrap_outputs(ctx, lib_name, configure_name, script_text, build_script_file =
cleanup_on_success_function = create_function(
ctx,
"cleanup_on_success",
"rm -rf $BUILD_TMPDIR $EXT_BUILD_DEPS",
"rm -rf $$BUILD_TMPDIR$$ $$EXT_BUILD_DEPS$$",
)
cleanup_on_failure_function = create_function(
ctx,
"cleanup_on_failure",
"\n".join([
"##echo## \"rules_foreign_cc: Build failed!\"",
"##echo## \"rules_foreign_cc: Keeping temp build directory $$BUILD_TMPDIR$$ and dependencies directory $$EXT_BUILD_DEPS$$ for debug.\"",
"##echo## \"rules_foreign_cc: Please note that the directories inside a sandbox are still cleaned unless you specify '--sandbox_debug' Bazel command line flag.\"",
"##echo## \"rules_foreign_cc: Printing build logs:\"",
"##echo## \"_____ BEGIN BUILD LOGS _____\"",
"##cat## $$BUILD_LOG$$",
"##echo## \"_____ END BUILD LOGS _____\"",
"##echo## \"rules_foreign_cc: Build wrapper script location: $$BUILD_WRAPPER_SCRIPT$$\"",
"##echo## \"rules_foreign_cc: Build script location: $$BUILD_SCRIPT$$\"",
"##echo## \"rules_foreign_cc: Build log location: $$BUILD_LOG$$\"",
"##echo## \"rules_foreign_cc: Keeping these below directories for debug, but note that the directories inside a sandbox\"",
"##echo## \"rules_foreign_cc: are still cleaned unless you specify the '--sandbox_debug' Bazel command line flag.\"",
"##echo## \"rules_foreign_cc: Build Dir: $$BUILD_TMPDIR$$\"",
"##echo## \"rules_foreign_cc: Deps Dir: $$EXT_BUILD_DEPS$$\"",
"##echo## \"\"",
]),
)
trap_function = "##cleanup_function## cleanup_on_success cleanup_on_failure"

build_command_lines = [
"##script_prelude##",
"##assert_script_errors##",
cleanup_on_success_function,
cleanup_on_failure_function,
# the call trap is defined inside, in a way how the shell function should be called
# see, for instance, linux_commands.bzl
trap_function,
] + env_prelude + [
"export BUILD_WRAPPER_SCRIPT=\"{}\"".format(wrapper_script_file.path),
"export BUILD_SCRIPT=\"{}\"".format(build_script_file.path),
"export BUILD_LOG=\"{}\"".format(build_log_file.path),
Expand Down

0 comments on commit 47078b0

Please sign in to comment.