Skip to content

Commit

Permalink
Fix confusion between native image name and executable name
Browse files Browse the repository at this point in the history
This confusion broke the native executable build on Windows in
1.12.1.Final.

Fixes quarkusio#15459
  • Loading branch information
gsmet committed Mar 4, 2021
1 parent 7719a69 commit 3db801d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class NativeImageBuildRemoteContainerRunner extends NativeImageBuildConta

private static final Logger log = Logger.getLogger(NativeImageBuildRemoteContainerRunner.class);

private final String nativeImageName;
private final String resultingExecutableName;
private String containerId;

public NativeImageBuildRemoteContainerRunner(NativeConfig nativeConfig, Path outputDir, String nativeImageName) {
public NativeImageBuildRemoteContainerRunner(NativeConfig nativeConfig, Path outputDir, String resultingExecutableName) {
super(nativeConfig, outputDir);
this.nativeImageName = nativeImageName;
this.resultingExecutableName = resultingExecutableName;
}

@Override
Expand Down Expand Up @@ -47,7 +47,7 @@ protected String[] getBuildCommand(List<String> args) {

@Override
protected void postBuild() throws InterruptedException, IOException {
copyFromBuilder(nativeImageName, "Failed to copy native image from container back to the host.");
copyFromBuilder(resultingExecutableName, "Failed to copy native executable from container back to the host.");
if (nativeConfig.debug.enabled) {
copyFromBuilder("sources", "Failed to copy sources from container back to the host.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ ArtifactResultBuildItem nativeSourcesResult(NativeConfig nativeConfig,

Path runnerJar = outputDir.resolve(nativeImageSourceJarBuildItem.getPath().getFileName());

String nativeImageName = getResultingBinaryName(outputTargetBuildItem, packageConfig, isContainerBuild(nativeConfig));
String nativeImageName = getNativeImageName(outputTargetBuildItem, packageConfig);

NativeImageInvokerInfo nativeImageArgs = new NativeImageInvokerInfo.Builder()
.setNativeConfig(nativeConfig)
Expand All @@ -98,7 +98,7 @@ ArtifactResultBuildItem nativeSourcesResult(NativeConfig nativeConfig,
.setOutputDir(outputDir)
.setRunnerJarName(runnerJar.getFileName().toString())
// the path to native-image is not known now, it is only known at the time the native-sources will be consumed
.setResultingBinaryName(nativeImageName)
.setNativeImageName(nativeImageName)
.setContainerBuild(nativeConfig.containerRuntime.isPresent() || nativeConfig.containerBuild)
.build();
List<String> command = nativeImageArgs.getArgs();
Expand Down Expand Up @@ -144,9 +144,10 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
noPIE = detectNoPIE();
}

String nativeImageName = getResultingBinaryName(outputTargetBuildItem, packageConfig, isContainerBuild);
String nativeImageName = getNativeImageName(outputTargetBuildItem, packageConfig);
String resultingExecutableName = getResultingExecutableName(nativeImageName, isContainerBuild);

NativeImageBuildRunner buildRunner = getNativeImageBuildRunner(nativeConfig, outputDir, nativeImageName);
NativeImageBuildRunner buildRunner = getNativeImageBuildRunner(nativeConfig, outputDir, resultingExecutableName);
buildRunner.setup(processInheritIODisabled.isPresent());
final GraalVM.Version graalVMVersion = buildRunner.getGraalVMVersion();

Expand All @@ -167,7 +168,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
.setNativeImageProperties(nativeImageProperties)
.setOutputDir(outputDir)
.setRunnerJarName(runnerJarName)
.setResultingBinaryName(nativeImageName)
.setNativeImageName(nativeImageName)
.setNoPIE(noPIE)
.setContainerBuild(isContainerBuild)
.setGraalVMVersion(graalVMVersion)
Expand All @@ -179,31 +180,31 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
if (exitCode != 0) {
throw imageGenerationFailed(exitCode, nativeImageArgs);
}
Path generatedImage = outputDir.resolve(nativeImageName);
Path finalPath = outputTargetBuildItem.getOutputDirectory().resolve(nativeImageName);
IoUtils.copy(generatedImage, finalPath);
Files.delete(generatedImage);
Path generatedExecutablePath = outputDir.resolve(resultingExecutableName);
Path finalExecutablePath = outputTargetBuildItem.getOutputDirectory().resolve(resultingExecutableName);
IoUtils.copy(generatedExecutablePath, finalExecutablePath);
Files.delete(generatedExecutablePath);
if (nativeConfig.debug.enabled) {
final String sources = "sources";
final Path generatedSources = outputDir.resolve(sources);
final Path finalSources = outputTargetBuildItem.getOutputDirectory().resolve(sources);
IoUtils.copy(generatedSources, finalSources);
IoUtils.recursiveDelete(generatedSources);
}
System.setProperty("native.image.path", finalPath.toAbsolutePath().toString());
System.setProperty("native.image.path", finalExecutablePath.toAbsolutePath().toString());

if (objcopyExists()) {
if (nativeConfig.debug.enabled) {
splitDebugSymbols(finalPath);
splitDebugSymbols(finalExecutablePath);
}
// Strip debug symbols regardless, because the underlying JDK might contain them
objcopy("--strip-debug", finalPath.toString());
objcopy("--strip-debug", finalExecutablePath.toString());
} 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.");
}

return new NativeImageBuildItem(finalPath);
return new NativeImageBuildItem(finalExecutablePath);
} catch (Exception e) {
throw new RuntimeException("Failed to build native image", e);
} finally {
Expand All @@ -214,22 +215,25 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
}
}

private String getResultingBinaryName(OutputTargetBuildItem outputTargetBuildItem, PackageConfig packageConfig,
boolean isContainerBuild) {
String nativeImageName = outputTargetBuildItem.getBaseName() + packageConfig.runnerSuffix;
private String getNativeImageName(OutputTargetBuildItem outputTargetBuildItem, PackageConfig packageConfig) {
return outputTargetBuildItem.getBaseName() + packageConfig.runnerSuffix;
}

private String getResultingExecutableName(String nativeImageName, boolean isContainerBuild) {
String resultingBinaryName = nativeImageName;
if (SystemUtils.IS_OS_WINDOWS && !isContainerBuild) {
//once image is generated it gets added .exe on Windows
nativeImageName = nativeImageName + ".exe";
resultingBinaryName = resultingBinaryName + ".exe";
}
return nativeImageName;
return resultingBinaryName;
}

public static boolean isContainerBuild(NativeConfig nativeConfig) {
return nativeConfig.containerRuntime.isPresent() || nativeConfig.containerBuild || nativeConfig.remoteContainerBuild;
}

private static NativeImageBuildRunner getNativeImageBuildRunner(NativeConfig nativeConfig, Path outputDir,
String nativeImageName) {
String resultingExecutableName) {
if (!isContainerBuild(nativeConfig)) {
NativeImageBuildLocalRunner localRunner = getNativeImageBuildLocalRunner(nativeConfig);
if (localRunner != null) {
Expand All @@ -244,7 +248,7 @@ private static NativeImageBuildRunner getNativeImageBuildRunner(NativeConfig nat
log.warn(errorMessage + " Attempting to fall back to container build.");
}
if (nativeConfig.remoteContainerBuild) {
return new NativeImageBuildRemoteContainerRunner(nativeConfig, outputDir, nativeImageName);
return new NativeImageBuildRemoteContainerRunner(nativeConfig, outputDir, resultingExecutableName);
}
return new NativeImageBuildLocalContainerRunner(nativeConfig, outputDir);
}
Expand Down Expand Up @@ -611,7 +615,7 @@ static class Builder {
private String noPIE = "";
private boolean isContainerBuild = false;
private GraalVM.Version graalVMVersion = GraalVM.Version.UNVERSIONED;
private String resultingBinaryName;
private String nativeImageName;

public Builder setNativeConfig(NativeConfig nativeConfig) {
this.nativeConfig = nativeConfig;
Expand Down Expand Up @@ -653,8 +657,8 @@ public Builder setGraalVMVersion(GraalVM.Version graalVMVersion) {
return this;
}

public Builder setResultingBinaryName(String resultingBinaryName) {
this.resultingBinaryName = resultingBinaryName;
public Builder setNativeImageName(String nativeImageName) {
this.nativeImageName = nativeImageName;
return this;
}

Expand Down Expand Up @@ -797,7 +801,7 @@ public NativeImageInvokerInfo build() {
nativeImageArgs.add("-H:+DashboardAll");
}

nativeImageArgs.add(resultingBinaryName);
nativeImageArgs.add(nativeImageName);

return new NativeImageInvokerInfo(nativeImageArgs);
}
Expand Down

0 comments on commit 3db801d

Please sign in to comment.