CMakeSetupGUIImplementation.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. // These entries should not be seen by the user
  409. m_CacheEntriesList.RemoveProperty(key);
  410. break;
  411. }
  412. }
  413. // Add the old entry to the end of the pack
  414. for(std::set<fltk::PropertyItem*>::iterator i = items.begin();
  415. i != items.end(); ++i)
  416. {
  417. fltk::PropertyItem* item = *i;
  418. if( !(item->m_NewValue) )
  419. {
  420. new fltk::PropertyItemRow( item ); // GUI of the old property row
  421. }
  422. }
  423. propertyListPack->end();
  424. propertyListPack->init_sizes();
  425. cacheValuesScroll->position( 0, 0 );
  426. propertyListPack->redraw();
  427. Fl::check();
  428. this->UpdateData(false);
  429. }
  430. /**
  431. * UpdateData
  432. */
  433. void
  434. CMakeSetupGUIImplementation
  435. ::UpdateData( bool option )
  436. {
  437. dialogWindow->redraw();
  438. Fl::check();
  439. }
  440. /**
  441. * Fill cache manager from Cache GUI
  442. */
  443. void
  444. CMakeSetupGUIImplementation
  445. ::FillCacheManagerFromCacheGUI( void )
  446. {
  447. cmCacheManager::GetInstance()->GetCacheMap();
  448. std::set<fltk::PropertyItem*> items = m_CacheEntriesList.GetItems();
  449. for(std::set<fltk::PropertyItem*>::iterator i = items.begin();
  450. i != items.end(); ++i)
  451. {
  452. fltk::PropertyItem* item = *i;
  453. cmCacheManager::CacheEntry *entry =
  454. cmCacheManager::GetInstance()->GetCacheEntry(
  455. (const char*)item->m_propName.c_str() );
  456. if (entry)
  457. {
  458. entry->m_Value = item->m_curValue;
  459. }
  460. if( item->m_Dirty )
  461. {
  462. m_CacheEntriesList.SetDirty();
  463. }
  464. }
  465. }
  466. /**
  467. * Load Recent Directories
  468. */
  469. void
  470. CMakeSetupGUIImplementation
  471. ::LoadRecentDirectories( void )
  472. {
  473. std::string home = getenv("HOME");
  474. std::string filename = home + "/.cmakerc";
  475. std::ifstream input;
  476. input.open(filename.c_str());
  477. if( input.fail() )
  478. {
  479. // probably the file doesn't exist
  480. return;
  481. }
  482. m_RecentBinaryDirectories.clear();
  483. m_RecentSourceDirectories.clear();
  484. std::string key;
  485. std::string onedirectory;
  486. while( !input.eof() )
  487. {
  488. input >> key;
  489. if( input.eof() ) break;
  490. if( key == "MostRecentSource" )
  491. {
  492. input >> onedirectory;
  493. m_WhereSource = onedirectory;
  494. sourcePathTextInput->value( m_WhereSource.c_str() );
  495. } else
  496. if( key == "MostRecentBinary" )
  497. {
  498. input >> onedirectory;
  499. m_WhereBuild = onedirectory;
  500. binaryPathTextInput->value( m_WhereBuild.c_str() );
  501. } else
  502. if( key == "Binary" )
  503. {
  504. input >> onedirectory;
  505. // insert is only done if the directory doesn't exist
  506. m_RecentBinaryDirectories.insert( onedirectory );
  507. recentBinaryDirectoriesBrowser->add(
  508. (onedirectory.c_str()),
  509. (void*)(onedirectory.c_str()) );
  510. } else
  511. if( key == "Source" )
  512. {
  513. input >> onedirectory;
  514. // insert is only done if the directory doesn't exist
  515. m_RecentSourceDirectories.insert( onedirectory );
  516. recentSourceDirectoriesBrowser->add(
  517. (onedirectory.c_str()),
  518. (void*)(onedirectory.c_str()) );
  519. }
  520. }
  521. input.close();
  522. }
  523. /**
  524. * Save Recent Directories
  525. */
  526. void
  527. CMakeSetupGUIImplementation
  528. ::SaveRecentDirectories( void )
  529. {
  530. std::string home = getenv("HOME");
  531. if( home.empty() )
  532. {
  533. return;
  534. }
  535. std::string filename = home + "/.cmakerc";
  536. std::ofstream output;
  537. output.open(filename.c_str());
  538. output << "MostRecentBinary " << m_WhereBuild << std::endl;
  539. output << "MostRecentSource " << m_WhereSource << std::endl;
  540. // Save Recent binary directories
  541. std::set< std::string >::iterator bindir =
  542. m_RecentBinaryDirectories.begin();
  543. while( bindir != m_RecentBinaryDirectories.end() )
  544. {
  545. output << "Binary " << *bindir << std::endl;
  546. bindir++;
  547. }
  548. // Save Recent source directories
  549. std::set< std::string >::iterator srcdir =
  550. m_RecentSourceDirectories.begin();
  551. while( srcdir != m_RecentSourceDirectories.end() )
  552. {
  553. output << "Source " << *srcdir << std::endl;
  554. srcdir++;
  555. }
  556. }
  557. /**
  558. * Show Recent Binary Directories
  559. */
  560. void
  561. CMakeSetupGUIImplementation
  562. ::ShowRecentBinaryDirectories( void )
  563. {
  564. if( recentBinaryDirectoriesBrowser->size() )
  565. {
  566. recentBinaryDirectoriesBrowser->Fl_Widget::show();
  567. }
  568. }
  569. /**
  570. * Show Recent Source Directories
  571. */
  572. void
  573. CMakeSetupGUIImplementation
  574. ::ShowRecentSourceDirectories( void )
  575. {
  576. if( recentSourceDirectoriesBrowser->size() )
  577. {
  578. recentSourceDirectoriesBrowser->Fl_Widget::show();
  579. }
  580. }
  581. /**
  582. * Select one Recent Binary Directory
  583. */
  584. void
  585. CMakeSetupGUIImplementation
  586. ::SelectOneRecentBinaryDirectory( void )
  587. {
  588. const int selected = recentBinaryDirectoriesBrowser->value();
  589. if( selected == 0 )
  590. {
  591. return;
  592. }
  593. m_WhereBuild = static_cast<char *>(
  594. recentBinaryDirectoriesBrowser->data( selected ));
  595. binaryPathTextInput->value( m_WhereBuild.c_str() );
  596. recentBinaryDirectoriesBrowser->Fl_Widget::hide();
  597. }
  598. /**
  599. * Select one Recent Source Directory
  600. */
  601. void
  602. CMakeSetupGUIImplementation
  603. ::SelectOneRecentSourceDirectory( void )
  604. {
  605. const int selected = recentSourceDirectoriesBrowser->value();
  606. if( selected == 0 )
  607. {
  608. return;
  609. }
  610. m_WhereSource = static_cast< char * >(
  611. recentSourceDirectoriesBrowser->data( selected ));
  612. sourcePathTextInput->value( m_WhereSource.c_str() );
  613. recentSourceDirectoriesBrowser->Fl_Widget::hide();
  614. }
  615. /**
  616. * Update List of Recent Directories
  617. */
  618. void
  619. CMakeSetupGUIImplementation
  620. ::UpdateListOfRecentDirectories( void )
  621. {
  622. // Update Recent binary directories
  623. // insert is only done if the directory doesn't exist
  624. m_RecentBinaryDirectories.insert( m_WhereBuild );
  625. // Update Recent source directories
  626. // insert is only done if the directory doesn't exist
  627. m_RecentSourceDirectories.insert( m_WhereSource );
  628. }
  629. /**
  630. * Clicked on Configure Button
  631. */
  632. void
  633. CMakeSetupGUIImplementation
  634. ::ClickOnConfigure( void )
  635. {
  636. this->RunCMake(false);
  637. }
  638. /**
  639. * Clicked on OK Button
  640. */
  641. void
  642. CMakeSetupGUIImplementation
  643. ::ClickOnOK( void )
  644. {
  645. m_CacheEntriesList.ClearDirty();
  646. this->RunCMake(true);
  647. cmMakefileGenerator::UnRegisterGenerators();
  648. this->Close();
  649. }
  650. /**
  651. * Clicked on Cancel Button
  652. */
  653. void
  654. CMakeSetupGUIImplementation
  655. ::ClickOnCancel( void )
  656. {
  657. if(m_CacheEntriesList.IsDirty())
  658. {
  659. int userWantsExitEvenThoughOptionsHaveChanged =
  660. fl_ask("You have changed options but not rebuilt, \n"
  661. "are you sure you want to exit?");
  662. if( userWantsExitEvenThoughOptionsHaveChanged )
  663. {
  664. this->Close();
  665. }
  666. }
  667. else
  668. {
  669. this->Close();
  670. }
  671. }