CMakeSetupGUIImplementation.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. std::cout << "absolutePath = " << absolutePath << std::endl;
  105. m_PathToExecutable = absolutePath;
  106. #if defined(_WIN32)
  107. m_PathToExecutable += "/Debug/CMake.exe";
  108. #else
  109. m_PathToExecutable += "/cmake";
  110. #endif
  111. std::cout << "Path to CMake executable = " << m_PathToExecutable << std::endl;
  112. }
  113. /**
  114. * Set the source path
  115. */
  116. bool
  117. CMakeSetupGUIImplementation
  118. ::SetSourcePath( const char * path )
  119. {
  120. if( !path || strlen(path)==0 )
  121. {
  122. fl_alert("Please select the path to the sources");
  123. return false;
  124. }
  125. std::string expandedAbsolutePath = ExpandPathAndMakeItAbsolute( path );
  126. sourcePathTextInput->value( expandedAbsolutePath.c_str() );
  127. if( VerifySourcePath( expandedAbsolutePath ) )
  128. {
  129. m_WhereSource = expandedAbsolutePath;
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * Expand environment variables in the path and make it absolute
  136. */
  137. std::string
  138. CMakeSetupGUIImplementation
  139. ::ExpandPathAndMakeItAbsolute( const std::string & inputPath ) const
  140. {
  141. char expandedPath[3000];
  142. filename_expand( expandedPath, inputPath.c_str() );
  143. char absolutePath[3000];
  144. filename_absolute( absolutePath, expandedPath );
  145. std::string expandedAbsolutePath = absolutePath;
  146. return expandedAbsolutePath;
  147. }
  148. /**
  149. * Set the binary path
  150. */
  151. bool
  152. CMakeSetupGUIImplementation
  153. ::SetBinaryPath( const char * path )
  154. {
  155. if( !path || strlen(path)==0 )
  156. {
  157. fl_alert("Please select the path to the binaries");
  158. return false;
  159. }
  160. std::string expandedAbsolutePath = ExpandPathAndMakeItAbsolute( path );
  161. binaryPathTextInput->value( expandedAbsolutePath.c_str() );
  162. if( !VerifyBinaryPath( expandedAbsolutePath.c_str() ) )
  163. {
  164. return false;
  165. }
  166. if( m_WhereBuild != expandedAbsolutePath )
  167. {
  168. m_BuildPathChanged = true;
  169. m_WhereBuild = expandedAbsolutePath;
  170. }
  171. LoadCacheFromDiskToGUI();
  172. return true;
  173. }
  174. /**
  175. * Verify the path to binaries
  176. */
  177. bool
  178. CMakeSetupGUIImplementation
  179. ::VerifyBinaryPath( const std::string & path ) const
  180. {
  181. bool pathIsOK = false;
  182. if( filename_isdir( path.c_str() ) )
  183. {
  184. pathIsOK = true;
  185. }
  186. else
  187. {
  188. int userWantsToCreateDirectory =
  189. fl_ask("The directory \n %s \n Doesn't exist. Do you want to create it ?",
  190. path.c_str() );
  191. if( userWantsToCreateDirectory )
  192. {
  193. std::string command = "mkdir ";
  194. command += path;
  195. system( command.c_str() );
  196. pathIsOK = true;
  197. }
  198. else
  199. {
  200. pathIsOK = false;
  201. }
  202. }
  203. return pathIsOK;
  204. }
  205. /**
  206. * Verify the path to sources
  207. */
  208. bool
  209. CMakeSetupGUIImplementation
  210. ::VerifySourcePath( const std::string & path ) const
  211. {
  212. if( !filename_isdir( path.c_str() ) )
  213. {
  214. fl_alert("The Source directory \n %s \n Doesn't exist or is not a directory", path.c_str() );
  215. return false;
  216. }
  217. return true;
  218. }
  219. /**
  220. * Build the project files
  221. */
  222. void
  223. CMakeSetupGUIImplementation
  224. ::BuildProjectFiles( void )
  225. {
  226. // Take and verify the source path from the GUI
  227. if( !SetSourcePath( sourcePathTextInput->value() ) )
  228. {
  229. return;
  230. }
  231. // Take and verify the binary path from the GUI
  232. if( !SetBinaryPath( binaryPathTextInput->value() ) )
  233. {
  234. return;
  235. }
  236. SaveCacheFromGUI();
  237. // set the wait cursor
  238. fl_cursor(FL_CURSOR_WAIT,FL_BLACK,FL_WHITE);
  239. // get all the info from the dialog
  240. // this->UpdateData();
  241. if(!m_BuildPathChanged)
  242. {
  243. // if the build path has not changed save the
  244. // current GUI values to the cache
  245. this->SaveCacheFromGUI();
  246. }
  247. // Make sure we are working from the cache on disk
  248. this->LoadCacheFromDiskToGUI();
  249. // create a cmake object
  250. cmake make;
  251. // create the arguments for the cmake object
  252. std::vector<std::string> args;
  253. args.push_back( m_PathToExecutable.c_str() );
  254. std::string arg;
  255. arg = "-H";
  256. arg += m_WhereSource;
  257. args.push_back(arg);
  258. arg = "-B";
  259. arg += m_WhereBuild;
  260. args.push_back(arg);
  261. // run the generate process
  262. if(make.Generate(args) != 0)
  263. {
  264. cmSystemTools::Error(
  265. "Error in generation process, project files may be invalid");
  266. }
  267. // update the GUI with any new values in the caused by the
  268. // generation process
  269. this->LoadCacheFromDiskToGUI();
  270. // path is up-to-date now
  271. m_BuildPathChanged = false;
  272. // put the cursor back
  273. fl_cursor(FL_CURSOR_DEFAULT,FL_BLACK,FL_WHITE);
  274. fl_message("Done !");
  275. }
  276. /**
  277. * Load Cache from disk to GUI
  278. */
  279. void
  280. CMakeSetupGUIImplementation
  281. ::LoadCacheFromDiskToGUI( void )
  282. {
  283. if( m_WhereBuild != "" )
  284. {
  285. cmCacheManager::GetInstance()->LoadCache( m_WhereBuild.c_str() );
  286. this->FillCacheGUIFromCacheManager();
  287. }
  288. }
  289. /**
  290. * Save Cache from disk to GUI
  291. */
  292. void
  293. CMakeSetupGUIImplementation
  294. ::SaveCacheFromGUI( void )
  295. {
  296. this->FillCacheManagerFromCacheGUI();
  297. if( m_WhereBuild != "" )
  298. {
  299. cmCacheManager::GetInstance()->SaveCache(
  300. m_WhereBuild.c_str() );
  301. }
  302. }
  303. /**
  304. * Fill Cache GUI from cache manager
  305. */
  306. void
  307. CMakeSetupGUIImplementation
  308. ::FillCacheGUIFromCacheManager( void )
  309. {
  310. // Prepare to add rows to the scroll
  311. propertyListPack->begin();
  312. const cmCacheManager::CacheEntryMap &cache =
  313. cmCacheManager::GetInstance()->GetCacheMap();
  314. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  315. i != cache.end(); ++i)
  316. {
  317. const char* key = i->first.c_str();
  318. const cmCacheManager::CacheEntry& value = i->second;
  319. switch(value.m_Type )
  320. {
  321. case cmCacheManager::BOOL:
  322. if(cmCacheManager::GetInstance()->IsOn(key))
  323. {
  324. m_CacheEntriesList.AddProperty(key,
  325. "ON",
  326. value.m_HelpString.c_str(),
  327. fltk::PropertyList::CHECKBOX,"");
  328. }
  329. else
  330. {
  331. m_CacheEntriesList.AddProperty(key,
  332. "OFF",
  333. value.m_HelpString.c_str(),
  334. fltk::PropertyList::CHECKBOX,"");
  335. }
  336. break;
  337. case cmCacheManager::PATH:
  338. m_CacheEntriesList.AddProperty(key,
  339. value.m_Value.c_str(),
  340. value.m_HelpString.c_str(),
  341. fltk::PropertyList::PATH,"");
  342. break;
  343. case cmCacheManager::FILEPATH:
  344. m_CacheEntriesList.AddProperty(key,
  345. value.m_Value.c_str(),
  346. value.m_HelpString.c_str(),
  347. fltk::PropertyList::FILE,"");
  348. break;
  349. case cmCacheManager::STRING:
  350. m_CacheEntriesList.AddProperty(key,
  351. value.m_Value.c_str(),
  352. value.m_HelpString.c_str(),
  353. fltk::PropertyList::EDIT,"");
  354. break;
  355. case cmCacheManager::INTERNAL:
  356. break;
  357. }
  358. }
  359. propertyListPack->end();
  360. propertyListPack->init_sizes();
  361. cacheValuesScroll->position( 0, 0 );
  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. }