[Libreoffice-commits] core.git: Branch 'feature/gccwrapper' - solenv/gcc-wrappers

Peter Foley pefoley2 at verizon.net
Wed Mar 6 15:58:09 PST 2013


 solenv/gcc-wrappers/wrapper.cxx |  105 ++++++++++++++++++++++++++++++----------
 1 file changed, 79 insertions(+), 26 deletions(-)

New commits:
commit 863aeceb42a955fcbe37963fe2db592a7bb5b79f
Author: Peter Foley <pefoley2 at verizon.net>
Date:   Wed Mar 6 18:35:32 2013 -0500

    use CreateProcess to get exit code
    
    Change-Id: I76bf9501243d173b20e8194748ca6c29e097efb4

diff --git a/solenv/gcc-wrappers/wrapper.cxx b/solenv/gcc-wrappers/wrapper.cxx
index 5ad0486..43c3d66 100644
--- a/solenv/gcc-wrappers/wrapper.cxx
+++ b/solenv/gcc-wrappers/wrapper.cxx
@@ -11,9 +11,13 @@
 #include <iostream>
 #include <vector>
 
+#include <windows.h>
+
 using namespace std;
 
-void setupenv() {
+vector<char> setupenv() {
+    vector<char> enviroment;
+
     // Set-up library path
     string libpath="LIB=";
     char* libbuf;
@@ -21,7 +25,8 @@ void setupenv() {
     _dupenv_s(&libbuf,&liblen,"ILIB");
     libpath.append(libbuf);
     free(libbuf);
-    _putenv(libpath.c_str());
+    enviroment.insert(enviroment.end(),libpath.begin(),libpath.end());
+    enviroment.push_back('\0');
 
     // Set-up include path
     string includepath="INCLUDE=.;";
@@ -31,8 +36,8 @@ void setupenv() {
     string incvar(incbuf);
     free(incbuf);
 
+    // 3 = strlen(" -I")
     for(size_t pos=0; pos != string::npos;) {
-        // 3 = strlen(" -I")
         size_t endpos=incvar.find(" -I",pos+3);
         size_t len=endpos-pos-3;
         if(endpos==string::npos)
@@ -41,50 +46,98 @@ void setupenv() {
             includepath.append(incvar,pos+3,len);
             includepath.append(";");
         }
-         pos=incvar.find(" -I",pos+len);
+        pos=incvar.find(" -I",pos+len);
     }
+    enviroment.insert(enviroment.end(),includepath.begin(),includepath.end());
+    enviroment.push_back('\0');
+
+    // CreateProcess requires the enviroment block to be null terminated
+    enviroment.push_back('\0');
 
-    _putenv(includepath.c_str());
+    return enviroment;
 }
 
-void processargs(string& command,vector<string> args) {
-    for(vector<string>::iterator i = args.begin(); i != args.end(); i++) {
-        command.append(" ");
+string processargs(vector<string> rawargs) {
+    string args;
+    for(vector<string>::iterator i = rawargs.begin(); i != rawargs.end(); i++) {
+        args.append(" ");
         if(*i == "-o") {
-        // TODO: handle more than just exe output
-            command.append("-Fe");
+            // TODO: handle more than just exe output
+            args.append("-Fe");
             i++;
-            command.append(*i);
-        }
-        else {
-            command.append(*i);
+            args.append(*i);
         }
+        else if(*i == "-g")
+            args.append("-Zi");
+        else
+            args.append(*i);
     }
+    return args;
 }
 
-extern int main(int argc, char *argv[]) {
+int startprocess(string command, string args, vector<char> enviroment) {
+    STARTUPINFO si;
+    PROCESS_INFORMATION pi;
+    DWORD ret;
+
+    HANDLE stdout_handle=GetStdHandle(STD_OUTPUT_HANDLE);
+    HANDLE stderr_handle=GetStdHandle(STD_ERROR_HANDLE);
+
+    memset(&si,0,sizeof(si));
+    si.cb=sizeof(si);
+    si.dwFlags |= STARTF_USESTDHANDLES;
+    si.hStdError=stderr_handle;
+    si.hStdOutput=stdout_handle;
+
+    memset(&pi,0,sizeof(pi));
+
+    void* env;
+    env=&enviroment.front();
+
+    if(!CreateProcess(command.c_str(), // Process Name
+        (char*)args.c_str(), // Command Line
+        NULL, // Process Handle not Inheritable
+        NULL, // Thread Handle not Inheritable
+        FALSE, // Handles are not Inherited
+        0, // No creation flags
+        env, // Enviroment for process
+        NULL, // Use same starting directory
+        &si, // Startup Info
+        &pi) // Process Information
+        ) {
+            cerr << "Error: could not create process" << endl;
+            exit(1);
+    }
+
+    WaitForSingleObject(&pi.hProcess, INFINITE);
+    GetExitCodeProcess(pi.hProcess, &ret);
+    CloseHandle(pi.hThread);
+    CloseHandle(pi.hProcess);
+    return int(ret);
+}
+
+int main(int argc, char *argv[]) {
+    vector<string> rawargs(argv + 1, argv + argc);
+
     char* ccbuf;
     size_t cclen;
     _dupenv_s(&ccbuf,&cclen,REAL_EXE);
     if(!ccbuf) {
-        cout << "Error" << REAL_EXE << "not defined. Did you forget to source the enviroment?" << endl;
+        cout << "Error " << REAL_EXE << " not defined. Did you forget to source the enviroment?" << endl;
         exit(1);
     }
     string command(ccbuf);
     free(ccbuf);
 
-    vector<string> args(argv + 1, argv + argc);
-    processargs(command,args);
-    setupenv();
+    string args=processargs(rawargs);
 
-    cout << "CC=" << command << endl;
+    vector<char> enviroment=setupenv();
+
+    cerr << "CC=" << command << "ARGS=" << args << endl;
+
+    int ret=startprocess(command,args,enviroment);
+    return ret;
 
-    FILE* output=_popen(command.c_str(),"r");
-    char outbuf[256];
-    while(fgets(outbuf, 256, output)) {
-        cout << outbuf;
-    }
-    cout << endl;
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list