2020-02-18 12:43:47 -05:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This adds 'ecjLint' task.
|
|
|
|
|
|
|
|
configure(rootProject) {
|
|
|
|
configurations {
|
|
|
|
ecjDeps
|
|
|
|
}
|
|
|
|
|
|
|
|
dependencies {
|
2024-06-17 03:49:21 -04:00
|
|
|
ecjDeps deps.ecj
|
2020-02-18 12:43:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 09:35:14 -04:00
|
|
|
def resources = scriptResources(buildscript)
|
|
|
|
|
2020-02-18 12:43:47 -05:00
|
|
|
allprojects {
|
|
|
|
plugins.withType(JavaPlugin) {
|
|
|
|
// Create a [sourceSetName]EcjLint task for each source set
|
|
|
|
// with a non-empty java.srcDirs. These tasks are then
|
|
|
|
// attached to project's "ecjLint" task.
|
2022-10-02 14:41:46 -04:00
|
|
|
def lintTasks = sourceSets.collect { sourceSet ->
|
2021-12-19 02:51:13 -05:00
|
|
|
def srcDirs = sourceSet.java.sourceDirectories
|
|
|
|
.filter { dir -> dir.exists() }
|
2020-02-18 12:43:47 -05:00
|
|
|
|
2021-12-20 13:27:37 -05:00
|
|
|
tasks.create(sourceSet.getTaskName("ecjLint", null), JavaExec, { JavaExec task ->
|
2020-02-18 12:43:47 -05:00
|
|
|
// This dependency is on a configuration; technically it causes
|
|
|
|
// all dependencies to be resolved before this task executes
|
|
|
|
// (this includes scheduling tasks that compile the
|
|
|
|
// sources from other projects for example).
|
|
|
|
dependsOn sourceSet.compileClasspath
|
|
|
|
|
|
|
|
// The inputs are all source files from the sourceSet.
|
|
|
|
inputs.files sourceSet.allSource.asFileTree
|
2020-09-04 05:11:57 -04:00
|
|
|
|
2020-02-18 12:43:47 -05:00
|
|
|
// We create a task for all source sets but ignore those
|
|
|
|
// that don't have any Java source directories.
|
|
|
|
enabled = !srcDirs.isEmpty()
|
|
|
|
|
2022-10-02 14:41:46 -04:00
|
|
|
// Ignore MR-JAR sourceSets.
|
|
|
|
if (sourceSet.name ==~ /main\d+/) {
|
|
|
|
enabled = false
|
|
|
|
}
|
|
|
|
|
2020-02-18 12:43:47 -05:00
|
|
|
classpath = rootProject.configurations.ecjDeps
|
2021-10-29 10:59:47 -04:00
|
|
|
mainClass = "org.eclipse.jdt.internal.compiler.batch.Main"
|
2020-02-18 12:43:47 -05:00
|
|
|
|
|
|
|
// Don't emit any .class files.
|
|
|
|
// Hack around "-d none" still emitting package-info.class
|
|
|
|
// by running in a temporary directory.
|
|
|
|
def tmpDst = getTemporaryDir()
|
|
|
|
workingDir tmpDst
|
|
|
|
|
|
|
|
args += [ "-d", "none" ]
|
|
|
|
|
|
|
|
// Compilation environment.
|
2021-11-28 15:10:32 -05:00
|
|
|
// we use -source/-target as it is significantly faster than --release
|
|
|
|
args += [ "-source", project.java.sourceCompatibility ]
|
|
|
|
args += [ "-target", project.java.targetCompatibility ]
|
2020-02-18 12:43:47 -05:00
|
|
|
args += [ "-encoding", "UTF-8"]
|
|
|
|
args += [ "-proc:none" ]
|
|
|
|
args += [ "-nowarn" ]
|
|
|
|
args += [ "-enableJavadoc" ]
|
2020-08-31 09:35:14 -04:00
|
|
|
args += [ "-properties", file("${resources}/ecj.javadocs.prefs").absolutePath ]
|
2020-02-18 12:43:47 -05:00
|
|
|
|
2021-12-19 02:51:13 -05:00
|
|
|
// We depend on modular paths.
|
2022-01-05 14:42:02 -05:00
|
|
|
def modularPaths = sourceSet.modularPathsForEcj
|
|
|
|
dependsOn modularPaths
|
2021-12-19 02:51:13 -05:00
|
|
|
|
2022-01-05 14:42:02 -05:00
|
|
|
// Add modular dependencies and their transitive dependencies to module path.
|
|
|
|
task.argumentProviders.add(modularPaths.compilationArguments)
|
2021-12-20 13:27:37 -05:00
|
|
|
|
2022-01-05 14:42:02 -05:00
|
|
|
// Add classpath, if needed.
|
2021-12-19 02:51:13 -05:00
|
|
|
task.argumentProviders.add((CommandLineArgumentProvider) {
|
|
|
|
// Add classpath locations in a lazy provider (can't resolve the
|
2020-02-18 12:43:47 -05:00
|
|
|
// configuration at evaluation time). Filter out non-existing entries
|
|
|
|
// (output folders for non-existing input source dirs like resources).
|
2022-01-05 14:42:02 -05:00
|
|
|
FileCollection cpath = modularPaths.compilationClasspath.filter { p -> p.exists() }
|
2020-02-18 12:43:47 -05:00
|
|
|
if (!cpath.isEmpty()) {
|
2022-01-05 14:42:02 -05:00
|
|
|
return ["-classpath", cpath.join(File.pathSeparator)]
|
|
|
|
} else {
|
|
|
|
return []
|
2020-02-18 12:43:47 -05:00
|
|
|
}
|
2022-01-05 14:42:02 -05:00
|
|
|
})
|
2020-02-18 12:43:47 -05:00
|
|
|
|
2022-01-05 14:42:02 -05:00
|
|
|
// Place input files in an external file to dodge command line argument
|
|
|
|
// limits. We could pass a directory but ecj seems to be buggy: when it
|
|
|
|
// encounters a module-info.java file it no longer compiles other source files.
|
|
|
|
def inputsFile = file("${tmpDst}/ecj-inputs.txt")
|
|
|
|
task.argumentProviders.add((CommandLineArgumentProvider) {
|
|
|
|
return ["@" + inputsFile.absolutePath]
|
2021-12-19 02:51:13 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
doFirst {
|
2022-01-05 14:42:02 -05:00
|
|
|
modularPaths.logCompilationPaths(logger)
|
|
|
|
|
2021-12-19 02:51:13 -05:00
|
|
|
tmpDst.mkdirs()
|
2021-12-20 13:27:37 -05:00
|
|
|
|
|
|
|
// escape filename accoring to ECJ's rules:
|
|
|
|
// https://github.com/eclipse/aspectj.eclipse.jdt.core/blob/a05312e746b9bc2b48b4b039f6e7b5e061b5b393/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/Main.java#L1533-L1537
|
|
|
|
// Basically surround all whitespace by quotes:
|
|
|
|
def escapeFileName = { String s -> s.replaceAll(/ +/, /"$0"/) }
|
|
|
|
inputsFile.setText(
|
|
|
|
srcDirs.collectMany { dir ->
|
2022-01-05 14:42:02 -05:00
|
|
|
project.fileTree(
|
|
|
|
dir: dir,
|
|
|
|
include: "**/*.java",
|
|
|
|
// Exclude the benchmark class with dependencies on nekohtml, which causes module-classpath conflicts and breaks ecj.
|
|
|
|
exclude: "**/DemoHTMLParser.java"
|
|
|
|
).files
|
2021-12-20 13:27:37 -05:00
|
|
|
}
|
|
|
|
// Try to sort all input files; a side-effect of this should be that module-info.java
|
|
|
|
// is placed first on the list, which works around ECJ bug:
|
|
|
|
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=569833
|
|
|
|
.sort()
|
|
|
|
.collect {file -> escapeFileName(file.absolutePath.toString())}.join("\n"), "UTF-8")
|
2020-02-18 12:43:47 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
task ecjLint() {
|
|
|
|
description "Lint Java sources using ECJ."
|
|
|
|
group "Verification"
|
|
|
|
|
|
|
|
dependsOn lintTasks
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach ecjLint to check.
|
|
|
|
check.dependsOn ecjLint
|
|
|
|
}
|
|
|
|
}
|