process_jam_log.cpp 18 KB

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