regression.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 {
  27. using ::exit; using ::system; using ::strftime; using ::gmtime;
  28. using ::time; using ::time_t;
  29. }
  30. # endif
  31. std::string get_host()
  32. {
  33. #if defined __linux__
  34. return "linux";
  35. #elif defined _WIN32
  36. return "win32";
  37. #elif defined __BEOS__
  38. return "beos";
  39. #else
  40. # error Please adapt for your platform
  41. #endif
  42. }
  43. // retrieve precise system configuration as descriptive string
  44. #ifdef __unix__
  45. #include <sys/utsname.h>
  46. #define STRINGIFY(x) #x
  47. std::string get_system_configuration()
  48. {
  49. utsname ut;
  50. if(uname(&ut) < 0)
  51. return "";
  52. std::string config = std::string(ut.sysname) + " " + ut.release;
  53. return config;
  54. }
  55. #elif defined _WIN32
  56. std::string get_system_configuration()
  57. {
  58. return "Microsoft Windows 32bit";
  59. }
  60. #elif defined __BEOS__
  61. std::string get_system_configuration()
  62. {
  63. return "BeOS";
  64. }
  65. #else
  66. # error Please adapt for your platform
  67. #endif
  68. struct entry
  69. {
  70. std::string os, identifier, name, compile_only_command, compile_link_command, html;
  71. };
  72. // replace the first %name in s with value
  73. void replace(std::string & s,
  74. const std::string & name, const std::string & value)
  75. {
  76. std::string::size_type p = s.find(name);
  77. if(p != std::string::npos)
  78. s.replace(p, name.length(), value);
  79. }
  80. // replace all $XXX in s with the value of getenv("XXX")
  81. void replace_environment(std::string & s)
  82. {
  83. std::string::size_type end = 0;
  84. for(;;) {
  85. std::string::size_type pos = s.find('$', end);
  86. if(pos == std::string::npos)
  87. break;
  88. end = s.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", pos+1);
  89. const char * env = getenv(s.substr(pos+1, end-pos-1).c_str());
  90. if(env)
  91. replace(s, s.substr(pos, end-pos), env);
  92. else
  93. break;
  94. }
  95. }
  96. // get a string line, ignoring empty lines and comment lines
  97. void getstringline( std::ifstream & is, std::string & s )
  98. {
  99. do { std::getline( is, s ); }
  100. while ( is.good()
  101. && (!s.size() || (s.size() >= 2 && s[0] == '/' && s[1] == '/')) );
  102. }
  103. // read the compiler configuration from file and push entry's to out
  104. template<class OutputIterator>
  105. void read_compiler_configuration(const std::string & file, OutputIterator out)
  106. {
  107. std::ifstream f(file.c_str());
  108. int lineno = 0;
  109. while(f.good()) {
  110. entry e;
  111. getstringline(f, e.os);
  112. getstringline(f, e.identifier);
  113. getstringline(f, e.name);
  114. getstringline(f, e.compile_only_command);
  115. getstringline(f, e.compile_link_command);
  116. getstringline(f, e.html);
  117. *out = e;
  118. ++out;
  119. std::string l;
  120. std::getline(f, l);
  121. lineno += 6;
  122. if(l != "") {
  123. std::cerr << file << ", line " << lineno
  124. << ": Empty line expected, got " << l << "\n";
  125. std::exit(1);
  126. }
  127. }
  128. }
  129. // run command (possibly needs portability adjustment)
  130. bool execute(const std::string & command)
  131. {
  132. std::cout << command << std::endl; // fix: endl ensures cout ordering
  133. return std::system(command.c_str()) == 0;
  134. }
  135. enum test_result {
  136. ok = 0,
  137. unknown_type,
  138. compile_failed, compile_ok, link_failed, link_ok, run_failed, run_ok
  139. };
  140. test_result compile(std::string command, const std::string & boostpath,
  141. const std::string & file)
  142. {
  143. replace(command, "%source", boostpath + "/" + file);
  144. return execute(command) ? compile_ok : compile_failed;
  145. }
  146. test_result link(std::string command, const std::string & boostpath,
  147. const std::string & file)
  148. {
  149. replace(command, "%source", boostpath + "/" + file);
  150. return execute(command) ? link_ok : link_failed;
  151. }
  152. test_result run(std::string command, const std::string & boostpath,
  153. const std::string & file)
  154. {
  155. std::string exename = "boosttmp.exe";
  156. replace(command, "%source", boostpath + "/" + file);
  157. if(execute(command)) {
  158. return execute( (get_host() == "win32" ? "" : "./") + exename ) ?
  159. run_ok : run_failed;
  160. } else {
  161. return link_failed;
  162. }
  163. }
  164. std::pair<test_result, test_result>
  165. run_test(const std::string & type, std::string compile_only_command,
  166. std::string compile_link_command,
  167. const std::string & boostpath, const std::string & source)
  168. {
  169. replace(compile_only_command, "%include", boostpath);
  170. replace(compile_link_command, "%include", boostpath);
  171. if(type == "compile")
  172. return std::make_pair(compile(compile_only_command, boostpath, source), compile_ok);
  173. else if(type == "compile-fail")
  174. return std::make_pair(compile(compile_only_command, boostpath, source), compile_failed);
  175. else if(type == "link")
  176. return std::make_pair(link(compile_link_command, boostpath, source), link_ok);
  177. else if(type == "link-fail")
  178. return std::make_pair(link(compile_link_command, boostpath, source), link_failed);
  179. else if(type == "run")
  180. return std::make_pair(run(compile_link_command, boostpath, source), run_ok);
  181. else if(type == "run-fail")
  182. return std::make_pair(run(compile_link_command, boostpath, source), run_failed);
  183. else
  184. return std::make_pair(unknown_type, ok);
  185. }
  186. template<class ForwardIterator>
  187. void do_tests(std::ostream & out,
  188. ForwardIterator firstcompiler, ForwardIterator lastcompiler,
  189. const std::string & testconfig, const std::string & boostpath)
  190. {
  191. out << "<tr>\n"
  192. << "<td>Program</td>\n"
  193. << "<td>Test<br>Type</td>\n";
  194. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it) {
  195. out << "<td>" << it->html << "</td>\n";
  196. }
  197. out << "</tr>\n";
  198. std::ifstream f(testconfig.c_str());
  199. while(f.good()) {
  200. std::string l;
  201. getstringline(f, l);
  202. if (!f.good()) break;
  203. typedef std::string::size_type sz_type;
  204. sz_type p = l.find(' ');
  205. if(p == std::string::npos) {
  206. std::cerr << "Test " << l << " is wrong\n";
  207. continue;
  208. }
  209. std::string type(l, 0, p);
  210. std::string file(l, p+1, std::string::npos); // 3rd arg to fix VC++ bug
  211. std::cout << "*** " << file << " ***\n\n";
  212. out << "<tr>\n"
  213. << "<td><a href=\"../" << file << "\">" << file << "</a></td>\n"
  214. << "<td>" << type << "</td>\n";
  215. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it) {
  216. std::cout << "** " << it->name << "\n";
  217. std::pair<test_result, test_result> result =
  218. run_test(type, it->compile_only_command, it->compile_link_command, boostpath, file);
  219. if(result.first == unknown_type) {
  220. std::cerr << "Unknown test type " << type << ", skipped\n";
  221. continue;
  222. }
  223. out << "<td>"
  224. << (result.first == result.second ? "Pass" : "<font color=\"#FF0000\">Fail</font>")
  225. << "</td>" << std::endl;
  226. std::cout << (result.first == result.second ? "Pass" : "Fail") << "\n\n";
  227. }
  228. out << "</tr>\n";
  229. }
  230. }
  231. int main(int argc, char * argv[])
  232. {
  233. // std::vector<std::string> args(argv+1, argv+argc);
  234. // hack around VC++ lack of ctor taking iterator args
  235. std::vector<std::string> args;
  236. for ( const char ** ait = (const char **)(argv+1);
  237. ait != (const char **)(argv+argc); ++ait)
  238. args.push_back(std::string( *ait ));
  239. if(args.size() < 3) {
  240. std::cerr << argv[0]
  241. << " usage: compiler-config test-config boost-path [compiler|* [[command] file]]\n";
  242. std::exit(1);
  243. }
  244. std::string compiler = (args.size() >= 4 ? args[3] : "*");
  245. std::list<entry> l;
  246. read_compiler_configuration(args[0], std::back_inserter(l));
  247. std::string host = get_host();
  248. for(std::list<entry>::iterator it = l.begin(); it != l.end(); ) {
  249. if(it->os == host && (compiler == "*" || it->identifier == compiler)) {
  250. replace_environment(it->compile_only_command);
  251. replace_environment(it->compile_link_command);
  252. ++it;
  253. } else {
  254. it = l.erase(it);
  255. }
  256. }
  257. std::string boostpath = args[2];
  258. // if file argument present, write temporary test configuration file for do_tests
  259. if(args.size() >= 5) { // file argument present
  260. std::ofstream tmp((args[1]="boosttmp.tmp").c_str());
  261. if (args.size() >= 6) tmp << args[4] << " " << args[5] << std::endl; // command present
  262. else tmp << "compile " << args[4] << std::endl; // command not present
  263. }
  264. std::ofstream out( ("cs-" + host + ".html").c_str() );
  265. char run_date[100];
  266. std::time_t ct;
  267. std::time(&ct);
  268. std::strftime(run_date, sizeof(run_date), "%d %b %Y %H:%M GMT", std::gmtime(&ct));
  269. out << "<html>\n<head>\n<title>\nCompiler Status: " + host + "\n</title>\n</head>\n"
  270. << "<body bgcolor=\"#ffffff\" text=\"#000000\">\n"
  271. << "<h1><img border border=\"0\" src=\"../c++boost.gif\" width=\"277\" height=\"86\"></h1>\n"
  272. << "<h1>Compiler Status: " + host + "</h1>\n"
  273. << "\n"
  274. << "<p><b>Run Date:</b> " << run_date << "</p>\n"
  275. << "<p><b>System Configuration:</b> " << get_system_configuration()
  276. << "</p>\n"
  277. << "<p>\n"
  278. << "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n";
  279. do_tests(out, l.begin(), l.end(), args[1], boostpath);
  280. out << "</table></p>\n<p>\n";
  281. if(host == "linux")
  282. out << "Notes: A hand-crafted &lt;limits&gt; Standard header has been\n"
  283. << "applied to all configurations.\n"
  284. << "The tests were run on a GNU libc 2.2 system which has improved\n"
  285. << "wide character support compared to previous versions.";
  286. else if(host == "irix" || host == "tru64")
  287. out << "Note: For the 'clib' configuration, the missing new-style C\n"
  288. << "library headers &lt;cXXX&gt; have been supplied.\n";
  289. out << "</p>\n</body>\n</html>" << std::endl;
  290. return 0;
  291. }
粤ICP备19079148号