From 89e95a3fd1d0419281fc7d0a2c24b5a5cc8bbb4d Mon Sep 17 00:00:00 2001 From: Jacob Rodal Date: Mon, 23 Dec 2024 09:43:38 -0800 Subject: [PATCH] introduce runtime_dependency_handling Reviewed By: milend Differential Revision: D67122935 fbshipit-source-id: dd5fcb3fb9cfe1c0a81753d561641c9acdb16abf --- prelude/cxx/cxx.bzl | 6 ++++++ prelude/cxx/cxx_executable.bzl | 6 +++++- prelude/cxx/cxx_types.bzl | 6 ++++++ prelude/cxx/runtime_dependency_handling.bzl | 18 ++++++++++++++++++ prelude/decls/common.bzl | 2 ++ prelude/decls/cxx_common.bzl | 12 +++++++++++- prelude/decls/cxx_rules.bzl | 2 ++ 7 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 prelude/cxx/runtime_dependency_handling.bzl diff --git a/prelude/cxx/cxx.bzl b/prelude/cxx/cxx.bzl index 4f8419eca8ae..48834b67fe96 100644 --- a/prelude/cxx/cxx.bzl +++ b/prelude/cxx/cxx.bzl @@ -17,6 +17,10 @@ load( "@prelude//cxx:link_groups_types.bzl", "LinkGroupInfo", # @unused Used as a type ) +load( + "@prelude//cxx:runtime_dependency_handling.bzl", + "cxx_attr_runtime_dependency_handling", +) load("@prelude//linking:execution_preference.bzl", "LinkExecutionPreference") load( "@prelude//linking:link_groups.bzl", @@ -275,6 +279,7 @@ def cxx_binary_impl(ctx: AnalysisContext) -> list[Provider]: platform_preprocessor_flags = ctx.attrs.platform_preprocessor_flags, lang_platform_preprocessor_flags = ctx.attrs.lang_platform_preprocessor_flags, use_header_units = ctx.attrs.use_header_units, + runtime_dependency_handling = cxx_attr_runtime_dependency_handling(ctx), ) output = cxx_executable(ctx, params) @@ -770,6 +775,7 @@ def cxx_test_impl(ctx: AnalysisContext) -> list[Provider]: platform_preprocessor_flags = ctx.attrs.platform_preprocessor_flags, lang_platform_preprocessor_flags = ctx.attrs.lang_platform_preprocessor_flags, use_header_units = ctx.attrs.use_header_units, + runtime_dependency_handling = cxx_attr_runtime_dependency_handling(ctx), ) output = cxx_executable(ctx, params, is_cxx_test = True) diff --git a/prelude/cxx/cxx_executable.bzl b/prelude/cxx/cxx_executable.bzl index 42dd57c310a4..341f82855ddd 100644 --- a/prelude/cxx/cxx_executable.bzl +++ b/prelude/cxx/cxx_executable.bzl @@ -32,6 +32,10 @@ load( "LinkGroupsDebugLinkInfo", "LinkGroupsDebugLinkableItem", ) +load( + "@prelude//cxx:runtime_dependency_handling.bzl", + "RuntimeDependencyHandling", +) load( "@prelude//dist:dist_info.bzl", "DistInfo", @@ -508,7 +512,7 @@ def cxx_executable(ctx: AnalysisContext, impl_params: CxxRuleConstructorParams, # Only setup a shared library symlink tree when shared linkage or link_groups is used gnu_use_link_groups = cxx_is_gnu(ctx) and len(link_group_mappings) > 0 shlib_deps = [] - if link_strategy == LinkStrategy("shared") or gnu_use_link_groups: + if impl_params.runtime_dependency_handling == RuntimeDependencyHandling("symlink") or link_strategy == LinkStrategy("shared") or gnu_use_link_groups: shlib_deps = ( [d.shared_library_info for d in link_deps] + [d.shared_library_info for d in impl_params.extra_link_roots] diff --git a/prelude/cxx/cxx_types.bzl b/prelude/cxx/cxx_types.bzl index 0ae7606c50c1..e26ce99ad7cf 100644 --- a/prelude/cxx/cxx_types.bzl +++ b/prelude/cxx/cxx_types.bzl @@ -10,6 +10,10 @@ load( "@prelude//cxx:link_groups_types.bzl", "LinkGroupInfo", # @unused Used as a type ) +load( + "@prelude//cxx:runtime_dependency_handling.bzl", + "RuntimeDependencyHandling", # @unused Used as a type +) load( "@prelude//linking:link_info.bzl", "LinkArgs", @@ -242,4 +246,6 @@ CxxRuleConstructorParams = record( export_header_unit = field([str, None], None), # Filter what headers to include in header units. export_header_unit_filter = field(list[str], []), + # Additional behavior for how to handle runtime dependencies + runtime_dependency_handling = field([RuntimeDependencyHandling, None], None), ) diff --git a/prelude/cxx/runtime_dependency_handling.bzl b/prelude/cxx/runtime_dependency_handling.bzl new file mode 100644 index 000000000000..af8c838abb54 --- /dev/null +++ b/prelude/cxx/runtime_dependency_handling.bzl @@ -0,0 +1,18 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under both the MIT license found in the +# LICENSE-MIT file in the root directory of this source tree and the Apache +# License, Version 2.0 found in the LICENSE-APACHE file in the root directory +# of this source tree. + +# Additional behavior for how to handle runtime dependencies +RuntimeDependencyHandling = enum( + # Do no additional handling + "none", + # Always include runtime dependencies in a symlink tree, regardless + # of whether shared linkage is used or not + "symlink", +) + +def cxx_attr_runtime_dependency_handling(ctx: AnalysisContext) -> RuntimeDependencyHandling: + return RuntimeDependencyHandling(ctx.attrs.runtime_dependency_handling or "none") diff --git a/prelude/decls/common.bzl b/prelude/decls/common.bzl index efd87bc959d2..607c9a00a571 100644 --- a/prelude/decls/common.bzl +++ b/prelude/decls/common.bzl @@ -51,6 +51,8 @@ TestType = ["junit", "junit5", "testng"] UnusedDependenciesAction = ["unknown", "fail", "warn", "ignore", "unrecognized"] +RuntimeDependencyHandling = ["none", "symlink"] + def _name_arg(name_type): return { "name": name_type, diff --git a/prelude/decls/cxx_common.bzl b/prelude/decls/cxx_common.bzl index faa97ef255f8..9a52635d9212 100644 --- a/prelude/decls/cxx_common.bzl +++ b/prelude/decls/cxx_common.bzl @@ -10,7 +10,7 @@ # the generated docs, and so those should be verified to be accurate and # well-formatted (and then delete this TODO) -load(":common.bzl", "CxxSourceType", "IncludeType", "RawHeadersAsHeadersMode") +load(":common.bzl", "CxxSourceType", "IncludeType", "RawHeadersAsHeadersMode", "RuntimeDependencyHandling") def _srcs_arg(): return { @@ -442,6 +442,15 @@ def _version_arg(): """), } +def _runtime_dependency_handling_arg(): + return { + "runtime_dependency_handling": attrs.option(attrs.enum(RuntimeDependencyHandling), default = None, doc = """ + When this is set to `symlink`, shared library dependencies are included in a symlink tree + alongside the resulting executable, even if the link style is not shared. Can be `none` or + `symlink`. +"""), + } + cxx_common = struct( srcs_arg = _srcs_arg, deps_arg = _deps_arg, @@ -485,4 +494,5 @@ cxx_common = struct( public_include_directories_arg = _public_include_directories_arg, public_system_include_directories_arg = _public_system_include_directories_arg, version_arg = _version_arg, + runtime_dependency_handling_arg = _runtime_dependency_handling_arg, ) diff --git a/prelude/decls/cxx_rules.bzl b/prelude/decls/cxx_rules.bzl index 8792f16b2c93..e0c738e52c7b 100644 --- a/prelude/decls/cxx_rules.bzl +++ b/prelude/decls/cxx_rules.bzl @@ -100,6 +100,7 @@ cxx_binary = prelude_rule( cxx_common.raw_headers_arg() | cxx_common.include_directories_arg() | cxx_common.raw_headers_as_headers_mode_arg() | + cxx_common.runtime_dependency_handling_arg() | { "contacts": attrs.list(attrs.string(), default = []), "cxx_runtime_type": attrs.option(attrs.enum(CxxRuntimeType), default = None), @@ -863,6 +864,7 @@ cxx_test = prelude_rule( cxx_common.raw_headers_arg() | cxx_common.raw_headers_as_headers_mode_arg() | cxx_common.include_directories_arg() | + cxx_common.runtime_dependency_handling_arg() | { "framework": attrs.option(attrs.enum(CxxTestType), default = None, doc = """ Unused.