CMakeSetupGUIImplementation.cxx 17 KB

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