Skip to content

Commit

Permalink
Starlarkify CompilationArtifacts.java
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 676804465
Change-Id: I11f649b5bea8c06bb93f6af330d7f14b54759431
  • Loading branch information
pzembrod authored and copybara-github committed Sep 20, 2024
1 parent bed55b4 commit 254f97a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 194 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,6 @@ public class CompilationSupport {

static final ImmutableList<String> DEFAULT_COMPILER_FLAGS = ImmutableList.of("-DOS_IOS");

/** Returns information about the given rule's compilation artifacts. */
// TODO(bazel-team): Remove this information from ObjcCommon and move it internal to this class.
static CompilationArtifacts compilationArtifacts(RuleContext ruleContext) {
return compilationArtifacts(ruleContext, new IntermediateArtifacts(ruleContext));
}

/**
* Returns information about the given rule's compilation artifacts. Dependencies specified in the
* current rule's attributes are obtained via {@code ruleContext}. Output locations are determined
* using the given {@code intermediateArtifacts} object. The fact that these are distinct objects
* allows the caller to generate compilation actions pertaining to a configuration separate from
* the current rule's configuration.
*/
static CompilationArtifacts compilationArtifacts(
RuleContext ruleContext, IntermediateArtifacts intermediateArtifacts) {
return new CompilationArtifacts(ruleContext, intermediateArtifacts);
}

private CompilationSupport() {}

public static Optional<Artifact> getCustomModuleMap(RuleContext ruleContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@
import java.util.Map;
import javax.annotation.Nullable;
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.Starlark;
import net.starlark.java.eval.StarlarkList;
Expand Down Expand Up @@ -173,54 +171,6 @@ public IntermediateArtifacts j2objcCreateIntermediateArtifacts(
AlwaysLink.TRUE);
}

@StarlarkMethod(
name = "create_compilation_artifacts",
documented = false,
parameters = {
@Param(
name = "ctx",
positional = false,
named = true,
defaultValue = "None",
allowedTypes = {
@ParamType(type = StarlarkRuleContext.class),
@ParamType(type = NoneType.class)
}),
})
public CompilationArtifacts createCompilationArtifacts(Object starlarkRuleContextObject) {
StarlarkRuleContext starlarkRuleContext =
convertFromNoneable(starlarkRuleContextObject, /* defaultValue= */ null);
if (starlarkRuleContext != null) {
return CompilationSupport.compilationArtifacts(starlarkRuleContext.getRuleContext());
} else {
return new CompilationArtifacts();
}
}

@StarlarkMethod(
name = "j2objc_create_compilation_artifacts",
documented = false,
parameters = {
@Param(name = "srcs", positional = false, named = true),
@Param(name = "non_arc_srcs", positional = false, named = true),
@Param(name = "hdrs", positional = false, named = true),
@Param(name = "intermediate_artifacts", positional = false, named = true),
})
public CompilationArtifacts j2objcCreateCompilationArtifacts(
Sequence<?> srcs,
Sequence<?> nonArcSrcs,
Sequence<?> hdrs,
Object intermediateArtifactsObject)
throws EvalException {
IntermediateArtifacts intermediateArtifacts =
convertFromNoneable(intermediateArtifactsObject, /* defaultValue= */ null);
return new CompilationArtifacts(
Sequence.cast(srcs, Artifact.class, "srcs"),
Sequence.cast(nonArcSrcs, Artifact.class, "non_arc_srcs"),
Sequence.cast(hdrs, Artifact.class, "hdrs"),
intermediateArtifacts);
}

@StarlarkMethod(
name = "create_compilation_context",
documented = false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright 2024 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Artifacts related to compilation."""

# File types that should actually be compiled, or that are already compiled.
# Important: this tuple must not contain any header file extensions. See comment where it's used.
COMPILABLE_OR_PRECOMPILED_SRC_EXTENSIONS = (
"cc", # CPP extensions
"cpp",
"mm",
"cxx",
"C",
"m", # Non-CPP extensions
"c",
"s", # Assembly extensions
"S",
"asm",
"o", # Object file extensions
)

def _compilation_artifacts_init(
ctx = None,
srcs = None,
non_arc_srcs = None,
hdrs = None,
intermediate_artifacts = None):
if ctx == None and srcs == None and non_arc_srcs == None and hdrs == None and intermediate_artifacts == None:
return {
"srcs": [],
"non_arc_srcs": [],
"additional_hdrs": [],
"archive": None,
}
if ctx != None and (srcs != None or non_arc_srcs != None or hdrs != None):
fail("CompilationArtifactsInfo() params ctx and (srcs, non_arc_srcs, hdrs) are mutually exclusive")
if ctx != None:
srcs = ctx.files.srcs if hasattr(ctx.files, "srcs") else []
non_arc_srcs = ctx.files.non_arc_srcs if hasattr(ctx.files, "non_arc_srcs") else []
hdrs = []

# Note: the condition under which we set an archive artifact needs to match the condition for
# which we create the archive in compilation_support.bzl. In particular, if srcs are all
# headers, we don't generate an archive.
if (
non_arc_srcs or
[s for s in srcs if s.is_directory or
s.extension in COMPILABLE_OR_PRECOMPILED_SRC_EXTENSIONS]
):
archive = intermediate_artifacts.archive()
else:
archive = None
return {
"srcs": srcs,
"non_arc_srcs": non_arc_srcs,
"additional_hdrs": hdrs,
"archive": archive,
}

CompilationArtifactsInfo, _new_compilationartifactsinfo = provider(
"Any rule containing compilable sources will create an instance of this provider.",
fields = ["srcs", "non_arc_srcs", "additional_hdrs", "archive"],
init = _compilation_artifacts_init,
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/cc/semantics.bzl", cc_semantics = "semantics")
load(":common/objc/apple_configuration.bzl", "apple_configuration")
load(":common/objc/apple_env.bzl", "apple_host_system_env", "target_apple_env")
load(":common/objc/compilation_artifacts_info.bzl", "CompilationArtifactsInfo")
load(":common/objc/objc_common.bzl", "objc_common")
load(":common/objc/providers.bzl", "J2ObjcEntryClassInfo", "J2ObjcMappingFileInfo")
load(":common/xcode/providers.bzl", "XcodeVersionInfo")
Expand Down Expand Up @@ -56,9 +57,12 @@ def _build_common_variables(
compilation_attributes = objc_internal.create_compilation_attributes(ctx = ctx)
intermediate_artifacts = objc_internal.create_intermediate_artifacts(ctx = ctx)
if empty_compilation_artifacts:
compilation_artifacts = objc_internal.create_compilation_artifacts()
compilation_artifacts = CompilationArtifactsInfo()
else:
compilation_artifacts = objc_internal.create_compilation_artifacts(ctx = ctx)
compilation_artifacts = CompilationArtifactsInfo(
ctx = ctx,
intermediate_artifacts = intermediate_artifacts,
)

(
objc_provider,
Expand Down
5 changes: 3 additions & 2 deletions src/main/starlark/builtins_bzl/common/objc/j2objc_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ load(":common/cc/semantics.bzl", cc_semantics = "semantics")
load(":common/java/java_info.bzl", "JavaInfo")
load(":common/java/java_semantics.bzl", java_semantics = "semantics")
load(":common/objc/apple_common.bzl", "apple_common")
load(":common/objc/compilation_artifacts_info.bzl", "CompilationArtifactsInfo")
load(":common/objc/compilation_support.bzl", "compilation_support")
load(":common/objc/objc_common.bzl", "objc_common")
load(":common/objc/providers.bzl", "J2ObjcMappingFileInfo")
Expand Down Expand Up @@ -425,14 +426,14 @@ def _common(
has_module_map = False
if transpiled_sources or transpiled_headers:
if compile_with_arc:
compilation_artifacts = objc_internal.j2objc_create_compilation_artifacts(
compilation_artifacts = CompilationArtifactsInfo(
srcs = transpiled_sources,
non_arc_srcs = [],
hdrs = transpiled_headers,
intermediate_artifacts = intermediate_artifacts,
)
else:
compilation_artifacts = objc_internal.j2objc_create_compilation_artifacts(
compilation_artifacts = CompilationArtifactsInfo(
srcs = [],
non_arc_srcs = transpiled_sources,
hdrs = transpiled_headers,
Expand Down

0 comments on commit 254f97a

Please sign in to comment.