Skip to content

Commit 4a0cc3b

Browse files
oquenchilcopybara-github
authored andcommitted
Rewrite ObjcCommon in Starlark
RELNOTES:none PiperOrigin-RevId: 385983265
1 parent 00bd291 commit 4a0cc3b

File tree

13 files changed

+409
-69
lines changed

13 files changed

+409
-69
lines changed

src/main/java/com/google/devtools/build/lib/rules/objc/AppleStarlarkCommon.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
import com.google.devtools.build.lib.rules.apple.DottedVersion;
4040
import com.google.devtools.build.lib.rules.apple.XcodeConfigInfo;
4141
import com.google.devtools.build.lib.rules.apple.XcodeVersionProperties;
42+
import com.google.devtools.build.lib.rules.cpp.CcModule;
4243
import com.google.devtools.build.lib.rules.cpp.CppSemantics;
44+
import com.google.devtools.build.lib.rules.objc.ObjcProvider.Flag;
4345
import com.google.devtools.build.lib.starlarkbuildapi.SplitTransitionProviderApi;
4446
import com.google.devtools.build.lib.starlarkbuildapi.apple.AppleCommonApi;
4547
import java.util.Map;
@@ -193,12 +195,34 @@ public ObjcProvider newObjcProvider(Dict<String, Object> kwargs, StarlarkThread
193195
ObjcProvider.Key<?> key = ObjcProvider.getStarlarkKeyForString(entry.getKey());
194196
if (key != null) {
195197
resultBuilder.addElementsFromStarlark(key, entry.getValue());
196-
} else if (entry.getKey().equals("strict_include")) {
197-
resultBuilder.addStrictIncludeFromStarlark(entry.getValue());
198-
} else if (entry.getKey().equals("providers")) {
199-
resultBuilder.addProvidersFromStarlark(entry.getValue());
200198
} else {
201-
throw Starlark.errorf(BAD_KEY_ERROR, entry.getKey());
199+
switch (entry.getKey()) {
200+
case "cc_library":
201+
CcModule.checkPrivateStarlarkificationAllowlist(thread);
202+
resultBuilder.uncheckedAddTransitive(
203+
ObjcProvider.CC_LIBRARY,
204+
ObjcProviderStarlarkConverters.convertToJava(
205+
ObjcProvider.CC_LIBRARY, entry.getValue()));
206+
break;
207+
case "linkstamp":
208+
CcModule.checkPrivateStarlarkificationAllowlist(thread);
209+
resultBuilder.uncheckedAddTransitive(
210+
ObjcProvider.LINKSTAMP,
211+
ObjcProviderStarlarkConverters.convertToJava(
212+
ObjcProvider.LINKSTAMP, entry.getValue()));
213+
break;
214+
case "flag":
215+
resultBuilder.add(ObjcProvider.FLAG, Flag.USES_CPP);
216+
break;
217+
case "strict_include":
218+
resultBuilder.addStrictIncludeFromStarlark(entry.getValue());
219+
break;
220+
case "providers":
221+
resultBuilder.addProvidersFromStarlark(entry.getValue());
222+
break;
223+
default:
224+
throw Starlark.errorf(BAD_KEY_ERROR, entry.getKey());
225+
}
202226
}
203227
}
204228
return resultBuilder.build();

src/main/java/com/google/devtools/build/lib/rules/objc/CompilationAttributes.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.devtools.build.lib.rules.cpp.CppHelper;
3434
import com.google.devtools.build.lib.util.Pair;
3535
import com.google.devtools.build.lib.vfs.PathFragment;
36+
import net.starlark.java.annot.Param;
3637
import net.starlark.java.annot.StarlarkMethod;
3738
import net.starlark.java.eval.Sequence;
3839
import net.starlark.java.eval.StarlarkList;
@@ -351,6 +352,11 @@ public Depset hdrsForStarlark() {
351352
return Depset.of(Artifact.TYPE, hdrs());
352353
}
353354

355+
@StarlarkMethod(name = "textual_hdrs", documented = false, structField = true)
356+
public Depset textualHdrsForStarlark() {
357+
return Depset.of(Artifact.TYPE, textualHdrs());
358+
}
359+
354360
/**
355361
* Returns the headers that cannot be compiled individually.
356362
*/
@@ -376,6 +382,17 @@ public Depset includesForStarlark() {
376382
.collect(ImmutableList.toImmutableList())));
377383
}
378384

385+
@StarlarkMethod(name = "sdk_includes", documented = false, structField = true)
386+
public Depset sdkIncludesForStarlark() {
387+
return Depset.of(
388+
ElementType.STRING,
389+
NestedSetBuilder.wrap(
390+
Order.COMPILE_ORDER,
391+
sdkIncludes().toList().stream()
392+
.map(PathFragment::getSafePathString)
393+
.collect(ImmutableList.toImmutableList())));
394+
}
395+
379396
/**
380397
* Returns the paths for SDK includes.
381398
*/
@@ -390,27 +407,56 @@ public NestedSet<SdkFramework> sdkFrameworks() {
390407
return this.sdkFrameworks;
391408
}
392409

393-
@StarlarkMethod(name = "sdk_framework", documented = false, structField = true)
394-
public Depset sdkFramework() {
410+
@StarlarkMethod(name = "sdk_frameworks", documented = false, structField = true)
411+
public Depset sdkFrameworksForStarlark() {
395412
return (Depset)
396413
ObjcProviderStarlarkConverters.convertToStarlark(
397414
ObjcProvider.SDK_FRAMEWORK, sdkFrameworks());
398415
}
399416

417+
@StarlarkMethod(name = "weak_sdk_frameworks", documented = false, structField = true)
418+
public Depset weakSdkFrameworksForStarlark() {
419+
return (Depset)
420+
ObjcProviderStarlarkConverters.convertToStarlark(
421+
ObjcProvider.SDK_FRAMEWORK, weakSdkFrameworks());
422+
}
423+
400424
/**
401425
* Returns the SDK frameworks to be linked weakly.
402426
*/
403427
public NestedSet<SdkFramework> weakSdkFrameworks() {
404428
return this.weakSdkFrameworks;
405429
}
406430

431+
@StarlarkMethod(name = "sdk_dylibs", documented = false, structField = true)
432+
public Depset sdkDylibsForStarlark() {
433+
return Depset.of(ElementType.STRING, sdkDylibs);
434+
}
435+
407436
/**
408437
* Returns the SDK Dylibs to link against.
409438
*/
410439
public NestedSet<String> sdkDylibs() {
411440
return this.sdkDylibs;
412441
}
413442

443+
@StarlarkMethod(
444+
name = "header_search_paths",
445+
documented = false,
446+
parameters = {
447+
@Param(name = "genfiles_dir", positional = false, named = true),
448+
})
449+
public Depset headerSearchPathsForStarlark(String genfilesDir) {
450+
return Depset.of(
451+
ElementType.STRING,
452+
NestedSetBuilder.stableOrder()
453+
.addAll(
454+
headerSearchPaths(PathFragment.create(genfilesDir)).toList().stream()
455+
.map(PathFragment::toString)
456+
.collect(ImmutableList.toImmutableList()))
457+
.build());
458+
}
459+
414460
/**
415461
* Returns the exec paths of all header search paths that should be added to this target and
416462
* dependers on this target, obtained from the {@code includes} attribute.
@@ -454,6 +500,11 @@ public ImmutableList<Artifact> linkInputs() {
454500
return this.linkInputs;
455501
}
456502

503+
@StarlarkMethod(name = "defines", documented = false, structField = true)
504+
public Sequence<String> getDefinesForStarlark() {
505+
return StarlarkList.immutableCopyOf(defines());
506+
}
507+
457508
/** Returns the defines. */
458509
public ImmutableList<String> defines() {
459510
return this.defines;

src/main/java/com/google/devtools/build/lib/rules/objc/ObjcCommon.java

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import java.util.Iterator;
6060
import java.util.List;
6161
import java.util.Set;
62-
import javax.annotation.Nullable;
6362
import net.starlark.java.annot.StarlarkMethod;
6463
import net.starlark.java.eval.StarlarkSemantics;
6564
import net.starlark.java.eval.StarlarkValue;
@@ -512,23 +511,6 @@ public Optional<Artifact> getCompiledArchive() {
512511
return Optional.absent();
513512
}
514513

515-
/**
516-
* Returns the compiled {@code .a} file, or null if this object contains no {@link
517-
* CompilationArtifacts} or the compilation information has no sources.
518-
*/
519-
@StarlarkMethod(
520-
name = "compiled_archive",
521-
documented = false,
522-
structField = true,
523-
allowReturnNones = true)
524-
@Nullable
525-
public Artifact getCompiledArchiveForStarlark() {
526-
if (compilationArtifacts.isPresent() && compilationArtifacts.get().getArchive().isPresent()) {
527-
return compilationArtifacts.get().getArchive().get();
528-
}
529-
return null;
530-
}
531-
532514
/**
533515
* Returns effective compilation options that do not arise from the crosstool.
534516
*/
@@ -569,27 +551,6 @@ static Optional<PathFragment> nearestContainerMatching(
569551
return Optional.absent();
570552
}
571553

572-
/**
573-
* Similar to {@link #nearestContainerMatching(FileType, Artifact)}, but returns the container
574-
* closest to the root that matches the given type.
575-
*/
576-
static Optional<PathFragment> farthestContainerMatching(FileType type, Artifact artifact) {
577-
PathFragment container = artifact.getExecPath();
578-
Optional<PathFragment> lastMatch = Optional.absent();
579-
do {
580-
if (type.matches(container)) {
581-
lastMatch = Optional.of(container);
582-
}
583-
container = container.getParentDirectory();
584-
} while (container != null);
585-
return lastMatch;
586-
}
587-
588-
static Iterable<String> notInContainerErrors(
589-
Iterable<Artifact> artifacts, FileType containerType) {
590-
return notInContainerErrors(artifacts, ImmutableList.of(containerType));
591-
}
592-
593554
static Iterable<String> notInContainerErrors(
594555
Iterable<Artifact> artifacts, Iterable<FileType> containerTypes) {
595556
Set<String> errors = new HashSet<>();

src/main/java/com/google/devtools/build/lib/rules/objc/ObjcProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,11 @@ public Depset weakSdkFramework() {
400400
@VisibleForTesting
401401
static final ImmutableList<Key<?>> KEYS_NOT_IN_STARLARK =
402402
ImmutableList.<Key<?>>of(
403-
// LibraryToLink not exposed to Starlark.
404-
CC_LIBRARY,
405403
// Flag enum is not exposed to Starlark.
406404
FLAG,
407-
// Linkstamp is not exposed to Starlark. See commentary at its definition.
405+
// cc_library is handled specially.
406+
CC_LIBRARY,
407+
// linkstamp is handled specially.
408408
LINKSTAMP,
409409
// Strict include is handled specially.
410410
STRICT_INCLUDE);

src/main/java/com/google/devtools/build/lib/rules/objc/ObjcProviderStarlarkConverters.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.google.devtools.build.lib.collect.nestedset.Depset;
2222
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
2323
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
24+
import com.google.devtools.build.lib.rules.cpp.CcLinkingContext.Linkstamp;
25+
import com.google.devtools.build.lib.rules.cpp.LibraryToLink;
2426
import com.google.devtools.build.lib.rules.objc.ObjcProvider.Key;
2527
import com.google.devtools.build.lib.vfs.PathFragment;
2628
import net.starlark.java.eval.EvalException;
@@ -49,6 +51,11 @@ public static Object convertToStarlark(Key<?> javaKey, NestedSet<?> javaValue) {
4951
/** Returns a value for a java ObjcProvider given a key and a corresponding Starlark value. */
5052
public static NestedSet<?> convertToJava(Key<?> javaKey, Object starlarkValue)
5153
throws EvalException {
54+
if (javaKey.getType().equals(LibraryToLink.class)) {
55+
return Depset.noneableCast(starlarkValue, LibraryToLink.class, "cc_library");
56+
} else if (javaKey.getType().equals(Linkstamp.class)) {
57+
return Depset.noneableCast(starlarkValue, Linkstamp.class, "linkstamp");
58+
}
5259
return CONVERTERS.get(javaKey.getType()).valueForJava(javaKey, starlarkValue);
5360
}
5461

src/main/java/com/google/devtools/build/lib/rules/objc/ObjcRuleClasses.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,7 @@ public Metadata getMetadata() {
253253
}
254254
}
255255

256-
/**
257-
* Iff a file matches this type, it is considered to use C++.
258-
*/
256+
/** Iff a file matches this type, it is considered to use C++. */
259257
static final FileType CPP_SOURCES = FileType.of(".cc", ".cpp", ".mm", ".cxx", ".C");
260258

261259
static final FileType NON_CPP_SOURCES = FileType.of(".m", ".c");

src/main/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkInternal.java

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.google.devtools.build.lib.rules.objc;
1616

17+
import static com.google.common.collect.ImmutableList.toImmutableList;
18+
1719
import com.google.common.collect.ImmutableList;
1820
import com.google.common.collect.ImmutableMap;
1921
import com.google.devtools.build.docgen.annot.DocCategory;
@@ -25,9 +27,11 @@
2527
import com.google.devtools.build.lib.packages.NativeInfo;
2628
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
2729
import com.google.devtools.build.lib.packages.Type;
30+
import com.google.devtools.build.lib.rules.cpp.CcCompilationContext;
2831
import com.google.devtools.build.lib.rules.cpp.CppSemantics;
2932
import com.google.devtools.build.lib.shell.ShellUtils;
3033
import com.google.devtools.build.lib.shell.ShellUtils.TokenizationException;
34+
import com.google.devtools.build.lib.vfs.PathFragment;
3135
import java.util.ArrayList;
3236
import java.util.List;
3337
import javax.annotation.Nullable;
@@ -76,10 +80,11 @@ public CompilationAttributes createCompilationAttributes(StarlarkRuleContext sta
7680
builder, starlarkRuleContext.getRuleContext());
7781
CompilationAttributes.Builder.addSdkAttributesFromRuleContext(
7882
builder, starlarkRuleContext.getRuleContext());
79-
List<String> copts =
83+
Sequence<String> copts =
8084
expandToolchainAndRuleContextVariables(
8185
starlarkRuleContext,
82-
starlarkRuleContext.getRuleContext().attributes().get("copts", Type.STRING_LIST));
86+
StarlarkList.immutableCopyOf(
87+
starlarkRuleContext.getRuleContext().attributes().get("copts", Type.STRING_LIST)));
8388
CompilationAttributes.Builder.addCompileOptionsFromRuleContext(
8489
builder, starlarkRuleContext.getRuleContext(), copts);
8590
CompilationAttributes.Builder.addModuleOptionsFromRuleContext(
@@ -88,8 +93,15 @@ public CompilationAttributes createCompilationAttributes(StarlarkRuleContext sta
8893
return builder.build();
8994
}
9095

91-
private List<String> expandToolchainAndRuleContextVariables(
92-
StarlarkRuleContext starlarkRuleContext, Iterable<String> flags) throws EvalException {
96+
@StarlarkMethod(
97+
name = "expand_toolchain_and_ctx_variables",
98+
documented = false,
99+
parameters = {
100+
@Param(name = "ctx", positional = false, named = true),
101+
@Param(name = "flags", positional = false, defaultValue = "[]", named = true),
102+
})
103+
public Sequence<String> expandToolchainAndRuleContextVariables(
104+
StarlarkRuleContext starlarkRuleContext, Sequence<?> flags) throws EvalException {
93105
ImmutableMap<String, String> toolchainMap =
94106
starlarkRuleContext
95107
.getRuleContext()
@@ -98,15 +110,15 @@ private List<String> expandToolchainAndRuleContextVariables(
98110
ImmutableMap<String, String> starlarkRuleContextMap =
99111
ImmutableMap.<String, String>builder().putAll(starlarkRuleContext.var()).build();
100112
List<String> expandedFlags = new ArrayList<>();
101-
for (String flag : flags) {
113+
for (String flag : Sequence.cast(flags, String.class, "flags")) {
102114
String expandedFlag = expandFlag(flag, toolchainMap, starlarkRuleContextMap);
103115
try {
104116
ShellUtils.tokenize(expandedFlags, expandedFlag);
105117
} catch (TokenizationException e) {
106118
throw new EvalException(e);
107119
}
108120
}
109-
return expandedFlags;
121+
return StarlarkList.immutableCopyOf(expandedFlags);
110122
}
111123

112124
private String expandFlag(
@@ -280,4 +292,55 @@ public InstrumentedFilesInfo createInstrumentedFilesInfo(
280292
starlarkRuleContext.getRuleContext(),
281293
Sequence.cast(objectFiles, Artifact.class, "object_files").getImmutableList());
282294
}
295+
296+
@StarlarkMethod(
297+
name = "create_compilation_context",
298+
documented = false,
299+
parameters = {
300+
@Param(name = "public_hdrs", positional = false, defaultValue = "[]", named = true),
301+
@Param(name = "public_textual_hdrs", positional = false, defaultValue = "[]", named = true),
302+
@Param(name = "private_hdrs", positional = false, defaultValue = "[]", named = true),
303+
@Param(name = "providers", positional = false, defaultValue = "[]", named = true),
304+
@Param(
305+
name = "direct_cc_compilation_contexts",
306+
positional = false,
307+
defaultValue = "[]",
308+
named = true),
309+
@Param(
310+
name = "cc_compilation_contexts",
311+
positional = false,
312+
defaultValue = "[]",
313+
named = true),
314+
@Param(name = "defines", positional = false, defaultValue = "[]", named = true),
315+
@Param(name = "includes", positional = false, defaultValue = "[]", named = true),
316+
})
317+
public ObjcCompilationContext createCompilationContext(
318+
Sequence<?> publicHdrs,
319+
Sequence<?> publicTextualHdrs,
320+
Sequence<?> privateHdrs,
321+
Sequence<?> providers,
322+
Sequence<?> directCcCompilationContexts,
323+
Sequence<?> ccCompilationContexts,
324+
Sequence<?> defines,
325+
Sequence<?> includes)
326+
throws InterruptedException, EvalException {
327+
return ObjcCompilationContext.builder()
328+
.addPublicHeaders(Sequence.cast(publicHdrs, Artifact.class, "public_hdrs"))
329+
.addPublicTextualHeaders(
330+
Sequence.cast(publicTextualHdrs, Artifact.class, "public_textual_hdrs"))
331+
.addPrivateHeaders(Sequence.cast(privateHdrs, Artifact.class, "private_hdrs"))
332+
.addObjcProviders(Sequence.cast(providers, ObjcProvider.class, "providers"))
333+
.addDirectCcCompilationContexts(
334+
Sequence.cast(
335+
directCcCompilationContexts, CcCompilationContext.class, "cc_compilation_contexts"))
336+
.addCcCompilationContexts(
337+
Sequence.cast(
338+
ccCompilationContexts, CcCompilationContext.class, "cc_compilation_contexts"))
339+
.addDefines(Sequence.cast(defines, String.class, "defines"))
340+
.addIncludes(
341+
Sequence.cast(includes, String.class, "includes").stream()
342+
.map(PathFragment::create)
343+
.collect(toImmutableList()))
344+
.build();
345+
}
283346
}

0 commit comments

Comments
 (0)