Skip to content

Commit

Permalink
Ensure that building native image doesn't fail when objcopy is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jul 29, 2021
1 parent 54bc9af commit f8dbe0f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void setup(boolean processInheritIODisabled) {
public void addShutdownHook(Process buildNativeProcess) {
}

public int build(List<String> args, String nativeImageName, String resultingExecutableName, Path outputDir,
boolean debugEnabled, boolean processInheritIODisabled)
public Result build(List<String> args, String nativeImageName, String resultingExecutableName, Path outputDir,
boolean debugSymbolsEnabled, boolean processInheritIODisabled)
throws InterruptedException, IOException {
preBuild(args);
try {
Expand All @@ -63,22 +63,26 @@ public int build(List<String> args, String nativeImageName, String resultingExec
executor.shutdown();
errorReportLatch.await();
int exitCode = process.waitFor();
boolean objcopyExists = objcopyExists();
if (exitCode != 0) {
return exitCode;
return new Result(exitCode, objcopyExists);
}

if (objcopyExists()) {
if (debugEnabled) {
if (objcopyExists) {
if (debugSymbolsEnabled) {
splitDebugSymbols(nativeImageName, resultingExecutableName);
} else {
// Strip debug symbols regardless, because the underlying JDK might contain them
objcopy("--strip-debug", resultingExecutableName);
}
} else {
log.warn("objcopy executable not found in PATH. Debug symbols will not be separated from executable.");
log.warn("That will result in a larger native image with debug symbols embedded in it.");
if (!debugSymbolsEnabled) {
log.warn(
"objcopy executable not found in PATH. Debug symbols will therefore not be separated from executable.");
log.warn("That also means that resulting native image is larger as it embeds the debug symbols.");
}
}
return 0;
return new Result(0, objcopyExists);
} finally {
postBuild();
}
Expand Down Expand Up @@ -143,4 +147,22 @@ static void runCommand(String[] command, String errorMsg, File workingDirectory)
}
}
}

static class Result {
private final int exitCode;
private final boolean objcopyExists;

public Result(int exitCode, boolean objcopyExists) {
this.exitCode = exitCode;
this.objcopyExists = objcopyExists;
}

public int getExitCode() {
return exitCode;
}

public boolean isObjcopyExists() {
return objcopyExists;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,24 +198,32 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa

List<String> nativeImageArgs = commandAndExecutable.args;

int exitCode = buildRunner.build(nativeImageArgs, nativeImageName, resultingExecutableName, outputDir,
NativeImageBuildRunner.Result buildNativeResult = buildRunner.build(nativeImageArgs, nativeImageName,
resultingExecutableName, outputDir,
nativeConfig.debug.enabled, processInheritIODisabled.isPresent());
if (exitCode != 0) {
throw imageGenerationFailed(exitCode, nativeImageArgs);
if (buildNativeResult.getExitCode() != 0) {
throw imageGenerationFailed(buildNativeResult.getExitCode(), nativeImageArgs);
}
IoUtils.copy(generatedExecutablePath, finalExecutablePath);
Files.delete(generatedExecutablePath);
if (nativeConfig.debug.enabled) {
final String symbolsName = String.format("%s.debug", nativeImageName);
Path generatedSymbols = outputDir.resolve(symbolsName);
Path finalSymbolsPath = outputTargetBuildItem.getOutputDirectory().resolve(symbolsName);
IoUtils.copy(generatedSymbols, finalSymbolsPath);
Files.delete(generatedSymbols);
final String sources = "sources";
final Path generatedSources = outputDir.resolve(sources);
final Path finalSources = outputTargetBuildItem.getOutputDirectory().resolve(sources);
IoUtils.copy(generatedSources, finalSources);
IoUtils.recursiveDelete(generatedSources);
if (buildNativeResult.isObjcopyExists()) {
final String symbolsName = String.format("%s.debug", nativeImageName);
Path generatedSymbols = outputDir.resolve(symbolsName);
Path finalSymbolsPath = outputTargetBuildItem.getOutputDirectory().resolve(symbolsName);
IoUtils.copy(generatedSymbols, finalSymbolsPath);
Files.delete(generatedSymbols);
final String sources = "sources";
final Path generatedSources = outputDir.resolve(sources);
final Path finalSources = outputTargetBuildItem.getOutputDirectory().resolve(sources);
IoUtils.copy(generatedSources, finalSources);
IoUtils.recursiveDelete(generatedSources);
} else {
log.warn(
"objcopy executable not found in PATH. Debug symbols therefore cannot be placed into the dedicated directory.");
log.warn("That also means that resulting native image is larger as it embeds the debug symbols.");
}

}
System.setProperty("native.image.path", finalExecutablePath.toAbsolutePath().toString());

Expand Down

0 comments on commit f8dbe0f

Please sign in to comment.