CMakeSetupGUIImplementation.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. #include "CMakeSetupGUIImplementation.h"
  2. #include "FL/fl_file_chooser.H"
  3. #include "FL/filename.H"
  4. #include "FL/fl_ask.H"
  5. #include "../cmCacheManager.h"
  6. #include "../cmMakefile.h"
  7. #include <iostream>
  8. #include "FLTKPropertyList.h"
  9. #include "FL/fl_draw.H"
  10. #include "../cmake.h"
  11. /**
  12. * Constructor
  13. */
  14. CMakeSetupGUIImplementation
  15. ::CMakeSetupGUIImplementation():m_CacheEntriesList( this )
  16. {
  17. m_BuildPathChanged = false;
  18. }
  19. /**
  20. * Destructor
  21. */
  22. CMakeSetupGUIImplementation
  23. ::~CMakeSetupGUIImplementation()
  24. {
  25. }
  26. /**
  27. * Show the graphic interface
  28. */
  29. void
  30. CMakeSetupGUIImplementation
  31. ::Show( void )
  32. {
  33. dialogWindow->show();
  34. }
  35. /**
  36. * Hide the graphic interface
  37. */
  38. void
  39. CMakeSetupGUIImplementation
  40. ::Close( void )
  41. {
  42. SaveRecentDirectories();
  43. dialogWindow->hide();
  44. }
  45. /**
  46. * Browse for the path to the sources
  47. */
  48. void
  49. CMakeSetupGUIImplementation
  50. ::BrowseForSourcePath( void )
  51. {
  52. const char * path =
  53. fl_file_chooser(
  54. "Path to Sources",
  55. "",
  56. sourcePathTextInput->value() );
  57. if( !path )
  58. {
  59. return;
  60. }
  61. SetSourcePath( path );
  62. }
  63. /**
  64. * Browse for the path to the binaries
  65. */
  66. void
  67. CMakeSetupGUIImplementation
  68. ::BrowseForBinaryPath( void )
  69. {
  70. const char * path =
  71. fl_file_chooser(
  72. "Path to Binaries",
  73. "",
  74. binaryPathTextInput->value() );
  75. if( !path )
  76. {
  77. return;
  78. }
  79. SetBinaryPath( path );
  80. }
  81. /**
  82. * Set path to executable. Used to get the path to CMake
  83. */
  84. void
  85. CMakeSetupGUIImplementation
  86. ::SetPathToExecutable( const char * path )
  87. {
  88. char expandedPath[1024];
  89. filename_expand( expandedPath, path );
  90. char absolutePath[1024];
  91. filename_absolute( absolutePath, expandedPath );
  92. char * p = absolutePath + strlen( absolutePath );
  93. while( *p != '/' && *p != '\\' )
  94. {
  95. p--;
  96. }
  97. p--;
  98. while( *p != '/' && *p != '\\' )
  99. {
  100. p--;
  101. }
  102. *p = '\0';
  103. m_PathToExecutable = absolutePath;
  104. #if defined(_WIN32)
  105. m_PathToExecutable += "/Debug/CMake.exe";
  106. #else
  107. m_PathToExecutable += "/cmake";
  108. #endif
  109. }
  110. /**
  111. * Set the source path
  112. */
  113. bool
  114. CMakeSetupGUIImplementation
  115. ::SetSourcePath( const char * path )
  116. {
  117. if( !path || strlen(path)==0 )
  118. {
  119. fl_alert("Please select the path to the sources");
  120. return false;
  121. }
  122. std::string expandedAbsolutePath = ExpandPathAndMakeItAbsolute( path );
  123. sourcePathTextInput->value( expandedAbsolutePath.c_str() );
  124. if( VerifySourcePath( expandedAbsolutePath ) )
  125. {
  126. m_WhereSource = expandedAbsolutePath;
  127. return true;
  128. }
  129. return false;
  130. }
  131. /**
  132. * Expand environment variables in the path and make it absolute
  133. */
  134. std::string
  135. CMakeSetupGUIImplementation
  136. ::ExpandPathAndMakeItAbsolute( const std::string & inputPath ) const
  137. {
  138. char expandedPath[3000];
  139. filename_expand( expandedPath, inputPath.c_str() );
  140. char absolutePath[3000];
  141. filename_absolute( absolutePath, expandedPath );
  142. std::string expandedAbsolutePath = absolutePath;
  143. return expandedAbsolutePath;
  144. }
  145. /**
  146. * Set the binary path
  147. */
  148. bool
  149. CMakeSetupGUIImplementation
  150. ::SetBinaryPath( const char * path )
  151. {
  152. if( !path || strlen(path)==0 )
  153. {
  154. fl_alert("Please select the path to the binaries");
  155. return false;
  156. }
  157. std::string expandedAbsolutePath = ExpandPathAndMakeItAbsolute( path );
  158. binaryPathTextInput->value( expandedAbsolutePath.c_str() );
  159. if( !VerifyBinaryPath( expandedAbsolutePath.c_str() ) )
  160. {
  161. return false;
  162. }
  163. if( m_WhereBuild != expandedAbsolutePath )
  164. {
  165. m_BuildPathChanged = true;
  166. m_WhereBuild = expandedAbsolutePath;
  167. LoadCacheFromDiskToGUI();
  168. }
  169. else
  170. {
  171. m_BuildPathChanged = false;
  172. }
  173. return true;
  174. }
  175. /**
  176. * Verify the path to binaries
  177. */
  178. bool
  179. CMakeSetupGUIImplementation
  180. ::VerifyBinaryPath( const std::string & path ) const
  181. {
  182. bool pathIsOK = false;
  183. if( filename_isdir( path.c_str() ) )
  184. {
  185. pathIsOK = true;
  186. }
  187. else
  188. {
  189. int userWantsToCreateDirectory =
  190. fl_ask("The directory \n %s \n Doesn't exist. Do you want to create it ?",
  191. path.c_str() );
  192. if( userWantsToCreateDirectory )
  193. {
  194. std::string command = "mkdir ";
  195. command += path;
  196. system( command.c_str() );
  197. pathIsOK = true;
  198. }
  199. else
  200. {
  201. pathIsOK = false;
  202. }
  203. }
  204. return pathIsOK;
  205. }
  206. /**
  207. * Verify the path to sources
  208. */
  209. bool
  210. CMakeSetupGUIImplementation
  211. ::VerifySourcePath( const std::string & path ) const
  212. {
  213. if( !filename_isdir( path.c_str() ) )
  214. {
  215. fl_alert("The Source directory \n %s \n Doesn't exist or is not a directory", path.c_str() );
  216. return false;
  217. }
  218. return true;
  219. }
  220. /**
  221. * Build the project files
  222. */
  223. void
  224. CMakeSetupGUIImplementation
  225. ::BuildProjectFiles( void )
  226. {
  227. // Take and verify the source path from the GUI
  228. if( !SetSourcePath( sourcePathTextInput->value() ) )
  229. {
  230. return;
  231. }
  232. // Take and verify the binary path from the GUI
  233. if( !SetBinaryPath( binaryPathTextInput->value() ) )
  234. {
  235. return;
  236. }
  237. // set the wait cursor
  238. fl_cursor(FL_CURSOR_WAIT,FL_BLACK,FL_WHITE);
  239. // save the current GUI values to the cache
  240. this->SaveCacheFromGUI();
  241. // Make sure we are working from the cache on disk
  242. this->LoadCacheFromDiskToGUI();
  243. UpdateListOfRecentDirectories();
  244. SaveRecentDirectories();
  245. // create a cmake object
  246. cmake make;
  247. // create the arguments for the cmake object
  248. std::vector<std::string> args;
  249. args.push_back( m_PathToExecutable.c_str() );
  250. std::string arg;
  251. arg = "-H";
  252. arg += m_WhereSource;
  253. args.push_back(arg);
  254. arg = "-B";
  255. arg += m_WhereBuild;
  256. args.push_back(arg);
  257. // run the generate process
  258. if(make.Generate(args) != 0)
  259. {
  260. cmSystemTools::Error(
  261. "Error in generation process, project files may be invalid");
  262. }
  263. // update the GUI with any new values in the caused by the
  264. // generation process
  265. this->LoadCacheFromDiskToGUI();
  266. // path is up-to-date now
  267. m_BuildPathChanged = false;
  268. // put the cursor back
  269. fl_cursor(FL_CURSOR_DEFAULT,FL_BLACK,FL_WHITE);
  270. fl_message("Done !");
  271. }
  272. /**
  273. * Load Cache from disk to GUI
  274. */
  275. void
  276. CMakeSetupGUIImplementation
  277. ::LoadCacheFromDiskToGUI( void )
  278. {
  279. if( m_WhereBuild != "" )
  280. {
  281. cmCacheManager::GetInstance()->LoadCache( m_WhereBuild.c_str() );
  282. this->FillCacheGUIFromCacheManager();
  283. }
  284. }
  285. /**
  286. * Save Cache from disk to GUI
  287. */
  288. void
  289. CMakeSetupGUIImplementation
  290. ::SaveCacheFromGUI( void )
  291. {
  292. this->FillCacheManagerFromCacheGUI();
  293. if( m_WhereBuild != "" )
  294. {
  295. cmCacheManager::GetInstance()->SaveCache(
  296. m_WhereBuild.c_str() );
  297. }
  298. }
  299. /**
  300. * Fill Cache GUI from cache manager
  301. */
  302. void
  303. CMakeSetupGUIImplementation
  304. ::FillCacheGUIFromCacheManager( void )
  305. {
  306. // Prepare to add rows to the scroll
  307. m_CacheEntriesList.RemoveAll();
  308. propertyListPack->clear();
  309. propertyListPack->begin();
  310. const cmCacheManager::CacheEntryMap &cache =
  311. cmCacheManager::GetInstance()->GetCacheMap();
  312. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  313. i != cache.end(); ++i)
  314. {
  315. const char* key = i->first.c_str();
  316. const cmCacheManager::CacheEntry& value = i->second;
  317. switch(value.m_Type )
  318. {
  319. case cmCacheManager::BOOL:
  320. if(cmCacheManager::GetInstance()->IsOn(key))
  321. {
  322. m_CacheEntriesList.AddProperty(key,
  323. "ON",
  324. value.m_HelpString.c_str(),
  325. fltk::PropertyList::CHECKBOX,"");
  326. }
  327. else
  328. {
  329. m_CacheEntriesList.AddProperty(key,
  330. "OFF",
  331. value.m_HelpString.c_str(),
  332. fltk::PropertyList::CHECKBOX,"");
  333. }
  334. break;
  335. case cmCacheManager::PATH:
  336. m_CacheEntriesList.AddProperty(key,
  337. value.m_Value.c_str(),
  338. value.m_HelpString.c_str(),
  339. fltk::PropertyList::PATH,"");
  340. break;
  341. case cmCacheManager::FILEPATH:
  342. m_CacheEntriesList.AddProperty(key,
  343. value.m_Value.c_str(),
  344. value.m_HelpString.c_str(),
  345. fltk::PropertyList::FILE,"");
  346. break;
  347. case cmCacheManager::STRING:
  348. m_CacheEntriesList.AddProperty(key,
  349. value.m_Value.c_str(),
  350. value.m_HelpString.c_str(),
  351. fltk::PropertyList::EDIT,"");
  352. break;
  353. case cmCacheManager::INTERNAL:
  354. // These entries should not be seen by the user
  355. break;
  356. }
  357. }
  358. propertyListPack->end();
  359. propertyListPack->init_sizes();
  360. cacheValuesScroll->position( 0, 0 );
  361. propertyListPack->redraw();
  362. Fl::check();
  363. this->UpdateData(false);
  364. }
  365. /**
  366. * UpdateData
  367. */
  368. void
  369. CMakeSetupGUIImplementation
  370. ::UpdateData( bool option )
  371. {
  372. dialogWindow->redraw();
  373. Fl::check();
  374. }
  375. /**
  376. * Fill cache manager from Cache GUI
  377. */
  378. void
  379. CMakeSetupGUIImplementation
  380. ::FillCacheManagerFromCacheGUI( void )
  381. {
  382. cmCacheManager::GetInstance()->GetCacheMap();
  383. std::set<fltk::PropertyItem*> items = m_CacheEntriesList.GetItems();
  384. for(std::set<fltk::PropertyItem*>::iterator i = items.begin();
  385. i != items.end(); ++i)
  386. {
  387. fltk::PropertyItem* item = *i;
  388. cmCacheManager::CacheEntry *entry =
  389. cmCacheManager::GetInstance()->GetCacheEntry(
  390. (const char*)item->m_propName.c_str() );
  391. if (entry)
  392. {
  393. entry->m_Value = item->m_curValue;
  394. }
  395. }
  396. }
  397. /**
  398. * Load Recent Directories
  399. */
  400. void
  401. CMakeSetupGUIImplementation
  402. ::LoadRecentDirectories( void )
  403. {
  404. std::string home = getenv("HOME");
  405. std::string filename = home + "/.cmakerc";
  406. std::ifstream input;
  407. input.open(filename.c_str());
  408. if( input.fail() )
  409. {
  410. // probably the file doesn't exist
  411. return;
  412. }
  413. m_RecentBinaryDirectories.clear();
  414. m_RecentSourceDirectories.clear();
  415. std::string key;
  416. std::string onedirectory;
  417. while( !input.eof() )
  418. {
  419. input >> key;
  420. if( input.eof() ) break;
  421. if( key == "MostRecentSource" )
  422. {
  423. input >> onedirectory;
  424. m_WhereSource = onedirectory;
  425. sourcePathTextInput->value( m_WhereSource.c_str() );
  426. } else
  427. if( key == "MostRecentBinary" )
  428. {
  429. input >> onedirectory;
  430. m_WhereBuild = onedirectory;
  431. binaryPathTextInput->value( m_WhereBuild.c_str() );
  432. } else
  433. if( key == "Binary" )
  434. {
  435. input >> onedirectory;
  436. // insert is only done if the directory doesn't exist
  437. m_RecentBinaryDirectories.insert( onedirectory );
  438. recentBinaryDirectoriesBrowser->add(
  439. (onedirectory.c_str()),
  440. (void*)(onedirectory.c_str()) );
  441. } else
  442. if( key == "Source" )
  443. {
  444. input >> onedirectory;
  445. // insert is only done if the directory doesn't exist
  446. m_RecentSourceDirectories.insert( onedirectory );
  447. recentSourceDirectoriesBrowser->add(
  448. (onedirectory.c_str()),
  449. (void*)(onedirectory.c_str()) );
  450. }
  451. }
  452. input.close();
  453. }
  454. /**
  455. * Save Recent Directories
  456. */
  457. void
  458. CMakeSetupGUIImplementation
  459. ::SaveRecentDirectories( void )
  460. {
  461. std::string home = getenv("HOME");
  462. if( home.empty() )
  463. {
  464. return;
  465. }
  466. std::string filename = home + "/.cmakerc";
  467. std::ofstream output;
  468. output.open(filename.c_str());
  469. output << "MostRecentBinary " << m_WhereBuild << std::endl;
  470. output << "MostRecentSource " << m_WhereSource << std::endl;
  471. // Save Recent binary directories
  472. std::set< std::string >::iterator bindir =
  473. m_RecentBinaryDirectories.begin();
  474. while( bindir != m_RecentBinaryDirectories.end() )
  475. {
  476. output << "Binary " << *bindir << std::endl;
  477. bindir++;
  478. }
  479. // Save Recent source directories
  480. std::set< std::string >::iterator srcdir =
  481. m_RecentSourceDirectories.begin();
  482. while( srcdir != m_RecentSourceDirectories.end() )
  483. {
  484. output << "Source " << *srcdir << std::endl;
  485. srcdir++;
  486. }
  487. }
  488. /**
  489. * Show Recent Binary Directories
  490. */
  491. void
  492. CMakeSetupGUIImplementation
  493. ::ShowRecentBinaryDirectories( void )
  494. {
  495. recentBinaryDirectoriesBrowser->Fl_Widget::show();
  496. }
  497. /**
  498. * Show Recent Source Directories
  499. */
  500. void
  501. CMakeSetupGUIImplementation
  502. ::ShowRecentSourceDirectories( void )
  503. {
  504. recentSourceDirectoriesBrowser->Fl_Widget::show();
  505. }
  506. /**
  507. * Select one Recent Binary Directory
  508. */
  509. void
  510. CMakeSetupGUIImplementation
  511. ::SelectOneRecentBinaryDirectory( void )
  512. {
  513. const int selected = recentBinaryDirectoriesBrowser->value();
  514. if( selected == 0 )
  515. {
  516. return;
  517. }
  518. m_WhereBuild = static_cast<char *>(
  519. recentBinaryDirectoriesBrowser->data( selected ));
  520. binaryPathTextInput->value( m_WhereBuild.c_str() );
  521. recentBinaryDirectoriesBrowser->Fl_Widget::hide();
  522. }
  523. /**
  524. * Select one Recent Source Directory
  525. */
  526. void
  527. CMakeSetupGUIImplementation
  528. ::SelectOneRecentSourceDirectory( void )
  529. {
  530. const int selected = recentSourceDirectoriesBrowser->value();
  531. if( selected == 0 )
  532. {
  533. return;
  534. }
  535. m_WhereSource = static_cast< char * >(
  536. recentSourceDirectoriesBrowser->data( selected ));
  537. sourcePathTextInput->value( m_WhereSource.c_str() );
  538. recentSourceDirectoriesBrowser->Fl_Widget::hide();
  539. }
  540. /**
  541. * Update List of Recent Directories
  542. */
  543. void
  544. CMakeSetupGUIImplementation
  545. ::UpdateListOfRecentDirectories( void )
  546. {
  547. // Update Recent binary directories
  548. // insert is only done if the directory doesn't exist
  549. m_RecentBinaryDirectories.insert( m_WhereBuild );
  550. // Update Recent source directories
  551. // insert is only done if the directory doesn't exist
  552. m_RecentSourceDirectories.insert( m_WhereSource );
  553. }