name.remal.gradle_plugins.toolkit.build_logic.cross-compile.gradle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of build-logic Show documentation
Show all versions of build-logic Show documentation
Remal Gradle plugins: toolkit: build-logic
import groovy.transform.MapConstructor
import java.util.regex.Matcher
import java.util.regex.Pattern
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.tree.AnnotationNode
import org.objectweb.asm.tree.ClassNode
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
buildscript {
dependencies {
classpath platform("org.ow2.asm:asm-bom:9.7")
classpath 'org.ow2.asm:asm-tree'
}
repositories {
mavenCentral()
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
NamedDomainObjectContainer allCrossCompileDependencies = project.objects.domainObjectContainer(CrossCompileDependencyInfo, { String name -> new CrossCompileDependencyInfo(name) })
project.ext.registerCrossCompileDependencyInfo = { Map params ->
CrossCompileDependencyInfo crossCompileDependencyInfo = new CrossCompileDependencyInfo(params)
allCrossCompileDependencies.add(crossCompileDependencyInfo)
}
allCrossCompileDependencies.create('java') {
maxVersionNumbers = 1
configureCrossCompileProject = { Project crossCompileProject, CrossCompileProjectInfo crossCompileProjectInfo ->
int majorVersion = Integer.parseInt(crossCompileProjectInfo.version)
JavaVersion javaVersion = JavaVersion.toVersion(majorVersion)
crossCompileProject.java.sourceCompatibility = crossCompileProject.java.targetCompatibility = javaVersion.toString()
}
}
allCrossCompileDependencies.create('gradle') {
maxVersionNumbers = 3
transitiveDependencyNotations.add('name.remal.gradle-api:local-groovy')
dependencyNotations.add('name.remal.gradle-api:gradle-api')
dependencyNotations.add('name.remal.gradle-api:gradle-test-kit')
}
@MapConstructor
class CrossCompileDependencyInfo implements Named {
int maxVersionNumbers = Integer.MAX_VALUE
Collection transitiveDependencyNotations = [].toSet()
Collection dependencyNotations = [].toSet()
Closure configureCrossCompileProject = { Project crossCompileProject, CrossCompileProjectInfo crossCompileProjectInfo ->
// do nothing by default
}
final String name
CrossCompileDependencyInfo(String name) {
this.name = name
}
void setMaxVersionNumbers(int maxVersionNumbers) {
if (maxVersionNumbers <= 0) throw new IllegalArgumentException("maxVersionNumbers <= 0: $maxVersionNumbers")
this.maxVersionNumbers = maxVersionNumbers
}
@Override
String getName() {
return this.name
}
@Override
String toString() {
return this.name
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
NamedDomainObjectContainer allCrossCompileProjects = project.objects.domainObjectContainer(CrossCompileProjectInfo, { throw new UnsupportedOperationException() })
project.ext.allCrossCompileProjects = allCrossCompileProjects
allprojects {
if (CrossCompileProjectInfo.matches(project)) {
allCrossCompileProjects.add(new CrossCompileProjectInfo(project))
}
}
allCrossCompileProjects.all { CrossCompileProjectInfo crossCompileProjectInfo ->
Project crossCompileProject = crossCompileProjectInfo.project
if (!crossCompileProject.subprojects.isEmpty()) {
throw new GradleException("Cross-compile project ${crossCompileProject} can't have subprojects")
}
Project targetProject = crossCompileProject.parent
if (targetProject == null) {
throw new GradleException("Cross-compile project ${crossCompileProject} can't be root project")
}
; [
'maven-publish',
].forEach { forbiddenPluginId ->
crossCompileProject.pluginManager.withPlugin(forbiddenPluginId) { appliedPlugin ->
throw new GradleException("${appliedPlugin.id} plugin can't applied for cross-compile project ${crossCompileProject}")
}
}
crossCompileProject.apply plugin: 'java-library'
crossCompileProject.apply plugin: 'name.remal.test-source-sets'
targetProject.fatJarWith(crossCompileProject)
crossCompileProject.configurations.matching { ['api', 'implementation'].contains(it.name) }.all { Configuration conf ->
conf.allDependencies.all { Dependency dependency ->
String category = dependency.attributes.getAttribute(Category.CATEGORY_ATTRIBUTE)?.name
if (![Category.REGULAR_PLATFORM, Category.ENFORCED_PLATFORM, Category.DOCUMENTATION, Category.VERIFICATION].contains(category)) {
throw new GradleException("Can't add ${dependency} dependcency to cross-compile project $crossCompileProject '${conf.name}' configuration, as this configuration can't have dependencies")
}
}
}
if (crossCompileProjectInfo.dependency == null) {
targetProject.configurations.matching { it.name == 'compileOnlyAll' }.configureEach { Configuration conf ->
Dependency crossCompileDep = targetProject.dependencies.create(crossCompileProject)
conf.dependencies.add(crossCompileDep)
}
} else {
targetProject.findProject('cross-compile--common')?.with { Project commonProject ->
Dependency dep = crossCompileProject.dependencies.create(commonProject)
crossCompileProject.configurations.optional.dependencies.add(dep)
}
Configuration depsConf = crossCompileProject.configurations.create("crossCompile${crossCompileProjectInfo.dependency.capitalize()}") { Configuration conf ->
conf.canBeResolved = false
conf.canBeConsumed = false
crossCompileProject.configurations.optionalHidden.extendsFrom(conf)
}
Configuration transitiveDepsConf = crossCompileProject.configurations.create("crossCompile${crossCompileProjectInfo.dependency.capitalize()}Transitive") { Configuration conf ->
conf.canBeResolved = false
conf.canBeConsumed = false
conf.defaultDependencies { DependencySet deps ->
deps.addAll(
depsConf.dependencies.toList().collect { Dependency dep ->
dep = dep.copy()
if (dep instanceof ModuleDependency) {
dep.transitive = true
}
return dep
}
)
}
}
allCrossCompileDependencies.matching { it.name == crossCompileProjectInfo.dependency }.all { CrossCompileDependencyInfo crossCompileDependencyInfo ->
int versionNumbers = crossCompileProjectInfo.version.split(/\./).length
if (versionNumbers > crossCompileDependencyInfo.maxVersionNumbers) {
throw new GradleException("Cross-compile project $crossCompileProject can have version of not more than ${crossCompileDependencyInfo.maxVersionNumbers} numbers only")
}
crossCompileDependencyInfo.configureCrossCompileProject(crossCompileProject, crossCompileProjectInfo)
Set allDependencyNotations = (crossCompileDependencyInfo.transitiveDependencyNotations + crossCompileDependencyInfo.dependencyNotations).toSet()
List