-
Notifications
You must be signed in to change notification settings - Fork 43
/
DependencyResolver.groovy
198 lines (181 loc) · 9.28 KB
/
DependencyResolver.groovy
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (c) 2015 the authors of j2objc-gradle (see AUTHORS file)
*
* 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.
*/
package com.github.j2objccontrib.j2objcgradle
import groovy.transform.CompileStatic
import groovy.transform.PackageScope
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.SelfResolvingDependency
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.bundling.AbstractArchiveTask
import org.gradle.api.tasks.util.PatternSet
/**
* Resolves `j2objc*` dependencies into their `j2objc` constructs:
* <p/>
* <ul>
* <li><b>j2objcTranslationClosure</b> - The plugin will translate only the subset of
* the configuration's source jars that are actually used by this project's
* code (via --build-closure), and
* compile and link the translated code directly into this project's libraries.
* Note that if multiple projects use j2objcTranslationClosure with the same
* external library, you will likely get duplicate symbol definition errors
* when linking them together. Consider instead creating a separate Gradle
* project for that external library using j2objcTranslation.
* </li>
* <li><b>j2objcTranslation</b> - The plugin will translate the entire source jar
* provided in this configuration. Usually, this configuration is used
* to translate a single external Java library into a standalone Objective C library, that
* can then be linked (via j2objcLinkage) into your projects.
* </li>
* <li><b>j2objcLinkage</b> - The plugin will include the headers of, and link to
* the static library within, the referenced project. Usually this configuration
* is used with other projects (your own, or external libraries translated
* with j2objcTranslation) that the J2ObjC Gradle Plugin has also been applied to.
* </li>
* </ul>
*/
@PackageScope
@CompileStatic
class DependencyResolver {
final Project project
final J2objcConfig j2objcConfig
DependencyResolver(Project project, J2objcConfig j2objcConfig) {
this.project = project
this.j2objcConfig = j2objcConfig
}
void configureAll() {
project.configurations.getByName('j2objcTranslationClosure').each { File it ->
// These are the resolved files, NOT the dependencies themselves.
// Usually source jars.
visitTranslationClosureFile(it)
}
project.configurations.getByName('j2objcTranslation').each { File it ->
// These are the resolved files, NOT the dependencies themselves.
// Usually source jars.
visitTranslationSourceJar(it, false)
}
project.configurations.getByName('j2objcTestTranslation').each { File it ->
// These are the resolved files, NOT the dependencies themselves.
// Usually source jars.
visitTranslationSourceJar(it, true)
}
project.configurations.getByName('j2objcLinkage').dependencies.each {
visitLink(it, false)
}
project.configurations.getByName('j2objcTestLinkage').dependencies.each {
visitLink(it, true)
}
}
protected void visitTranslationClosureFile(File depFile) {
j2objcConfig.translateSourcepaths(depFile.absolutePath)
j2objcConfig.enableBuildClosure()
}
private static final String MAIN_EXTRACTION_TASK_NAME = 'j2objcTranslatedMainLibraryExtraction'
private static final String TEST_EXTRACTION_TASK_NAME = 'j2objcTranslatedTestLibraryExtraction'
/**
* Adds to the main java sourceSet a to-be-generated directory that contains the contents
* of `j2objcTranslation` dependency libraries (if any).
*/
static void configureSourceSets(Project project) {
configureSourceSet(project, "${project.buildDir}/mainTranslationExtraction", SourceSet.MAIN_SOURCE_SET_NAME,
MAIN_EXTRACTION_TASK_NAME)
configureSourceSet(project, "${project.buildDir}/testTranslationExtraction", SourceSet.TEST_SOURCE_SET_NAME,
TEST_EXTRACTION_TASK_NAME)
}
protected static void configureSourceSet(Project project, String dir, String sourceSetName, String taskName) {
JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention)
SourceSet sourceSet = javaConvention.sourceSets.findByName(sourceSetName)
sourceSet.java.srcDirs(project.file(dir))
Copy copy = project.tasks.create(taskName, Copy,
{ Copy task ->
task.into(project.file(dir))
// If two libraries define the same file, fail early.
task.duplicatesStrategy = DuplicatesStrategy.FAIL
})
project.tasks.getByName(sourceSet.compileJavaTaskName).dependsOn(copy)
}
// Copy contents of sourceJarFile to build/translationExtraction
protected void visitTranslationSourceJar(File sourceJarFile, boolean isTest) {
if (!sourceJarFile.absolutePath.endsWith('.jar')) {
String msg = "`j2objc[Test]Translation` dependencies can only handle " +
"source jar files, not ${sourceJarFile.absolutePath}"
throw new InvalidUserDataException(msg)
}
PatternSet pattern = new PatternSet()
pattern.include('**/*.java')
Copy copy = project.tasks.getByName(isTest ? TEST_EXTRACTION_TASK_NAME : MAIN_EXTRACTION_TASK_NAME) as Copy
copy.from(project.zipTree(sourceJarFile).matching(pattern))
}
protected void visitLink(Dependency dep, boolean isTest) {
if (dep instanceof ProjectDependency) {
visitLinkProjectDependency((ProjectDependency) dep, isTest)
} else if (dep instanceof SelfResolvingDependency) {
visitLinkSelfResolvingDependency((SelfResolvingDependency) dep, isTest)
} else {
visitLinkGenericDependency(dep, isTest)
}
}
protected void visitLinkSelfResolvingDependency(
SelfResolvingDependency dep, boolean isTest) {
// TODO: handle native prebuilt libraries as files.
throw new UnsupportedOperationException(
"Cannot automatically link J2ObjC dependency: $dep, test: $isTest")
}
protected void visitLinkProjectDependency(ProjectDependency dep, boolean isTest) {
Project beforeProject = dep.dependencyProject
// We need to have j2objcConfig on the beforeProject configured first.
project.evaluationDependsOn beforeProject.path
if (!beforeProject.plugins.hasPlugin(JavaPlugin)) {
String message = "$beforeProject is not a Java project.\n" +
"dependsOnJ2ObjcLib can only automatically resolve a\n" +
"dependency on a Java project also converted using the\n" +
"J2ObjC Gradle Plugin."
throw new InvalidUserDataException(message)
}
if (!beforeProject.plugins.hasPlugin(J2objcPlugin)) {
String message = "$beforeProject does not use the J2ObjC Gradle Plugin.\n" +
"dependsOnJ2objcLib can be used only with another project that\n" +
"itself uses the J2ObjC Gradle Plugin."
throw new InvalidUserDataException(message)
}
// Build and test the java/objc libraries and the objc headers of
// the other project first.
// Since we assert the presence of the J2objcPlugin above,
// we are guaranteed that the java plugin, which creates the jar task,
// is also present.
Task j2objcPrebuild = project.tasks.getByName('j2objcPreBuild')
project.logger.debug("${project.name}:j2objcPreBuild dependsOn ${beforeProject.name}:j2objcBuild")
project.logger.debug("${project.name}:j2objcPreBuild dependsOn ${beforeProject.name}:jar")
j2objcPrebuild.dependsOn(beforeProject.tasks.getByName('j2objcBuild'))
j2objcPrebuild.dependsOn(beforeProject.tasks.getByName('jar'))
AbstractArchiveTask jarTask = beforeProject.tasks.getByName('jar') as AbstractArchiveTask
project.logger.debug("$project:j2objcTranslate must use ${jarTask.archivePath}")
// TODO: Handle separate classpaths for main translation and test translation.
j2objcConfig.translateClasspaths += jarTask.archivePath.absolutePath
j2objcConfig.nativeCompilation.dependsOnJ2objcLib(beforeProject, isTest)
}
protected void visitLinkGenericDependency(Dependency dep, boolean isTest) {
throw new UnsupportedOperationException(
"Cannot automatically link J2ObjC dependency: $dep, test: $isTest")
}
}