process_jam_log.cpp 18 KB

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