regression.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* boost regression test program
  2. *
  3. * Copyright Jens Maurer 2000
  4. * Permission to use, copy, modify, sell, and distribute this software
  5. * is hereby granted without free provided that the above copyright notice
  6. * appears in all copies and that both that copyright notice and this
  7. * permission notice appear in supporting documentation,
  8. *
  9. * Jens Maurer makes no representations about the suitability of this
  10. * software for any purpose. It is provided "as is" without express or
  11. * implied warranty.
  12. *
  13. * See http://www.boost.org for most recent version including documentation.
  14. */
  15. #include <iostream>
  16. #include <string>
  17. #include <list>
  18. #include <vector>
  19. #include <cstdlib>
  20. #include <fstream>
  21. #include <utility>
  22. #include <ctime>
  23. // It is OK to use boost headers which contain entirely inline code.
  24. #include <boost/config.hpp>
  25. # ifdef BOOST_NO_STDC_NAMESPACE
  26. namespace std { using ::exit; using ::system; using ::strftime; using ::gmtime; using ::time; }
  27. # endif
  28. std::string get_host()
  29. {
  30. #if defined __linux__
  31. return "linux";
  32. #elif defined _MSC_VER
  33. return "win32";
  34. #else
  35. #error Please adapt for your platform
  36. #endif
  37. }
  38. struct entry
  39. {
  40. std::string os, identifier, name, compile_only_command, compile_link_command, html;
  41. };
  42. void replace(std::string & s,
  43. const std::string & name, const std::string & value)
  44. {
  45. std::string::size_type p = s.find(name);
  46. if(p != std::string::npos)
  47. s.replace(p, name.length(), value);
  48. }
  49. void replace_environment(std::string & s)
  50. {
  51. std::string::size_type end = 0;
  52. for(;;) {
  53. std::string::size_type pos = s.find('$', end);
  54. if(pos == std::string::npos)
  55. break;
  56. end = s.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", pos+1);
  57. const char * env = getenv(s.substr(pos+1, end-pos-1).c_str());
  58. if(env)
  59. replace(s, s.substr(pos, end-pos), env);
  60. else
  61. break;
  62. }
  63. }
  64. // get a string line, ignoring empty lines and comment lines
  65. void getstringline( std::ifstream & is, std::string & s )
  66. {
  67. do { std::getline( is, s ); }
  68. while ( is.good()
  69. && (!s.size() || (s.size() >= 2 && s[0] == '/' && s[1] == '/')) );
  70. }
  71. template<class OutputIterator>
  72. void read_compiler_configuration(const std::string & file, OutputIterator out)
  73. {
  74. std::ifstream f(file.c_str());
  75. int lineno = 0;
  76. while(f.good()) {
  77. entry e;
  78. getstringline(f, e.os);
  79. getstringline(f, e.identifier);
  80. getstringline(f, e.name);
  81. getstringline(f, e.compile_only_command);
  82. getstringline(f, e.compile_link_command);
  83. getstringline(f, e.html);
  84. *out = e;
  85. ++out;
  86. std::string l;
  87. std::getline(f, l);
  88. lineno += 6;
  89. if(l != "") {
  90. std::cerr << file << ", line " << lineno
  91. << ": Empty line expected, got " << l << "\n";
  92. std::exit(1);
  93. }
  94. }
  95. }
  96. bool execute(const std::string & command)
  97. {
  98. std::cout << command << std::endl; // fix: endl ensures cout ordering
  99. return std::system(command.c_str()) == 0;
  100. }
  101. enum test_result {
  102. ok = 0,
  103. unknown_type,
  104. compile_failed, compile_ok, link_failed, link_ok, run_failed, run_ok
  105. };
  106. test_result compile(std::string command, const std::string & boostpath,
  107. const std::string & file)
  108. {
  109. replace(command, "%source", boostpath + "/" + file);
  110. return execute(command) ? compile_ok : compile_failed;
  111. }
  112. test_result link(std::string command, const std::string & boostpath,
  113. const std::string & file)
  114. {
  115. replace(command, "%source", boostpath + "/" + file);
  116. return execute(command) ? link_ok : link_failed;
  117. }
  118. test_result run(std::string command, const std::string & boostpath,
  119. const std::string & file)
  120. {
  121. std::string exename = "boosttmp.exe";
  122. replace(command, "%source", boostpath + "/" + file);
  123. if(execute(command)) {
  124. return execute( (get_host() == "win32" ? "" : "./") + exename ) ?
  125. run_ok : run_failed;
  126. } else {
  127. return link_failed;
  128. }
  129. }
  130. std::pair<test_result, test_result>
  131. run_test(const std::string & type, std::string compile_only_command,
  132. std::string compile_link_command,
  133. const std::string & boostpath, const std::string & source)
  134. {
  135. replace(compile_only_command, "%include", boostpath);
  136. replace(compile_link_command, "%include", boostpath);
  137. if(type == "compile")
  138. return std::make_pair(compile(compile_only_command, boostpath, source), compile_ok);
  139. else if(type == "compile-fail")
  140. return std::make_pair(compile(compile_only_command, boostpath, source), compile_failed);
  141. else if(type == "link")
  142. return std::make_pair(link(compile_link_command, boostpath, source), link_ok);
  143. else if(type == "link-fail")
  144. return std::make_pair(link(compile_link_command, boostpath, source), link_failed);
  145. else if(type == "run")
  146. return std::make_pair(run(compile_link_command, boostpath, source), run_ok);
  147. else if(type == "run-fail")
  148. return std::make_pair(run(compile_link_command, boostpath, source), run_failed);
  149. else
  150. return std::make_pair(unknown_type, ok);
  151. }
  152. template<class ForwardIterator>
  153. void do_tests(std::ostream & out,
  154. ForwardIterator firstcompiler, ForwardIterator lastcompiler,
  155. const std::string & testconfig, const std::string & boostpath)
  156. {
  157. out << "<tr>\n"
  158. << "<td>Program</td>\n"
  159. << "<td>Test<br>Type</td>\n";
  160. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it) {
  161. out << "<td>" << it->html << "</td>\n";
  162. }
  163. out << "</tr>\n";
  164. std::ifstream f(testconfig.c_str());
  165. while(f.good()) {
  166. std::string l;
  167. getstringline(f, l);
  168. if (!f.good()) break;
  169. typedef std::string::size_type sz_type;
  170. sz_type p = l.find(' ');
  171. if(p == std::string::npos) {
  172. std::cerr << "Test " << l << " is wrong\n";
  173. continue;
  174. }
  175. std::string type(l, 0, p);
  176. std::string file(l, p+1, std::string::npos); // 3rd arg to fix VC++ bug
  177. std::cout << "*** " << file << " ***\n\n";
  178. out << "<tr>\n"
  179. << "<td><a href=\"" << file << "\">" << file << "</a></td>\n"
  180. << "<td>" << type << "</td>\n";
  181. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it) {
  182. std::cout << "** " << it->name << "\n";
  183. std::pair<test_result, test_result> result =
  184. run_test(type, it->compile_only_command, it->compile_link_command, boostpath, file);
  185. if(result.first == unknown_type) {
  186. std::cerr << "Unknown test type " << type << ", skipped\n";
  187. continue;
  188. }
  189. out << "<td>"
  190. << (result.first == result.second ? "Pass" : "<font color=\"#FF0000\">Fail</font>")
  191. << "</td>" << std::endl;
  192. std::cout << (result.first == result.second ? "Pass" : "Fail") << "\n\n";
  193. }
  194. out << "</tr>\n";
  195. }
  196. }
  197. int main(int argc, char * argv[])
  198. {
  199. // std::vector<std::string> args(argv+1, argv+argc);
  200. // hack around VC++ lack of ctor taking iterator args
  201. std::vector<std::string> args;
  202. for ( const char ** ait = (const char **)(argv+1);
  203. ait != (const char **)(argv+argc); ++ait)
  204. args.push_back(std::string( *ait ));
  205. if(args.size() < 3) {
  206. std::cerr << argv[0]
  207. << " usage: compiler-config test-config boost-path [compiler|* [[command] file]]\n";
  208. std::exit(1);
  209. }
  210. std::string compiler = (args.size() >= 4 ? args[3] : "*");
  211. std::list<entry> l;
  212. read_compiler_configuration(args[0], std::back_inserter(l));
  213. std::string host = get_host();
  214. for(std::list<entry>::iterator it = l.begin(); it != l.end(); ) {
  215. if(it->os == host && (compiler == "*" || it->identifier == compiler)) {
  216. replace_environment(it->compile_only_command);
  217. replace_environment(it->compile_link_command);
  218. ++it;
  219. } else {
  220. it = l.erase(it);
  221. }
  222. }
  223. std::string boostpath = args[2];
  224. // if file argument present, write temporary test configuration file for do_tests
  225. if(args.size() >= 5) { // file argument present
  226. std::ofstream tmp((args[1]="boosttmp.tmp").c_str());
  227. if (args.size() >= 6) tmp << args[4] << " " << args[5] << std::endl; // command present
  228. else tmp << "compile " << args[4] << std::endl; // command not present
  229. }
  230. std::ofstream out( ("cs-" + host + ".html").c_str() );
  231. char run_date[100];
  232. time_t ct;
  233. std::time(&ct);
  234. std::strftime(run_date, sizeof(run_date), "%d %b %Y %H:%M GMT", std::gmtime(&ct));
  235. out << "<html>\n<head>\n<title>\nCompiler Status: " + host + "\n</title>\n</head>\n"
  236. << "<body bgcolor=\"#ffffff\" text=\"#000000\">\n"
  237. << "<h1><img border border=\"0\" src=\"../c++boost.gif\" width=\"277\" height=\"86\"></h1>\n"
  238. << "<h1>Compiler Status: " + host + "</h1>\n"
  239. << "</p>\n"
  240. << "<p><b>Run Date:</b> " << run_date << "</p>\n"
  241. << "<p>\n"
  242. << "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n";
  243. do_tests(out, l.begin(), l.end(), args[1], boostpath);
  244. out << "</table>\n";
  245. return 0;
  246. }
粤ICP备19079148号