Skip to content

Commit 386561b

Browse files
Mr-Pineparttimenerd
authored andcommitted
Fix findNewestClang
1 parent d465fee commit 386561b

File tree

1 file changed

+24
-3
lines changed
  • bpf-processor/src/main/java/me/bechberger/ebpf/bpf/processor

1 file changed

+24
-3
lines changed

bpf-processor/src/main/java/me/bechberger/ebpf/bpf/processor/Processor.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
@SupportedSourceVersion(SourceVersion.RELEASE_22)
3636
public class Processor extends AbstractProcessor {
3737

38+
private static final int MINIMUM_CLANG_VERSION = 19;
3839
private static final String BPF = "me.bechberger.ebpf.annotations.bpf.BPF";
3940
private final CompilationCache cache = new CompilationCache(Paths.get("."));
4041

@@ -481,9 +482,26 @@ private List<String> findAutoAttachablePrograms(String ebpfProgram) {
481482
}
482483

483484
private static String findNewestClangVersion() {
484-
for (int i = 12; i > 11; i--) {
485+
var versionPattern = Pattern.compile("version (?<version>\\d+)");
486+
487+
var minimumVersion = MINIMUM_CLANG_VERSION - 1;
488+
try {
489+
var defaultClangProcess = new ProcessBuilder("clang", "--version").start();
490+
if (defaultClangProcess.waitFor() == 0) {
491+
var output = new String(defaultClangProcess.getInputStream().readAllBytes());
492+
var versionMatcher = versionPattern.matcher(output);
493+
versionMatcher.find();
494+
var version = Integer.parseInt(versionMatcher.group("version"));
495+
if (version > minimumVersion) {
496+
minimumVersion = version;
497+
}
498+
}
499+
} catch (IOException | InterruptedException e) {
500+
// ignore
501+
}
502+
for (int i = 20; i > minimumVersion; i--) {
485503
try {
486-
var name = i == 12 ? "clang" : "clang-" + i;
504+
var name = "clang-" + i;
487505
var process = new ProcessBuilder(name, "--version").start();
488506
if (process.waitFor() == 0) {
489507
return name;
@@ -492,7 +510,10 @@ private static String findNewestClangVersion() {
492510
// ignore
493511
}
494512
}
495-
throw new RuntimeException("Could not find clang");
513+
if (minimumVersion >= MINIMUM_CLANG_VERSION) {
514+
return "clang";
515+
}
516+
throw new RuntimeException("Could not find clang >= 19");
496517
}
497518

498519
private static final String newestClang = findNewestClangVersion();

0 commit comments

Comments
 (0)