| 88 | } |
| 89 | |
| 90 | public static String gcc( String obj, String c_conv, String cfile, boolean stdin, String... args ) throws IOException { |
| 91 | |
| 92 | // Compile the C program. Compiling code and constants in the low |
| 93 | // 2Gig. Pointers are 64b BUT since always in the low 2G all the |
| 94 | // high bits are zero - and Simple code can be emitted treating |
| 95 | // pointers as 4bytes. |
| 96 | var params = new Ary<>(String.class); |
| 97 | params.add("gcc"); |
| 98 | if( cfile!=null ) params.add(cfile); // Associated C driver, usually has a `main` |
| 99 | params.addAll(new String[] { |
| 100 | obj, |
| 101 | "-lm", // Picks up 'sqrt' for newtonFloat tests to compare |
| 102 | "-g", |
| 103 | "-o", |
| 104 | args[0], |
| 105 | }); |
| 106 | // Calling convention for C calls, if any |
| 107 | if( cfile!=null ) { |
| 108 | params.add("-D"); |
| 109 | params.add("CALL_CONV="+c_conv); |
| 110 | } |
| 111 | |
| 112 | // Run GCC to link (optionally compile C driver code) |
| 113 | Process gcc = new ProcessBuilder(params.asAry()).redirectErrorStream(true).start(); |
| 114 | int exit; |
| 115 | try { |
| 116 | boolean normal = gcc.waitFor(2, TimeUnit.SECONDS); |
| 117 | exit = normal ? gcc.exitValue() : -1; // no exit??? |
| 118 | } catch( InterruptedException e ) { |
| 119 | throw new IOException("interrupted"); |
| 120 | } |
| 121 | String result = new String(gcc.getInputStream().readAllBytes()); |
| 122 | if( exit!=0 ) { |
| 123 | System.err.println("gcc error code: "+exit); |
| 124 | System.err.println(result); |
| 125 | } |
| 126 | assertEquals( 0, exit ); |
| 127 | //assertTrue(result.isEmpty()); // No data in error stream |
| 128 | |
| 129 | // Execute results |
| 130 | ProcessBuilder smp = new ProcessBuilder(args); |
| 131 | if( stdin ) smp.redirectInput(ProcessBuilder.Redirect.INHERIT); |
| 132 | Process p = smp.start(); |
| 133 | try { exit = (byte)p.waitFor(); } catch( InterruptedException e ) { throw new IOException("interrupted"); } |
| 134 | result = new String(p.getInputStream().readAllBytes()); |
| 135 | if( exit!=0 ) |
| 136 | System.err.println("exec exit code: "+exit); |
| 137 | return result; |
| 138 | } |
| 139 | } |