File: build.gradle

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,840 kB
  • sloc: java: 145,910; xml: 839; sh: 518; makefile: 401; perl: 26
file content (377 lines) | stat: -rw-r--r-- 13,104 bytes parent folder | download | duplicates (2)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
ext {
    // See instructions for updating jdk8.jar at
    // https://github.com/typetools/annotated-libraries/blob/master/README.md .
    // Set jdkShaHash to 'local' to use a locally-built version of jdk8.jar .
    jdkShaHash = 'f1f958b7e6667b4511162b70274babb2e3005ff3'
    if (rootProject.hasProperty("useLocalJdk")) {
        jdkShaHash = 'local'
    }

    jdkHome = '../../jdk'
}
sourceSets {
    main {
        resources {
            // Stub files, message.properties, etc.
            srcDirs += ['src/main/java', "${buildDir}/jdk"]
        }
    }
    testannotations
}

dependencies {
    implementation project(':framework')
    implementation project(':dataflow')
    implementation project(':javacutil')
    implementation project(':checker-qual')
    // As of 2019/12/16, the version of reflection-util in the Annotation
    // File Utilities takes priority over this version, in the fat jar
    // file. :-( So update it and re-build it locally when updating this.
    implementation 'org.plumelib:reflection-util:0.2.2'

    testImplementation group: 'junit', name: 'junit', version: '4.13'
    testImplementation project(':framework-test')
    testImplementation sourceSets.testannotations.output

    testannotationsImplementation project(':checker-qual')
}

task cloneTypetoolsJdk() {
    description 'Obtain or update the jdk'
    doLast {
        if (file(jdkHome).exists()) {
            exec {
                workingDir jdkHome
                executable 'git'
                args = ['pull', '-q']
                ignoreExitValue = true
            }
        } else {
            println 'Cloning JDK repository.'
            exec {
                workingDir "${jdkHome}/../"
                executable 'git'
                args = ['clone', '-q', '--depth', '1', 'https://github.com/typetools/jdk.git', 'jdk']
            }
        }
    }
}

task copyJdk11Files(dependsOn: cloneTypetoolsJdk, group: 'Build') {
    description "Copy annotated jdk 11 files to ${buildDir}/jdk/jdk11/"
    doLast {
        FileTree tree = fileTree(dir: "${jdkHome}/src/")
        SortedSet<String> annotatedForFiles = new TreeSet<>();
        tree.visit { FileVisitDetails fvd ->
            if (!fvd.file.isDirectory() && fvd.file.name.matches(".*\\.java")) {
                fvd.getFile().readLines().any { line ->
                    if (line.contains("@AnnotatedFor") || line.contains("org.checkerframework")) {
                        annotatedForFiles.add(fvd.file.absolutePath)
                        return true;
                    }
                }
            }
        }
        String absolutejdkHome = file(jdkHome).absolutePath
        int jdkDirStringSize = absolutejdkHome.size()
        copy {
            from(jdkHome)
            into("${buildDir}/jdk/jdk11/")
            for (String filename : annotatedForFiles) {
                include filename.substring(jdkDirStringSize)
            }
        }
    }
}

processResources.dependsOn(copyJdk11Files)

jar {
    manifest {
        attributes("Main-Class": "org.checkerframework.framework.util.CheckerMain")
    }
    doLast {
        new File("$projectDir/build/libs/README.txt").text =
"""Do not use file checker-X.Y.Z.jar, which contains only the checker subproject
and lacks other parts of the Checker Framework.
Instead, use checker/dist/checker.jar.
"""
    }
}

task copyJarsOtherThanJdk8ToDist(dependsOn: shadowJar, group: 'Build') {
    description 'Builds jars required by CheckerMain, except for jdk8.jar, and puts them in checker/dist.'
    dependsOn project(':checker-qual').tasks.jar
    // Also moves jdk8.jar to checker/dist
    doLast {
        copy {
            from file(project(':checker-qual').tasks.getByName("jar").archivePath)
            into "${projectDir}/dist"
            rename { String fileName ->
                // remove version number on checker-qual.jar
                fileName.replace(fileName, "checker-qual.jar")
            }
        }

        copy {
            from configurations.javacJar
            into "${projectDir}/dist"
            rename { String fileName ->
                fileName.replace(fileName, "javac.jar")
            }
        }
    }
}

task forceBuildJdk(group: 'Build') {
    description 'Builds jdk.jar even if no file in the JDK has changed. ' +
            'Use to update jdk.jar if defaulting has changed in a checker.'
    doLast {
        delete 'jdk/jdk.jar'
        buildJdk.execute()
    }
}

task buildJdk(group: 'Build', dependsOn: copyJarsOtherThanJdk8ToDist) {
    description 'Builds jdk8.jar. Requires -PuseLocalJdk, or that jdkShaHash == \'local\' in checker/build.gradle.'

    doLast {
        if (System.getenv('JAVA_HOME') == null) {
            throw new GradleException("Environment variable JAVA_HOME is not set.")
        }
        if (!jdkShaHash.is('local')) {
            throw new GradleException('To build jdk.jar, pass -PuseLocalJdk, or set jdkShaHash to \'local\' in checker/build.gradle.')
        }
        delete 'dist/jdk8.jar'
        exec {
            executable 'make'
            environment CHECKERFRAMEWORK: "${projectDir}/..",
                    ANNOTATION_TOOLS: "${afu}/..",
                    CLASSPATH: project(':checker').tasks.getByName('shadowJar').archivePath
            args += ["-C", "jdk/"]
        }

        copy {
            from 'jdk/jdk.jar'
            rename { String fileName ->
                fileName.replace(fileName, "jdk8.jar")
            }
            into 'jdk'
        }

        copy {
            from 'jdk/jdk8.jar'
            into 'dist'
        }
    }
}

task cleanJdk(type: Exec){
    description 'Runs \'make clean\' in checker/jdk directory.'
    workingDir "${projectDir}/jdk"
    executable 'make'
    args = ["clean"]
}

task updateJdk() {
    description 'Downloads or builds jdk8.jar and copies into checker/dist'
    if (jdkShaHash.is('local')) {
        dependsOn(buildJdk)
    }
    doLast {
        if (!jdkShaHash.is('local')) {
            File downloadedJdk = new File(buildDir, "jdk8-${jdkShaHash}.jar");
            if (!downloadedJdk.exists()) {
                download {
                    src "https://github.com/typetools/annotated-libraries/raw/${jdkShaHash}/jdk8.jar"
                    overwrite true
                    dest downloadedJdk
                }
            }
            copy {
                from(downloadedJdk) {
                    rename downloadedJdk.name, 'jdk8.jar'
                }
                into "dist"
            }
        }
    }
}

task printJdkJarManifest(type: Copy) {
    description 'Outputs the manifest file for checker/dist/jdk8.jar'
    outputs.upToDateWhen { false }

    from(zipTree('dist/jdk8.jar')) {
        include "META-INF/MANIFEST.MF"
    }

    String tmpDir = "${buildDir}/tmpJDKManifest"
    into tmpDir
    doLast {
        println(new File(tmpDir, 'META-INF/MANIFEST.MF').text)
        delete tmpDir
    }

}

task copyJarsToDist(dependsOn: shadowJar, group: 'Build') {
    description 'Builds or downloads jars required by CheckerMain and puts them in checker/dist.'
    dependsOn project(':checker-qual').tasks.jar
    // Also moves jdk8.jar to checker/dist
    dependsOn updateJdk
    dependsOn copyJarsOtherThanJdk8ToDist
}
assemble.dependsOn copyJarsToDist

task printPlumeUtilJarPath {
    description "Print the path to plume-util.jar"
    doFirst { println project.configurations.compile.find { it.name.startsWith("plume-util") } }
}

task allSourcesJar(type: Jar) {
    description 'Creates a sources jar that includes sources for all Checker Framework classes in checker.jar'
    destinationDirectory = file("${projectDir}/dist")
    archiveFileName = "checker-source.jar"
    from (sourceSets.main.java, project(':framework').sourceSets.main.allJava,
            project(':dataflow').sourceSets.main.allJava, project(':javacutil').sourceSets.main.allJava)
}

task allJavadocJar(type: Jar) {
    description 'Creates javadoc jar include Javadoc for all of the framework'
    dependsOn rootProject.tasks.allJavadoc
    destinationDirectory = file("${projectDir}/dist")
    archiveFileName = "checker-javadoc.jar"
    from rootProject.tasks.allJavadoc.destinationDir
}


shadowJar {
    description 'Creates the "fat" checker.jar in dist/.'
    destinationDirectory = file("${projectDir}/dist")
    archiveFileName = "checker.jar"
    // To see what files are incorporated into the shadow jar file:
    // doFirst { println sourceSets.main.runtimeClasspath.asPath }
}
artifacts {
    // Don't add this here or else the Javadoc and the sources jar is built during the assemble task.
    // archives allJavadocJar
    // archives allSourcesJar
    archives shadowJar
}

clean {
    dependsOn cleanJdk
    delete "${projectDir}/dist"
}

// Add non-junit tests
createCheckTypeTask(project.name, 'org.checkerframework.checker.compilermsgs.CompilerMessagesChecker', "CompilerMessages")
checkCompilerMessages {
    doFirst {
        options.compilerArgs += [
                '-Apropfiles=' + sourceSets.main.resources.filter { file -> file.name.equals('messages.properties') }.asPath + ":"
                        + project(':framework').sourceSets.main.resources.filter { file -> file.name.equals('messages.properties') }.asPath
        ]
    }
}

task nullnessExtraTests(type: Exec, dependsOn: copyJarsToDist, group: 'Verification') {
    description 'Run extra tests for the Nullness Checker.'
    executable 'make'
    environment JAVAC: "${projectDir}/bin/javac", JAVAP: 'javap'
    args = ['-C', 'tests/nullness-extra/']
}

task commandLineTests(type: Exec, dependsOn: copyJarsToDist, group: 'Verification') {
    description 'Run tests that need a special command line.'
    executable 'make'
    environment JAVAC: "${projectDir}/bin/javac"
    args = ['-C', 'tests/command-line/']
}

task tutorialTests(dependsOn: copyJarsToDist, group: 'Verification') {
    description 'Test that the tutorial is working as expected.'
    doLast {
        ant.ant(dir: "${rootDir}/docs/tutorial/tests", useNativeBasedir: 'true', inheritAll: 'false') {
            target(name: 'check-tutorial')
        }
    }
}

task exampleTests(type: Exec, dependsOn: copyJarsToDist, group: 'Verification') {
    description 'Run tests for the example programs.'
    executable 'make'
    environment JAVAC: "${projectDir}/bin/javac"
    args = ['-C', '../docs/examples']
}

task demosTests(dependsOn: copyJarsToDist, group: 'Verification') {
    description 'Test that the demos are working as expected.'
    doLast {
        if (JavaVersion.current() == JavaVersion.VERSION_1_8) {
            File demosDir = new File(projectDir, '../../checker-framework.demos');
            if (!demosDir.exists()) {
                exec {
                    workingDir file(demosDir.toString() + '/../')
                    executable 'git'
                    args = ['clone', 'https://github.com/typetools/checker-framework.demos.git']
                }
            } else {
                exec {
                    workingDir demosDir
                    executable 'git'
                    args = ['pull', 'https://github.com/typetools/checker-framework.demos.git']
                    ignoreExitValue = true
                }
            }
            ant.properties.put('checker.lib', file("${projectDir}/dist/checker.jar").absolutePath)
            ant.ant(dir: demosDir.toString())
        } else {
            println("Skipping demosTests because they only work with Java 8.")
        }
    }
}

task allNullnessTests(type: Test, group: 'Verification') {
    description 'Run all Junit tests for the Nullness Checker.'
    include '**/Nullness*.class'
}

// These are tests that should only be run with JDK 11.
task jtregJdk11Tests(dependsOn: ':downloadJtreg', group: 'Verification') {
    description 'Run the jtreg tests made for JDK 11.'
    dependsOn('compileJava')
    dependsOn('compileTestJava')
    dependsOn(':checker:updateJdk')
    dependsOn('shadowJar')

    String jtregOutput = "${buildDir}/jtregJdk11"
    String name = 'all'
    doLast {
        if(isJava8) {
            println "This test is only run with JDK 11."
            return;
        }
        exec {
            executable "${jtregHome}/bin/jtreg"
            args = [
                    "-dir:${projectDir}/jtregJdk11",
                    "-workDir:${jtregOutput}/${name}/work",
                    "-reportDir:${jtregOutput}/${name}/report",
                    "-verbose:summary",
                    "-javacoptions:-g",
                    "-keywords:!ignore",
                    "-samevm",
                    "-javacoptions:-classpath ${tasks.shadowJar.archiveFile.get()}:${sourceSets.test.output.asPath}",
                    "-vmoptions:-classpath ${tasks.shadowJar.archiveFile.get()}:${sourceSets.test.output.asPath}",
                    "-vmoptions:--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
                    "-javacoptions:-classpath ${sourceSets.testannotations.output.asPath}",
                    // Location of jtreg tests
                    '.'
            ]
        }


    }
}