process_jam_log.cpp 21 KB

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