MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / gcc

Method gcc

chapter23/src/test/java/com/seaofnodes/simple/TestC.java:73–121  ·  view source on GitHub ↗
( String obj, String c_conv, String cfile, boolean stdin, String... args )

Source from the content-addressed store, hash-verified

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

Callers 2

runMethod · 0.95
mainMethod · 0.45

Calls 3

addMethod · 0.95
addAllMethod · 0.95
asAryMethod · 0.95

Tested by

no test coverage detected