regression.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 fee 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. * 2001-01-23 made it compile with Borland (David Abrahams)
  16. * 2001-01-22 added --diff option (Jens Maurer)
  17. */
  18. #include <iostream>
  19. #include <string>
  20. #include <list>
  21. #include <map>
  22. #include <cstdlib>
  23. #include <fstream>
  24. #include <utility>
  25. #include <ctime>
  26. #ifdef __unix
  27. #include <cstring>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <sys/types.h>
  31. #include <sys/time.h>
  32. #include <sys/wait.h>
  33. #include <sys/resource.h>
  34. #endif
  35. // It is OK to use boost headers which contain entirely inline code.
  36. #include <boost/config.hpp>
  37. #ifdef BOOST_NO_STDC_NAMESPACE
  38. namespace std {
  39. using ::exit; using ::system; using ::strftime; using ::gmtime;
  40. using ::time; using ::time_t; using ::getenv;
  41. }
  42. #endif
  43. std::string get_host()
  44. {
  45. #if defined __linux__
  46. return "linux";
  47. #elif defined __osf__
  48. return "tru64";
  49. #elif defined __sgi
  50. return "irix";
  51. #elif defined __sun
  52. return "solaris";
  53. #elif defined _WIN32
  54. return "win32";
  55. #elif defined __BEOS__
  56. return "beos";
  57. #elif defined __hpux
  58. return "hpux";
  59. #elif defined __IBMCPP__
  60. return "aix";
  61. #elif defined __MSL__ && __dest_os == __mac_os
  62. return "macos";
  63. #elif defined __MSL__ && __dest_os == __mac_os_x || defined(__APPLE_CC__)
  64. return "macosx";
  65. #else
  66. # error Please adapt for your platform
  67. #endif
  68. }
  69. // retrieve precise system configuration as descriptive string
  70. #ifdef __unix
  71. #include <sys/utsname.h>
  72. std::string get_system_configuration()
  73. {
  74. struct utsname ut; // "struct" is required for the DEC Tru64 compiler
  75. if(uname(&ut) < 0)
  76. return "";
  77. std::string config = std::string(ut.sysname) + " " + ut.release;
  78. config = config + " (CPU: " + ut.machine + ")";
  79. return config;
  80. }
  81. #elif defined _WIN32
  82. std::string get_system_configuration()
  83. {
  84. return "Microsoft Windows 32bit";
  85. }
  86. #elif defined __BEOS__
  87. std::string get_system_configuration()
  88. {
  89. return "BeOS 5 Intel Edition";
  90. }
  91. #elif defined __MSL__ && (__dest_os == __mac_os || __dest_os == __mac_os_x) || defined(__APPLE_CC__)
  92. std::string get_system_configuration()
  93. {
  94. #if __dest_os == _mac_os
  95. return "Mac OS 9";
  96. #else
  97. return "Mac OS X";
  98. #endif
  99. }
  100. #else
  101. # error Please adapt for your platform
  102. #endif
  103. struct configuration
  104. {
  105. std::string compiler_config_file, test_config_file;
  106. std::string boostpath;
  107. std::string html_output;
  108. bool highlight_differences;
  109. std::string compiler, test;
  110. // defaults
  111. configuration()
  112. : compiler_config_file("compiler.cfg"), test_config_file("regression.cfg"),
  113. boostpath(".."), html_output("cs-" + get_host() + ".html"),
  114. highlight_differences(false),
  115. compiler("*"), test("") { }
  116. };
  117. configuration parse_command_line(char **first, char **last)
  118. {
  119. configuration cfg;
  120. bool output_redirected = false;
  121. for( ; first != last; ++first) {
  122. std::string arg = *first;
  123. if(arg == "--config") {
  124. cfg.compiler_config_file = *++first;
  125. } else if(arg == "--tests") {
  126. cfg.test_config_file = *++first;
  127. } else if(arg == "--boost") {
  128. cfg.boostpath = *++first;
  129. } else if(arg == "-o" || arg == "--output") {
  130. cfg.html_output = *++first;
  131. output_redirected = true;
  132. } else if(arg == "--diff") {
  133. cfg.highlight_differences = true;
  134. } else if(arg == "--compiler") {
  135. cfg.compiler = *++first;
  136. } else if(arg.substr(0,1) == "-") {
  137. std::cerr << "usage: regression [-h | --help] [--config compiler.cfg]\n"
  138. << " [--tests regression.cfg] [--boost path] [-o output.html] [--compiler <name>]\n"
  139. << " [test]\n"
  140. << " -h or --help print this help message\n"
  141. << " --config <file> compiler configuration file (default: compiler.cfg)\n"
  142. << " --tests <file> test configuration file (default: regression.cfg)\n"
  143. << " --boost <path> filesystem path to main boost directory (default: ..)\n"
  144. << " -o <file> name of output file (default: cs-OS.html)\n"
  145. << " --diff highlight differences in output file\n"
  146. << " --compiler <name> use only compiler <name> (default: *)\n"
  147. << " test a single test, including the action (compile, run, etc.)\n";
  148. std::exit(1);
  149. } else {
  150. // add the rest of the command line to the "test" line
  151. for( ; first != last; ++first)
  152. cfg.test += std::string(*first) + " ";
  153. break;
  154. }
  155. }
  156. if(cfg.test != "" && !output_redirected) {
  157. std::cerr << "Error: Please specify the HTML output file explicitly\n"
  158. << "(using \"--output file\") when running a single test only.\n";
  159. std::exit(1);
  160. }
  161. return cfg;
  162. }
  163. struct entry
  164. {
  165. std::string os, identifier, name, compile_only_command, compile_link_command, html;
  166. };
  167. // replace the first %name in s with value
  168. void replace(std::string & s,
  169. const std::string & name, const std::string & value)
  170. {
  171. std::string::size_type p = s.find(name);
  172. if(p != std::string::npos)
  173. s.replace(p, name.length(), value);
  174. }
  175. // replace all $XXX in s with the value of getenv("XXX")
  176. void replace_environment(std::string & s)
  177. {
  178. std::string::size_type end = 0;
  179. for(;;) {
  180. std::string::size_type pos = s.find('$', end);
  181. if(pos == std::string::npos)
  182. break;
  183. end = s.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", pos+1);
  184. const char * env = std::getenv(s.substr(pos+1, end-pos-1).c_str());
  185. if(env)
  186. replace(s, s.substr(pos, end-pos), env);
  187. else
  188. break;
  189. }
  190. }
  191. // get a string line, ignoring empty lines and comment lines
  192. void getstringline( std::ifstream & is, std::string & s )
  193. {
  194. do {
  195. std::getline( static_cast<std::istream&>(is), s ); // cast required by IRIX
  196. } while ( is.good()
  197. && (!s.size() || (s.size() >= 2 && s[0] == '/' && s[1] == '/')) );
  198. }
  199. // read the compiler configuration from file and push entry's to out
  200. template<class OutputIterator>
  201. void read_compiler_configuration(const std::string & file, OutputIterator out)
  202. {
  203. std::ifstream f(file.c_str());
  204. int lineno = 0;
  205. while(f.good()) {
  206. entry e;
  207. getstringline(f, e.os);
  208. getstringline(f, e.identifier);
  209. getstringline(f, e.name);
  210. getstringline(f, e.compile_only_command);
  211. getstringline(f, e.compile_link_command);
  212. getstringline(f, e.html);
  213. *out = e;
  214. ++out;
  215. std::string l;
  216. std::getline(f, l);
  217. lineno += 6;
  218. if(l != "") {
  219. std::cerr << file << ", line " << lineno
  220. << ": Empty line expected, got " << l << "\n";
  221. std::exit(1);
  222. }
  223. }
  224. }
  225. std::string pass_string = "Pass";
  226. std::string fail_string = "<font color=\"#FF0000\">Fail</font>";
  227. // map test name to results, one character ("P" or "F") for each compiler
  228. typedef std::map<std::string, std::string> previous_results_type;
  229. previous_results_type read_previous_results(std::istream & is)
  230. {
  231. previous_results_type result;
  232. // finite state machine
  233. enum { prefix, testname, command, testresult } status = prefix;
  234. std::string line, current_test;
  235. while(std::getline(is, line)) {
  236. if(status == prefix) {
  237. if(line.substr(0, 6) == "<table")
  238. status = testname;
  239. } else if(status == testname) {
  240. if(line.substr(0, 6) == "<td><a") {
  241. std::string::size_type pos = line.find(">", 5);
  242. if(pos == std::string::npos || pos+1 >= line.size()) {
  243. std::cerr << "Line '" << line << "' has unknown format.";
  244. continue;
  245. }
  246. std::string::size_type pos_end = line.find("<", pos);
  247. if(pos_end == std::string::npos) {
  248. std::cerr << "Line '" << line << "' has unknown format.";
  249. continue;
  250. }
  251. current_test = line.substr(pos+1, pos_end - (pos+1));
  252. status = command;
  253. } else if(line.substr(0, 8) == "</table>") {
  254. break;
  255. }
  256. } else if(status == command) {
  257. status = testresult;
  258. } else if(status == testresult) {
  259. if(line == "</tr>")
  260. status = testname;
  261. else if(line.find(pass_string) != std::string::npos)
  262. result[current_test].append("P");
  263. else if(line.find(fail_string) != std::string::npos)
  264. result[current_test].append("F");
  265. else
  266. std::cerr << "Line '" << line << "' has unknown format.";
  267. }
  268. }
  269. return result;
  270. }
  271. // run command (possibly needs portability adjustment)
  272. bool execute(const std::string & command)
  273. {
  274. std::cout << command << std::endl; // fix: endl ensures cout ordering
  275. #ifdef __unix
  276. int ret = 0;
  277. pid_t pid = fork();
  278. if(pid == -1) {
  279. ret = 1;
  280. std::cout << "ERROR: cannot fork: " << std::strerror(errno) << std::endl;
  281. } else if(pid == 0) {
  282. // child context
  283. execl("/bin/sh", "sh", "-c", command.c_str(), 0);
  284. std::cout << "ERROR: cannot execl: " << std::strerror(errno) << std::endl;
  285. std::exit(1);
  286. } else {
  287. int status;
  288. struct rusage usage;
  289. int result = wait3(&status, 0, &usage);
  290. if(WIFEXITED(status))
  291. ret = WEXITSTATUS(status);
  292. else if(WIFSIGNALED(status))
  293. ret = 1000+WTERMSIG(status);
  294. std::cout << "CPU time: "
  295. << usage.ru_utime.tv_sec + usage.ru_utime.tv_usec/1e6
  296. << " s user, "
  297. << usage.ru_stime.tv_sec + usage.ru_stime.tv_usec/1e6
  298. << " s system"
  299. << std::endl;
  300. }
  301. #else
  302. int ret = std::system(command.c_str());
  303. #endif
  304. if(ret != 0)
  305. std::cout << "Return code: " << ret << std::endl;
  306. return ret == 0;
  307. }
  308. enum test_result {
  309. ok = 0,
  310. unknown_type,
  311. compile_failed, compile_ok, link_failed, link_ok, run_failed, run_ok
  312. };
  313. test_result compile(std::string command, const std::string & boostpath,
  314. const std::string & file)
  315. {
  316. replace(command, "%source", boostpath + "/" + file);
  317. return execute(command) ? compile_ok : compile_failed;
  318. }
  319. test_result link(std::string command, const std::string & boostpath,
  320. const std::string & file)
  321. {
  322. replace(command, "%source", boostpath + "/" + file);
  323. return execute(command) ? link_ok : link_failed;
  324. }
  325. test_result run(std::string command, const std::string & boostpath,
  326. const std::string & file, std::string args)
  327. {
  328. std::string exename = "boosttmp.exe";
  329. replace(command, "%source", boostpath + "/" + file);
  330. if(execute(command)) {
  331. // cygwin seems to require leading ./ on some systems (JM)
  332. #if !defined(__CYGWIN__) && defined(_WIN32)
  333. if(get_host() != "win32")
  334. #endif
  335. exename = "./" + exename;
  336. replace(args, "%boost", boostpath);
  337. return execute(exename + " " + args) ? run_ok : run_failed;
  338. } else {
  339. return link_failed;
  340. }
  341. }
  342. std::pair<test_result, test_result>
  343. run_test(const std::string & type, std::string compile_only_command,
  344. std::string compile_link_command,
  345. const std::string & boostpath, const std::string & source,
  346. const std::string & args)
  347. {
  348. replace(compile_only_command, "%include", boostpath);
  349. replace(compile_link_command, "%include", boostpath);
  350. if(type == "compile")
  351. return std::make_pair(compile(compile_only_command, boostpath, source), compile_ok);
  352. else if(type == "compile-fail")
  353. return std::make_pair(compile(compile_only_command, boostpath, source), compile_failed);
  354. else if(type == "link")
  355. return std::make_pair(link(compile_link_command, boostpath, source), link_ok);
  356. else if(type == "link-fail")
  357. return std::make_pair(link(compile_link_command, boostpath, source), link_failed);
  358. else if(type == "run")
  359. return std::make_pair(run(compile_link_command, boostpath, source, args), run_ok);
  360. else if(type == "run-fail")
  361. return std::make_pair(run(compile_link_command, boostpath, source, args), run_failed);
  362. else
  363. return std::make_pair(unknown_type, ok);
  364. }
  365. template<class ForwardIterator>
  366. void do_tests(std::ostream & out,
  367. ForwardIterator firstcompiler, ForwardIterator lastcompiler,
  368. const std::string & testconfig, const std::string & boostpath,
  369. const previous_results_type& previous_results,
  370. bool highlight_diff)
  371. {
  372. out << "<tr>\n"
  373. << "<td>Program</td>\n"
  374. << "<td>Test<br>Type</td>\n";
  375. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it) {
  376. out << "<td>" << it->html << "</td>\n";
  377. }
  378. out << "</tr>\n";
  379. std::ifstream f(testconfig.c_str());
  380. while(f.good()) {
  381. std::string l;
  382. getstringline(f, l);
  383. if (!f.good()) break;
  384. typedef std::string::size_type sz_type;
  385. sz_type p = l.find(' ');
  386. if(p == std::string::npos) {
  387. std::cerr << "Test " << l << " is wrong\n";
  388. continue;
  389. }
  390. std::string type(l, 0, p);
  391. sz_type end_filename = l.find(' ', p+1);
  392. std::string file, args;
  393. if(end_filename == std::string::npos) {
  394. file = l.substr(p+1, std::string::npos);
  395. } else {
  396. file = l.substr(p+1, end_filename-(p+1));
  397. args = l.substr(end_filename+1, std::string::npos);
  398. }
  399. std::cout << "*** " << file << " ***\n\n";
  400. out << "<tr>\n"
  401. << "<td><a href=\"../" << file << "\">" << file << "</a></td>\n"
  402. << "<td>" << type << "</td>\n";
  403. previous_results_type::const_iterator prev_iter =
  404. previous_results.find(file);
  405. std::string previous = (prev_iter == previous_results.end() ?
  406. std::string("") : prev_iter->second);
  407. std::string::size_type i = 0;
  408. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it, ++i) {
  409. std::cout << "** " << it->name << "\n";
  410. std::pair<test_result, test_result> result =
  411. run_test(type, it->compile_only_command, it->compile_link_command, boostpath, file, args);
  412. if(result.first == unknown_type) {
  413. std::cerr << "Unknown test type " << type << ", skipped\n";
  414. continue;
  415. }
  416. bool pass = result.first == result.second;
  417. char prev = (i < previous.size() ? previous[i] : ' ');
  418. bool changed = highlight_diff &&
  419. ((prev == 'F' && pass) || (prev == 'P' && !pass) || prev == ' ');
  420. out << "<td>"
  421. << (changed ? "<font size=\"+3\"><em>" : "")
  422. << (pass ? pass_string : fail_string)
  423. << (changed ? "</em></font>" : "")
  424. << "</td>" << std::endl;
  425. std::cout << (result.first == result.second ? "Pass" : "Fail") << "\n\n";
  426. }
  427. out << "</tr>\n";
  428. }
  429. }
  430. int main(int argc, char * argv[])
  431. {
  432. configuration config = parse_command_line(argv+1, argv+argc);
  433. std::list<entry> compilers;
  434. read_compiler_configuration(config.compiler_config_file,
  435. std::back_inserter(compilers));
  436. std::string host = get_host();
  437. for(std::list<entry>::iterator it = compilers.begin(); it != compilers.end(); ) {
  438. if(it->os == host && (config.compiler == "*" ||
  439. it->identifier == config.compiler)) {
  440. replace_environment(it->compile_only_command);
  441. replace_environment(it->compile_link_command);
  442. ++it;
  443. } else {
  444. it = compilers.erase(it);
  445. }
  446. }
  447. if(compilers.empty())
  448. std::cerr << "You do not have any compatible compilers defined." << std::endl;
  449. // if explicit test requested, write temporary file for do_tests
  450. if(config.test != "") {
  451. std::ofstream tmp((config.test_config_file="boosttmp.tmp").c_str());
  452. tmp << config.test << std::endl;
  453. }
  454. previous_results_type previous_results;
  455. if(config.highlight_differences) {
  456. std::ifstream in(config.html_output.c_str());
  457. previous_results = read_previous_results(in);
  458. }
  459. std::ofstream out( config.html_output.c_str() );
  460. char run_date[100];
  461. std::time_t ct;
  462. std::time(&ct);
  463. std::strftime(run_date, sizeof(run_date), "%d %b %Y %H:%M GMT", std::gmtime(&ct));
  464. out << "<html>\n<head>\n<title>\nCompiler Status: " + host + "\n</title>\n</head>\n"
  465. << "<body bgcolor=\"#ffffff\" text=\"#000000\">\n"
  466. << "<table border=\"0\">\n<tr>\n"
  467. << "<td><img border=\"0\" src=\"../c++boost.gif\" width=\"277\" height=\"86\"></td>\n"
  468. << "<td>\n"
  469. << "<h1>Compiler Status: " + host + "</h1>\n"
  470. << "\n"
  471. << "<p><b>System Configuration:</b> " << get_system_configuration() << "<br>\n"
  472. << "<b>Run Date:</b> " << run_date << "</p>\n"
  473. << "</td>\n</tr>\n</table>\n"
  474. << "<p>\n\n"
  475. << "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n";
  476. do_tests(out, compilers.begin(), compilers.end(), config.test_config_file, config.boostpath,
  477. previous_results, config.highlight_differences);
  478. out << "</table></p>\n<p>\n";
  479. if(host == "linux")
  480. out << "Notes: The tests were run on a GNU libc 2.2.4 system which has improved\n"
  481. << "wide character support compared to 2.1.x and earlier versions.";
  482. else if(host == "irix" || host == "tru64")
  483. out << "Note: For the 'clib' configuration, the missing new-style C\n"
  484. << "library headers &lt;cXXX&gt; have been supplied.\n";
  485. out << "</p>\n</body>\n</html>" << std::endl;
  486. return 0;
  487. }
粤ICP备19079148号