Skip to content

Commit

Permalink
[8.0.0] Move Apple multi arch linking support out of the bazel binary (
Browse files Browse the repository at this point in the history
…#24273)

Remove internal functions for Apple multi arch linking from apple_common
and instead expose functions needed by multi arch linking functions in
the repo.

---------

Co-authored-by: Yun Peng <[email protected]>
  • Loading branch information
pzembrod and meteorcloudy authored Nov 14, 2024
1 parent 9c081db commit c877f8a
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 689 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public final class BuiltinRestriction {
BuiltinRestriction.allowlistEntry("rules_android", ""),
BuiltinRestriction.allowlistEntry("build_bazel_rules_android", ""),

// Apple rules
BuiltinRestriction.allowlistEntry("", "third_party/bazel_rules/rules_apple"),
BuiltinRestriction.allowlistEntry("rules_apple", ""),

// Cc rules
BuiltinRestriction.allowlistEntry("", "third_party/bazel_rules/rules_cc"),
BuiltinRestriction.allowlistEntry("rules_cc", ""),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,16 @@

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.docgen.annot.DocCategory;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkRuleContextApi;
import com.google.devtools.build.lib.starlarkbuildapi.apple.DottedVersionApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcInfoApi;
import com.google.devtools.build.lib.starlarkbuildapi.platform.ConstraintValueInfoApi;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.ParamType;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.NoneType;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.StarlarkInt;
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.eval.StarlarkValue;

/** Interface for a module with useful functions for creating apple-related rule implementations. */
Expand Down Expand Up @@ -121,50 +112,6 @@ default ProviderApi getXcodeVersionConfigConstructor() {
throw new UnsupportedOperationException(); // just for docs
}

@StarlarkMethod(
name = "AppleDynamicFramework",
doc =
"The constructor/key for the <code>AppleDynamicFramework</code> provider.<p>"
+ "If a target propagates the <code>AppleDynamicFramework</code> provider, use this "
+ "as the key with which to retrieve it. Example:<br>"
+ "<pre class='language-python'>\n"
+ "dep = ctx.attr.deps[0]\n"
+ "p = dep[apple_common.AppleDynamicFramework]\n"
+ "</pre>",
structField = true)
default ProviderApi getAppleDynamicFrameworkConstructor() {
throw new UnsupportedOperationException(); // just for docs
}

@StarlarkMethod(
name = "AppleExecutableBinary",
doc =
"The constructor/key for the <code>AppleExecutableBinary</code> provider.<p>"
+ "If a target propagates the <code>AppleExecutableBinary</code> provider,"
+ " use this as the key with which to retrieve it. Example:<br>"
+ "<pre class='language-python'>\n"
+ "dep = ctx.attr.deps[0]\n"
+ "p = dep[apple_common.AppleExecutableBinary]\n"
+ "</pre>",
structField = true)
default ProviderApi getAppleExecutableBinaryConstructor() {
throw new UnsupportedOperationException(); // just for docs
}

@StarlarkMethod(
name = "AppleDebugOutputs",
doc =
"The constructor/key for the <code>AppleDebugOutputs</code> provider.<p>If a target"
+ " propagates the <code>AppleDebugOutputs</code> provider, use this as the key with"
+ " which to retrieve it. Example:<br><pre class='language-python'>\n"
+ "dep = ctx.attr.deps[0]\n"
+ "p = dep[apple_common.AppleDebugOutputs]\n"
+ "</pre>",
structField = true)
default void getAppleDebugOutputsConstructor() {
throw new UnsupportedOperationException();
}

@StarlarkMethod(
name = "apple_host_system_env",
doc =
Expand Down Expand Up @@ -203,200 +150,6 @@ default ImmutableMap<String, String> getTargetAppleEnvironment(
throw new UnsupportedOperationException(); // just for docs
}

@StarlarkMethod(
name = "new_dynamic_framework_provider",
doc = "Creates a new AppleDynamicFramework provider instance.",
parameters = {
@Param(
name = "binary",
allowedTypes = {
@ParamType(type = FileApi.class),
@ParamType(type = NoneType.class),
},
named = true,
positional = false,
defaultValue = "None",
doc = "The dylib binary artifact of the dynamic framework."),
@Param(
name = "cc_info",
named = true,
positional = false,
doc =
"A CcInfo which contains information about the transitive dependencies "
+ "linked into the binary."),
@Param(
name = "framework_dirs",
allowedTypes = {
@ParamType(type = Depset.class, generic1 = String.class),
@ParamType(type = NoneType.class),
},
named = true,
positional = false,
defaultValue = "None",
doc =
"The framework path names used as link inputs in order to link against the dynamic "
+ "framework."),
@Param(
name = "framework_files",
allowedTypes = {
@ParamType(type = Depset.class, generic1 = FileApi.class),
@ParamType(type = NoneType.class),
},
named = true,
positional = false,
defaultValue = "None",
doc =
"The full set of artifacts that should be included as inputs to link against the "
+ "dynamic framework")
},
useStarlarkThread = true)
default Object newDynamicFrameworkProvider(
Object dylibBinary,
CcInfoApiT depsCcInfo,
Object dynamicFrameworkDirs,
Object dynamicFrameworkFiles,
StarlarkThread thread)
throws EvalException {
throw new UnsupportedOperationException(); // just for docs
}

@StarlarkMethod(
name = "new_executable_binary_provider",
doc = "Creates a new AppleExecutableBinaryInfo provider instance.",
parameters = {
@Param(
name = "binary",
allowedTypes = {
@ParamType(type = FileApi.class),
@ParamType(type = NoneType.class),
},
named = true,
positional = false,
defaultValue = "None",
doc = "The binary artifact of the executable."),
@Param(
name = "cc_info",
named = true,
positional = false,
doc =
"A CcInfo which contains information about the transitive dependencies "
+ "linked into the binary."),
},
useStarlarkThread = true)
default Object newExecutableBinaryProvider(
Object executableBinary, CcInfoApiT depsCcInfo, StarlarkThread thread) throws EvalException {
throw new UnsupportedOperationException(); // just for docs
}

@StarlarkMethod(
name = "link_multi_arch_binary",
doc =
"Links a (potentially multi-architecture) binary targeting Apple platforms. This "
+ "method comprises a bulk of the logic of the Starlark <code>apple_binary</code> "
+ "rule in the rules_apple domain and exists to aid in the migration of its "
+ "linking logic to Starlark in rules_apple.\n"
+ "<p>This API is <b>highly experimental</b> and subject to change at any time. Do "
+ "not depend on the stability of this function at this time.",
parameters = {
@Param(name = "ctx", named = true, positional = false, doc = "The Starlark rule context."),
@Param(
name = "avoid_deps",
allowedTypes = {
@ParamType(type = Sequence.class, generic1 = TransitiveInfoCollection.class),
@ParamType(type = NoneType.class),
},
named = true,
positional = false,
defaultValue = "None",
doc =
"A list of <code>Target</code>s that are in the dependency graph of the binary but"
+ " whose libraries should not be linked into the binary. This is the case for"
+ " dependencies that will be found at runtime in another image, such as the"
+ " bundle loader or any dynamic libraries/frameworks that will be loaded by"
+ " this binary."),
@Param(
name = "extra_linkopts",
allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)},
named = true,
positional = false,
defaultValue = "[]",
doc = "Extra linkopts to be passed to the linker action."),
@Param(
name = "extra_link_inputs",
allowedTypes = {@ParamType(type = Sequence.class, generic1 = FileApi.class)},
named = true,
positional = false,
defaultValue = "[]",
doc = "Extra files to pass to the linker action."),
@Param(
name = "extra_requested_features",
allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)},
named = true,
positional = false,
defaultValue = "[]",
doc = "Extra requested features to be passed to the linker action."),
@Param(
name = "extra_disabled_features",
allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)},
named = true,
positional = false,
defaultValue = "[]",
doc = "Extra disabled features to be passed to the linker action."),
@Param(
name = "stamp",
named = true,
positional = false,
defaultValue = "-1",
doc =
"Whether to include build information in the linked binary. If 1, build "
+ "information is always included. If 0, build information is always excluded. "
+ "If -1 (the default), then the behavior is determined by the --[no]stamp "
+ "flag. This should be set to 0 when generating the executable output for "
+ "test rules."),
@Param(
name = "variables_extension",
positional = false,
named = true,
documented = false,
allowedTypes = {
@ParamType(type = Dict.class),
@ParamType(type = NoneType.class),
},
defaultValue = "None"),
},
useStarlarkThread = true)
// TODO(b/70937317): Iterate on, improve, and solidify this API.
default StructApi linkMultiArchBinary(
StarlarkRuleContextT starlarkRuleContext,
Object avoidDeps, // Sequence<TransitiveInfoCollection> expected.
Sequence<?> extraLinkopts, // <String> expected.
Sequence<?> extraLinkInputs, // <? extends FileApi> expected.
Sequence<?> extraRequestedFeatures, // <String> expected.
Sequence<?> extraDisabledFeatures, // <String> expected.
StarlarkInt stamp,
Object variablesExtension,
StarlarkThread thread) {
throw new UnsupportedOperationException(); // just for docs
}

@StarlarkMethod(
name = "link_multi_arch_static_library",
doc =
"Links a (potentially multi-architecture) static library targeting Apple platforms."
+ " This method comprises a part of the Starlark <code>apple_static_library</code>"
+ " rule logic, in the rules_apple domain and exists to aid in the migration of its"
+ " linking logic to Starlark in rules_apple.\n"
+ "<p>This API is <b>highly experimental</b> and subject to change at any time."
+ " Do not depend on the stability of this function at this time.",
parameters = {
@Param(name = "ctx", named = true, positional = false, doc = "The Starlark rule context."),
},
useStarlarkThread = true)
default StructApi linkMultiArchStaticLibrary(
StarlarkRuleContextT starlarkRuleContext, StarlarkThread thread) {
throw new UnsupportedOperationException(); // just for docs
}

@StarlarkMethod(
name = "dotted_version",
doc = "Creates a new <a href=\"../builtins/DottedVersion.html\">DottedVersion</a> instance.",
Expand Down
18 changes: 9 additions & 9 deletions src/main/starlark/builtins_bzl/common/objc/apple_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
"""Legacy apple_common module"""

load(":common/objc/apple_env.bzl", "apple_host_system_env", "target_apple_env")
load(":common/objc/apple_platform.bzl", "PLATFORM", "PLATFORM_TYPE")
load(":common/objc/apple_platform.bzl", "PLATFORM", "PLATFORM_TYPE", "apple_platform")
load(":common/objc/apple_toolchain.bzl", "apple_toolchain")
load(":common/objc/linking_support.bzl", "AppleDebugOutputsInfo", "AppleDynamicFrameworkInfo", "AppleExecutableBinaryInfo", "linking_support")
load(":common/objc/compilation_support.bzl", "compilation_support")
load(":common/objc/objc_info.bzl", "ObjcInfo")
load(":common/xcode/providers.bzl", "XcodeVersionInfo", "XcodeVersionPropertiesInfo")

native_apple_common = _builtins.internal.apple_common
native_objc_internal = _builtins.internal.objc_internal

apple_common = struct(
apple_toolchain = lambda: apple_toolchain,
Expand All @@ -30,15 +31,14 @@ apple_common = struct(
XcodeProperties = XcodeVersionPropertiesInfo,
XcodeVersionConfig = XcodeVersionInfo,
Objc = ObjcInfo,
AppleDynamicFramework = AppleDynamicFrameworkInfo,
AppleExecutableBinary = AppleExecutableBinaryInfo,
AppleDebugOutputs = AppleDebugOutputsInfo,
apple_host_system_env = apple_host_system_env,
target_apple_env = target_apple_env,
new_objc_provider = ObjcInfo,
new_dynamic_framework_provider = lambda **kwargs: AppleDynamicFrameworkInfo(**kwargs),
new_executable_binary_provider = lambda **kwargs: AppleExecutableBinaryInfo(**kwargs),
link_multi_arch_binary = linking_support.link_multi_arch_binary,
link_multi_arch_static_library = linking_support.link_multi_arch_static_library,
dotted_version = lambda version: native_apple_common.dotted_version(version),
apple_platform = apple_platform,
compilation_support = compilation_support,
get_cpu = lambda config: native_objc_internal.get_cpu(config),
get_apple_config = lambda config: native_objc_internal.get_apple_config(config),
get_split_build_configs = lambda ctx: native_objc_internal.get_split_build_configs(ctx),
get_split_prerequisites = lambda ctx: native_objc_internal.get_split_prerequisites(ctx),
)
Loading

0 comments on commit c877f8a

Please sign in to comment.