1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include "wrapper.hxx"
12 #define WIN32_LEAN_AND_MEAN
18 string
getexe(string exename
) {
21 _dupenv_s(&cmdbuf
,&cmdlen
,exename
.c_str());
23 cout
<< "Error " << exename
<< " not defined. Did you forget to source the enviroment?" << endl
;
26 string
command(cmdbuf
);
32 // Set-up library path
33 string libpath
="LIB=";
36 _dupenv_s(&libbuf
,&liblen
,"ILIB");
37 libpath
.append(libbuf
);
39 if(_putenv(libpath
.c_str())<0) {
40 cerr
<< "Error: could not export LIB" << endl
;
44 // Set-up include path
45 string includepath
="INCLUDE=.;";
48 _dupenv_s(&incbuf
,&inclen
,"SOLARINC");
49 string
inctmp(incbuf
);
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
);
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
;
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"))
81 args
.append(" -Zc:wchar_t-");
82 args
.append(" -Ob1 -Oxs -Oy-");
84 for(vector
<string
>::iterator i
= rawargs
.begin(); i
!= rawargs
.end(); ++i
) {
87 // TODO: handle more than just exe output
89 size_t dot
=(*i
).find_last_of(".");
90 if(!(*i
).compare(dot
+1,3,"obj"))
92 else if(!(*i
).compare(dot
+1,3,"exe"))
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,"\\");
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")
123 int startprocess(string command
, string args
) {
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
;
144 si
.dwFlags
|= STARTF_USESTDHANDLES
;
145 si
.hStdOutput
=childout_write
;
146 si
.hStdError
=childout_write
;
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
169 &pi
) // Process Information
171 cerr
<< "Error: could not create process" << endl
;
175 // if you don't close this the process will hang
176 CloseHandle(childout_write
);
178 // Get Process output
180 DWORD readlen
, writelen
, ret
;
181 HANDLE stdout_handle
=GetStdHandle(STD_OUTPUT_HANDLE
);
183 int success
=ReadFile(childout_read
,buffer
,BUFLEN
,&readlen
,NULL
);
184 // check if the child process has exited
185 if(GetLastError()==ERROR_BROKEN_PIPE
)
188 cerr
<< "Error: could not read from subprocess stdout" << endl
;
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
);
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */