Skip to content

Commit cfc333f

Browse files
authored
Merge pull request #49386 from gastaldi/cpu
Use SmallRye Common library for OS and CPU detection
2 parents a26b6cf + fd84cb2 commit cfc333f

File tree

3 files changed

+79
-18
lines changed

3 files changed

+79
-18
lines changed

core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/UnsupportedOSBuildItem.java

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@
55
import static io.quarkus.dev.console.QuarkusConsole.IS_WINDOWS;
66

77
import io.quarkus.builder.item.MultiBuildItem;
8+
import io.smallrye.common.cpu.CPU;
9+
import io.smallrye.common.os.OS;
810

911
/**
1012
* Native-image might not be supported for a particular
1113
* extension on a given OS or architecture.
1214
*/
1315
public final class UnsupportedOSBuildItem extends MultiBuildItem {
1416

17+
@Deprecated(forRemoval = true, since = "3.26.0")
1518
public static String ARCH = System.getProperty("os.arch");
1619

20+
/**
21+
* @deprecated Use {@link OS} instead
22+
*/
23+
@Deprecated(forRemoval = true, since = "3.26.0")
1724
public enum Os {
1825
WINDOWS(IS_WINDOWS),
1926
MAC(IS_MAC),
@@ -27,6 +34,10 @@ public enum Os {
2734
}
2835
}
2936

37+
/**
38+
* @deprecated Use {@link CPU} instead
39+
*/
40+
@Deprecated(forRemoval = true, since = "3.26.0")
3041
public enum Arch {
3142
AMD64("amd64".equalsIgnoreCase(ARCH)),
3243
AARCH64("aarch64".equalsIgnoreCase(ARCH)),
@@ -39,40 +50,91 @@ public enum Arch {
3950
}
4051
}
4152

42-
public final Os os;
43-
public final Arch arch;
44-
public final String error;
53+
private final OS os;
54+
private final CPU cpu;
55+
private final String error;
4556

57+
/**
58+
* @deprecated Use {@link UnsupportedOSBuildItem#UnsupportedOSBuildItem(io.smallrye.common.os.OS, java.lang.String)} instead
59+
*/
60+
@Deprecated(forRemoval = true, since = "3.26.0")
4661
public UnsupportedOSBuildItem(Os os, String error) {
47-
this.os = os;
48-
this.arch = Arch.NONE;
62+
this.os = switch (os) {
63+
case WINDOWS -> OS.WINDOWS;
64+
case MAC -> OS.MAC;
65+
case LINUX -> OS.LINUX;
66+
case NONE -> null;
67+
};
68+
this.cpu = null;
4969
this.error = error;
5070
}
5171

72+
/**
73+
* @deprecated Use {@link UnsupportedOSBuildItem#UnsupportedOSBuildItem(io.smallrye.common.cpu.CPU, java.lang.String)}
74+
* instead
75+
*/
76+
@Deprecated(forRemoval = true, since = "3.26.0")
5277
public UnsupportedOSBuildItem(Arch arch, String error) {
53-
this.os = Os.NONE;
54-
this.arch = arch;
78+
this.os = null;
79+
this.cpu = switch (arch) {
80+
case AMD64 -> CPU.x64;
81+
case AARCH64 -> CPU.aarch64;
82+
case NONE -> null;
83+
};
5584
this.error = error;
5685
}
5786

87+
/**
88+
* @deprecated Use
89+
* {@link UnsupportedOSBuildItem#UnsupportedOSBuildItem(io.smallrye.common.os.OS, io.smallrye.common.cpu.CPU, java.lang.String)}
90+
* instead
91+
*/
92+
@Deprecated(forRemoval = true, since = "3.26.0")
5893
public UnsupportedOSBuildItem(Os os, Arch arch, String error) {
94+
this.os = switch (os) {
95+
case WINDOWS -> OS.WINDOWS;
96+
case MAC -> OS.MAC;
97+
case LINUX -> OS.LINUX;
98+
case NONE -> null;
99+
};
100+
this.cpu = switch (arch) {
101+
case AMD64 -> CPU.x64;
102+
case AARCH64 -> CPU.aarch64;
103+
case NONE -> null;
104+
};
105+
this.error = error;
106+
}
107+
108+
public UnsupportedOSBuildItem(OS os, String error) {
109+
this(os, null, error);
110+
}
111+
112+
public UnsupportedOSBuildItem(CPU cpu, String error) {
113+
this(null, cpu, error);
114+
}
115+
116+
public UnsupportedOSBuildItem(OS os, CPU cpu, String error) {
59117
this.os = os;
60-
this.arch = arch;
118+
this.cpu = cpu;
61119
this.error = error;
62120
}
63121

64122
public boolean triggerError(boolean isContainerBuild) {
65123
return
66124
// When the host OS is unsupported, it could have helped to
67125
// run in a Linux builder image (e.g. an extension unsupported on Windows).
68-
((os.active && !isContainerBuild) ||
126+
((os != null && os == OS.current() && !isContainerBuild) ||
69127
// If Linux is the OS the extension does not support,
70128
// it fails in a container build regardless the host OS,
71129
// because we have only Linux based builder images.
72-
(isContainerBuild && os == Os.LINUX)) ||
130+
(isContainerBuild && os == OS.LINUX)) ||
73131
// We don't do cross-compilation, even builder images have to be
74132
// of the same arch, e.g. aarch64 Mac using aarch64 Linux builder image.
75133
// So if the arch is unsupported, it fails.
76-
arch.active;
134+
cpu != null && cpu == CPU.host();
135+
}
136+
137+
public String error() {
138+
return error;
77139
}
78140
}

core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ public NativeImageInvokerInfo build() {
10461046
if (unsupportedOSes != null && !unsupportedOSes.isEmpty()) {
10471047
final String errs = unsupportedOSes.stream()
10481048
.filter(o -> o.triggerError(containerBuild))
1049-
.map(o -> o.error)
1049+
.map(UnsupportedOSBuildItem::error)
10501050
.collect(Collectors.joining(", "));
10511051
if (!errs.isEmpty()) {
10521052
throw new UnsupportedOperationException(errs);

extensions/awt/deployment/src/main/java/io/quarkus/awt/deployment/AwtProcessor.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package io.quarkus.awt.deployment;
22

3-
import static io.quarkus.deployment.builditem.nativeimage.UnsupportedOSBuildItem.Arch.AARCH64;
4-
import static io.quarkus.deployment.builditem.nativeimage.UnsupportedOSBuildItem.Os.MAC;
5-
import static io.quarkus.deployment.builditem.nativeimage.UnsupportedOSBuildItem.Os.WINDOWS;
63
import static io.quarkus.runtime.graal.GraalVM.Version.CURRENT;
74

85
import java.util.ArrayList;
@@ -32,6 +29,8 @@
3229
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
3330
import io.quarkus.deployment.pkg.steps.NoopNativeImageBuildRunner;
3431
import io.quarkus.runtime.graal.GraalVM;
32+
import io.smallrye.common.cpu.CPU;
33+
import io.smallrye.common.os.OS;
3534

3635
class AwtProcessor {
3736

@@ -51,10 +50,10 @@ void nativeImageFeatures(BuildProducer<NativeImageFeatureBuildItem> nativeImageF
5150
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
5251
void supportCheck(BuildProducer<UnsupportedOSBuildItem> unsupported,
5352
NativeImageRunnerBuildItem nativeImageRunnerBuildItem) {
54-
unsupported.produce(new UnsupportedOSBuildItem(WINDOWS,
53+
unsupported.produce(new UnsupportedOSBuildItem(OS.WINDOWS,
5554
"Windows AWT integration is not ready in Quarkus native-image and would result in " +
5655
"java.lang.UnsatisfiedLinkError: no awt in java.library.path."));
57-
unsupported.produce(new UnsupportedOSBuildItem(MAC,
56+
unsupported.produce(new UnsupportedOSBuildItem(OS.MAC,
5857
"MacOS AWT integration is not ready in Quarkus native-image and would result in " +
5958
"java.lang.UnsatisfiedLinkError: Can't load library: awt | java.library.path = [.]."));
6059
final GraalVM.Version v;
@@ -68,7 +67,7 @@ void supportCheck(BuildProducer<UnsupportedOSBuildItem> unsupported,
6867

6968
if (v.compareTo(io.quarkus.deployment.pkg.steps.GraalVM.Version.VERSION_24_2_0) >= 0
7069
&& v.compareTo(GraalVM.Version.VERSION_25_0_0) < 0) {
71-
unsupported.produce(new UnsupportedOSBuildItem(AARCH64,
70+
unsupported.produce(new UnsupportedOSBuildItem(CPU.aarch64,
7271
"AWT needs JDK's JEP 454 FFI/FFM support and that is not available for AArch64 with " +
7372
"GraalVM's native-image prior to JDK 25, see: " +
7473
"https://www.graalvm.org/latest/reference-manual/native-image/native-code-interoperability/foreign-interface/#foreign-functions"));

0 commit comments

Comments
 (0)