update credits
[LibreOffice.git] / solenv / gcc-wrappers / wrapper.cxx
blob151979ca7991b27ca816a211c5582c49e4b8aa73
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include "wrapper.hxx"
12 #define WIN32_LEAN_AND_MEAN
14 #include <windows.h>
16 #define BUFLEN 2048
18 string getexe(string exename) {
19 char* cmdbuf;
20 size_t cmdlen;
21 _dupenv_s(&cmdbuf,&cmdlen,exename.c_str());
22 if(!cmdbuf) {
23 cout << "Error " << exename << " not defined. Did you forget to source the enviroment?" << endl;
24 exit(1);
26 string command(cmdbuf);
27 free(cmdbuf);
28 return command;
31 void setupccenv() {
32 // Set-up library path
33 string libpath="LIB=";
34 char* libbuf;
35 size_t liblen;
36 _dupenv_s(&libbuf,&liblen,"ILIB");
37 libpath.append(libbuf);
38 free(libbuf);
39 if(_putenv(libpath.c_str())<0) {
40 cerr << "Error: could not export LIB" << endl;
41 exit(1);
44 // Set-up include path
45 string includepath="INCLUDE=.;";
46 char* incbuf;
47 size_t inclen;
48 _dupenv_s(&incbuf,&inclen,"SOLARINC");
49 string inctmp(incbuf);
50 free(incbuf);
52 // 3 = strlen(" -I")
53 for(size_t pos=0; pos != string::npos;) {
54 size_t endpos=inctmp.find(" -I",pos+3);
55 size_t len=endpos-pos-3;
56 if(endpos==string::npos)
57 includepath.append(inctmp,pos+3,endpos);
58 else if(len>0) {
59 includepath.append(inctmp,pos+3,len);
60 includepath.append(";");
62 pos=inctmp.find(" -I",pos+len);
64 if(_putenv(includepath.c_str())<0) {
65 cerr << "Error: could not export INCLUDE" << endl;
66 exit(1);
70 string processccargs(vector<string> rawargs) {
71 // suppress the msvc banner
72 string args=" -nologo";
73 // TODO: should these options be enabled globally?
74 args.append(" -EHsc");
75 const char *const pDebugRuntime(getenv("MSVC_USE_DEBUG_RUNTIME"));
76 if (pDebugRuntime && !strcmp(pDebugRuntime, "TRUE"))
77 args.append(" -MDd");
78 else
79 args.append(" -MD");
80 args.append(" -Gy");
81 args.append(" -Zc:wchar_t-");
82 args.append(" -Ob1 -Oxs -Oy-");
84 for(vector<string>::iterator i = rawargs.begin(); i != rawargs.end(); ++i) {
85 args.append(" ");
86 if(*i == "-o") {
87 // TODO: handle more than just exe output
88 ++i;
89 size_t dot=(*i).find_last_of(".");
90 if(!(*i).compare(dot+1,3,"obj"))
91 args.append("-Fo");
92 else if(!(*i).compare(dot+1,3,"exe"))
93 args.append("-Fe");
94 args.append(*i);
96 else if(*i == "-g")
97 args.append("-Zi");
98 else if(!(*i).compare(0,2,"-D")) {
99 // need to re-escape strings for preprocessor
100 for(size_t pos=(*i).find("\"",0); pos!=string::npos; pos=(*i).find("\"",pos)) {
101 (*i).replace(pos,0,"\\");
102 pos+=2;
104 args.append(*i);
106 else if(!(*i).compare(0,2,"-L")) {
107 args.append("-link -LIBPATH:"+(*i).substr(2));
109 else if(!(*i).compare(0,2,"-l")) {
110 args.append((*i).substr(2)+".lib");
112 else if(!(*i).compare(0,12,"-fvisibility")) {
113 //TODO: drop other gcc-specific options
115 else if(*i == "-Werror")
116 args.append("-WX");
117 else
118 args.append(*i);
120 return args;
123 int startprocess(string command, string args) {
124 STARTUPINFO si;
125 PROCESS_INFORMATION pi;
126 SECURITY_ATTRIBUTES sa;
128 HANDLE childout_read;
129 HANDLE childout_write;
131 memset(&sa,0,sizeof(sa));
132 memset(&si,0,sizeof(si));
133 memset(&pi,0,sizeof(pi));
135 sa.nLength=sizeof(sa);
136 sa.bInheritHandle=TRUE;
138 if(!CreatePipe(&childout_read,&childout_write,&sa,0)) {
139 cerr << "Error: could not create stdout pipe" << endl;
140 exit(1);
143 si.cb=sizeof(si);
144 si.dwFlags |= STARTF_USESTDHANDLES;
145 si.hStdOutput=childout_write;
146 si.hStdError=childout_write;
148 // support ccache
149 size_t pos=command.find("ccache ");
150 if(pos != string::npos) {
151 args.insert(0,"cl.exe");
152 command=command.substr(0,pos+strlen("ccache"))+".exe";
155 //cerr << "CMD= " << command << " " << args << endl;
157 // Commandline may be modified by CreateProcess
158 char* cmdline=_strdup(args.c_str());
160 if(!CreateProcess(command.c_str(), // Process Name
161 cmdline, // Command Line
162 NULL, // Process Handle not Inheritable
163 NULL, // Thread Handle not Inheritable
164 TRUE, // Handles are Inherited
165 0, // No creation flags
166 NULL, // Enviroment for process
167 NULL, // Use same starting directory
168 &si, // Startup Info
169 &pi) // Process Information
171 cerr << "Error: could not create process" << endl;
172 exit(1);
175 // if you don't close this the process will hang
176 CloseHandle(childout_write);
178 // Get Process output
179 char buffer[BUFLEN];
180 DWORD readlen, writelen, ret;
181 HANDLE stdout_handle=GetStdHandle(STD_OUTPUT_HANDLE);
182 while(true) {
183 int success=ReadFile(childout_read,buffer,BUFLEN,&readlen,NULL);
184 // check if the child process has exited
185 if(GetLastError()==ERROR_BROKEN_PIPE)
186 break;
187 if(!success) {
188 cerr << "Error: could not read from subprocess stdout" << endl;
189 exit(1);
191 if(readlen!=0) {
192 WriteFile(stdout_handle,buffer,readlen,&writelen,NULL);
195 WaitForSingleObject(pi.hProcess, INFINITE);
196 GetExitCodeProcess(pi.hProcess, &ret);
197 CloseHandle(pi.hThread);
198 CloseHandle(pi.hProcess);
199 return int(ret);
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */