generated from WilderForge/ExampleMod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverifyCompilation.gradle
More file actions
130 lines (102 loc) · 3.97 KB
/
verifyCompilation.gradle
File metadata and controls
130 lines (102 loc) · 3.97 KB
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
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.ow2.asm:asm:9.8'
classpath 'org.ow2.asm:asm-tree:9.8'
}
}
import org.objectweb.asm.*
import org.objectweb.asm.tree.*
def versionMap = [
'1.0' : 45, '1.1' : 45, '1.2' : 46, '1.3' : 47, '1.4' : 48, '1.5' : 49, '1.6' : 50,
'1.7' : 51, '1.8' : 52, '9' : 53, '10' : 54, '11' : 55, '12' : 56, '13' : 57,
'14' : 58, '15' : 59, '16' : 60, '17' : 61, '18' : 62, '19' : 63, '20' : 64,
'21' : 65, '22' : 66, '23' : 67, '24' : 68, '25' : 69
]
def getProjectCompilationVersionConstant = { ->
def annotationClassFile = file("$buildDir/classes/java/java8/com/wildermods/multimyth/internal/CompileStrictly.class") //LITERAL REFERENCE
if (!annotationClassFile.exists()) {
throw new GradleException("Can't find CompileStrictly.class at ${annotationClassFile}")
}
def inputStream = annotationClassFile.newInputStream()
def cr = new ClassReader(inputStream)
def classNode = new ClassNode()
cr.accept(classNode, 0)
inputStream.close()
def field = classNode.fields.find { it.name == "PROJECT_COMPILATION_VERSION" } //LITERAL REFERENCE
if (!field || !field.value) {
throw new GradleException("PROJECT_COMPILATION_VERSION constant not found in CompileStrictly")
}
return field.value.toString()
}
task enforceJavaLevels {
group = 'verification'
description = 'Fails build if any @CompileStrictly classes were compiled with the wrong Java version.'
def classDirs = [
file("$buildDir/classes/java/main"),
file("$buildDir/classes/java/java8")
]
inputs.files classDirs.collect { it.exists() ? fileTree(it) : null }.findAll { it != null }
outputs.upToDateWhen { false }
doLast {
classDirs.each { classDir ->
if (!classDir.exists()) {
logger.lifecycle("Class directory does not exist: ${classDir}")
return
}
logger.lifecycle("Scanning classes in: ${classDir}")
classDir.eachFileRecurse { file ->
if (!file.name.endsWith('.class')) return
logger.lifecycle("Checking class file: ${file}")
def input = file.newInputStream()
def cr = new ClassReader(input)
def classNode = new ClassNode()
cr.accept(classNode, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG)
def allAnnotations = (classNode.visibleAnnotations ?: []) + (classNode.invisibleAnnotations ?: [])
def compileStrictlyDesc = 'Lcom/wildermods/multimyth/internal/CompileStrictly;' //LITERAL REFERENCE
def compileStrictlyAnnotation = allAnnotations.find {
it.desc == compileStrictlyDesc
}
if (compileStrictlyAnnotation) {
logger.lifecycle("Found @CompileStrictly on ${classNode.name} in file ${file}")
def versionStr = getProjectCompilationVersionConstant()
def values = compileStrictlyAnnotation.values
if(values != null) {
compileStrictlyAnnotation.values.collate(2).each { pair ->
if (pair[0] == 'value') {
versionStr = pair[1].toString()
}
}
}
logger.lifecycle("Annotation value: ${versionStr}")
if (versionStr != null) {
def expected = versionMap[versionStr]
if (expected == null) {
throw new GradleException("Unknown Java version '$versionStr' in @CompileStrictly")
}
int actual = cr.readUnsignedShort(6) // ASM recommended way
logger.lifecycle("Class ${classNode.name} bytecode version: $actual (expected $expected)")
if (actual != expected) {
throw new GradleException("Class ${classNode.name} compiled with bytecode $actual, expected $expected (Java $versionStr)")
}
}
}
input.close()
}
}
}
}
def getNormalizedJavaVersion() {
if (project.hasProperty('java') && project.java.toolchain.languageVersion.isPresent()) {
def version = project.java.toolchain.languageVersion.get()
return version.asInt() >= 9 ? version.toString() : "1.${version}"
}
else {
throw new GradleException("Could not determine project's java version")
}
}
// Run this during `check`
check.dependsOn enforceJavaLevels
enforceJavaLevels.mustRunAfter(compileJava)