3939import processing .app .debug .*;
4040import processing .app .helpers .PreferencesMap ;
4141import processing .app .helpers .PreferencesMapException ;
42+ import processing .app .helpers .ProcessUtils ;
4243import processing .app .helpers .StringReplacer ;
4344import processing .app .legacy .PApplet ;
4445import processing .app .tools .DoubleQuotedArgumentsOnWindowsCommandLine ;
@@ -141,7 +142,7 @@ public String build(CompilerProgressListener progListener, boolean exportHex) th
141142 MessageConsumerOutputStream out = new MessageConsumerOutputStream (new ProgressAwareMessageConsumer (new I18NAwareMessageConsumer (System .out , System .err ), progListener ), "\n " );
142143 MessageConsumerOutputStream err = new MessageConsumerOutputStream (new I18NAwareMessageConsumer (System .err , Compiler .this ), "\n " );
143144
144- callArduinoBuilder (board , platform , aPackage , vidpid , BuilderAction .COMPILE , new PumpStreamHandler ( out , err ) );
145+ callArduinoBuilder (board , platform , aPackage , vidpid , BuilderAction .COMPILE , out , err );
145146
146147 out .flush ();
147148 err .flush ();
@@ -179,7 +180,7 @@ private PreferencesMap loadPreferences(TargetBoard board, TargetPlatform platfor
179180 ByteArrayOutputStream stderr = new ByteArrayOutputStream ();
180181 MessageConsumerOutputStream err = new MessageConsumerOutputStream (new I18NAwareMessageConsumer (new PrintStream (stderr ), Compiler .this ), "\n " );
181182 try {
182- callArduinoBuilder (board , platform , aPackage , vidpid , BuilderAction .DUMP_PREFS , new PumpStreamHandler ( stdout , err ) );
183+ callArduinoBuilder (board , platform , aPackage , vidpid , BuilderAction .DUMP_PREFS , stdout , err );
183184 } catch (RunnerException e ) {
184185 System .err .println (new String (stderr .toByteArray ()));
185186 throw e ;
@@ -189,90 +190,101 @@ private PreferencesMap loadPreferences(TargetBoard board, TargetPlatform platfor
189190 return prefs ;
190191 }
191192
192- private void callArduinoBuilder (TargetBoard board , TargetPlatform platform , TargetPackage aPackage , String vidpid , BuilderAction action , PumpStreamHandler streamHandler ) throws RunnerException {
193- File executable = BaseNoGui . getContentFile ( "arduino-builder" );
194- CommandLine commandLine = new CommandLine ( executable );
195- commandLine . addArgument (action .value , false );
196- commandLine . addArgument ("-logger=machine" , false );
193+ private void callArduinoBuilder (TargetBoard board , TargetPlatform platform , TargetPackage aPackage , String vidpid , BuilderAction action , OutputStream outStream , OutputStream errStream ) throws RunnerException {
194+ List < String > cmd = new ArrayList <>( );
195+ cmd . add ( BaseNoGui . getContentFile ( "arduino-builder" ). getAbsolutePath () );
196+ cmd . add (action .value );
197+ cmd . add ("-logger=machine" );
197198
198199 Stream .of (BaseNoGui .getHardwarePath (), new File (BaseNoGui .getSettingsFolder (), "packages" ).getAbsolutePath (), BaseNoGui .getSketchbookHardwareFolder ().getAbsolutePath ())
199200 .forEach (p -> {
200201 if (Files .exists (Paths .get (p ))) {
201- commandLine . addArgument ("-hardware" , false );
202- commandLine . addArgument ( " \" " + p + " \" " , false );
202+ cmd . add ("-hardware" );
203+ cmd . add ( p );
203204 }
204205 });
205206
206207 Stream .of (BaseNoGui .getContentFile ("tools-builder" ).getAbsolutePath (), Paths .get (BaseNoGui .getHardwarePath (), "tools" , "avr" ).toAbsolutePath ().toString (), new File (BaseNoGui .getSettingsFolder (), "packages" ).getAbsolutePath ())
207208 .forEach (p -> {
208209 if (Files .exists (Paths .get (p ))) {
209- commandLine . addArgument ("-tools" , false );
210- commandLine . addArgument ( " \" " + p + " \" " , false );
210+ cmd . add ("-tools" );
211+ cmd . add ( p );
211212 }
212213 });
213214
214- commandLine . addArgument ("-built-in-libraries" , false );
215- commandLine . addArgument ( " \" " + BaseNoGui .getContentFile ("libraries" ).getAbsolutePath () + " \" " , false );
216- commandLine . addArgument ("-libraries" , false );
217- commandLine . addArgument ( " \" " + BaseNoGui .getSketchbookLibrariesFolder ().getAbsolutePath () + " \" " , false );
215+ cmd . add ("-built-in-libraries" );
216+ cmd . add ( BaseNoGui .getContentFile ("libraries" ).getAbsolutePath ());
217+ cmd . add ("-libraries" );
218+ cmd . add ( BaseNoGui .getSketchbookLibrariesFolder ().getAbsolutePath ());
218219
219220 String fqbn = Stream .of (aPackage .getId (), platform .getId (), board .getId (), boardOptions (board )).filter (s -> !s .isEmpty ()).collect (Collectors .joining (":" ));
220- commandLine . addArgument ("-fqbn=" + fqbn , false );
221+ cmd . add ("-fqbn=" + fqbn );
221222
222223 if (!"" .equals (vidpid )) {
223- commandLine . addArgument ("-vid-pid=" + vidpid , false );
224+ cmd . add ("-vid-pid=" + vidpid );
224225 }
225226
226- commandLine . addArgument ("-ide-version=" + BaseNoGui .REVISION , false );
227- commandLine . addArgument ("-build-path" , false );
228- commandLine . addArgument ( " \" " + buildPath + " \" " , false );
229- commandLine . addArgument ("-warnings=" + PreferencesData .get ("compiler.warning_level" ), false );
227+ cmd . add ("-ide-version=" + BaseNoGui .REVISION );
228+ cmd . add ("-build-path" );
229+ cmd . add ( buildPath );
230+ cmd . add ("-warnings=" + PreferencesData .get ("compiler.warning_level" ));
230231
231232 PreferencesData .getMap ()
232233 .subTree ("runtime.build_properties_custom" )
233234 .entrySet ()
234235 .stream ()
235- .forEach (kv -> commandLine . addArgument ("-prefs=\" " + kv .getKey () + "=" + kv .getValue () + " \" " , false ));
236+ .forEach (kv -> cmd . add ("-prefs=" + kv .getKey () + "=" + kv .getValue ()));
236237
237- commandLine . addArgument ("-prefs=build.warn_data_percentage=" + PreferencesData .get ("build.warn_data_percentage" ));
238+ cmd . add ("-prefs=build.warn_data_percentage=" + PreferencesData .get ("build.warn_data_percentage" ));
238239
239240 for (Map .Entry <String , String > entry : BaseNoGui .getBoardPreferences ().entrySet ()) {
240241 if (entry .getKey ().startsWith ("runtime.tools" )) {
241- commandLine . addArgument ("-prefs=" + entry .getKey () + "=" + entry .getValue ());
242+ cmd . add ("-prefs=" + entry .getKey () + "=" + entry .getValue ());
242243 }
243244 }
244245
245246 //commandLine.addArgument("-debug-level=10", false);
246247
247248 if (verbose ) {
248- commandLine . addArgument ("-verbose" , false );
249+ cmd . add ("-verbose" );
249250 }
250251
251- commandLine . addArgument ( " \" " + pathToSketch + " \" " , false );
252+ cmd . add ( pathToSketch );
252253
253254 if (verbose ) {
254- System .out .println (commandLine );
255+ System .out .println (StringUtils . join ( cmd , ' ' ) );
255256 }
256257
257- DefaultExecutor executor = new DefaultExecutor ();
258- executor .setStreamHandler (streamHandler );
259-
260258 int result ;
261- executor .setExitValues (null );
262259 try {
263- result = executor .execute (commandLine );
264- } catch (IOException e ) {
265- RunnerException re = new RunnerException (e .getMessage ());
266- re .hideStackTrace ();
267- throw re ;
260+ Process proc = ProcessUtils .exec (cmd .toArray (new String [0 ]));
261+ MessageSiphon in = new MessageSiphon (proc .getInputStream (), (msg ) -> {
262+ try {
263+ outStream .write (msg .getBytes ());
264+ } catch (Exception e ) {
265+ exception = new RunnerException (e );
266+ }
267+ });
268+ MessageSiphon err = new MessageSiphon (proc .getErrorStream (), (msg ) -> {
269+ try {
270+ errStream .write (msg .getBytes ());
271+ } catch (Exception e ) {
272+ exception = new RunnerException (e );
273+ }
274+ });
275+
276+ in .join ();
277+ err .join ();
278+ result = proc .waitFor ();
279+ } catch (Exception e ) {
280+ throw new RunnerException (e );
268281 }
269- executor .setExitValues (new int [0 ]);
270282
271283 if (exception != null )
272284 throw exception ;
273285
274286 if (result > 1 ) {
275- System .err .println (I18n .format (tr ("{0} returned {1}" ), executable . getName ( ), result ));
287+ System .err .println (I18n .format (tr ("{0} returned {1}" ), cmd . get ( 0 ), result ));
276288 }
277289
278290 if (result != 0 ) {
0 commit comments