(unfinished) Compile and Upload program (C#)

Alright.. this is nothing huge.. i just prepared it yesterday and wrote some lines today i stopped it today evening already again as i got some new 'project' at the university to attend.. hence i will give whoever wants it the code for a more or less standalone ardiono 'compiler' and uploader.
It compiles all libraries nicely and links them into the o's and a's but it doesnt want to compile the sketch. Uploading is not yet implemented, but i wanted to finish compiling first.
Done in VS10(beta) with Net3.5 - but should be compatible with every .Net version i know..

Main program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;


namespace ArduinoNPP
{
    class Program
    {



        static void Main(string[] args)
        {
            Console.WriteLine("Commadnline compiler");
            Console.WriteLine("By martin@shalmirane.de");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Starting");
            Console.WriteLine();

            string gcc = ConfigurationManager.AppSettings.Get("path_to_arduino") + @"\hardware\tools\avr\bin\avr-gcc";
            string gpp = ConfigurationManager.AppSettings.Get("path_to_arduino") + @"\hardware\tools\avr\bin\avr-g++";
            string ar = ConfigurationManager.AppSettings.Get("path_to_arduino") + @"\hardware\tools\avr\bin\avr-ar";
            string objcp = ConfigurationManager.AppSettings.Get("path_to_arduino") + @"\hardware\tools\avr\bin\avr-objcopy";

            string mcu = ConfigurationManager.AppSettings.Get("mcu");
            string df_cpu = ConfigurationManager.AppSettings.Get("df_cpu");
            string temp = ConfigurationManager.AppSettings.Get("path_to_temp_files");
            string ardu = ConfigurationManager.AppSettings.Get("path_to_arduino");

            Console.WriteLine("");
            Console.WriteLine("Parsing PDE");

            string sketch = "";
            if (args[0].EndsWith(".pde"))
            {
                sketch = File.ReadAllText(args[0]);
            }


            //Read file and convert from PDE to cpp...

            /*
                        ArrayList list = new ArrayList();
                        //Find all functions
                        string pat = @"(?i)void.[^{]*"; //Need to change towards int, char etc....
                        string text = sketch;
                        foreach (Match match in Regex.Matches(text,pat))
                        {
                            int index = match.Index;

                            while (text[index++] != '{') ;
                            int bracketCount = 1;
                            while (bracketCount > 0)
                            {
                                if (text[index] == '{')
                                    bracketCount++;
                                else if (text[index] == '}')
                                    bracketCount--;
                                index++;
                            }

                            string methodContent = text.Substring(match.Index, index - match.Index);
                            list.Add(methodContent);
            //                Console.WriteLine(methodContent);
                        }


                        string setup = "";
                        string loop = "";


                        int i = -1;
                        foreach (string fn in list)
                        { 
                        i++;
    
                            if(fn.IndexOf("setup()")>0)
                            {
                                setup = fn;
                                list.Remove(i);
                            }
                            if(fn.IndexOf("loop()")>0)
                            {
                                loop = fn;
                                list.Remove(i);
                            }         
                        }
            */




            Console.WriteLine("Compiling libraries");
            run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu="+mcu+" -DF_CPU="+df_cpu+@" -I"+ardu+@"\hardware\cores\arduino "+ardu+@"\hardware\cores\arduino\pins_arduino.c -o"+temp+@"\pins_arduino.c.o");
            run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\WInterrupts.c -o" + temp + @"\WInterrupts.c.o");
            run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\wiring.c -o" + temp + @"\wiring.c.o");
            run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\wiring_analog.c -o" + temp + @"\wiring_analog.c.o");
            run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\wiring_digital.c -o" + temp + @"\wiring_digital.c.o");
            run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\wiring_pulse.c -o" + temp + @"\wiring_pulse.c.o");
            run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\wiring_shift.c -o" + temp + @"\wiring_shift.c.o");

            run(gpp, @"-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=" + mcu + @" -DF_CPU=" + df_cpu + " -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\HardwareSerial.cpp -o" + temp + @"\HardwareSerial.cpp.o");
            run(gpp, @"-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=" + mcu + @" -DF_CPU=" + df_cpu + " -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\Print.cpp -o" + temp + @"\Print.cpp.o");
            run(gpp, @"-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=" + mcu + @" -DF_CPU=" + df_cpu + " -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\WMath.cpp -o" + temp + @"\WMath.cpp.o");



            Console.WriteLine("");
            Console.WriteLine("Linking");
            run(ar,"rcs " + temp + @"core.a " + temp +@"pins_arduino.c.o");
            run(ar,"rcs " + temp + @"core.a " + temp +@"WInterrupts.c.o" );
            run(ar,"rcs " + temp + @"core.a " + temp +@"wiring.c.o" );
            run(ar,"rcs " + temp + @"core.a " + temp +@"wiring_analog.c.o");
            run(ar,"rcs " + temp + @"core.a " + temp +@"wiring_digital.c.o" );
            run(ar,"rcs " + temp + @"core.a " + temp +@"wiring_pulse.c.o" );
            run(ar,"rcs " + temp + @"core.a " + temp +@"wiring_shift.c.o");
            run(ar,"rcs " + temp + @"core.a " + temp +@"HardwareSerial.cpp.o");
            run(ar,"rcs " + temp + @"core.a " + temp +@"Print.cpp.o");
            run(ar,"rcs " + temp + @"core.a " + temp +@"WMath.cpp.o");



            Console.WriteLine("");
            Console.WriteLine("Compiling sketch");


            FileInfo fi = new FileInfo(args[0]);
            string fullpath = fi.FullName;
            string filename = fi.Name;

            Console.WriteLine(temp + filename);

            run(gpp, "-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + " -I" + ardu + @"\hardware\cores\arduino " + fullpath + " -o" + temp + filename + ".o");
            run(gcc, " -Os -Wl,--gc-sections -mmcu=" + mcu + @" -o " + temp + filename + ".elf " + temp + filename + ".o " + temp + "core.a -L" + temp + @"file.tmp -lm ");

            run(objcp, "-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 " + temp + @"\" + filename + ".elf " + temp + @"\" + filename + ".eep ");
            run(objcp, " -O ihex -R .eeprom " + temp + filename + ".elf  " + temp  + filename + ".hex ");

            Console.WriteLine("");
            Console.WriteLine("Upload");
            Console.WriteLine("...umm - not yet...");



            Console.WriteLine("");
            Console.WriteLine("Done. Press ENTER to exit");
            Console.ReadLine();
        }

        public static string run(string filename, string arguments)
        {
            System.IO.StreamReader sr;
            Process builder = new Process();
            
            builder.StartInfo.RedirectStandardOutput = true;
            builder.StartInfo.UseShellExecute = false;
            builder.StartInfo.CreateNoWindow = true;
            builder.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            builder.StartInfo.FileName = filename;
            builder.StartInfo.Arguments = arguments;
            builder.Start();
            builder.WaitForExit();
            sr = builder.StandardOutput;
            Console.Write(".");
            return "";
        }
}

example app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="mcu" value="atmega328p" />
    <add key="df_cpu" value="16000000L" />
    <add key="path_to_arduino" value="C:\arduino-0017\" />
    <add key="path_to_temp_files" value="C:\ardutemp\" />
  </appSettings>
</configuration>

and example sketch:

#include "WProgram.h"
// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

int main(void) {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
  
  while(true)
  {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, is the difference
  // between the current time and last time we blinked the LED bigger than
  // the interval at which we want to blink the LED.
        if (millis() - previousMillis > interval) {
            // save the last time you blinked the LED 
            previousMillis = millis();   

            // if the LED is off turn it on and vice-versa:
            if (ledState == LOW)
              ledState = HIGH;
            else
              ledState = LOW;
              
            // set the LED with the ledState of the variable:
            digitalWrite(ledPin, ledState);
        }
  }
  
}