CMakeSetupGUIImplementation.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. }
  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. this->FillCacheManagerFromCacheGUI();
  288. if( m_WhereBuild != "" )
  289. {
  290. cmCacheManager::GetInstance()->SaveCache(
  291. m_WhereBuild.c_str() );
  292. }
  293. }
  294. /**
  295. * Fill Cache GUI from cache manager
  296. */
  297. void
  298. CMakeSetupGUIImplementation
  299. ::FillCacheGUIFromCacheManager( void )
  300. {
  301. // Prepare to add rows to the scroll
  302. propertyListPack->begin();
  303. const cmCacheManager::CacheEntryMap &cache =
  304. cmCacheManager::GetInstance()->GetCacheMap();
  305. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  306. i != cache.end(); ++i)
  307. {
  308. const char* key = i->first.c_str();
  309. const cmCacheManager::CacheEntry& value = i->second;
  310. switch(value.m_Type )
  311. {
  312. case cmCacheManager::BOOL:
  313. if(cmCacheManager::GetInstance()->IsOn(key))
  314. {
  315. m_CacheEntriesList.AddProperty(key,
  316. "ON",
  317. value.m_HelpString.c_str(),
  318. fltk::PropertyList::CHECKBOX,"");
  319. }
  320. else
  321. {
  322. m_CacheEntriesList.AddProperty(key,
  323. "OFF",
  324. value.m_HelpString.c_str(),
  325. fltk::PropertyList::CHECKBOX,"");
  326. }
  327. break;
  328. case cmCacheManager::PATH:
  329. m_CacheEntriesList.AddProperty(key,
  330. value.m_Value.c_str(),
  331. value.m_HelpString.c_str(),
  332. fltk::PropertyList::PATH,"");
  333. break;
  334. case cmCacheManager::FILEPATH:
  335. m_CacheEntriesList.AddProperty(key,
  336. value.m_Value.c_str(),
  337. value.m_HelpString.c_str(),
  338. fltk::PropertyList::FILE,"");
  339. break;
  340. case cmCacheManager::STRING:
  341. m_CacheEntriesList.AddProperty(key,
  342. value.m_Value.c_str(),
  343. value.m_HelpString.c_str(),
  344. fltk::PropertyList::EDIT,"");
  345. break;
  346. case cmCacheManager::INTERNAL:
  347. break;
  348. }
  349. }
  350. propertyListPack->end();
  351. propertyListPack->init_sizes();
  352. cacheValuesScroll->position( 0, 0 );
  353. this->UpdateData(false);
  354. }
  355. /**
  356. * UpdateData
  357. */
  358. void
  359. CMakeSetupGUIImplementation
  360. ::UpdateData( bool option )
  361. {
  362. dialogWindow->redraw();
  363. Fl::check();
  364. }
  365. /**
  366. * Fill cache manager from Cache GUI
  367. */
  368. void
  369. CMakeSetupGUIImplementation
  370. ::FillCacheManagerFromCacheGUI( void )
  371. {
  372. cmCacheManager::GetInstance()->GetCacheMap();
  373. std::set<fltk::PropertyItem*> items = m_CacheEntriesList.GetItems();
  374. for(std::set<fltk::PropertyItem*>::iterator i = items.begin();
  375. i != items.end(); ++i)
  376. {
  377. fltk::PropertyItem* item = *i;
  378. cmCacheManager::CacheEntry *entry =
  379. cmCacheManager::GetInstance()->GetCacheEntry(
  380. (const char*)item->m_propName.c_str() );
  381. if (entry)
  382. {
  383. entry->m_Value = item->m_curValue;
  384. }
  385. }
  386. }