-
Notifications
You must be signed in to change notification settings - Fork 19
/
PlatformRule.java
111 lines (106 loc) · 5.62 KB
/
PlatformRule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (C) 2020-2022 Samuel Audet
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
* the Free Software Foundation (subject to the "Classpath" exception),
* either version 2, or any later version (collectively, 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
* http://www.gnu.org/licenses/
* http://www.gnu.org/software/classpath/license.html
*
* or as provided in the LICENSE.txt file that accompanied this code.
* 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.
*/
package org.bytedeco.gradle.javacpp;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import javax.inject.Inject;
import org.gradle.api.Action;
import org.gradle.api.artifacts.ComponentMetadataContext;
import org.gradle.api.artifacts.ComponentMetadataDetails;
import org.gradle.api.artifacts.ComponentMetadataRule;
import org.gradle.api.artifacts.DirectDependencyMetadata;
import org.gradle.api.artifacts.DirectDependenciesMetadata;
import org.gradle.api.artifacts.VariantMetadata;
import org.gradle.api.internal.artifacts.repositories.resolver.AbstractDependencyMetadataAdapter;
import org.gradle.internal.component.external.model.ConfigurationBoundExternalDependencyMetadata;
import org.gradle.internal.component.external.model.ExternalDependencyDescriptor;
import org.gradle.internal.component.external.model.maven.MavenDependencyDescriptor;
import org.gradle.internal.component.model.IvyArtifactName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A rule that looks at dependencies of artifacts containing "-platform" in their names, and
* removes any dependency whose classifier doesn't start with values in the "javacppPlatform" property,
* which allows matches against platform extensions such as "-gpu" without specifying them.
*
* @author Samuel Audet
*/
class PlatformRule implements ComponentMetadataRule {
private final Logger logger = LoggerFactory.getLogger(PlatformRule.class);
final List<String> platform;
/** Takes a comma-separated list of platform names to keep. */
@Inject public PlatformRule(String platform) {
this.platform = Arrays.asList(platform.split(","));
}
@Override public void execute(ComponentMetadataContext context) {
ComponentMetadataDetails component = context.getDetails();
if (!component.getId().getName().contains("-platform")) {
return;
}
component.allVariants(new Action<VariantMetadata>() { public void execute(VariantMetadata variant) {
variant.withDependencies(new Action<DirectDependenciesMetadata>() { public void execute(DirectDependenciesMetadata dependencies) {
Iterator<DirectDependencyMetadata> i = dependencies.iterator();
while (i.hasNext()) {
DirectDependencyMetadata d = i.next();
String classifier = null;
try {
// This only works starting with Gradle 6.3:
List<org.gradle.api.artifacts.DependencyArtifact> as = d.getArtifactSelectors();
if (as != null && as.size() > 0) {
classifier = as.get(0).getClassifier();
}
} catch (NoSuchMethodError ex) {
// So try to get the classifier some other way...
try {
if (d instanceof AbstractDependencyMetadataAdapter) {
Method getMetadata = AbstractDependencyMetadataAdapter.class.getDeclaredMethod("getOriginalMetadata");
getMetadata.setAccessible(true);
Object o = getMetadata.invoke(d);
if (o instanceof ConfigurationBoundExternalDependencyMetadata) {
ConfigurationBoundExternalDependencyMetadata m = (ConfigurationBoundExternalDependencyMetadata)o;
ExternalDependencyDescriptor dd = m.getDependencyDescriptor();
if (dd instanceof MavenDependencyDescriptor) {
MavenDependencyDescriptor mdd = (MavenDependencyDescriptor)dd;
IvyArtifactName da = mdd.getDependencyArtifact();
if (da != null) {
classifier = da.getClassifier();
}
}
}
}
} catch (ReflectiveOperationException e) {
logger.warn("Could not get the classifier of " + d + ": " + e);
}
}
final String c = classifier;
if (classifier != null && platform.stream()
.filter(new Predicate<String>() { public boolean test(String p) { return c.startsWith(p); }}).count() == 0) {
i.remove();
}
}
}});
}});
}
}