Skip to content

Commit

Permalink
Create source cache when building native in container quarkusio#12283
Browse files Browse the repository at this point in the history
* Let Mandrel/GraalVM decide where to put the source cache,
which naturally leads to a folder
that's stored within the module shared with container.
* Copy the application sources in advance,
to avoid the need to make src/main/java available to the container.
  • Loading branch information
galderz committed Sep 23, 2020
1 parent 2a45739 commit f7126b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
final Optional<ProcessInheritIODisabled> processInheritIODisabled) {
if (nativeConfig.debug.enabled) {
copyJarSourcesToLib(outputTargetBuildItem, curateOutcomeBuildItem);
copySourcesToSourceCache(outputTargetBuildItem);
}

Path runnerJar = nativeImageSourceJarBuildItem.getPath();
Expand Down Expand Up @@ -230,14 +231,6 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
if (nativeConfig.debug.enabled) {
if (graalVMVersion.isMandrel() || graalVMVersion.isNewerThan(GraalVM.Version.VERSION_20_1)) {
command.add("-g");

final Path javaSourcesPath = Paths.get("..", "..", "src", "main", "java");
if (outputDir.resolve(javaSourcesPath).toFile().exists()) {
command.add("-H:DebugInfoSourceSearchPath=" + javaSourcesPath.toString());
}

final Path sourcesCachePath = outputDir.getParent().resolve("sources-cache");
command.add("-H:DebugInfoSourceCacheRoot=" + sourcesCachePath.toString());
}
}
if (nativeConfig.debugBuildProcess) {
Expand Down Expand Up @@ -450,6 +443,33 @@ private void removeJarSourcesFromLib(OutputTargetBuildItem outputTargetBuildItem
Stream.of(Objects.requireNonNull(jarSources)).forEach(File::delete);
}

private static void copySourcesToSourceCache(OutputTargetBuildItem outputTargetBuildItem) {
Path targetDirectory = outputTargetBuildItem.getOutputDirectory()
.resolve(outputTargetBuildItem.getBaseName() + "-native-image-source-jar");

final Path targetSrc = targetDirectory.resolve(Paths.get("sources", "src"));
final File targetSrcFile = targetSrc.toFile();
if (!targetSrcFile.exists())
targetSrcFile.mkdirs();

final Path javaSourcesPath = outputTargetBuildItem.getOutputDirectory().resolve(
Paths.get("..", "src", "main", "java"));

try {
Files.walk(javaSourcesPath).forEach(path -> {
Path targetPath = Paths.get(targetSrc.toString(),
path.toString().substring(javaSourcesPath.toString().length()));
try {
Files.copy(path, targetPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new UncheckedIOException("Unable to copy from " + path + " to " + targetPath, e);
}
});
} catch (IOException e) {
throw new UncheckedIOException("Unable to walk path " + javaSourcesPath, e);
}
}

private void handleAdditionalProperties(NativeConfig nativeConfig, List<String> command, boolean isContainerBuild,
Path outputDir) {
if (nativeConfig.additionalBuildArgs.isPresent()) {
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/building-native-image.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ To include those, make sure you invoke `mvn dependency:sources` first.
This step is required in order to pull the sources for these dependencies,
and get them included in the source cache.

The location of source cache is `target/sources-cache` folder.
The location of source cache is `target/{project.name}-{project.version}-native-image-source-jar/sources` folder.

[[configuration-reference]]
== Configuring the Native Executable
Expand Down

0 comments on commit f7126b7

Please sign in to comment.