Skip to content

Commit 29313e6

Browse files
Add a sharedLibrary configuration option for Gradle plugin
This commit adds `sharedLibrary` configuration option to the Gradle plugin as it was a requested feature. `java-library` test sample is also included as well as corresponding functional test.
1 parent d0564b4 commit 29313e6

File tree

14 files changed

+442
-0
lines changed

14 files changed

+442
-0
lines changed

docs/src/docs/asciidoc/gradle-plugin.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ nativeBuild {
146146
verbose = true // Add verbose output, defaults to false
147147
fallback = true // Sets the fallback mode of native-image, defaults to false
148148
server = true // Sets the server mode, defaults to false
149+
sharedLibrary = false // Determines if image is a shared library, defaults to false if `java-library` plugin isn't included
150+
149151
systemProperties = [name1: 'value1', name2: 'value2'] // Sets the system properties to use for the native image builder
150152
configurationFileDirectories.from(file('src/my-config')) // Adds a native image configuration file directory, containing files like reflection configuration
151153

@@ -171,6 +173,8 @@ nativeBuild {
171173
verbose.set(true) // Add verbose output, defaults to false
172174
fallback.set(true) // Sets the fallback mode of native-image, defaults to false
173175
server.set(true) // Sets the server mode, defaults to false
176+
sharedLibrary.set(false) // Determines if image is a shared library, defaults to false if `java-library` plugin isn't included
177+
174178
systemProperties.putAll(mapOf(name1 to "value1", name2 to "value2")) // Sets the system properties to use for the native image builder
175179
configurationFileDirectories.from(file("src/my-config")) // Adds a native image configuration file directory, containing files like reflection configuration
176180

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
package org.graalvm.buildtools.gradle
43+
44+
import org.graalvm.buildtools.gradle.fixtures.AbstractFunctionalTest
45+
46+
class JavaLibraryFunctionalTest extends AbstractFunctionalTest {
47+
48+
def "can build a native image for a simple library"() {
49+
gradleVersion = version
50+
51+
def libExt = ""
52+
if (IS_LINUX) {
53+
libExt = ".so"
54+
} else if (IS_WINDOWS) {
55+
libExt = ".dll"
56+
} else if (IS_MAC) {
57+
libExt = ".dylib"
58+
}
59+
60+
def library = file("build/native/nativeBuild/java-library" + libExt)
61+
debug = true
62+
63+
given:
64+
withSample("java-library")
65+
66+
when:
67+
run 'nativeBuild'
68+
69+
then:
70+
tasks {
71+
succeeded ':jar', ':nativeBuild'
72+
doesNotContain ':build'
73+
}
74+
75+
outputContains "-H:+SharedLibrary"
76+
77+
and:
78+
library.exists()
79+
80+
where:
81+
version << TESTED_GRADLE_VERSIONS
82+
}
83+
}

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/NativeImagePlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ private void configureJavaProject(Project project, Provider<NativeImageService>
134134
project.getExtensions().findByType(JavaApplication.class).getMainClass()
135135
));
136136

137+
project.getPlugins().withId("java-library", p -> buildExtension.getSharedLibrary().convention(true));
138+
137139
registerServiceProvider(project, nativeImageServiceProvider);
138140

139141
// Register Native Image tasks

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/dsl/NativeImageOptions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ public abstract class NativeImageOptions {
177177
@Input
178178
public abstract Property<Boolean> getAgent();
179179

180+
/**
181+
* Gets the value which determines if shared library is being built.
182+
*
183+
* @return The value which determines if shared library is being built.
184+
*/
185+
@Input
186+
public abstract Property<Boolean> getSharedLibrary();
187+
180188
/**
181189
* Returns the toolchain used to invoke native-image. Currently pointing
182190
* to a Java launcher due to Gradle limitations.
@@ -210,6 +218,7 @@ public NativeImageOptions(ObjectFactory objectFactory,
210218
getFallback().convention(false);
211219
getVerbose().convention(false);
212220
getAgent().convention(false);
221+
getSharedLibrary().convention(false);
213222
getImageName().convention(defaultImageName);
214223
getJavaLauncher().convention(
215224
toolchains.launcherFor(spec -> {

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/NativeImageCommandLineProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public List<String> asArguments() {
105105
appendBooleanOption(cliArgs, options.getFallback().map(NEGATE), "--no-fallback");
106106
appendBooleanOption(cliArgs, options.getVerbose(), "--verbose");
107107
appendBooleanOption(cliArgs, options.getServer(), "-Dcom.oracle.graalvm.isaot=true");
108+
appendBooleanOption(cliArgs, options.getSharedLibrary(), "--shared");
108109
if (getOutputDirectory().isPresent()) {
109110
cliArgs.add("-H:Path=" + getOutputDirectory().get());
110111
}

native-gradle-plugin/src/testFixtures/groovy/org/graalvm/buildtools/gradle/fixtures/AbstractFunctionalTest.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ abstract class AbstractFunctionalTest extends Specification {
7070
String gradleVersion
7171
boolean debug
7272

73+
boolean IS_WINDOWS = System.getProperty("os.name", "unknown").contains("Windows");
74+
boolean IS_LINUX = System.getProperty("os.name", "unknown").contains("Linux");
75+
boolean IS_MAC = System.getProperty("os.name", "unknown").contains("Mac");
76+
7377
private StringWriter outputWriter
7478
private StringWriter errorOutputWriter
7579
private String output
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
package org.graalvm.buildtools.maven
43+
44+
class JavaLibraryFunctionalTest extends AbstractGraalVMMavenFunctionalTest {
45+
def "can build native image of a library with the Maven plugin"() {
46+
withSample("java-library")
47+
48+
def libExt = ""
49+
if (IS_LINUX) {
50+
libExt = ".so"
51+
} else if (IS_WINDOWS) {
52+
libExt = ".dll"
53+
} else if (IS_MAC) {
54+
libExt = ".dylib"
55+
}
56+
57+
def library = file("target/java-library" + libExt)
58+
59+
when:
60+
mvn '-Pnative', '-DskipTests', 'package'
61+
62+
then:
63+
buildSucceeded
64+
outputContains "--shared"
65+
and:
66+
library.exists()
67+
68+
}
69+
}

native-maven-plugin/src/testFixtures/groovy/org/graalvm/buildtools/maven/AbstractGraalVMMavenFunctionalTest.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ abstract class AbstractGraalVMMavenFunctionalTest extends Specification {
5656

5757
MavenExecutionResult result
5858

59+
boolean IS_WINDOWS = System.getProperty("os.name", "unknown").contains("Windows");
60+
boolean IS_LINUX = System.getProperty("os.name", "unknown").contains("Linux");
61+
boolean IS_MAC = System.getProperty("os.name", "unknown").contains("Mac");
62+
5963
def setup() {
6064
executor = new IsolatedMavenExecutor(
6165
new File(System.getProperty("java.executable")),

samples/java-library/build.gradle

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
plugins {
43+
id 'java-library'
44+
id 'org.graalvm.buildtools.native'
45+
}
46+
47+
repositories {
48+
mavenCentral()
49+
}
50+
51+
def junitVersion = providers.gradleProperty('junit.jupiter.version')
52+
.forUseAtConfigurationTime()
53+
.get()
54+
55+
dependencies {
56+
testImplementation(platform("org.junit:junit-bom:${junitVersion}"))
57+
testImplementation('org.junit.jupiter:junit-jupiter')
58+
}
59+
60+
nativeBuild {
61+
verbose=true
62+
}
63+
64+
test {
65+
useJUnitPlatform()
66+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
native.gradle.plugin.version = 0.9.3-SNAPSHOT
2+
junit.jupiter.version = 5.7.2
3+
junit.platform.version = 1.7.2

0 commit comments

Comments
 (0)