00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00026 #include <iostream>
00027 #include <stdexcept>
00028 #include <sstream>
00029 #include <cstdio>
00030 #include <iomanip>
00031 #include "Makefile.h"
00032 #include "test_util.h"
00033
00034 #include <windows.h>
00035 #include <tchar.h>
00036
00037 #define BOOST_IOSTREAMS_USE_DEPRECATED
00038 #include <boost/iostreams/stream.hpp>
00039 #include <boost/iostreams/device/file_descriptor.hpp>
00040 #include <boost/iostreams/code_converter.hpp>
00041
00042 using namespace makefile;
00043 namespace fs = boost::filesystem;
00044 namespace io = boost::iostreams;
00045
00046 namespace makefile { namespace tester {
00056 int exec_make(std::stringbuf& makeoutbuf, std::stringbuf& makeerrbuf)
00057 {
00058
00059 HANDLE hmakein_rd = NULL;
00060 HANDLE hmakein_wr = NULL;
00061 HANDLE hmakeout_rd = NULL;
00062 HANDLE hmakeout_wr = NULL;
00063 HANDLE hmakeerr_rd = NULL;
00064 HANDLE hmakeerr_wr = NULL;
00065
00066
00067 SECURITY_ATTRIBUTES sa;
00068 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
00069 sa.bInheritHandle = true;
00070 sa.lpSecurityDescriptor = NULL;
00071
00072 if( !CreatePipe(&hmakeout_rd, &hmakeout_wr, &sa, 0) ||
00073 !CreatePipe(&hmakein_rd, &hmakein_wr, &sa, 0) ||
00074 !CreatePipe(&hmakeerr_rd, &hmakeerr_wr, &sa, 0) ||
00075 !SetHandleInformation(hmakeout_rd, HANDLE_FLAG_INHERIT, 0) ||
00076 !SetHandleInformation(hmakein_wr, HANDLE_FLAG_INHERIT, 0) ||
00077 !SetHandleInformation(hmakeerr_rd, HANDLE_FLAG_INHERIT, 0))
00078 {
00079 std::cerr << "CreatePipe() failed." << std::endl;
00080 return -1;
00081 }
00082
00083
00084
00085 PROCESS_INFORMATION pi;
00086 STARTUPINFO si;
00087
00088
00089
00090 ZeroMemory( &pi, sizeof(PROCESS_INFORMATION) );
00091
00092
00093
00094
00095 ZeroMemory( &si, sizeof(STARTUPINFO) );
00096 si.cb = sizeof(STARTUPINFO);
00097 si.hStdError = hmakeerr_wr;
00098 si.hStdOutput = hmakeout_wr;
00099 si.hStdInput = hmakein_rd;
00100 si.dwFlags |= STARTF_USESTDHANDLES;
00101
00102
00103
00104 if( !CreateProcess(NULL,
00105 "make -f-",
00106 NULL,
00107 NULL,
00108 TRUE,
00109 0,
00110 NULL,
00111 NULL,
00112 &si,
00113 &pi))
00114 {
00115 std::cerr << "CreateProcess failed" << std::endl;
00116 return -1;
00117 }
00118
00119 CloseHandle(hmakeout_wr);
00120 CloseHandle(hmakeerr_wr);
00121 CloseHandle(hmakein_rd);
00122
00123
00124
00125
00126 #ifdef USE_WCHAR
00127 io::stream< io::code_converter<io::file_descriptor_sink> > makein(fdmakein[1]);
00128 #else
00129 io::stream<io::file_descriptor_sink> makein(hmakein_wr);
00130 #endif
00131 io::stream<io::file_descriptor_source> makeout(hmakeout_rd);
00132 io::stream<io::file_descriptor_source> makeerr(hmakeerr_rd);
00133
00134
00135 Makefile::getSingleton().writeMakefile(makein);
00136 Makefile::getSingleton().clean();
00137 makein.close();
00138
00139 makeout.get(makeoutbuf, '\0');
00140 makeerr.get(makeerrbuf, '\0');
00141
00142 WaitForSingleObject(pi.hProcess, INFINITE);
00143 DWORD status;
00144 GetExitCodeProcess(pi.hProcess, &status);
00145 return status;
00146 }
00147
00148 bool Test::run()
00149 {
00150 int status = exec_make(makeoutbuf, makeerrbuf);
00151
00152 std::cout << std::setw(30) << std::left << name;
00153 if(eval())
00154 {
00155 std::cout << "PASS" << std::endl;
00156 return true;
00157 }
00158 std::cout << "FAIL" << std::endl;
00159 std::cout << "ret: " << status << std::endl;
00160 std::cout << "out: " << makeoutbuf.str() << std::endl;
00161 std::cout << "cmp: " << goodout << std::endl;
00162 std::cout << "err: " << makeerrbuf.str() << std::endl;
00163 return false;
00164 }
00165 }}