Skip to content

Commit c4ed812

Browse files
authored
Add a method to manually grab native dependencies (#187)
Grapple needs this to be able to hook into their rust libraries easier. Also much cleaner and easier to use
1 parent a297000 commit c4ed812

14 files changed

+154
-92
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ java {
1313

1414
allprojects {
1515
group = "edu.wpi.first"
16-
version = "2024.6.1"
16+
version = "2024.7.0"
1717

1818
if (project.hasProperty('publishVersion')) {
1919
version = project.publishVersion

src/main/java/edu/wpi/first/nativeutils/NativeUtilsExtension.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.gradle.api.model.ObjectFactory;
1717
import org.gradle.api.tasks.TaskProvider;
1818
import org.gradle.internal.os.OperatingSystem;
19+
import org.gradle.nativeplatform.BuildTypeContainer;
1920
import org.gradle.nativeplatform.NativeBinarySpec;
2021
import org.gradle.nativeplatform.StaticLibraryBinarySpec;
2122
import org.gradle.nativeplatform.platform.NativePlatform;
@@ -253,6 +254,22 @@ public void usePlatform(PlatformAwareComponentSpec component, String platform) {
253254
}
254255
}
255256

257+
private PlatformContainer platforms;
258+
public PlatformContainer getPlatforms() {
259+
return platforms;
260+
}
261+
262+
private BuildTypeContainer buildTypes;
263+
264+
public BuildTypeContainer getBuildTypes() {
265+
return buildTypes;
266+
}
267+
268+
public void addPlatformsAndBuildTypes(PlatformContainer platforms, BuildTypeContainer buildTypes) {
269+
this.platforms = platforms;
270+
this.buildTypes = buildTypes;
271+
}
272+
256273
// Internal, used from the model to add the platforms
257274
public void addPlatformsToConfigure(PlatformContainer platforms) {
258275
List<String> tmpList = new ArrayList<>();

src/main/java/edu/wpi/first/nativeutils/dependencies/AllPlatformsCombinedNativeDependency.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package edu.wpi.first.nativeutils.dependencies;
22

33
import java.util.List;
4+
import java.util.Optional;
45

56
import javax.inject.Inject;
67

78
import org.gradle.api.NamedDomainObjectCollection;
89
import org.gradle.api.file.FileCollection;
910
import org.gradle.api.file.ProjectLayout;
1011
import org.gradle.api.provider.ListProperty;
11-
import org.gradle.nativeplatform.NativeBinarySpec;
12+
import org.gradle.nativeplatform.BuildType;
13+
import org.gradle.nativeplatform.platform.NativePlatform;
1214

1315
public abstract class AllPlatformsCombinedNativeDependency implements NativeDependency {
1416
private final String name;
@@ -33,7 +35,7 @@ public String getName() {
3335
}
3436

3537
@Override
36-
public ResolvedNativeDependency resolveNativeDependency(NativeBinarySpec binary, FastDownloadDependencySet loaderDependencySet) {
38+
public Optional<ResolvedNativeDependency> resolveNativeDependency(NativePlatform platform, BuildType buildType, Optional<FastDownloadDependencySet> loaderDependencySet) {
3739
List<String> dependencies = getDependencies().get();
3840

3941
ProjectLayout projectLayout = getProjectLayout();
@@ -44,13 +46,13 @@ public ResolvedNativeDependency resolveNativeDependency(NativeBinarySpec binary,
4446
FileCollection runtimeFiles = projectLayout.files();
4547

4648
for (String dep : dependencies) {
47-
ResolvedNativeDependency resolved = dependencyCollection.getByName(dep).resolveNativeDependency(binary, loaderDependencySet);
49+
ResolvedNativeDependency resolved = dependencyCollection.getByName(dep).resolveNativeDependency(platform, buildType, loaderDependencySet).get();
4850
includeRoots = includeRoots.plus(resolved.getIncludeRoots());
4951
sourceRoots = sourceRoots.plus(resolved.getSourceRoots());
5052
linkFiles = linkFiles.plus(resolved.getLinkFiles());
5153
runtimeFiles = runtimeFiles.plus(resolved.getRuntimeFiles());
5254
}
5355

54-
return new ResolvedNativeDependency(includeRoots, sourceRoots, linkFiles, runtimeFiles);
56+
return Optional.of(new ResolvedNativeDependency(includeRoots, sourceRoots, linkFiles, runtimeFiles));
5557
}
5658
}

src/main/java/edu/wpi/first/nativeutils/dependencies/CombinedIgnoreMissingPlatformNativeDependency.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
import java.util.List;
44
import java.util.Map;
5+
import java.util.Optional;
56

67
import javax.inject.Inject;
78

89
import org.gradle.api.NamedDomainObjectCollection;
910
import org.gradle.api.file.FileCollection;
1011
import org.gradle.api.file.ProjectLayout;
1112
import org.gradle.api.provider.MapProperty;
12-
import org.gradle.nativeplatform.NativeBinarySpec;
13+
import org.gradle.nativeplatform.BuildType;
14+
import org.gradle.nativeplatform.platform.NativePlatform;
1315

1416
public abstract class CombinedIgnoreMissingPlatformNativeDependency implements NativeDependency {
1517

@@ -35,7 +37,7 @@ public String getName() {
3537
}
3638

3739
@Override
38-
public ResolvedNativeDependency resolveNativeDependency(NativeBinarySpec binary, FastDownloadDependencySet loaderDependencySet) {
40+
public Optional<ResolvedNativeDependency> resolveNativeDependency(NativePlatform platform, BuildType buildType, Optional<FastDownloadDependencySet> loaderDependencySet) {
3941
Map<String, List<String>> dependencies = getDependencies().get();
4042

4143
ProjectLayout projectLayout = getProjectLayout();
@@ -45,19 +47,19 @@ public ResolvedNativeDependency resolveNativeDependency(NativeBinarySpec binary,
4547
FileCollection linkFiles = projectLayout.files();
4648
FileCollection runtimeFiles = projectLayout.files();
4749

48-
List<String> depsForPlatform = dependencies.getOrDefault(binary.getTargetPlatform().getName(), null);
50+
List<String> depsForPlatform = dependencies.getOrDefault(platform.getName(), null);
4951
if (depsForPlatform == null) {
50-
return new ResolvedNativeDependency(includeRoots, sourceRoots, linkFiles, runtimeFiles);
52+
return Optional.of(new ResolvedNativeDependency(includeRoots, sourceRoots, linkFiles, runtimeFiles));
5153
}
5254

5355
for (String dep : depsForPlatform) {
54-
ResolvedNativeDependency resolved = dependencyCollection.getByName(dep).resolveNativeDependency(binary, loaderDependencySet);
56+
ResolvedNativeDependency resolved = dependencyCollection.getByName(dep).resolveNativeDependency(platform, buildType, loaderDependencySet).get();
5557
includeRoots = includeRoots.plus(resolved.getIncludeRoots());
5658
sourceRoots = sourceRoots.plus(resolved.getSourceRoots());
5759
linkFiles = linkFiles.plus(resolved.getLinkFiles());
5860
runtimeFiles = runtimeFiles.plus(resolved.getRuntimeFiles());
5961
}
6062

61-
return new ResolvedNativeDependency(includeRoots, sourceRoots, linkFiles, runtimeFiles);
63+
return Optional.of(new ResolvedNativeDependency(includeRoots, sourceRoots, linkFiles, runtimeFiles));
6264
}
6365
}

src/main/java/edu/wpi/first/nativeutils/dependencies/CombinedNativeDependency.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
import java.util.List;
44
import java.util.Map;
5+
import java.util.Optional;
56

67
import javax.inject.Inject;
78

89
import org.gradle.api.NamedDomainObjectCollection;
910
import org.gradle.api.file.FileCollection;
1011
import org.gradle.api.file.ProjectLayout;
1112
import org.gradle.api.provider.MapProperty;
12-
import org.gradle.nativeplatform.NativeBinarySpec;
13+
import org.gradle.nativeplatform.BuildType;
14+
import org.gradle.nativeplatform.platform.NativePlatform;
1315

1416
public abstract class CombinedNativeDependency implements NativeDependency {
1517

@@ -35,12 +37,12 @@ public String getName() {
3537
}
3638

3739
@Override
38-
public ResolvedNativeDependency resolveNativeDependency(NativeBinarySpec binary, FastDownloadDependencySet loaderDependencySet) {
40+
public Optional<ResolvedNativeDependency> resolveNativeDependency(NativePlatform platform, BuildType buildType, Optional<FastDownloadDependencySet> loaderDependencySet) {
3941
Map<String, List<String>> dependencies = getDependencies().get();
4042

41-
List<String> depsForPlatform = dependencies.getOrDefault(binary.getTargetPlatform().getName(), null);
43+
List<String> depsForPlatform = dependencies.getOrDefault(platform.getName(), null);
4244
if (depsForPlatform == null) {
43-
return null;
45+
return Optional.empty();
4446
}
4547

4648
ProjectLayout projectLayout = getProjectLayout();
@@ -51,13 +53,13 @@ public ResolvedNativeDependency resolveNativeDependency(NativeBinarySpec binary,
5153
FileCollection runtimeFiles = projectLayout.files();
5254

5355
for (String dep : depsForPlatform) {
54-
ResolvedNativeDependency resolved = dependencyCollection.getByName(dep).resolveNativeDependency(binary, loaderDependencySet);
56+
ResolvedNativeDependency resolved = dependencyCollection.getByName(dep).resolveNativeDependency(platform, buildType, loaderDependencySet).get();
5557
includeRoots = includeRoots.plus(resolved.getIncludeRoots());
5658
sourceRoots = sourceRoots.plus(resolved.getSourceRoots());
5759
linkFiles = linkFiles.plus(resolved.getLinkFiles());
5860
runtimeFiles = runtimeFiles.plus(resolved.getRuntimeFiles());
5961
}
6062

61-
return new ResolvedNativeDependency(includeRoots, sourceRoots, linkFiles, runtimeFiles);
63+
return Optional.of(new ResolvedNativeDependency(includeRoots, sourceRoots, linkFiles, runtimeFiles));
6264
}
6365
}

src/main/java/edu/wpi/first/nativeutils/dependencies/DelegatedDependencySet.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package edu.wpi.first.nativeutils.dependencies;
22

33
import java.util.Objects;
4+
import java.util.Optional;
45

56
import javax.inject.Inject;
67

@@ -24,7 +25,7 @@ public class DelegatedDependencySet implements NativeDependencySet, Named, Sourc
2425
private final NamedDomainObjectCollection<NativeDependency> dependencyCollection;
2526
private boolean resolved = false;
2627
private final NativeBinarySpec binary;
27-
private final FastDownloadDependencySet fastDownloadSet;
28+
private final Optional<FastDownloadDependencySet> fastDownloadSet;
2829

2930
@Inject
3031
public ProjectLayout getProjectLayout() {
@@ -37,7 +38,7 @@ public DelegatedDependencySet(String name, NamedDomainObjectCollection<NativeDep
3738
this.required = required;
3839
this.dependencyCollection = Objects.requireNonNull(dependencyCollection, "Must have a valid depenedency collection");
3940
this.binary = binary;
40-
this.fastDownloadSet = Objects.requireNonNull(fastDownloadSet);
41+
this.fastDownloadSet = Optional.of(fastDownloadSet);
4142
resolve();
4243
}
4344

@@ -69,9 +70,9 @@ private void resolve() {
6970
return;
7071
}
7172

72-
ResolvedNativeDependency resolvedDep = resolvedDependency.resolveNativeDependency(binary, fastDownloadSet);
73+
Optional<ResolvedNativeDependency> resolvedDep = resolvedDependency.resolveNativeDependency(binary.getTargetPlatform(), binary.getBuildType(), fastDownloadSet);
7374

74-
if (resolvedDep == null) {
75+
if (resolvedDep.isEmpty()) {
7576
if (required) {
7677
// TODO better exceptions
7778
throw new GradleException("Missing Dependency " + resolvedDependency.getName());
@@ -84,10 +85,12 @@ private void resolve() {
8485
return;
8586
}
8687

87-
includeRoots = resolvedDep.getIncludeRoots();
88-
sourceRoots = resolvedDep.getSourceRoots();
89-
linkFiles = resolvedDep.getLinkFiles();
90-
runtimeFiles = resolvedDep.getRuntimeFiles();
88+
ResolvedNativeDependency dep = resolvedDep.get();
89+
90+
includeRoots = dep.getIncludeRoots();
91+
sourceRoots = dep.getSourceRoots();
92+
linkFiles = dep.getLinkFiles();
93+
runtimeFiles = dep.getRuntimeFiles();
9194
}
9295

9396
@Override
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package edu.wpi.first.nativeutils.dependencies;
22

3+
import java.util.Optional;
4+
35
import org.gradle.api.Named;
4-
import org.gradle.nativeplatform.NativeBinarySpec;
6+
import org.gradle.nativeplatform.BuildType;
7+
import org.gradle.nativeplatform.platform.NativePlatform;
58

69
public interface NativeDependency extends Named {
7-
ResolvedNativeDependency resolveNativeDependency(NativeBinarySpec binary, FastDownloadDependencySet loaderDependencySet);
10+
Optional<ResolvedNativeDependency> resolveNativeDependency(NativePlatform platform, BuildType buildType, Optional<FastDownloadDependencySet> loaderDependencySet);
811
}

src/main/java/edu/wpi/first/nativeutils/dependencies/WPIHeaderOnlyMavenDependency.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package edu.wpi.first.nativeutils.dependencies;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
3+
import java.util.Optional;
54

65
import javax.inject.Inject;
76

87
import org.gradle.api.Project;
98
import org.gradle.api.file.FileCollection;
109
import org.gradle.api.provider.Property;
11-
import org.gradle.nativeplatform.NativeBinarySpec;
10+
import org.gradle.nativeplatform.BuildType;
11+
import org.gradle.nativeplatform.platform.NativePlatform;
1212

1313
public abstract class WPIHeaderOnlyMavenDependency extends WPIMavenDependency {
1414
@Inject
1515
public WPIHeaderOnlyMavenDependency(String name, Project project) {
1616
super(name, project);
1717
}
1818

19-
private final Map<NativeBinarySpec, ResolvedNativeDependency> resolvedDependencies = new HashMap<>();
20-
2119
@Override
22-
public ResolvedNativeDependency resolveNativeDependency(NativeBinarySpec binary, FastDownloadDependencySet loaderDependencySet) {
23-
ResolvedNativeDependency resolvedDep = resolvedDependencies.get(binary);
24-
if (resolvedDep != null) {
20+
public Optional<ResolvedNativeDependency> resolveNativeDependency(NativePlatform platform, BuildType buildType, Optional<FastDownloadDependencySet> loaderDependencySet) {
21+
Optional<ResolvedNativeDependency> resolvedDep = tryFromCache(platform, buildType);
22+
if (resolvedDep.isPresent()) {
2523
return resolvedDep;
2624
}
2725

@@ -31,9 +29,9 @@ public ResolvedNativeDependency resolveNativeDependency(NativeBinarySpec binary,
3129
FileCollection linkFiles = getProject().files();
3230
FileCollection runtimeFiles = getProject().files();
3331

34-
resolvedDep = new ResolvedNativeDependency(headers, sources, linkFiles, runtimeFiles);
32+
resolvedDep = Optional.of(new ResolvedNativeDependency(headers, sources, linkFiles, runtimeFiles));
3533

36-
resolvedDependencies.put(binary, resolvedDep);
34+
addToCache(platform, buildType, resolvedDep);
3735
return resolvedDep;
3836
}
3937

src/main/java/edu/wpi/first/nativeutils/dependencies/WPIMavenDependency.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.HashMap;
55
import java.util.List;
66
import java.util.Map;
7+
import java.util.Optional;
78
import java.util.Set;
89
import java.util.concurrent.Callable;
910

@@ -17,13 +18,38 @@
1718
import org.gradle.api.provider.SetProperty;
1819
import org.gradle.api.tasks.util.PatternFilterable;
1920
import org.gradle.api.tasks.util.PatternSet;
21+
import org.gradle.nativeplatform.BuildType;
22+
import org.gradle.nativeplatform.platform.NativePlatform;
2023

2124
import edu.wpi.first.nativeutils.NativeUtils;
2225

2326
public abstract class WPIMavenDependency implements NativeDependency {
2427
private final String name;
2528
private final Project project;
2629

30+
private final Map<NativePlatform, Map<BuildType, Optional<ResolvedNativeDependency>>> resolvedDependencies = new HashMap<>();
31+
32+
private Map<BuildType, Optional<ResolvedNativeDependency>> getInnerMap(NativePlatform platform) {
33+
Map<BuildType, Optional<ResolvedNativeDependency>> buildTypeMap = resolvedDependencies.get(platform);
34+
35+
if (buildTypeMap == null) {
36+
buildTypeMap = new HashMap<>();
37+
resolvedDependencies.put(platform, buildTypeMap);
38+
}
39+
40+
return buildTypeMap;
41+
}
42+
43+
protected void addToCache(NativePlatform platform, BuildType buildType, Optional<ResolvedNativeDependency> dependency) {
44+
Map<BuildType, Optional<ResolvedNativeDependency>> innerMap = getInnerMap(platform);
45+
innerMap.put(buildType, dependency);
46+
}
47+
48+
protected Optional<ResolvedNativeDependency> tryFromCache(NativePlatform platform, BuildType buildType) {
49+
Map<BuildType, Optional<ResolvedNativeDependency>> innerMap = getInnerMap(platform);
50+
return innerMap.getOrDefault(buildType, Optional.empty());
51+
}
52+
2753
@Inject
2854
public WPIMavenDependency(String name, Project project) {
2955
this.name = name;
@@ -42,7 +68,7 @@ private static class ViewConfigurationContainer {
4268

4369
private final Map<String, ViewConfigurationContainer> classifierViewMap = new HashMap<>();
4470

45-
protected FileCollection getArtifactRoots(String classifier, ArtifactType type, FastDownloadDependencySet loaderDependencySet) {
71+
protected FileCollection getArtifactRoots(String classifier, ArtifactType type, Optional<FastDownloadDependencySet> loaderDependencySet) {
4672
if (classifier == null) {
4773
return project.files();
4874
}
@@ -52,7 +78,7 @@ protected FileCollection getArtifactRoots(String classifier, ArtifactType type,
5278
}
5379

5480
protected FileCollection getArtifactFiles(String targetPlatform, String buildType, List<String> matches,
55-
List<String> excludes, ArtifactType type, FastDownloadDependencySet loaderDependencySet) {
81+
List<String> excludes, ArtifactType type, Optional<FastDownloadDependencySet> loaderDependencySet) {
5682
buildType = buildType.equalsIgnoreCase("debug") ? "debug" : "";
5783
ArtifactView view = getViewForArtifact(targetPlatform + buildType, type, loaderDependencySet);
5884
PatternFilterable filterable = new PatternSet();
@@ -62,16 +88,20 @@ protected FileCollection getArtifactFiles(String targetPlatform, String buildTyp
6288
return project.files(cbl);
6389
}
6490

65-
protected ArtifactView getViewForArtifact(String classifier, ArtifactType type, FastDownloadDependencySet loaderDependencySet) {
91+
protected ArtifactView getViewForArtifact(String classifier, ArtifactType type, Optional<FastDownloadDependencySet> loaderDependencySet) {
6692
ViewConfigurationContainer viewContainer = classifierViewMap.get(classifier);
6793
String configName = name + "_" + classifier;
6894
if (viewContainer != null) {
69-
loaderDependencySet.addConfiguration(type, viewContainer.configuration);
95+
if (loaderDependencySet.isPresent()) {
96+
loaderDependencySet.get().addConfiguration(type, viewContainer.configuration);
97+
}
7098
return viewContainer.view;
7199
}
72-
100+
73101
Configuration cfg = project.getConfigurations().create(configName);
74-
loaderDependencySet.addConfiguration(type, cfg);
102+
if (loaderDependencySet.isPresent()) {
103+
loaderDependencySet.get().addConfiguration(type, cfg);
104+
}
75105
String dep = getGroupId().get() + ":" + getArtifactId().get() + ":" + getVersion().get() + ":" + classifier
76106
+ "@" + getExt().get();
77107
project.getDependencies().add(configName, dep);

0 commit comments

Comments
 (0)