CMakeSetupGUIImplementation.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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()
  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. }
  169. LoadCacheFromDiskToGUI();
  170. return true;
  171. }
  172. /**
  173. * Verify the path to binaries
  174. */
  175. bool
  176. CMakeSetupGUIImplementation
  177. ::VerifyBinaryPath( const std::string & path ) const
  178. {
  179. bool pathIsOK = false;
  180. if( filename_isdir( path.c_str() ) )
  181. {
  182. pathIsOK = true;
  183. }
  184. else
  185. {
  186. int userWantsToCreateDirectory =
  187. fl_ask("The directory \n %s \n Doesn't exist. Do you want to create it ?",
  188. path.c_str() );
  189. if( userWantsToCreateDirectory )
  190. {
  191. std::string command = "mkdir ";
  192. command += path;
  193. system( command.c_str() );
  194. pathIsOK = true;
  195. }
  196. else
  197. {
  198. pathIsOK = false;
  199. }
  200. }
  201. return pathIsOK;
  202. }
  203. /**
  204. * Verify the path to sources
  205. */
  206. bool
  207. CMakeSetupGUIImplementation
  208. ::VerifySourcePath( const std::string & path ) const
  209. {
  210. if( !filename_isdir( path.c_str() ) )
  211. {
  212. fl_alert("The Source directory \n %s \n Doesn't exist or is not a directory", path.c_str() );
  213. return false;
  214. }
  215. return true;
  216. }
  217. /**
  218. * Build the project files
  219. */
  220. void
  221. CMakeSetupGUIImplementation
  222. ::BuildProjectFiles( void )
  223. {
  224. // Take and verify the source path from the GUI
  225. if( !SetSourcePath( sourcePathTextInput->value() ) )
  226. {
  227. return;
  228. }
  229. // Take and verify the binary path from the GUI
  230. if( !SetBinaryPath( binaryPathTextInput->value() ) )
  231. {
  232. return;
  233. }
  234. // set the wait cursor
  235. fl_cursor(FL_CURSOR_WAIT,FL_BLACK,FL_WHITE);
  236. // save the current GUI values to the cache
  237. this->SaveCacheFromGUI();
  238. // Make sure we are working from the cache on disk
  239. this->LoadCacheFromDiskToGUI();
  240. // create a cmake object
  241. cmake make;
  242. // create the arguments for the cmake object
  243. std::vector<std::string> args;
  244. args.push_back( m_PathToExecutable.c_str() );
  245. std::string arg;
  246. arg = "-H";
  247. arg += m_WhereSource;
  248. args.push_back(arg);
  249. arg = "-B";
  250. arg += m_WhereBuild;
  251. args.push_back(arg);
  252. // run the generate process
  253. if(make.Generate(args) != 0)
  254. {
  255. cmSystemTools::Error(
  256. "Error in generation process, project files may be invalid");
  257. }
  258. // update the GUI with any new values in the caused by the
  259. // generation process
  260. this->LoadCacheFromDiskToGUI();
  261. // path is up-to-date now
  262. m_BuildPathChanged = false;
  263. // put the cursor back
  264. fl_cursor(FL_CURSOR_DEFAULT,FL_BLACK,FL_WHITE);
  265. fl_message("Done !");
  266. }
  267. /**
  268. * Load Cache from disk to GUI
  269. */
  270. void
  271. CMakeSetupGUIImplementation
  272. ::LoadCacheFromDiskToGUI( void )
  273. {
  274. if( m_WhereBuild != "" )
  275. {
  276. cmCacheManager::GetInstance()->LoadCache( m_WhereBuild.c_str() );
  277. this->FillCacheGUIFromCacheManager();
  278. }
  279. }
  280. /**
  281. * Save Cache from disk to GUI
  282. */
  283. void
  284. CMakeSetupGUIImplementation
  285. ::SaveCacheFromGUI( void )
  286. {
  287. std::cout << "Saving cache from GUI ...";
  288. this->FillCacheManagerFromCacheGUI();
  289. if( m_WhereBuild != "" )
  290. {
  291. cmCacheManager::GetInstance()->SaveCache(
  292. m_WhereBuild.c_str() );
  293. }
  294. std::cout << " Done ! " << std::endl;
  295. }
  296. /**
  297. * Fill Cache GUI from cache manager
  298. */
  299. void
  300. CMakeSetupGUIImplementation
  301. ::FillCacheGUIFromCacheManager( void )
  302. {
  303. // Prepare to add rows to the scroll
  304. propertyListPack->begin();
  305. const cmCacheManager::CacheEntryMap &cache =
  306. cmCacheManager::GetInstance()->GetCacheMap();
  307. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  308. i != cache.end(); ++i)
  309. {
  310. const char* key = i->first.c_str();
  311. const cmCacheManager::CacheEntry& value = i->second;
  312. switch(value.m_Type )
  313. {
  314. case cmCacheManager::BOOL:
  315. if(cmCacheManager::GetInstance()->IsOn(key))
  316. {
  317. m_CacheEntriesList.AddProperty(key,
  318. "ON",
  319. value.m_HelpString.c_str(),
  320. fltk::PropertyList::CHECKBOX,"");
  321. }
  322. else
  323. {
  324. m_CacheEntriesList.AddProperty(key,
  325. "OFF",
  326. value.m_HelpString.c_str(),
  327. fltk::PropertyList::CHECKBOX,"");
  328. }
  329. break;
  330. case cmCacheManager::PATH:
  331. m_CacheEntriesList.AddProperty(key,
  332. value.m_Value.c_str(),
  333. value.m_HelpString.c_str(),
  334. fltk::PropertyList::PATH,"");
  335. break;
  336. case cmCacheManager::FILEPATH:
  337. m_CacheEntriesList.AddProperty(key,
  338. value.m_Value.c_str(),
  339. value.m_HelpString.c_str(),
  340. fltk::PropertyList::FILE,"");
  341. break;
  342. case cmCacheManager::STRING:
  343. m_CacheEntriesList.AddProperty(key,
  344. value.m_Value.c_str(),
  345. value.m_HelpString.c_str(),
  346. fltk::PropertyList::EDIT,"");
  347. break;
  348. case cmCacheManager::INTERNAL:
  349. break;
  350. }
  351. }
  352. propertyListPack->end();
  353. propertyListPack->init_sizes();
  354. cacheValuesScroll->position( 0, 0 );
  355. this->UpdateData(false);
  356. }
  357. /**
  358. * UpdateData
  359. */
  360. void
  361. CMakeSetupGUIImplementation
  362. ::UpdateData( bool option )
  363. {
  364. dialogWindow->redraw();
  365. Fl::check();
  366. }
  367. /**
  368. * Fill cache manager from Cache GUI
  369. */
  370. void
  371. CMakeSetupGUIImplementation
  372. ::FillCacheManagerFromCacheGUI( void )
  373. {
  374. cmCacheManager::GetInstance()->GetCacheMap();
  375. std::set<fltk::PropertyItem*> items = m_CacheEntriesList.GetItems();
  376. for(std::set<fltk::PropertyItem*>::iterator i = items.begin();
  377. i != items.end(); ++i)
  378. {
  379. fltk::PropertyItem* item = *i;
  380. cmCacheManager::CacheEntry *entry =
  381. cmCacheManager::GetInstance()->GetCacheEntry(
  382. (const char*)item->m_propName.c_str() );
  383. if (entry)
  384. {
  385. entry->m_Value = item->m_curValue;
  386. }
  387. }
  388. }