xref: /aosp_15_r20/external/nullaway/nullaway/build.gradle (revision f50c306653bc89b8210ce6c9e0b0b44fc134bc03)
1*f50c3066SAndroid Build Coastguard Worker/*
2*f50c3066SAndroid Build Coastguard Worker * Copyright (C) 2017. Uber Technologies
3*f50c3066SAndroid Build Coastguard Worker *
4*f50c3066SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*f50c3066SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*f50c3066SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*f50c3066SAndroid Build Coastguard Worker *
8*f50c3066SAndroid Build Coastguard Worker *    http://www.apache.org/licenses/LICENSE-2.0
9*f50c3066SAndroid Build Coastguard Worker *
10*f50c3066SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*f50c3066SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*f50c3066SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*f50c3066SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*f50c3066SAndroid Build Coastguard Worker * limitations under the License.
15*f50c3066SAndroid Build Coastguard Worker */
16*f50c3066SAndroid Build Coastguard Workerimport net.ltgt.gradle.errorprone.CheckSeverity
17*f50c3066SAndroid Build Coastguard Worker
18*f50c3066SAndroid Build Coastguard Workerplugins {
19*f50c3066SAndroid Build Coastguard Worker    id 'java-library'
20*f50c3066SAndroid Build Coastguard Worker    id 'nullaway.java-test-conventions'
21*f50c3066SAndroid Build Coastguard Worker}
22*f50c3066SAndroid Build Coastguard Worker
23*f50c3066SAndroid Build Coastguard Workerconfigurations {
24*f50c3066SAndroid Build Coastguard Worker    // A configuration holding the jars for the oldest supported version of Error Prone, to use with tests
25*f50c3066SAndroid Build Coastguard Worker    errorProneOldest
26*f50c3066SAndroid Build Coastguard Worker}
27*f50c3066SAndroid Build Coastguard Worker
28*f50c3066SAndroid Build Coastguard Workerdependencies {
29*f50c3066SAndroid Build Coastguard Worker    compileOnly project(":annotations")
30*f50c3066SAndroid Build Coastguard Worker    compileOnly deps.apt.autoValueAnnot
31*f50c3066SAndroid Build Coastguard Worker    annotationProcessor deps.apt.autoValue
32*f50c3066SAndroid Build Coastguard Worker    compileOnly deps.apt.autoServiceAnnot
33*f50c3066SAndroid Build Coastguard Worker    annotationProcessor deps.apt.autoService
34*f50c3066SAndroid Build Coastguard Worker    compileOnly deps.build.jsr305Annotations
35*f50c3066SAndroid Build Coastguard Worker    compileOnly deps.test.jetbrainsAnnotations
36*f50c3066SAndroid Build Coastguard Worker    compileOnly deps.apt.javaxInject
37*f50c3066SAndroid Build Coastguard Worker
38*f50c3066SAndroid Build Coastguard Worker
39*f50c3066SAndroid Build Coastguard Worker    compileOnly deps.build.errorProneCheckApi
40*f50c3066SAndroid Build Coastguard Worker    implementation deps.build.checkerDataflow
41*f50c3066SAndroid Build Coastguard Worker    implementation deps.build.guava
42*f50c3066SAndroid Build Coastguard Worker
43*f50c3066SAndroid Build Coastguard Worker    testImplementation project(":annotations")
44*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.junit4
45*f50c3066SAndroid Build Coastguard Worker    testImplementation(deps.build.errorProneTestHelpers) {
46*f50c3066SAndroid Build Coastguard Worker        exclude group: "junit", module: "junit"
47*f50c3066SAndroid Build Coastguard Worker    }
48*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.jetbrainsAnnotations
49*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.junit5Jupiter
50*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.cfQual
51*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.cfCompatQual
52*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.build.jspecify
53*f50c3066SAndroid Build Coastguard Worker    testImplementation project(":test-java-lib")
54*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.apt.jakartaInject
55*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.apt.javaxInject
56*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.rxjava2
57*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.commonsLang
58*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.commonsLang3
59*f50c3066SAndroid Build Coastguard Worker    testImplementation project(":test-library-models")
60*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.lombok
61*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.springBeans
62*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.springContext
63*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.grpcCore
64*f50c3066SAndroid Build Coastguard Worker    testImplementation project(":test-java-lib-lombok")
65*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.mockito
66*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.javaxAnnotationApi
67*f50c3066SAndroid Build Coastguard Worker    testImplementation deps.test.assertJ
68*f50c3066SAndroid Build Coastguard Worker    // This is for a test exposing a CFG construction failure in the Checker Framework.  We can probably remove it once
69*f50c3066SAndroid Build Coastguard Worker    // the issue is fixed upstream and we update. See https://github.com/typetools/checker-framework/issues/6396.
70*f50c3066SAndroid Build Coastguard Worker    testImplementation 'org.apache.spark:spark-sql_2.12:3.3.2'
71*f50c3066SAndroid Build Coastguard Worker
72*f50c3066SAndroid Build Coastguard Worker    errorProneOldest deps.build.errorProneCheckApiOld
73*f50c3066SAndroid Build Coastguard Worker    errorProneOldest(deps.build.errorProneTestHelpersOld) {
74*f50c3066SAndroid Build Coastguard Worker        exclude group: "junit", module: "junit"
75*f50c3066SAndroid Build Coastguard Worker    }
76*f50c3066SAndroid Build Coastguard Worker}
77*f50c3066SAndroid Build Coastguard Worker
78*f50c3066SAndroid Build Coastguard Workerjavadoc {
79*f50c3066SAndroid Build Coastguard Worker    failOnError = false
80*f50c3066SAndroid Build Coastguard Worker}
81*f50c3066SAndroid Build Coastguard Worker
82*f50c3066SAndroid Build Coastguard Workerapply plugin: 'com.vanniktech.maven.publish'
83*f50c3066SAndroid Build Coastguard Worker
84*f50c3066SAndroid Build Coastguard Worker// These --add-exports arguments are required when targeting JDK 11+ since Error Prone and NullAway access a bunch of
85*f50c3066SAndroid Build Coastguard Worker// JDK-internal APIs that are not exposed otherwise.  Since we currently target JDK 8, we do not need to pass the
86*f50c3066SAndroid Build Coastguard Worker// arguments, as encapsulation of JDK internals is not enforced on JDK 8.  In fact, the arguments cause a compiler error
87*f50c3066SAndroid Build Coastguard Worker// when targeting JDK 8.  Leaving commented so we can easily add them back once we target JDK 11.
88*f50c3066SAndroid Build Coastguard Worker//    tasks.withType(JavaCompile).configureEach {
89*f50c3066SAndroid Build Coastguard Worker//        options.compilerArgs += [
90*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
91*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
92*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
93*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
94*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
95*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
96*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
97*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
98*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
99*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
100*f50c3066SAndroid Build Coastguard Worker//                "--add-exports=jdk.compiler/com.sun.source.tree=ALL-UNNAMED",
101*f50c3066SAndroid Build Coastguard Worker//        ]
102*f50c3066SAndroid Build Coastguard Worker//    }
103*f50c3066SAndroid Build Coastguard Worker
104*f50c3066SAndroid Build Coastguard Worker// Create a task to test on JDK 8
105*f50c3066SAndroid Build Coastguard Worker// NOTE: even when we drop JDK 8 support, we will still need a test task similar to this one for testing building
106*f50c3066SAndroid Build Coastguard Worker// against a recent JDK and Error Prone version but then running on the oldest supported JDK and Error Prone version,
107*f50c3066SAndroid Build Coastguard Worker// to check for binary compatibility issues.
108*f50c3066SAndroid Build Coastguard Workerdef jdk8Test = tasks.register("testJdk8", Test) {
109*f50c3066SAndroid Build Coastguard Worker    javaLauncher = javaToolchains.launcherFor {
110*f50c3066SAndroid Build Coastguard Worker        languageVersion = JavaLanguageVersion.of(8)
111*f50c3066SAndroid Build Coastguard Worker    }
112*f50c3066SAndroid Build Coastguard Worker
113*f50c3066SAndroid Build Coastguard Worker    description = "Runs the test suite on JDK 8"
114*f50c3066SAndroid Build Coastguard Worker    group = LifecycleBasePlugin.VERIFICATION_GROUP
115*f50c3066SAndroid Build Coastguard Worker
116*f50c3066SAndroid Build Coastguard Worker    // Copy inputs from normal Test task.
117*f50c3066SAndroid Build Coastguard Worker    def testTask = tasks.getByName("test")
118*f50c3066SAndroid Build Coastguard Worker    // A bit of a hack: we add the dependencies of the oldest supported Error Prone version to the _beginning_ of the
119*f50c3066SAndroid Build Coastguard Worker    // classpath, so that they are used instead of the latest version.  This exercises the scenario of building
120*f50c3066SAndroid Build Coastguard Worker    // NullAway against the latest supported Error Prone version but then running on the oldest supported version.
121*f50c3066SAndroid Build Coastguard Worker    classpath = configurations.errorProneOldest + testTask.classpath
122*f50c3066SAndroid Build Coastguard Worker
123*f50c3066SAndroid Build Coastguard Worker    testClassesDirs = testTask.testClassesDirs
124*f50c3066SAndroid Build Coastguard Worker    jvmArgs "-Xbootclasspath/p:${configurations.errorproneJavac.asPath}"
125*f50c3066SAndroid Build Coastguard Worker    filter {
126*f50c3066SAndroid Build Coastguard Worker        // JDK 8 does not support diamonds on anonymous classes
127*f50c3066SAndroid Build Coastguard Worker        excludeTestsMatching "com.uber.nullaway.NullAwayJSpecifyGenericsTests.overrideDiamondAnonymousClass"
128*f50c3066SAndroid Build Coastguard Worker        // tests cannot run on JDK 8 since Mockito version no longer supports it
129*f50c3066SAndroid Build Coastguard Worker        excludeTestsMatching "com.uber.nullaway.NullAwaySerializationTest.initializationError"
130*f50c3066SAndroid Build Coastguard Worker        excludeTestsMatching "com.uber.nullaway.handlers.contract.ContractUtilsTest.getEmptyAntecedent"
131*f50c3066SAndroid Build Coastguard Worker    }
132*f50c3066SAndroid Build Coastguard Worker}
133*f50c3066SAndroid Build Coastguard Worker
134*f50c3066SAndroid Build Coastguard Workertasks.named('check').configure {
135*f50c3066SAndroid Build Coastguard Worker    dependsOn(jdk8Test)
136*f50c3066SAndroid Build Coastguard Worker}
137*f50c3066SAndroid Build Coastguard Worker
138*f50c3066SAndroid Build Coastguard Worker// Create a task to build NullAway with NullAway checking enabled
139*f50c3066SAndroid Build Coastguard Workertasks.register('buildWithNullAway', JavaCompile) {
140*f50c3066SAndroid Build Coastguard Worker    onlyIf {
141*f50c3066SAndroid Build Coastguard Worker        // We only do NullAway checks when compiling against the latest
142*f50c3066SAndroid Build Coastguard Worker        // version of Error Prone (as nullability annotations on the APIs
143*f50c3066SAndroid Build Coastguard Worker        // can change between versions)
144*f50c3066SAndroid Build Coastguard Worker        deps.versions.errorProneApi == deps.versions.errorProneLatest
145*f50c3066SAndroid Build Coastguard Worker    }
146*f50c3066SAndroid Build Coastguard Worker    // Configure compilation to run with Error Prone and NullAway
147*f50c3066SAndroid Build Coastguard Worker    source = sourceSets.main.java
148*f50c3066SAndroid Build Coastguard Worker    classpath = sourceSets.main.compileClasspath
149*f50c3066SAndroid Build Coastguard Worker    destinationDirectory = file("$buildDir/ignoredClasses")
150*f50c3066SAndroid Build Coastguard Worker    options.annotationProcessorPath = files(
151*f50c3066SAndroid Build Coastguard Worker            configurations.errorprone.asCollection(),
152*f50c3066SAndroid Build Coastguard Worker            sourceSets.main.annotationProcessorPath,
153*f50c3066SAndroid Build Coastguard Worker            // This refers to the NullAway jar built from the current source
154*f50c3066SAndroid Build Coastguard Worker            jar.archiveFile.get(),
155*f50c3066SAndroid Build Coastguard Worker            sourceSets.main.compileClasspath)
156*f50c3066SAndroid Build Coastguard Worker    options.errorprone.enabled = true
157*f50c3066SAndroid Build Coastguard Worker    options.errorprone {
158*f50c3066SAndroid Build Coastguard Worker        option("NullAway:AnnotatedPackages", "com.uber,org.checkerframework.nullaway,com.google.common")
159*f50c3066SAndroid Build Coastguard Worker        option("NullAway:CastToNonNullMethod", "com.uber.nullaway.NullabilityUtil.castToNonNull")
160*f50c3066SAndroid Build Coastguard Worker        option("NullAway:CheckOptionalEmptiness")
161*f50c3066SAndroid Build Coastguard Worker        option("NullAway:AcknowledgeRestrictiveAnnotations")
162*f50c3066SAndroid Build Coastguard Worker        option("NullAway:CheckContracts")
163*f50c3066SAndroid Build Coastguard Worker        option("NullAway:JSpecifyMode")
164*f50c3066SAndroid Build Coastguard Worker    }
165*f50c3066SAndroid Build Coastguard Worker    // Make sure the jar has already been built
166*f50c3066SAndroid Build Coastguard Worker    dependsOn 'jar'
167*f50c3066SAndroid Build Coastguard Worker    // Check that the NullAway jar actually exists (without this,
168*f50c3066SAndroid Build Coastguard Worker    // Gradle will run the compilation even if the jar doesn't exist)
169*f50c3066SAndroid Build Coastguard Worker    doFirst {
170*f50c3066SAndroid Build Coastguard Worker        assert jar.archiveFile.get().getAsFile().exists()
171*f50c3066SAndroid Build Coastguard Worker    }
172*f50c3066SAndroid Build Coastguard Worker}
173*f50c3066SAndroid Build Coastguard Worker
174*f50c3066SAndroid Build Coastguard Workerproject.tasks.named('check').configure {
175*f50c3066SAndroid Build Coastguard Worker    dependsOn 'buildWithNullAway'
176*f50c3066SAndroid Build Coastguard Worker}
177