@@ -3037,21 +3037,33 @@ private List<String> wordWrap(String line, int max) {
30373037 = new java .util .concurrent .ConcurrentHashMap <>();
30383038 static String bestCppStd (String gpp ) {
30393039 return _stdCache .computeIfAbsent (gpp , g -> {
3040- for ( String std : new String []{ "c++2c" , "c++23" , "c++20" , "c++17" }) {
3040+ // First verify g++ is actually available
30413041 try {
3042- ProcessBuilder pb = new ProcessBuilder (gpp , "-std=" + std , "-x" , "c++" , "-fsyntax-only" , "-" );
3043- pb .redirectErrorStream (true );
3044- Process proc = pb .start ();
3045- proc .getOutputStream ().close ();
3046- // 3 second timeout -- if g++ hangs on stdin, skip this std
3047- boolean finished = proc .waitFor (3 , java .util .concurrent .TimeUnit .SECONDS );
3048- if (!finished ) { proc .destroyForcibly (); continue ; }
3049- String out = new String (proc .getInputStream ().readAllBytes ());
3050- if (!out .contains ("unrecognized" ) && !out .contains ("invalid argument" ) && !out .contains ("note: use" )) {
3051- return std ;
3042+ Process test = new ProcessBuilder (g , "--version" ).redirectErrorStream (true ).start ();
3043+ test .getInputStream ().readAllBytes ();
3044+ if (!test .waitFor (3 , java .util .concurrent .TimeUnit .SECONDS )) {
3045+ test .destroyForcibly ();
3046+ return "c++17" ;
30523047 }
3053- } catch (Exception ignored ) {}
3054- }
3048+ if (test .exitValue () != 0 ) return "c++17" ;
3049+ } catch (Exception e ) {
3050+ return "c++17" ; // g++ not installed
3051+ }
3052+ // Probe for best supported standard
3053+ for (String std : new String []{"c++2c" , "c++23" , "c++20" , "c++17" }) {
3054+ try {
3055+ ProcessBuilder pb = new ProcessBuilder (g , "-std=" + std , "-x" , "c++" , "-fsyntax-only" , "-" );
3056+ pb .redirectErrorStream (true );
3057+ Process proc = pb .start ();
3058+ proc .getOutputStream ().close ();
3059+ boolean done = proc .waitFor (3 , java .util .concurrent .TimeUnit .SECONDS );
3060+ if (!done ) { proc .destroyForcibly (); continue ; }
3061+ String out = new String (proc .getInputStream ().readAllBytes ());
3062+ if (!out .contains ("unrecognized" ) && !out .contains ("invalid argument" ) && !out .contains ("note: use" )) {
3063+ return std ;
3064+ }
3065+ } catch (Exception ignored ) {}
3066+ }
30553067 return "c++17" ;
30563068 });
30573069 }
0 commit comments