process_jam_log.cpp 22 KB

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