-
-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completely revamp how we support JVM compiler plugins. (#4287)
- Loading branch information
Showing
34 changed files
with
868 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
examples/src/java/org/pantsbuild/example/javac/plugin/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
javac_plugin( | ||
name = 'simple_javac_plugin', | ||
sources = ['SimpleJavacPlugin.java'], | ||
dependencies = [], | ||
classname = 'org.pantsbuild.example.javac.plugin.SimpleJavacPlugin', | ||
scope='compile', | ||
) | ||
|
||
# The plugin will only run on this target if told to via options. | ||
java_library( | ||
name = 'global', | ||
sources = ['Global.java'], | ||
dependencies = [ | ||
':simple_javac_plugin' | ||
], | ||
) | ||
|
||
# The plugin will only run on this target if told to via options, but if it | ||
# does run, it will use the args specified here. | ||
java_library( | ||
name = 'global_with_local_args', | ||
sources = ['GlobalWithLocalArgs.java'], | ||
dependencies = [ | ||
':simple_javac_plugin' | ||
], | ||
javac_plugin_args = { | ||
'simple_javac_plugin': ['args', 'from', 'target', 'global_with_local_args'] | ||
} | ||
) | ||
|
||
# The plugin will always run on this target, with args specified via options. | ||
java_library( | ||
name = 'local_with_global_args', | ||
sources = ['LocalWithGlobalArgs.java'], | ||
dependencies = [ | ||
':simple_javac_plugin' | ||
], | ||
javac_plugins=['simple_javac_plugin'] | ||
) | ||
|
||
# The plugin will always run on this target, with the args specified here. | ||
java_library( | ||
name = 'local', | ||
sources = ['Local.java'], | ||
dependencies = [ | ||
':simple_javac_plugin' | ||
], | ||
javac_plugins=['simple_javac_plugin'], | ||
javac_plugin_args = { | ||
'simple_javac_plugin': ['args', 'from', 'target', 'local'] | ||
} | ||
) | ||
|
||
page( | ||
name='readme', | ||
source='README.md', | ||
) |
8 changes: 8 additions & 0 deletions
8
examples/src/java/org/pantsbuild/example/javac/plugin/Global.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package org.pantsbuild.example.javac.plugin; | ||
|
||
|
||
public class Global { | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/src/java/org/pantsbuild/example/javac/plugin/GlobalWithLocalArgs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package org.pantsbuild.example.javac.plugin; | ||
|
||
|
||
public class GlobalWithLocalArgs { | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/src/java/org/pantsbuild/example/javac/plugin/Local.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package org.pantsbuild.example.javac.plugin; | ||
|
||
|
||
public class Local { | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/src/java/org/pantsbuild/example/javac/plugin/LocalWithGlobalArgs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package org.pantsbuild.example.javac.plugin; | ||
|
||
|
||
public class LocalWithGlobalArgs { | ||
} |
117 changes: 117 additions & 0 deletions
117
examples/src/java/org/pantsbuild/example/javac/plugin/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
javac Plugins with Pants | ||
======================== | ||
|
||
For Java versions 8 and higher, the Java compiler, javac, has a | ||
[plugin](https://docs.oracle.com/javase/8/docs/jdk/api/javac/tree/com/sun/source/util/Plugin.html) mechanism. | ||
|
||
Plugins allow you to hook into the compiler while it's running, to perform various | ||
tasks such as custom error checking, code analysis and so on. | ||
|
||
Pants supports both building and using javac plugins. | ||
|
||
Building javac plugins | ||
---------------------- | ||
|
||
javac plugins are specified using a `javac_plugin` target: | ||
|
||
``` | ||
javac_plugin( | ||
name = 'plugin', | ||
plugin = 'simple_javac_plugin', | ||
sources = ['SimpleJavacPlugin.java'], | ||
dependencies = [], | ||
classname = 'org.pantsbuild.example.javac.plugin.SimpleJavacPlugin', | ||
scope='compile', | ||
) | ||
``` | ||
|
||
A javac plugin target has the same fields as a `java_library` target, | ||
plus two extra: | ||
|
||
- `classname`: The name of the `Plugin` implementation class. Required. | ||
- `plugin`: The logical name of the plugin, as returned by the `Plugin` | ||
class's `getName()` method. If unspecified, this field defaults to | ||
the target name. | ||
|
||
Building a plugin target will, in addition to compiling the code, generate | ||
the appropriate metadata into `META-INF/services/com.sun.source.util.Plugin`, so | ||
that javac can load the plugin by name at runtime. | ||
|
||
A plugin may be published for later consumption in any repo. It can also | ||
be consumed (with some restrictions) from source, in the same repo. | ||
|
||
|
||
Using javac plugins | ||
------------------- | ||
|
||
Plugins can be integrated in one of two ways: | ||
|
||
- Global plugins: specified in `pants.ini` and used on all Java code. | ||
- Per-target plugins: specified on a Java target and used only when compiling that target. | ||
|
||
#### Global plugins | ||
|
||
Global plugins are specified using the `javac_plugins` key in the `compile.zinc` section of `pants.ini`: | ||
|
||
``` | ||
[compile.zinc] | ||
javac_plugins: ['simple_javac_plugin'] | ||
``` | ||
|
||
Plugins can optionally take arguments, specified like this: | ||
|
||
``` | ||
[compile.zinc] | ||
javac_plugins: ['simple_javac_plugin] | ||
javac_plugin_args: { | ||
'simple_javac_plugin': ['arg1', 'arg2'] | ||
} | ||
``` | ||
|
||
|
||
#### Per-target plugins | ||
|
||
These are specified like this: | ||
|
||
``` | ||
java_library( | ||
... | ||
javac_plugins: ['simple_javac_plugin'], | ||
javac_plugin_args: { | ||
'simple_javac_plugin': ['arg1', 'arg2'] | ||
} | ||
) | ||
``` | ||
|
||
#### Depending on plugins | ||
|
||
In order to load a plugin, it has to be on javac's classpath. | ||
This can be achieved in one of two ways: | ||
- Have targets that must be compiled with a plugin depend (directly or indirectly) | ||
either on the `javac_plugin` target, or on a `jar_library` pointing to a published version | ||
of the plugin. | ||
- Have a `javac-plugin-dep` target in `BUILD.tools`: | ||
|
||
``` | ||
jar_library(name='java-plugin-dep', | ||
jars = [jar(org='com.foo', name='foo_plugin', rev='1.2.3')], | ||
``` | ||
|
||
Note that, as always with `BUILD.tools`, plugin locations specified via `java-plugin-dep` | ||
must be published jars. They cannot be local `javac_plugin` targets. | ||
|
||
Usually, it will make more sense to use `java-plugin-dep` with global plugins, to avoid | ||
laborious repetition of that dependency, and to use target dependencies for per-target plugins, | ||
to keep the dependencies selective. | ||
|
||
Depending directly on the `javac_plugin` has the added advantage of allowing plugin changes | ||
to be picked up when compiling the target that uses the plugin, with no need for an intermediate | ||
publishing step. | ||
|
||
Note that, to avoid a chicken-and-egg problem, an in-repo plugin will not be used when | ||
compiling its own `javac_plugin` target, or any of that target's dependencies. | ||
To use a plugin on its own code, you must publish it and consume the published plugin | ||
via `java-plugin-dep`. | ||
|
||
|
5 changes: 4 additions & 1 deletion
5
...ild/example/plugin/SimpleJavacPlugin.java → ...ample/javac/plugin/SimpleJavacPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
examples/src/java/org/pantsbuild/example/plugin/HelloPlugin.java
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
examples/src/scala/org/pantsbuild/example/scalac/plugin/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
scalac_plugin( | ||
name='simple_scalac_plugin', | ||
plugin='simple_scalac_plugin', | ||
classname='org.pantsbuild.example.scalac.plugin.SimpleScalacPlugin', | ||
sources=['SimpleScalacPlugin.scala'], | ||
scope='compile', | ||
) | ||
|
||
# The plugin will only run on this target if told to via options. | ||
scala_library( | ||
name = 'global', | ||
sources = ['Global.scala'], | ||
dependencies = [ | ||
':simple_scalac_plugin' | ||
], | ||
) | ||
|
||
# The plugin will only run on this target if told to via options, but if it | ||
# does run, it will use the args specified here. | ||
scala_library( | ||
name = 'global_with_local_args', | ||
sources = ['GlobalWithLocalArgs.scala'], | ||
dependencies = [ | ||
':simple_scalac_plugin' | ||
], | ||
scalac_plugin_args = { | ||
'simple_scalac_plugin': ['args', 'from', 'target', 'global_with_local_args'] | ||
} | ||
) | ||
|
||
# The plugin will always run on this target, with args specified via options. | ||
scala_library( | ||
name = 'local_with_global_args', | ||
sources = ['LocalWithGlobalArgs.scala'], | ||
dependencies = [ | ||
':simple_scalac_plugin' | ||
], | ||
scalac_plugins=['simple_scalac_plugin'] | ||
) | ||
|
||
# The plugin will always run on this target, with the args specified here. | ||
scala_library( | ||
name = 'local', | ||
sources = ['Local.scala'], | ||
dependencies = [ | ||
':simple_scalac_plugin' | ||
], | ||
scalac_plugins=['simple_scalac_plugin'], | ||
scalac_plugin_args = { | ||
'simple_scalac_plugin': ['args', 'from', 'target', 'local'] | ||
} | ||
) | ||
|
||
page( | ||
name='readme', | ||
source='README.md', | ||
) |
6 changes: 6 additions & 0 deletions
6
examples/src/scala/org/pantsbuild/example/scalac/plugin/Global.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package org.pantsbuild.example.scalac.plugin | ||
|
||
class Global |
6 changes: 6 additions & 0 deletions
6
examples/src/scala/org/pantsbuild/example/scalac/plugin/GlobalWithLocalArgs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package org.pantsbuild.example.scalac.plugin | ||
|
||
class GlobalWithLocalArgs |
Oops, something went wrong.