Skip to content

Commit 695cc43

Browse files
committed
Auto-detect best C++ standard (c++2c/c++23/c++20/c++17) per g++ version
1 parent 45d58a6 commit 695cc43

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

mode/CppMode.jar

423 Bytes
Binary file not shown.

src/java/CppBuild.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,10 +2998,29 @@ private List<String> wordWrap(String line, int max) {
29982998
return r;
29992999
}
30003000

3001+
/** Returns the best -std=c++XX flag supported by the given g++ binary. */
3002+
static String bestCppStd(String gpp) {
3003+
for (String std : new String[]{"c++2c", "c++23", "c++20", "c++17"}) {
3004+
try {
3005+
ProcessBuilder pb = new ProcessBuilder(gpp, "-std=" + std, "-x", "c++", "-fsyntax-only", "-");
3006+
pb.redirectErrorStream(true);
3007+
Process proc = pb.start();
3008+
proc.getOutputStream().close();
3009+
String out = new String(proc.getInputStream().readAllBytes());
3010+
proc.waitFor();
3011+
if (!out.contains("unrecognized") && !out.contains("invalid argument") && !out.contains("note: use")) {
3012+
return std;
3013+
}
3014+
} catch (Exception ignored) {}
3015+
}
3016+
return "c++17";
3017+
}
3018+
30013019
private List<String> buildCommand(String gpp, File src, File bin, boolean win, boolean mac) {
30023020
List<String> cmd = new ArrayList<>();
30033021
cmd.add(gpp);
3004-
cmd.add("-std=c++2c");
3022+
String cppStd = bestCppStd(gpp);
3023+
cmd.add("-std=" + cppStd);
30053024
cmd.add("-O2");
30063025
// -march=native isn't reliably supported by Apple's clang (which is
30073026
// what g++/gcc resolve to on macOS via Xcode Command Line Tools) —
@@ -3054,7 +3073,7 @@ private List<String> buildCommand(String gpp, File src, File bin, boolean win, b
30543073

30553074
if (needsPch) {
30563075
List<String> pchCmd = new ArrayList<>();
3057-
pchCmd.add(gpp); pchCmd.add("-std=c++2c"); pchCmd.add("-O2");
3076+
pchCmd.add(gpp); pchCmd.add("-std=" + cppStd); pchCmd.add("-O2");
30583077
if (!mac) pchCmd.add("-march=x86-64");
30593078
if (homebrewPrefix != null) pchCmd.add("-I" + homebrewPrefix + "/include");
30603079
if (bundledInclude.exists()) pchCmd.add("-I" + bundledInclude.getAbsolutePath());
@@ -3070,7 +3089,7 @@ private List<String> buildCommand(String gpp, File src, File bin, boolean win, b
30703089
}
30713090
if (needsProcessing) {
30723091
List<String> preCmd = new ArrayList<>();
3073-
preCmd.add(gpp); preCmd.add("-std=c++2c"); preCmd.add("-O2");
3092+
preCmd.add(gpp); preCmd.add("-std=" + cppStd); preCmd.add("-O2");
30743093
if (!mac) preCmd.add("-march=native");
30753094
if (homebrewPrefix != null) preCmd.add("-I" + homebrewPrefix + "/include");
30763095
if (bundledInclude.exists()) preCmd.add("-I" + bundledInclude.getAbsolutePath());
@@ -3095,7 +3114,7 @@ private List<String> buildCommand(String gpp, File src, File bin, boolean win, b
30953114
}
30963115
if (needsDefaults) {
30973116
List<String> preCmd2 = new ArrayList<>();
3098-
preCmd2.add(gpp); preCmd2.add("-std=c++2c"); preCmd2.add("-O2");
3117+
preCmd2.add(gpp); preCmd2.add("-std=" + cppStd); preCmd2.add("-O2");
30993118
if (!mac) preCmd2.add("-march=native");
31003119
if (homebrewPrefix != null) preCmd2.add("-I" + homebrewPrefix + "/include");
31013120
if (bundledInclude.exists()) preCmd2.add("-I" + bundledInclude.getAbsolutePath());
@@ -3253,7 +3272,7 @@ private String preprocessMacros(String code) {
32533272
// the .cpp extension's default. -P would strip line markers; we keep
32543273
// them (omit -P) since downstream code already re-stamps its own
32553274
// #line directive, and keeping g++'s markers costs nothing here.
3256-
ProcessBuilder pb = new ProcessBuilder(gpp, "-E", "-std=c++2c", "-x", "c++", scratch.getAbsolutePath());
3275+
ProcessBuilder pb = new ProcessBuilder(gpp, "-E", "-std=c++23", "-x", "c++", scratch.getAbsolutePath());
32573276
pb.directory(buildDir != null ? buildDir : scratch.getParentFile());
32583277
pb.redirectErrorStream(false);
32593278
Process proc = pb.start();
@@ -3791,7 +3810,7 @@ private void exportTarget(Sketch sketch, ExportTarget t,
37913810
} else {
37923811
cmd.add(t.compiler);
37933812
}
3794-
cmd.add("-std=c++2c"); cmd.add("-O2");
3813+
cmd.add("-std=" + bestCppStd(t.compiler)); cmd.add("-O2");
37953814
for (File s : sources) cmd.add(s.getAbsolutePath());
37963815
// Use pre-built cached .o if available for this target
37973816
String exportCacheDir = t.isWindows ? "cache/windows-x64"

src/java/CppLinter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private File ensurePch(File runtimeDir, File processingH, String gpp) {
8585
// Build PCH: g++ -x c++-header -std=c++23 Processing.h -o Processing.h.gch
8686
try {
8787
ProcessBuilder pb = new ProcessBuilder(
88-
gpp, "-x", "c++-header", "-std=c++23",
88+
gpp, "-x", "c++-header", "-std=c++2c", "-std=c++23",
8989
"-I", runtimeDir.getAbsolutePath(),
9090
processingH.getAbsolutePath(),
9191
"-o", gch.getAbsolutePath()
@@ -162,7 +162,7 @@ private void runCheck(Sketch sketch, int currentTab, String liveText) {
162162
List<String> cmd = new ArrayList<>();
163163
cmd.add(gpp);
164164
cmd.add("-fsyntax-only");
165-
cmd.add("-std=c++23");
165+
cmd.add("-std=" + CppBuild.bestCppStd(gpp));
166166
cmd.add("-I");
167167
cmd.add(runtimeDir.getAbsolutePath());
168168
// Bundled headers (GLFW, GLEW)

0 commit comments

Comments
 (0)