process_jam_log.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // process jam regression test output into XML -----------------------------//
  2. // Copyright Beman Dawes 2002.
  3. // See accompanying license for terms and conditions of use.
  4. // See http://www.boost.org/tools/regression for documentation.
  5. #include "detail/tiny_xml.hpp"
  6. #include "boost/filesystem/operations.hpp"
  7. #include "boost/filesystem/fstream.hpp"
  8. #include "boost/filesystem/exception.hpp"
  9. #include <iostream>
  10. #include <string>
  11. #include <cstring>
  12. #include <map>
  13. #include <utility> // for make_pair
  14. #include <ctime>
  15. #include <cctype> // for tolower
  16. using std::string;
  17. namespace xml = boost::tiny_xml;
  18. namespace fs = boost::filesystem;
  19. #define BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE
  20. #include <boost/test/included/prg_exec_monitor.hpp>
  21. namespace
  22. {
  23. struct test_info
  24. {
  25. string file_path; // relative boost-root
  26. string type;
  27. bool always_show_run_output;
  28. };
  29. typedef std::map< string, test_info > test2info_map; // key is test-name
  30. test2info_map test2info;
  31. fs::path boost_root;
  32. fs::path locate_root; // ALL_LOCATE_TARGET (or boost_root if none)
  33. // append_html -------------------------------------------------------------//
  34. void append_html( const string & src, string & target )
  35. {
  36. // there are a few lines we want to ignore
  37. if ( src.find( "th target..." ) != string::npos
  38. || src.find( "cc1plus.exe: warning: changing search order for system directory" ) != string::npos
  39. || src.find( "cc1plus.exe: warning: as it has already been specified as a non-system directory" ) != string::npos
  40. ) return;
  41. for ( string::size_type pos = 0; pos < src.size(); ++pos )
  42. {
  43. if ( src[pos] == '<' ) target += "&lt;";
  44. else if ( src[pos] == '>' ) target += "&gt;";
  45. else if ( src[pos] == '&' ) target += "&amp;";
  46. else target += src[pos];
  47. }
  48. }
  49. // timestamp ---------------------------------------------------------------//
  50. string timestamp()
  51. {
  52. char run_date[128];
  53. std::time_t tod;
  54. std::time( &tod );
  55. std::strftime( run_date, sizeof(run_date),
  56. "%Y-%m-%d %X UTC", std::gmtime( &tod ) );
  57. return string( run_date );
  58. }
  59. // convert path separators to forward slashes ------------------------------//
  60. void convert_path_separators( string & s )
  61. {
  62. for ( string::iterator itr = s.begin(); itr != s.end(); ++itr )
  63. if ( *itr == '\\' || *itr == '!' ) *itr = '/';
  64. }
  65. // extract a target directory path from a jam target string ----------------//
  66. // s may be relative to the initial_path:
  67. // ..\..\..\libs\foo\build\bin\libfoo.lib\vc7\debug\runtime-link-dynamic\boo.obj
  68. // s may be absolute:
  69. // d:\myboost\libs\foo\build\bin\libfoo.lib\vc7\debug\runtime-link-dynamic\boo.obj
  70. // return path is always relative to the boost directory tree:
  71. // libs/foo/build/bin/libfs.lib/vc7/debug/runtime-link-dynamic
  72. string target_directory( const string & s )
  73. {
  74. string temp( s );
  75. convert_path_separators( temp );
  76. temp.erase( temp.find_last_of( "/" ) ); // remove leaf
  77. string::size_type pos = temp.find_last_of( " " ); // remove leading spaces
  78. if ( pos != string::npos ) temp.erase( 0, pos+1 );
  79. if ( temp[0] == '.' ) temp.erase( 0, temp.find_first_not_of( "./" ) );
  80. else temp.erase( 0, locate_root.string().size()+1 );
  81. //std::cout << "\"" << s << "\", \"" << temp << "\"" << std::endl;
  82. return temp;
  83. }
  84. string::size_type target_name_end( const string & s )
  85. {
  86. string::size_type pos = s.find( ".test/" );
  87. if ( pos == string::npos ) pos = s.find( ".dll/" );
  88. if ( pos == string::npos ) pos = s.find( ".lib/" );
  89. return pos;
  90. }
  91. string toolset( const string & s )
  92. {
  93. string::size_type pos = target_name_end( s );
  94. if ( pos == string::npos ) return "";
  95. pos = s.find( "/", pos ) + 1;
  96. return s.substr( pos, s.find( "/", pos ) - pos );
  97. }
  98. string test_name( const string & s )
  99. {
  100. string::size_type pos = target_name_end( s );
  101. if ( pos == string::npos ) return "";
  102. string::size_type pos_start = s.rfind( '/', pos ) + 1;
  103. return s.substr( pos_start,
  104. (s.find( ".test/" ) != string::npos
  105. ? pos : s.find( "/", pos )) - pos_start );
  106. }
  107. string test_path_to_library_name( string const& path )
  108. {
  109. std::string result;
  110. string::size_type start_pos( path.find( "libs/" ) );
  111. if ( start_pos != string::npos )
  112. {
  113. start_pos += 5;
  114. string::size_type end_pos( path.find( '/', start_pos ) );
  115. result = path.substr( start_pos, end_pos - start_pos );
  116. // if a "sublibs" file exists, the library name includes the
  117. // next level down directory name.
  118. if ( fs::exists( ( boost_root / "libs" ) / result / "sublibs" ) )
  119. {
  120. result += path.substr( end_pos, path.find( '/', end_pos+1 ) - end_pos );
  121. }
  122. }
  123. return result;
  124. }
  125. // the format of paths is really kinky, so convert to normal form
  126. // first path is missing the leading "..\".
  127. // first path is missing "\bin" after "status".
  128. // second path is missing the leading "..\".
  129. // second path is missing "\bin" after "build".
  130. // second path uses "!" for some separators.
  131. void parse_skipped_msg( const string & msg,
  132. string & first_dir, string & second_dir )
  133. {
  134. first_dir.clear();
  135. second_dir.clear();
  136. string::size_type start_pos( msg.find( '<' ) );
  137. if ( start_pos == string::npos ) return;
  138. ++start_pos;
  139. string::size_type end_pos( msg.find( '>', start_pos ) );
  140. first_dir += msg.substr( start_pos, end_pos - start_pos );
  141. if ( first_dir[0] == '@' )
  142. {
  143. // new style build path, rooted build tree
  144. convert_path_separators( first_dir );
  145. first_dir.replace( 0, 1, "bin/" );
  146. }
  147. else
  148. {
  149. // old style build path, integrated build tree
  150. start_pos = first_dir.rfind( '!' );
  151. convert_path_separators( first_dir );
  152. first_dir.insert( first_dir.find( '/', start_pos + 1), "/bin" );
  153. }
  154. //std::cout << first_dir << std::endl;
  155. start_pos = msg.find( '<', end_pos );
  156. if ( start_pos == string::npos ) return;
  157. ++start_pos;
  158. end_pos = msg.find( '>', start_pos );
  159. second_dir += msg.substr( start_pos, end_pos - start_pos );
  160. if ( second_dir[0] == '@' )
  161. {
  162. // new style build path, rooted build tree
  163. convert_path_separators( second_dir );
  164. second_dir.replace( 0, 1, "bin/" );
  165. }
  166. else
  167. {
  168. // old style build path, integrated build tree
  169. start_pos = second_dir.rfind( '!' );
  170. convert_path_separators( second_dir );
  171. second_dir.insert( second_dir.find( '/', start_pos + 1), "/bin" );
  172. }
  173. //std::cout << second_dir << std::endl;
  174. }
  175. // test_log hides database details -----------------------------------------//
  176. class test_log
  177. : boost::noncopyable
  178. {
  179. const string & m_target_directory;
  180. xml::element_ptr m_root;
  181. public:
  182. test_log( const string & target_directory,
  183. const string & test_name,
  184. const string & toolset,
  185. bool force_new_file )
  186. : m_target_directory( target_directory )
  187. {
  188. if ( !force_new_file )
  189. {
  190. fs::path pth( locate_root / target_directory / "test_log.xml" );
  191. fs::ifstream file( pth );
  192. if ( file ) // existing file
  193. {
  194. try
  195. {
  196. m_root = xml::parse( file, pth.string() );
  197. return;
  198. }
  199. catch(...)
  200. {
  201. // unable to parse existing XML file, fall through
  202. }
  203. }
  204. }
  205. string library_name( test_path_to_library_name( target_directory ) );
  206. test_info info;
  207. test2info_map::iterator itr( test2info.find( library_name + "/" + test_name ) );
  208. if ( itr != test2info.end() )
  209. info = itr->second;
  210. if ( !info.file_path.empty() )
  211. library_name = test_path_to_library_name( info.file_path );
  212. if ( info.type.empty() )
  213. {
  214. if ( target_directory.find( ".lib/" ) != string::npos
  215. || target_directory.find( ".dll/" ) != string::npos )
  216. {
  217. info.type = "lib";
  218. }
  219. else if ( target_directory.find( ".pyd/" ) != string::npos )
  220. info.type = "pyd";
  221. }
  222. m_root.reset( new xml::element( "test-log" ) );
  223. m_root->attributes.push_back(
  224. xml::attribute( "library", library_name ) );
  225. m_root->attributes.push_back(
  226. xml::attribute( "test-name", test_name ) );
  227. m_root->attributes.push_back(
  228. xml::attribute( "test-type", info.type ) );
  229. m_root->attributes.push_back(
  230. xml::attribute( "test-program", info.file_path ) );
  231. m_root->attributes.push_back(
  232. xml::attribute( "target-directory", target_directory ) );
  233. m_root->attributes.push_back(
  234. xml::attribute( "toolset", toolset ) );
  235. m_root->attributes.push_back(
  236. xml::attribute( "show-run-output",
  237. info.always_show_run_output ? "true" : "false" ) );
  238. }
  239. ~test_log()
  240. {
  241. fs::path pth( locate_root / m_target_directory / "test_log.xml" );
  242. fs::ofstream file( pth );
  243. if ( !file )
  244. {
  245. std::cout << "*****Warning - can't open output file: "
  246. << pth.string() << "\n";
  247. }
  248. else xml::write( *m_root, file );
  249. }
  250. const string & target_directory() const { return m_target_directory; }
  251. void remove_action( const string & action_name )
  252. // no effect if action_name not found
  253. {
  254. xml::element_list::iterator itr;
  255. for ( itr = m_root->elements.begin();
  256. itr != m_root->elements.end() && (*itr)->name != action_name;
  257. ++itr ) {}
  258. if ( itr != m_root->elements.end() ) m_root->elements.erase( itr );
  259. }
  260. void add_action( const string & action_name,
  261. const string & result,
  262. const string & timestamp,
  263. const string & content )
  264. {
  265. remove_action( action_name );
  266. xml::element_ptr action( new xml::element(action_name) );
  267. m_root->elements.push_back( action );
  268. action->attributes.push_back( xml::attribute( "result", result ) );
  269. action->attributes.push_back( xml::attribute( "timestamp", timestamp ) );
  270. action->content = content;
  271. }
  272. };
  273. // message_manager maps input messages into test_log actions ---------------//
  274. class message_manager
  275. : boost::noncopyable
  276. {
  277. string m_action_name; // !empty() implies action pending
  278. // IOW, a start_message awaits stop_message
  279. string m_target_directory;
  280. string m_test_name;
  281. string m_toolset;
  282. bool m_note; // if true, run result set to "note"
  283. // set false by start_message()
  284. // data needed to stop further compile action after a compile failure
  285. // detected in the same target directory
  286. string m_previous_target_directory;
  287. bool m_compile_failed;
  288. public:
  289. message_manager() : m_note(false) {}
  290. ~message_manager() { /*assert( m_action_name.empty() );*/ }
  291. bool note() const { return m_note; }
  292. void note( bool value ) { m_note = value; }
  293. void start_message( const string & action_name,
  294. const string & target_directory,
  295. const string & test_name,
  296. const string & toolset,
  297. const string & prior_content )
  298. {
  299. if ( !m_action_name.empty() ) stop_message( prior_content );
  300. m_action_name = action_name;
  301. m_target_directory = target_directory;
  302. m_test_name = test_name;
  303. m_toolset = toolset;
  304. m_note = false;
  305. if ( m_previous_target_directory != target_directory )
  306. {
  307. m_previous_target_directory = target_directory;
  308. m_compile_failed = false;
  309. }
  310. }
  311. void stop_message( const string & content )
  312. {
  313. if ( m_action_name.empty() ) return;
  314. stop_message( m_action_name, m_target_directory,
  315. "succeed", timestamp(), content );
  316. }
  317. void stop_message( const string & action_name,
  318. const string & target_directory,
  319. const string & result,
  320. const string & timestamp,
  321. const string & content )
  322. // the only valid action_names are "compile", "link", "run", "lib"
  323. {
  324. // My understanding of the jam output is that there should never be
  325. // a stop_message that was not preceeded by a matching start_message.
  326. // That understanding is built into message_manager code.
  327. assert( m_action_name == action_name );
  328. assert( m_target_directory == target_directory );
  329. assert( result == "succeed" || result == "fail" );
  330. // if test_log.xml entry needed
  331. if ( !m_compile_failed
  332. || action_name != "compile"
  333. || m_previous_target_directory != target_directory )
  334. {
  335. if ( action_name == "compile"
  336. && result == "fail" ) m_compile_failed = true;
  337. test_log tl( target_directory,
  338. m_test_name, m_toolset, action_name == "compile" );
  339. tl.remove_action( "lib" ); // always clear out lib residue
  340. // dependency removal
  341. if ( action_name == "lib" )
  342. {
  343. tl.remove_action( "compile" );
  344. tl.remove_action( "link" );
  345. tl.remove_action( "run" );
  346. }
  347. else if ( action_name == "compile" )
  348. {
  349. tl.remove_action( "link" );
  350. tl.remove_action( "run" );
  351. if ( result == "fail" ) m_compile_failed = true;
  352. }
  353. else if ( action_name == "link" ) { tl.remove_action( "run" ); }
  354. // dependency removal won't work right with random names, so assert
  355. else { assert( action_name == "run" ); }
  356. // add the "run" stop_message action
  357. tl.add_action( action_name,
  358. result == "succeed" && note() ? "note" : result,
  359. timestamp, content );
  360. }
  361. m_action_name = ""; // signal no pending action
  362. m_previous_target_directory = target_directory;
  363. }
  364. };
  365. }
  366. // main --------------------------------------------------------------------//
  367. int cpp_main( int argc, char ** argv )
  368. {
  369. if ( argc <= 1 )
  370. std::cout << "Usage: bjam [bjam-args] | process_jam_log [--echo] [locate-root]\n"
  371. " locate-root is the same as the bjam ALL_LOCATE_TARGET\n"
  372. " parameter, if any. Default is boost-root.\n";
  373. boost_root = fs::initial_path();
  374. while ( !boost_root.empty()
  375. && !fs::exists( boost_root / "libs" ) )
  376. {
  377. boost_root /= "..";
  378. }
  379. if ( boost_root.empty() )
  380. {
  381. std::cout << "must be run from within the boost-root directory tree\n";
  382. return 1;
  383. }
  384. bool echo = false;
  385. if ( argc > 1 && std::strcmp( argv[1], "--echo" ) == 0 )
  386. {
  387. echo = true;
  388. --argc; ++argv;
  389. }
  390. if (argc > 1)
  391. {
  392. locate_root = fs::path( argv[1], fs::native );
  393. --argc; ++argv;
  394. }
  395. else
  396. {
  397. locate_root = boost_root;
  398. }
  399. std::cout << "boost_root: " << boost_root.string() << '\n'
  400. << "locate_root: " << locate_root.string() << '\n';
  401. message_manager mgr;
  402. string line;
  403. string content;
  404. bool capture_lines = false;
  405. std::istream* input;
  406. if (argc > 1)
  407. {
  408. input = new std::ifstream(argv[1]);
  409. }
  410. else
  411. {
  412. input = &std::cin;
  413. }
  414. // This loop looks at lines for certain signatures, and accordingly:
  415. // * Calls start_message() to start capturing lines. (start_message() will
  416. // automatically call stop_message() if needed.)
  417. // * Calls stop_message() to stop capturing lines.
  418. // * Capture lines if line capture on.
  419. while ( std::getline( *input, line ) )
  420. {
  421. if ( echo ) std::cout << line << "\n";
  422. // create map of test-name to test-info
  423. if ( line.find( "boost-test(" ) == 0 )
  424. {
  425. string::size_type pos = line.find( '"' );
  426. string test_name( line.substr( pos+1, line.find( '"', pos+1)-pos-1 ) );
  427. test_info info;
  428. info.always_show_run_output
  429. = line.find( "\"always_show_run_output\"" ) != string::npos;
  430. info.type = line.substr( 11, line.find( ')' )-11 );
  431. for (unsigned int i = 0; i!=info.type.size(); ++i )
  432. { info.type[i] = std::tolower( info.type[i] ); }
  433. pos = line.find( ':' );
  434. // the rest of line is missing if bjam didn't know how to make target
  435. if ( pos + 1 != line.size() )
  436. {
  437. info.file_path = line.substr( pos+3,
  438. line.find( "\"", pos+3 )-pos-3 );
  439. convert_path_separators( info.file_path );
  440. if ( info.file_path.find( "libs/libs/" ) == 0 ) info.file_path.erase( 0, 5 );
  441. if ( test_name.find( "/" ) == string::npos )
  442. test_name = "/" + test_name;
  443. test2info.insert( std::make_pair( test_name, info ) );
  444. // std::cout << test_name << ", " << info.type << ", " << info.file_path << "\n";
  445. }
  446. else
  447. {
  448. std::cout << "*****Warning - missing test path: " << line << "\n"
  449. << " (Usually occurs when bjam doesn't know how to make a target)\n";
  450. }
  451. continue;
  452. }
  453. // these actions represent both the start of a new action
  454. // and the end of a failed action
  455. else if ( line.find( "C++-action " ) != string::npos
  456. || line.find( "vc-C++ " ) != string::npos
  457. || line.find( "C-action " ) != string::npos
  458. || line.find( "Cc-action " ) != string::npos
  459. || line.find( "vc-Cc " ) != string::npos
  460. || line.find( "Link-action " ) != string::npos
  461. || line.find( "vc-Link " ) != string::npos
  462. || line.find( ".compile.") != string::npos
  463. || ( line.find( ".link") != string::npos &&
  464. // .linkonce is present in gcc linker messages about
  465. // unresolved symbols. We don't have to parse those
  466. line.find( ".linkonce" ) == string::npos )
  467. )
  468. {
  469. string action( ( line.find( "Link-action " ) != string::npos
  470. || line.find( "vc-Link " ) != string::npos
  471. || line.find( ".link") != string::npos)
  472. ? "link" : "compile" );
  473. if ( line.find( "...failed " ) != string::npos )
  474. mgr.stop_message( action, target_directory( line ),
  475. "fail", timestamp(), content );
  476. else
  477. {
  478. string target_dir( target_directory( line ) );
  479. mgr.start_message( action, target_dir,
  480. test_name( target_dir ), toolset( target_dir ), content );
  481. }
  482. content = "\n";
  483. capture_lines = true;
  484. }
  485. // these actions are only used to stop the previous action
  486. else if ( line.find( "-Archive" ) != string::npos
  487. || line.find( "MkDir" ) == 0 )
  488. {
  489. mgr.stop_message( content );
  490. content.clear();
  491. capture_lines = false;
  492. }
  493. else if ( line.find( "execute-test" ) != string::npos
  494. || line.find( "testing.capture-output" ) != string::npos )
  495. {
  496. if ( line.find( "...failed " ) != string::npos )
  497. {
  498. mgr.stop_message( "run", target_directory( line ),
  499. "fail", timestamp(), content );
  500. content = "\n";
  501. capture_lines = true;
  502. }
  503. else
  504. {
  505. string target_dir( target_directory( line ) );
  506. mgr.start_message( "run", target_dir,
  507. test_name( target_dir ), toolset( target_dir ), content );
  508. // contents of .output file for content
  509. capture_lines = false;
  510. content = "\n";
  511. fs::ifstream file( locate_root / target_dir
  512. / (test_name(target_dir) + ".output") );
  513. if ( file )
  514. {
  515. string ln;
  516. while ( std::getline( file, ln ) )
  517. {
  518. if ( ln.find( "<note>" ) != string::npos ) mgr.note( true );
  519. append_html( ln, content );
  520. content += "\n";
  521. }
  522. }
  523. }
  524. }
  525. // bjam indicates some prior dependency failed by a "...skipped" message
  526. else if ( line.find( "...skipped <" ) != string::npos )
  527. {
  528. mgr.stop_message( content );
  529. content.clear();
  530. capture_lines = false;
  531. if ( line.find( " for lack of " ) != string::npos
  532. && line.find( ".run for lack of " ) == string::npos )
  533. {
  534. capture_lines = true;
  535. string target_dir;
  536. string lib_dir;
  537. parse_skipped_msg( line, target_dir, lib_dir );
  538. if ( target_dir != lib_dir ) // it's a lib problem
  539. {
  540. mgr.start_message( "lib", target_dir,
  541. test_name( target_dir ), toolset( target_dir ), content );
  542. content = lib_dir;
  543. mgr.stop_message( "lib", target_dir, "fail", timestamp(), content );
  544. content = "\n";
  545. }
  546. }
  547. }
  548. else if ( line.find( "**passed**" ) != string::npos
  549. || line.find( "failed-test-file " ) != string::npos
  550. || line.find( "command-file-dump" ) != string::npos )
  551. {
  552. mgr.stop_message( content );
  553. content = "\n";
  554. capture_lines = true;
  555. }
  556. else if ( capture_lines ) // hang onto lines for possible later use
  557. {
  558. append_html( line, content );;
  559. content += "\n";
  560. }
  561. }
  562. mgr.stop_message( content );
  563. if (input != &std::cin)
  564. delete input;
  565. return 0;
  566. }
粤ICP备19079148号