CMakeSetupGUIImplementation.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. #include "CMakeSetupGUIImplementation.h"
  2. #include "FL/fl_file_chooser.H"
  3. #include "FL/filename.H"
  4. #include "FL/fl_ask.H"
  5. #include "cstring"
  6. #include "../cmCacheManager.h"
  7. #include "../cmMakefile.h"
  8. #include <iostream>
  9. #include "FLTKPropertyList.h"
  10. #include "FL/fl_draw.H"
  11. #include "../cmake.h"
  12. /**
  13. * Constructor
  14. */
  15. CMakeSetupGUIImplementation
  16. ::CMakeSetupGUIImplementation():m_CacheEntriesList( this )
  17. {
  18. m_BuildPathChanged = false;
  19. }
  20. /**
  21. * Destructor
  22. */
  23. CMakeSetupGUIImplementation
  24. ::~CMakeSetupGUIImplementation()
  25. {
  26. }
  27. /**
  28. * Show the graphic interface
  29. */
  30. void
  31. CMakeSetupGUIImplementation
  32. ::Show( void )
  33. {
  34. dialogWindow->show();
  35. }
  36. /**
  37. * Hide the graphic interface
  38. */
  39. void
  40. CMakeSetupGUIImplementation
  41. ::Close( void )
  42. {
  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. m_PathToExecutable = path;
  89. char expandedPath[1024];
  90. filename_expand( expandedPath, path );
  91. char absolutePath[1024];
  92. filename_absolute( absolutePath, expandedPath );
  93. char * p = absolutePath + strlen( absolutePath );
  94. while( *p != '/' && *p != '\\' )
  95. {
  96. p--;
  97. }
  98. p--;
  99. while( *p != '/' && *p != '\\' )
  100. {
  101. p--;
  102. }
  103. *p = '\0';
  104. m_PathToExecutable = absolutePath;
  105. #if defined(_WIN32)
  106. m_PathToExecutable += "/Debug/CMake.exe";
  107. #else
  108. m_PathToExecutable += "/cmake";
  109. #endif
  110. }
  111. /**
  112. * Set the source path
  113. */
  114. bool
  115. CMakeSetupGUIImplementation
  116. ::SetSourcePath( const char * path )
  117. {
  118. if( !path || strlen(path)==0 )
  119. {
  120. fl_alert("Please select the path to the sources");
  121. return false;
  122. }
  123. std::string expandedAbsolutePath = ExpandPathAndMakeItAbsolute( path );
  124. sourcePathTextInput->value( expandedAbsolutePath.c_str() );
  125. if( VerifySourcePath( expandedAbsolutePath ) )
  126. {
  127. m_WhereSource = expandedAbsolutePath;
  128. return true;
  129. }
  130. return false;
  131. }
  132. /**
  133. * Expand environment variables in the path and make it absolute
  134. */
  135. std::string
  136. CMakeSetupGUIImplementation
  137. ::ExpandPathAndMakeItAbsolute( const std::string & inputPath ) const
  138. {
  139. char expandedPath[3000];
  140. filename_expand( expandedPath, inputPath.c_str() );
  141. char absolutePath[3000];
  142. filename_absolute( absolutePath, expandedPath );
  143. std::string expandedAbsolutePath = absolutePath;
  144. return expandedAbsolutePath;
  145. }
  146. /**
  147. * Set the binary path
  148. */
  149. bool
  150. CMakeSetupGUIImplementation
  151. ::SetBinaryPath( const char * path )
  152. {
  153. if( !path || strlen(path)==0 )
  154. {
  155. fl_alert("Please select the path to the binaries");
  156. return false;
  157. }
  158. std::string expandedAbsolutePath = ExpandPathAndMakeItAbsolute( path );
  159. binaryPathTextInput->value( expandedAbsolutePath.c_str() );
  160. if( !VerifyBinaryPath( expandedAbsolutePath.c_str() ) )
  161. {
  162. return false;
  163. }
  164. if( m_WhereBuild != expandedAbsolutePath )
  165. {
  166. m_BuildPathChanged = true;
  167. m_WhereBuild = expandedAbsolutePath;
  168. LoadCacheFromDiskToGUI();
  169. }
  170. else
  171. {
  172. m_BuildPathChanged = false;
  173. }
  174. return true;
  175. }
  176. /**
  177. * Verify the path to binaries
  178. */
  179. bool
  180. CMakeSetupGUIImplementation
  181. ::VerifyBinaryPath( const std::string & path ) const
  182. {
  183. bool pathIsOK = false;
  184. if( filename_isdir( path.c_str() ) )
  185. {
  186. pathIsOK = true;
  187. }
  188. else
  189. {
  190. int userWantsToCreateDirectory =
  191. fl_ask("The directory \n %s \n Doesn't exist. Do you want to create it ?",
  192. path.c_str() );
  193. if( userWantsToCreateDirectory )
  194. {
  195. std::string command = "mkdir ";
  196. command += path;
  197. system( command.c_str() );
  198. pathIsOK = true;
  199. }
  200. else
  201. {
  202. pathIsOK = false;
  203. }
  204. }
  205. return pathIsOK;
  206. }
  207. /**
  208. * Verify the path to sources
  209. */
  210. bool
  211. CMakeSetupGUIImplementation
  212. ::VerifySourcePath( const std::string & path ) const
  213. {
  214. if( !filename_isdir( path.c_str() ) )
  215. {
  216. fl_alert("The Source directory \n %s \n Doesn't exist or is not a directory", path.c_str() );
  217. return false;
  218. }
  219. return true;
  220. }
  221. /**
  222. * Build the project files
  223. */
  224. void
  225. CMakeSetupGUIImplementation
  226. ::BuildProjectFiles( void )
  227. {
  228. // Take and verify the source path from the GUI
  229. if( !SetSourcePath( sourcePathTextInput->value() ) )
  230. {
  231. return;
  232. }
  233. // Take and verify the binary path from the GUI
  234. if( !SetBinaryPath( binaryPathTextInput->value() ) )
  235. {
  236. return;
  237. }
  238. // set the wait cursor
  239. fl_cursor(FL_CURSOR_WAIT,FL_BLACK,FL_WHITE);
  240. // save the current GUI values to the cache
  241. this->SaveCacheFromGUI();
  242. // Make sure we are working from the cache on disk
  243. this->LoadCacheFromDiskToGUI();
  244. // create a cmake object
  245. cmake make;
  246. // create the arguments for the cmake object
  247. std::vector<std::string> args;
  248. args.push_back( m_PathToExecutable.c_str() );
  249. std::string arg;
  250. arg = "-H";
  251. arg += m_WhereSource;
  252. args.push_back(arg);
  253. arg = "-B";
  254. arg += m_WhereBuild;
  255. args.push_back(arg);
  256. // run the generate process
  257. if(make.Generate(args) != 0)
  258. {
  259. cmSystemTools::Error(
  260. "Error in generation process, project files may be invalid");
  261. }
  262. // update the GUI with any new values in the caused by the
  263. // generation process
  264. this->LoadCacheFromDiskToGUI();
  265. // path is up-to-date now
  266. m_BuildPathChanged = false;
  267. // put the cursor back
  268. fl_cursor(FL_CURSOR_DEFAULT,FL_BLACK,FL_WHITE);
  269. fl_message("Done !");
  270. }
  271. /**
  272. * Load Cache from disk to GUI
  273. */
  274. void
  275. CMakeSetupGUIImplementation
  276. ::LoadCacheFromDiskToGUI( void )
  277. {
  278. if( m_WhereBuild != "" )
  279. {
  280. cmCacheManager::GetInstance()->LoadCache( m_WhereBuild.c_str() );
  281. this->FillCacheGUIFromCacheManager();
  282. }
  283. }
  284. /**
  285. * Save Cache from disk to GUI
  286. */
  287. void
  288. CMakeSetupGUIImplementation
  289. ::SaveCacheFromGUI( void )
  290. {
  291. this->FillCacheManagerFromCacheGUI();
  292. if( m_WhereBuild != "" )
  293. {
  294. cmCacheManager::GetInstance()->SaveCache(
  295. m_WhereBuild.c_str() );
  296. }
  297. }
  298. /**
  299. * Fill Cache GUI from cache manager
  300. */
  301. void
  302. CMakeSetupGUIImplementation
  303. ::FillCacheGUIFromCacheManager( void )
  304. {
  305. // Prepare to add rows to the scroll
  306. m_CacheEntriesList.RemoveAll();
  307. propertyListPack->clear();
  308. propertyListPack->begin();
  309. const cmCacheManager::CacheEntryMap &cache =
  310. cmCacheManager::GetInstance()->GetCacheMap();
  311. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  312. i != cache.end(); ++i)
  313. {
  314. const char* key = i->first.c_str();
  315. const cmCacheManager::CacheEntry& value = i->second;
  316. switch(value.m_Type )
  317. {
  318. case cmCacheManager::BOOL:
  319. if(cmCacheManager::GetInstance()->IsOn(key))
  320. {
  321. m_CacheEntriesList.AddProperty(key,
  322. "ON",
  323. value.m_HelpString.c_str(),
  324. fltk::PropertyList::CHECKBOX,"");
  325. }
  326. else
  327. {
  328. m_CacheEntriesList.AddProperty(key,
  329. "OFF",
  330. value.m_HelpString.c_str(),
  331. fltk::PropertyList::CHECKBOX,"");
  332. }
  333. break;
  334. case cmCacheManager::PATH:
  335. m_CacheEntriesList.AddProperty(key,
  336. value.m_Value.c_str(),
  337. value.m_HelpString.c_str(),
  338. fltk::PropertyList::PATH,"");
  339. break;
  340. case cmCacheManager::FILEPATH:
  341. m_CacheEntriesList.AddProperty(key,
  342. value.m_Value.c_str(),
  343. value.m_HelpString.c_str(),
  344. fltk::PropertyList::FILE,"");
  345. break;
  346. case cmCacheManager::STRING:
  347. m_CacheEntriesList.AddProperty(key,
  348. value.m_Value.c_str(),
  349. value.m_HelpString.c_str(),
  350. fltk::PropertyList::EDIT,"");
  351. break;
  352. case cmCacheManager::INTERNAL:
  353. // These entries should not be seen by the user
  354. break;
  355. }
  356. }
  357. propertyListPack->end();
  358. propertyListPack->init_sizes();
  359. cacheValuesScroll->position( 0, 0 );
  360. propertyListPack->redraw();
  361. Fl::check();
  362. this->UpdateData(false);
  363. }
  364. /**
  365. * UpdateData
  366. */
  367. void
  368. CMakeSetupGUIImplementation
  369. ::UpdateData( bool option )
  370. {
  371. dialogWindow->redraw();
  372. Fl::check();
  373. }
  374. /**
  375. * Fill cache manager from Cache GUI
  376. */
  377. void
  378. CMakeSetupGUIImplementation
  379. ::FillCacheManagerFromCacheGUI( void )
  380. {
  381. cmCacheManager::GetInstance()->GetCacheMap();
  382. std::set<fltk::PropertyItem*> items = m_CacheEntriesList.GetItems();
  383. for(std::set<fltk::PropertyItem*>::iterator i = items.begin();
  384. i != items.end(); ++i)
  385. {
  386. fltk::PropertyItem* item = *i;
  387. cmCacheManager::CacheEntry *entry =
  388. cmCacheManager::GetInstance()->GetCacheEntry(
  389. (const char*)item->m_propName.c_str() );
  390. if (entry)
  391. {
  392. entry->m_Value = item->m_curValue;
  393. }
  394. }
  395. }