cmGlobalGenerator.cxx 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #if defined(_WIN32) && !defined(__CYGWIN__)
  11. #include "windows.h" // this must be first to define GetCurrentDirectory
  12. #endif
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmExternalMakefileProjectGenerator.h"
  16. #include "cmake.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmVersion.h"
  20. #include "cmExportInstallFileGenerator.h"
  21. #include "cmComputeTargetDepends.h"
  22. #include "cmGeneratedFileStream.h"
  23. #include <cmsys/Directory.hxx>
  24. #if defined(CMAKE_BUILD_WITH_CMAKE)
  25. # include <cmsys/MD5.h>
  26. #endif
  27. #include <stdlib.h> // required for atof
  28. #include <assert.h>
  29. cmGlobalGenerator::cmGlobalGenerator()
  30. {
  31. // By default the .SYMBOLIC dependency is not needed on symbolic rules.
  32. this->NeedSymbolicMark = false;
  33. // by default use the native paths
  34. this->ForceUnixPaths = false;
  35. // By default do not try to support color.
  36. this->ToolSupportsColor = false;
  37. // By default do not use link scripts.
  38. this->UseLinkScript = false;
  39. // Whether an install target is needed.
  40. this->InstallTargetEnabled = false;
  41. // how long to let try compiles run
  42. this->TryCompileTimeout = 0;
  43. this->ExtraGenerator = 0;
  44. this->CurrentLocalGenerator = 0;
  45. this->TryCompileOuterMakefile = 0;
  46. }
  47. cmGlobalGenerator::~cmGlobalGenerator()
  48. {
  49. // Delete any existing cmLocalGenerators
  50. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  51. {
  52. delete this->LocalGenerators[i];
  53. }
  54. this->LocalGenerators.clear();
  55. if (this->ExtraGenerator)
  56. {
  57. delete this->ExtraGenerator;
  58. }
  59. this->ClearExportSets();
  60. }
  61. // Find the make program for the generator, required for try compiles
  62. void cmGlobalGenerator::FindMakeProgram(cmMakefile* mf)
  63. {
  64. if(this->FindMakeProgramFile.size() == 0)
  65. {
  66. cmSystemTools::Error(
  67. "Generator implementation error, "
  68. "all generators must specify this->FindMakeProgramFile");
  69. }
  70. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  71. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  72. {
  73. std::string setMakeProgram =
  74. mf->GetModulesFile(this->FindMakeProgramFile.c_str());
  75. if(setMakeProgram.size())
  76. {
  77. mf->ReadListFile(0, setMakeProgram.c_str());
  78. }
  79. }
  80. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  81. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  82. {
  83. cmOStringStream err;
  84. err << "CMake was unable to find a build program corresponding to \""
  85. << this->GetName() << "\". CMAKE_MAKE_PROGRAM is not set. You "
  86. << "probably need to select a different build tool.";
  87. cmSystemTools::Error(err.str().c_str());
  88. cmSystemTools::SetFatalErrorOccured();
  89. return;
  90. }
  91. std::string makeProgram = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  92. // if there are spaces in the make program use short path
  93. // but do not short path the actual program name, as
  94. // this can cause trouble with VSExpress
  95. if(makeProgram.find(' ') != makeProgram.npos)
  96. {
  97. std::string dir;
  98. std::string file;
  99. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  100. dir, file);
  101. std::string saveFile = file;
  102. cmSystemTools::GetShortPath(makeProgram.c_str(), makeProgram);
  103. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  104. dir, file);
  105. makeProgram = dir;
  106. makeProgram += "/";
  107. makeProgram += saveFile;
  108. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM", makeProgram.c_str(),
  109. "make program",
  110. cmCacheManager::FILEPATH);
  111. }
  112. if(makeProgram.find("xcodebuild") != makeProgram.npos)
  113. {
  114. // due to the text file busy /bin/sh problem with xcodebuild
  115. // use the cmakexbuild wrapper instead. This program
  116. // will run xcodebuild and if it sees the error text file busy
  117. // it will stop forwarding output, and let the build finish.
  118. // Then it will retry the build. It will continue this
  119. // untill no text file busy errors occur.
  120. std::string cmakexbuild =
  121. this->CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_COMMAND");
  122. cmakexbuild = cmakexbuild.substr(0, cmakexbuild.length()-5);
  123. cmakexbuild += "cmakexbuild";
  124. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM",
  125. cmakexbuild.c_str(),
  126. "make program",
  127. cmCacheManager::FILEPATH);
  128. }
  129. }
  130. // enable the given language
  131. //
  132. // The following files are loaded in this order:
  133. //
  134. // First figure out what OS we are running on:
  135. //
  136. // CMakeSystem.cmake - configured file created by CMakeDetermineSystem.cmake
  137. // CMakeDetermineSystem.cmake - figure out os info and create
  138. // CMakeSystem.cmake IF CMAKE_SYSTEM
  139. // not set
  140. // CMakeSystem.cmake - configured file created by
  141. // CMakeDetermineSystem.cmake IF CMAKE_SYSTEM_LOADED
  142. // Next try and enable all languages found in the languages vector
  143. //
  144. // FOREACH LANG in languages
  145. // CMake(LANG)Compiler.cmake - configured file create by
  146. // CMakeDetermine(LANG)Compiler.cmake
  147. // CMakeDetermine(LANG)Compiler.cmake - Finds compiler for LANG and
  148. // creates CMake(LANG)Compiler.cmake
  149. // CMake(LANG)Compiler.cmake - configured file created by
  150. // CMakeDetermine(LANG)Compiler.cmake
  151. //
  152. // CMakeSystemSpecificInformation.cmake
  153. // - includes Platform/${CMAKE_SYSTEM_NAME}.cmake
  154. // may use compiler stuff
  155. // FOREACH LANG in languages
  156. // CMake(LANG)Information.cmake
  157. // - loads Platform/${CMAKE_SYSTEM_NAME}-${COMPILER}.cmake
  158. // CMakeTest(LANG)Compiler.cmake
  159. // - Make sure the compiler works with a try compile if
  160. // CMakeDetermine(LANG) was loaded
  161. //
  162. // Now load a few files that can override values set in any of the above
  163. // (PROJECTNAME)Compatibility.cmake
  164. // - load any backwards compatibility stuff for current project
  165. // ${CMAKE_USER_MAKE_RULES_OVERRIDE}
  166. // - allow users a chance to override system variables
  167. //
  168. //
  169. void
  170. cmGlobalGenerator::EnableLanguage(std::vector<std::string>const& languages,
  171. cmMakefile *mf, bool)
  172. {
  173. if(languages.size() == 0)
  174. {
  175. cmSystemTools::Error("EnableLanguage must have a lang specified!");
  176. cmSystemTools::SetFatalErrorOccured();
  177. return;
  178. }
  179. if(this->TryCompileOuterMakefile)
  180. {
  181. // In a try-compile we can only enable languages provided by caller.
  182. for(std::vector<std::string>::const_iterator li = languages.begin();
  183. li != languages.end(); ++li)
  184. {
  185. if(*li == "NONE")
  186. {
  187. this->SetLanguageEnabled("NONE", mf);
  188. }
  189. else
  190. {
  191. const char* lang = li->c_str();
  192. if(this->LanguagesReady.find(lang) == this->LanguagesReady.end())
  193. {
  194. cmOStringStream e;
  195. e << "The test project needs language "
  196. << lang << " which is not enabled.";
  197. this->TryCompileOuterMakefile
  198. ->IssueMessage(cmake::FATAL_ERROR, e.str());
  199. cmSystemTools::SetFatalErrorOccured();
  200. return;
  201. }
  202. }
  203. }
  204. }
  205. mf->AddDefinition("RUN_CONFIGURE", true);
  206. std::string rootBin = mf->GetHomeOutputDirectory();
  207. rootBin += cmake::GetCMakeFilesDirectory();
  208. // If the configuration files path has been set,
  209. // then we are in a try compile and need to copy the enable language
  210. // files from the parent cmake bin dir, into the try compile bin dir
  211. if(this->ConfiguredFilesPath.size())
  212. {
  213. rootBin = this->ConfiguredFilesPath;
  214. }
  215. // set the dir for parent files so they can be used by modules
  216. mf->AddDefinition("CMAKE_PLATFORM_ROOT_BIN",rootBin.c_str());
  217. // find and make sure CMAKE_MAKE_PROGRAM is defined
  218. this->FindMakeProgram(mf);
  219. // try and load the CMakeSystem.cmake if it is there
  220. std::string fpath = rootBin;
  221. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  222. {
  223. fpath += "/CMakeSystem.cmake";
  224. if(cmSystemTools::FileExists(fpath.c_str()))
  225. {
  226. mf->ReadListFile(0,fpath.c_str());
  227. }
  228. }
  229. // Load the CMakeDetermineSystem.cmake file and find out
  230. // what platform we are running on
  231. if (!mf->GetDefinition("CMAKE_SYSTEM"))
  232. {
  233. #if defined(_WIN32) && !defined(__CYGWIN__)
  234. /* Windows version number data. */
  235. OSVERSIONINFO osvi;
  236. ZeroMemory(&osvi, sizeof(osvi));
  237. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  238. GetVersionEx (&osvi);
  239. cmOStringStream windowsVersionString;
  240. windowsVersionString << osvi.dwMajorVersion << "." << osvi.dwMinorVersion;
  241. windowsVersionString.str();
  242. mf->AddDefinition("CMAKE_HOST_SYSTEM_VERSION",
  243. windowsVersionString.str().c_str());
  244. #endif
  245. // Read the DetermineSystem file
  246. std::string systemFile = mf->GetModulesFile("CMakeDetermineSystem.cmake");
  247. mf->ReadListFile(0, systemFile.c_str());
  248. // load the CMakeSystem.cmake from the binary directory
  249. // this file is configured by the CMakeDetermineSystem.cmake file
  250. fpath = rootBin;
  251. fpath += "/CMakeSystem.cmake";
  252. mf->ReadListFile(0,fpath.c_str());
  253. }
  254. std::map<cmStdString, bool> needTestLanguage;
  255. std::map<cmStdString, bool> needSetLanguageEnabledMaps;
  256. // foreach language
  257. // load the CMakeDetermine(LANG)Compiler.cmake file to find
  258. // the compiler
  259. for(std::vector<std::string>::const_iterator l = languages.begin();
  260. l != languages.end(); ++l)
  261. {
  262. const char* lang = l->c_str();
  263. needSetLanguageEnabledMaps[lang] = false;
  264. if(*l == "NONE")
  265. {
  266. this->SetLanguageEnabled("NONE", mf);
  267. continue;
  268. }
  269. std::string loadedLang = "CMAKE_";
  270. loadedLang += lang;
  271. loadedLang += "_COMPILER_LOADED";
  272. // If the existing build tree was already configured with this
  273. // version of CMake then try to load the configured file first
  274. // to avoid duplicate compiler tests.
  275. unsigned int cacheMajor = mf->GetCacheMajorVersion();
  276. unsigned int cacheMinor = mf->GetCacheMinorVersion();
  277. unsigned int selfMajor = cmVersion::GetMajorVersion();
  278. unsigned int selfMinor = cmVersion::GetMinorVersion();
  279. if((this->CMakeInstance->GetIsInTryCompile() ||
  280. (selfMajor == cacheMajor && selfMinor == cacheMinor))
  281. && !mf->GetDefinition(loadedLang.c_str()))
  282. {
  283. fpath = rootBin;
  284. fpath += "/CMake";
  285. fpath += lang;
  286. fpath += "Compiler.cmake";
  287. if(cmSystemTools::FileExists(fpath.c_str()))
  288. {
  289. if(!mf->ReadListFile(0,fpath.c_str()))
  290. {
  291. cmSystemTools::Error("Could not find cmake module file:",
  292. fpath.c_str());
  293. }
  294. // if this file was found then the language was already determined
  295. // to be working
  296. needTestLanguage[lang] = false;
  297. this->SetLanguageEnabledFlag(lang, mf);
  298. needSetLanguageEnabledMaps[lang] = true;
  299. // this can only be called after loading CMake(LANG)Compiler.cmake
  300. }
  301. }
  302. if(!this->GetLanguageEnabled(lang) )
  303. {
  304. if (this->CMakeInstance->GetIsInTryCompile())
  305. {
  306. cmSystemTools::Error("This should not have happen. "
  307. "If you see this message, you are probably "
  308. "using a broken CMakeLists.txt file or a "
  309. "problematic release of CMake");
  310. }
  311. // if the CMake(LANG)Compiler.cmake file was not found then
  312. // load CMakeDetermine(LANG)Compiler.cmake
  313. std::string determineCompiler = "CMakeDetermine";
  314. determineCompiler += lang;
  315. determineCompiler += "Compiler.cmake";
  316. std::string determineFile =
  317. mf->GetModulesFile(determineCompiler.c_str());
  318. if(!mf->ReadListFile(0,determineFile.c_str()))
  319. {
  320. cmSystemTools::Error("Could not find cmake module file:",
  321. determineFile.c_str());
  322. }
  323. needTestLanguage[lang] = true;
  324. // Some generators like visual studio should not use the env variables
  325. // So the global generator can specify that in this variable
  326. if(!mf->GetDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV"))
  327. {
  328. // put ${CMake_(LANG)_COMPILER_ENV_VAR}=${CMAKE_(LANG)_COMPILER
  329. // into the environment, in case user scripts want to run
  330. // configure, or sub cmakes
  331. std::string compilerName = "CMAKE_";
  332. compilerName += lang;
  333. compilerName += "_COMPILER";
  334. std::string compilerEnv = "CMAKE_";
  335. compilerEnv += lang;
  336. compilerEnv += "_COMPILER_ENV_VAR";
  337. std::string envVar = mf->GetRequiredDefinition(compilerEnv.c_str());
  338. std::string envVarValue =
  339. mf->GetRequiredDefinition(compilerName.c_str());
  340. std::string env = envVar;
  341. env += "=";
  342. env += envVarValue;
  343. cmSystemTools::PutEnv(env.c_str());
  344. }
  345. // if determineLanguage was called then load the file it
  346. // configures CMake(LANG)Compiler.cmake
  347. fpath = rootBin;
  348. fpath += "/CMake";
  349. fpath += lang;
  350. fpath += "Compiler.cmake";
  351. if(!mf->ReadListFile(0,fpath.c_str()))
  352. {
  353. cmSystemTools::Error("Could not find cmake module file:",
  354. fpath.c_str());
  355. }
  356. this->SetLanguageEnabledFlag(lang, mf);
  357. needSetLanguageEnabledMaps[lang] = true;
  358. // this can only be called after loading CMake(LANG)Compiler.cmake
  359. // the language must be enabled for try compile to work, but we do
  360. // not know if it is a working compiler yet so set the test language
  361. // flag
  362. needTestLanguage[lang] = true;
  363. } // end if(!this->GetLanguageEnabled(lang) )
  364. } // end loop over languages
  365. // **** Load the system specific information if not yet loaded
  366. if (!mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  367. {
  368. fpath = mf->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  369. if(!mf->ReadListFile(0,fpath.c_str()))
  370. {
  371. cmSystemTools::Error("Could not find cmake module file:",
  372. fpath.c_str());
  373. }
  374. }
  375. // loop over languages again loading CMake(LANG)Information.cmake
  376. //
  377. for(std::vector<std::string>::const_iterator l = languages.begin();
  378. l != languages.end(); ++l)
  379. {
  380. const char* lang = l->c_str();
  381. if(*l == "NONE")
  382. {
  383. this->SetLanguageEnabled("NONE", mf);
  384. continue;
  385. }
  386. std::string langLoadedVar = "CMAKE_";
  387. langLoadedVar += lang;
  388. langLoadedVar += "_INFORMATION_LOADED";
  389. if (!mf->GetDefinition(langLoadedVar.c_str()))
  390. {
  391. fpath = "CMake";
  392. fpath += lang;
  393. fpath += "Information.cmake";
  394. std::string informationFile = mf->GetModulesFile(fpath.c_str());
  395. if (informationFile.empty())
  396. {
  397. cmSystemTools::Error("Could not find cmake module file:",
  398. fpath.c_str());
  399. }
  400. else if(!mf->ReadListFile(0, informationFile.c_str()))
  401. {
  402. cmSystemTools::Error("Could not process cmake module file:",
  403. informationFile.c_str());
  404. }
  405. }
  406. if (needSetLanguageEnabledMaps[lang])
  407. {
  408. this->SetLanguageEnabledMaps(lang, mf);
  409. }
  410. this->LanguagesReady.insert(lang);
  411. std::string compilerName = "CMAKE_";
  412. compilerName += lang;
  413. compilerName += "_COMPILER";
  414. std::string compilerLangFile = rootBin;
  415. compilerLangFile += "/CMake";
  416. compilerLangFile += lang;
  417. compilerLangFile += "Compiler.cmake";
  418. // Test the compiler for the language just setup
  419. // (but only if a compiler has been actually found)
  420. // At this point we should have enough info for a try compile
  421. // which is used in the backward stuff
  422. // If the language is untested then test it now with a try compile.
  423. if (!mf->IsSet(compilerName.c_str()))
  424. {
  425. // if the compiler did not work, then remove the
  426. // CMake(LANG)Compiler.cmake file so that it will get tested the
  427. // next time cmake is run
  428. cmSystemTools::RemoveFile(compilerLangFile.c_str());
  429. }
  430. else if(needTestLanguage[lang])
  431. {
  432. if (!this->CMakeInstance->GetIsInTryCompile())
  433. {
  434. std::string testLang = "CMakeTest";
  435. testLang += lang;
  436. testLang += "Compiler.cmake";
  437. std::string ifpath = mf->GetModulesFile(testLang.c_str());
  438. if(!mf->ReadListFile(0,ifpath.c_str()))
  439. {
  440. cmSystemTools::Error("Could not find cmake module file:",
  441. ifpath.c_str());
  442. }
  443. std::string compilerWorks = "CMAKE_";
  444. compilerWorks += lang;
  445. compilerWorks += "_COMPILER_WORKS";
  446. // if the compiler did not work, then remove the
  447. // CMake(LANG)Compiler.cmake file so that it will get tested the
  448. // next time cmake is run
  449. if(!mf->IsOn(compilerWorks.c_str()))
  450. {
  451. cmSystemTools::RemoveFile(compilerLangFile.c_str());
  452. }
  453. else
  454. {
  455. // load backwards compatibility stuff for C and CXX
  456. // for old versions of CMake ListFiles C and CXX had some
  457. // backwards compatibility files they have to load
  458. // These files have a bunch of try compiles in them so
  459. // should only be done
  460. if (mf->NeedBackwardsCompatibility(1,4))
  461. {
  462. if(strcmp(lang, "C") == 0)
  463. {
  464. ifpath =
  465. mf->GetModulesFile("CMakeBackwardCompatibilityC.cmake");
  466. mf->ReadListFile(0,ifpath.c_str());
  467. }
  468. if(strcmp(lang, "CXX") == 0)
  469. {
  470. ifpath =
  471. mf->GetModulesFile("CMakeBackwardCompatibilityCXX.cmake");
  472. mf->ReadListFile(0,ifpath.c_str());
  473. }
  474. }
  475. }
  476. } // end if in try compile
  477. } // end need test language
  478. } // end for each language
  479. // Now load files that can override any settings on the platform or for
  480. // the project First load the project compatibility file if it is in
  481. // cmake
  482. std::string projectCompatibility = mf->GetDefinition("CMAKE_ROOT");
  483. projectCompatibility += "/Modules/";
  484. projectCompatibility += mf->GetSafeDefinition("PROJECT_NAME");
  485. projectCompatibility += "Compatibility.cmake";
  486. if(cmSystemTools::FileExists(projectCompatibility.c_str()))
  487. {
  488. mf->ReadListFile(0,projectCompatibility.c_str());
  489. }
  490. }
  491. //----------------------------------------------------------------------------
  492. const char*
  493. cmGlobalGenerator::GetLanguageOutputExtension(cmSourceFile const& source)
  494. {
  495. if(const char* lang = source.GetLanguage())
  496. {
  497. if(this->LanguageToOutputExtension.count(lang) > 0)
  498. {
  499. return this->LanguageToOutputExtension[lang].c_str();
  500. }
  501. }
  502. else
  503. {
  504. // if no language is found then check to see if it is already an
  505. // ouput extension for some language. In that case it should be ignored
  506. // and in this map, so it will not be compiled but will just be used.
  507. std::string const& ext = source.GetExtension();
  508. if(!ext.empty())
  509. {
  510. if(this->OutputExtensions.count(ext))
  511. {
  512. return ext.c_str();
  513. }
  514. }
  515. }
  516. return "";
  517. }
  518. const char* cmGlobalGenerator::GetLanguageFromExtension(const char* ext)
  519. {
  520. // if there is an extension and it starts with . then move past the
  521. // . because the extensions are not stored with a . in the map
  522. if(ext && *ext == '.')
  523. {
  524. ++ext;
  525. }
  526. if(this->ExtensionToLanguage.count(ext) > 0)
  527. {
  528. return this->ExtensionToLanguage[ext].c_str();
  529. }
  530. return 0;
  531. }
  532. /* SetLanguageEnabled() is now split in two parts:
  533. at first the enabled-flag is set. This can then be used in EnabledLanguage()
  534. for checking whether the language is already enabled. After setting this
  535. flag still the values from the cmake variables have to be copied into the
  536. internal maps, this is done in SetLanguageEnabledMaps() which is called
  537. after the system- and compiler specific files have been loaded.
  538. This split was done originally so that compiler-specific configuration
  539. files could change the object file extension
  540. (CMAKE_<LANG>_OUTPUT_EXTENSION) before the CMake variables were copied
  541. to the C++ maps.
  542. */
  543. void cmGlobalGenerator::SetLanguageEnabled(const char* l, cmMakefile* mf)
  544. {
  545. this->SetLanguageEnabledFlag(l, mf);
  546. this->SetLanguageEnabledMaps(l, mf);
  547. }
  548. void cmGlobalGenerator::SetLanguageEnabledFlag(const char* l, cmMakefile* mf)
  549. {
  550. this->LanguageEnabled[l] = true;
  551. // Fill the language-to-extension map with the current variable
  552. // settings to make sure it is available for the try_compile()
  553. // command source file signature. In SetLanguageEnabledMaps this
  554. // will be done again to account for any compiler- or
  555. // platform-specific entries.
  556. this->FillExtensionToLanguageMap(l, mf);
  557. }
  558. void cmGlobalGenerator::SetLanguageEnabledMaps(const char* l, cmMakefile* mf)
  559. {
  560. // use LanguageToLinkerPreference to detect whether this functions has
  561. // run before
  562. if (this->LanguageToLinkerPreference.find(l) !=
  563. this->LanguageToLinkerPreference.end())
  564. {
  565. return;
  566. }
  567. std::string linkerPrefVar = std::string("CMAKE_") +
  568. std::string(l) + std::string("_LINKER_PREFERENCE");
  569. const char* linkerPref = mf->GetDefinition(linkerPrefVar.c_str());
  570. int preference = 0;
  571. if(linkerPref)
  572. {
  573. if (sscanf(linkerPref, "%d", &preference)!=1)
  574. {
  575. // backward compatibility: before 2.6 LINKER_PREFERENCE
  576. // was either "None" or "Prefered", and only the first character was
  577. // tested. So if there is a custom language out there and it is
  578. // "Prefered", set its preference high
  579. if (linkerPref[0]=='P')
  580. {
  581. preference = 100;
  582. }
  583. else
  584. {
  585. preference = 0;
  586. }
  587. }
  588. }
  589. if (preference < 0)
  590. {
  591. std::string msg = linkerPrefVar;
  592. msg += " is negative, adjusting it to 0";
  593. cmSystemTools::Message(msg.c_str(), "Warning");
  594. preference = 0;
  595. }
  596. this->LanguageToLinkerPreference[l] = preference;
  597. std::string outputExtensionVar = std::string("CMAKE_") +
  598. std::string(l) + std::string("_OUTPUT_EXTENSION");
  599. const char* outputExtension = mf->GetDefinition(outputExtensionVar.c_str());
  600. if(outputExtension)
  601. {
  602. this->LanguageToOutputExtension[l] = outputExtension;
  603. this->OutputExtensions[outputExtension] = outputExtension;
  604. if(outputExtension[0] == '.')
  605. {
  606. this->OutputExtensions[outputExtension+1] = outputExtension+1;
  607. }
  608. }
  609. // The map was originally filled by SetLanguageEnabledFlag, but
  610. // since then the compiler- and platform-specific files have been
  611. // loaded which might have added more entries.
  612. this->FillExtensionToLanguageMap(l, mf);
  613. std::string ignoreExtensionsVar = std::string("CMAKE_") +
  614. std::string(l) + std::string("_IGNORE_EXTENSIONS");
  615. std::string ignoreExts = mf->GetSafeDefinition(ignoreExtensionsVar.c_str());
  616. std::vector<std::string> extensionList;
  617. cmSystemTools::ExpandListArgument(ignoreExts, extensionList);
  618. for(std::vector<std::string>::iterator i = extensionList.begin();
  619. i != extensionList.end(); ++i)
  620. {
  621. this->IgnoreExtensions[*i] = true;
  622. }
  623. }
  624. void cmGlobalGenerator::FillExtensionToLanguageMap(const char* l,
  625. cmMakefile* mf)
  626. {
  627. std::string extensionsVar = std::string("CMAKE_") +
  628. std::string(l) + std::string("_SOURCE_FILE_EXTENSIONS");
  629. std::string exts = mf->GetSafeDefinition(extensionsVar.c_str());
  630. std::vector<std::string> extensionList;
  631. cmSystemTools::ExpandListArgument(exts, extensionList);
  632. for(std::vector<std::string>::iterator i = extensionList.begin();
  633. i != extensionList.end(); ++i)
  634. {
  635. this->ExtensionToLanguage[*i] = l;
  636. }
  637. }
  638. bool cmGlobalGenerator::IgnoreFile(const char* l)
  639. {
  640. if(this->GetLanguageFromExtension(l))
  641. {
  642. return false;
  643. }
  644. return (this->IgnoreExtensions.count(l) > 0);
  645. }
  646. bool cmGlobalGenerator::GetLanguageEnabled(const char* l) const
  647. {
  648. return (this->LanguageEnabled.find(l)!= this->LanguageEnabled.end());
  649. }
  650. void cmGlobalGenerator::ClearEnabledLanguages()
  651. {
  652. this->LanguageEnabled.clear();
  653. }
  654. bool cmGlobalGenerator::IsDependedOn(const char* project,
  655. cmTarget* targetIn)
  656. {
  657. // Get all local gens for this project
  658. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  659. // loop over local gens and get the targets for each one
  660. for(unsigned int i = 0; i < gens->size(); ++i)
  661. {
  662. cmTargets& targets = (*gens)[i]->GetMakefile()->GetTargets();
  663. for (cmTargets::iterator l = targets.begin();
  664. l != targets.end(); l++)
  665. {
  666. cmTarget& target = l->second;
  667. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  668. if(tgtdeps.count(targetIn))
  669. {
  670. return true;
  671. }
  672. }
  673. }
  674. return false;
  675. }
  676. void cmGlobalGenerator::Configure()
  677. {
  678. this->FirstTimeProgress = 0.0f;
  679. this->ClearExportSets();
  680. // Delete any existing cmLocalGenerators
  681. unsigned int i;
  682. for (i = 0; i < this->LocalGenerators.size(); ++i)
  683. {
  684. delete this->LocalGenerators[i];
  685. }
  686. this->LocalGenerators.clear();
  687. this->TargetDependencies.clear();
  688. this->TotalTargets.clear();
  689. this->LocalGeneratorToTargetMap.clear();
  690. this->ProjectMap.clear();
  691. this->RuleHashes.clear();
  692. this->DirectoryContentMap.clear();
  693. this->BinaryDirectories.clear();
  694. // start with this directory
  695. cmLocalGenerator *lg = this->CreateLocalGenerator();
  696. this->LocalGenerators.push_back(lg);
  697. // set the Start directories
  698. cmMakefile* mf = lg->GetMakefile();
  699. lg->GetMakefile()->SetStartDirectory
  700. (this->CMakeInstance->GetStartDirectory());
  701. lg->GetMakefile()->SetStartOutputDirectory
  702. (this->CMakeInstance->GetStartOutputDirectory());
  703. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  704. this->BinaryDirectories.insert(mf->GetStartOutputDirectory());
  705. // now do it
  706. lg->Configure();
  707. // update the cache entry for the number of local generators, this is used
  708. // for progress
  709. char num[100];
  710. sprintf(num,"%d",static_cast<int>(this->LocalGenerators.size()));
  711. this->GetCMakeInstance()->AddCacheEntry
  712. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS", num,
  713. "number of local generators", cmCacheManager::INTERNAL);
  714. // check for link libraries and include directories containing "NOTFOUND"
  715. // and for infinite loops
  716. this->CheckLocalGenerators();
  717. // at this point this->LocalGenerators has been filled,
  718. // so create the map from project name to vector of local generators
  719. this->FillProjectMap();
  720. if ( !this->CMakeInstance->GetScriptMode() )
  721. {
  722. const char* msg = "Configuring done";
  723. if(cmSystemTools::GetErrorOccuredFlag())
  724. {
  725. msg = "Configuring incomplete, errors occurred!";
  726. }
  727. this->CMakeInstance->UpdateProgress(msg, -1);
  728. }
  729. }
  730. bool cmGlobalGenerator::CheckALLOW_DUPLICATE_CUSTOM_TARGETS()
  731. {
  732. // If the property is not enabled then okay.
  733. if(!this->CMakeInstance
  734. ->GetPropertyAsBool("ALLOW_DUPLICATE_CUSTOM_TARGETS"))
  735. {
  736. return true;
  737. }
  738. // This generator does not support duplicate custom targets.
  739. cmOStringStream e;
  740. e << "This project has enabled the ALLOW_DUPLICATE_CUSTOM_TARGETS "
  741. << "global property. "
  742. << "The \"" << this->GetName() << "\" generator does not support "
  743. << "duplicate custom targets. "
  744. << "Consider using a Makefiles generator or fix the project to not "
  745. << "use duplicat target names.";
  746. cmSystemTools::Error(e.str().c_str());
  747. return false;
  748. }
  749. void cmGlobalGenerator::Generate()
  750. {
  751. // Some generators track files replaced during the Generate.
  752. // Start with an empty vector:
  753. this->FilesReplacedDuringGenerate.clear();
  754. // Check whether this generator is allowed to run.
  755. if(!this->CheckALLOW_DUPLICATE_CUSTOM_TARGETS())
  756. {
  757. return;
  758. }
  759. // Check that all targets are valid.
  760. if(!this->CheckTargets())
  761. {
  762. return;
  763. }
  764. // For each existing cmLocalGenerator
  765. unsigned int i;
  766. // Put a copy of each global target in every directory.
  767. cmTargets globalTargets;
  768. this->CreateDefaultGlobalTargets(&globalTargets);
  769. for (i = 0; i < this->LocalGenerators.size(); ++i)
  770. {
  771. cmMakefile* mf = this->LocalGenerators[i]->GetMakefile();
  772. cmTargets* targets = &(mf->GetTargets());
  773. cmTargets::iterator tit;
  774. for ( tit = globalTargets.begin(); tit != globalTargets.end(); ++ tit )
  775. {
  776. (*targets)[tit->first] = tit->second;
  777. (*targets)[tit->first].SetMakefile(mf);
  778. }
  779. }
  780. // Add generator specific helper commands
  781. for (i = 0; i < this->LocalGenerators.size(); ++i)
  782. {
  783. this->LocalGenerators[i]->AddHelperCommands();
  784. }
  785. // Trace the dependencies, after that no custom commands should be added
  786. // because their dependencies might not be handled correctly
  787. for (i = 0; i < this->LocalGenerators.size(); ++i)
  788. {
  789. this->LocalGenerators[i]->TraceDependencies();
  790. }
  791. // Compute the manifest of main targets generated.
  792. for (i = 0; i < this->LocalGenerators.size(); ++i)
  793. {
  794. this->LocalGenerators[i]->GenerateTargetManifest();
  795. }
  796. // Compute the inter-target dependencies.
  797. if(!this->ComputeTargetDepends())
  798. {
  799. return;
  800. }
  801. // Create a map from local generator to the complete set of targets
  802. // it builds by default.
  803. this->FillLocalGeneratorToTargetMap();
  804. // Generate project files
  805. for (i = 0; i < this->LocalGenerators.size(); ++i)
  806. {
  807. this->SetCurrentLocalGenerator(this->LocalGenerators[i]);
  808. this->LocalGenerators[i]->Generate();
  809. this->LocalGenerators[i]->GenerateInstallRules();
  810. this->LocalGenerators[i]->GenerateTestFiles();
  811. this->CMakeInstance->UpdateProgress("Generating",
  812. (static_cast<float>(i)+1.0f)/
  813. static_cast<float>(this->LocalGenerators.size()));
  814. }
  815. this->SetCurrentLocalGenerator(0);
  816. // Update rule hashes.
  817. this->CheckRuleHashes();
  818. this->WriteSummary();
  819. if (this->ExtraGenerator != 0)
  820. {
  821. this->ExtraGenerator->Generate();
  822. }
  823. this->CMakeInstance->UpdateProgress("Generating done", -1);
  824. this->CMakeInstance->RunCheckForUnusedVariables("generation");
  825. }
  826. //----------------------------------------------------------------------------
  827. bool cmGlobalGenerator::ComputeTargetDepends()
  828. {
  829. cmComputeTargetDepends ctd(this);
  830. if(!ctd.Compute())
  831. {
  832. return false;
  833. }
  834. std::vector<cmTarget*> const& targets = ctd.GetTargets();
  835. for(std::vector<cmTarget*>::const_iterator ti = targets.begin();
  836. ti != targets.end(); ++ti)
  837. {
  838. ctd.GetTargetDirectDepends(*ti, this->TargetDependencies[*ti]);
  839. }
  840. return true;
  841. }
  842. //----------------------------------------------------------------------------
  843. bool cmGlobalGenerator::CheckTargets()
  844. {
  845. // Make sure all targets can find their source files.
  846. for(unsigned int i=0; i < this->LocalGenerators.size(); ++i)
  847. {
  848. cmTargets& targets =
  849. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  850. for(cmTargets::iterator ti = targets.begin();
  851. ti != targets.end(); ++ti)
  852. {
  853. cmTarget& target = ti->second;
  854. if(target.GetType() == cmTarget::EXECUTABLE ||
  855. target.GetType() == cmTarget::STATIC_LIBRARY ||
  856. target.GetType() == cmTarget::SHARED_LIBRARY ||
  857. target.GetType() == cmTarget::MODULE_LIBRARY ||
  858. target.GetType() == cmTarget::UTILITY)
  859. {
  860. if(!target.FindSourceFiles())
  861. {
  862. return false;
  863. }
  864. }
  865. }
  866. }
  867. return true;
  868. }
  869. void cmGlobalGenerator::CheckLocalGenerators()
  870. {
  871. std::map<cmStdString, cmStdString> notFoundMap;
  872. // std::set<cmStdString> notFoundMap;
  873. // after it is all done do a ConfigureFinalPass
  874. cmCacheManager* manager = 0;
  875. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  876. {
  877. manager = this->LocalGenerators[i]->GetMakefile()->GetCacheManager();
  878. this->LocalGenerators[i]->ConfigureFinalPass();
  879. const cmTargets & targets =
  880. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  881. for (cmTargets::const_iterator l = targets.begin();
  882. l != targets.end(); l++)
  883. {
  884. const cmTarget::LinkLibraryVectorType& libs =
  885. l->second.GetOriginalLinkLibraries();
  886. for(cmTarget::LinkLibraryVectorType::const_iterator lib = libs.begin();
  887. lib != libs.end(); ++lib)
  888. {
  889. if(lib->first.size() > 9 &&
  890. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  891. {
  892. std::string varName = lib->first.substr(0, lib->first.size()-9);
  893. cmCacheManager::CacheIterator it =
  894. manager->GetCacheIterator(varName.c_str());
  895. if(it.GetPropertyAsBool("ADVANCED"))
  896. {
  897. varName += " (ADVANCED)";
  898. }
  899. std::string text = notFoundMap[varName];
  900. text += "\n linked by target \"";
  901. text += l->second.GetName();
  902. text += "\" in directory ";
  903. text+=this->LocalGenerators[i]->GetMakefile()->GetCurrentDirectory();
  904. notFoundMap[varName] = text;
  905. }
  906. }
  907. }
  908. const std::vector<std::string>& incs =
  909. this->LocalGenerators[i]->GetMakefile()->GetIncludeDirectories();
  910. for( std::vector<std::string>::const_iterator incDir = incs.begin();
  911. incDir != incs.end(); ++incDir)
  912. {
  913. if(incDir->size() > 9 &&
  914. cmSystemTools::IsNOTFOUND(incDir->c_str()))
  915. {
  916. std::string varName = incDir->substr(0, incDir->size()-9);
  917. cmCacheManager::CacheIterator it =
  918. manager->GetCacheIterator(varName.c_str());
  919. if(it.GetPropertyAsBool("ADVANCED"))
  920. {
  921. varName += " (ADVANCED)";
  922. }
  923. std::string text = notFoundMap[varName];
  924. text += "\n used as include directory in directory ";
  925. text += this->LocalGenerators[i]->GetMakefile()->GetCurrentDirectory();
  926. notFoundMap[varName] = text;
  927. }
  928. }
  929. this->CMakeInstance->UpdateProgress
  930. ("Configuring", 0.9f+0.1f*(static_cast<float>(i)+1.0f)/
  931. static_cast<float>(this->LocalGenerators.size()));
  932. }
  933. if(notFoundMap.size())
  934. {
  935. std::string notFoundVars;
  936. for(std::map<cmStdString, cmStdString>::const_iterator
  937. ii = notFoundMap.begin();
  938. ii != notFoundMap.end();
  939. ++ii)
  940. {
  941. notFoundVars += ii->first;
  942. notFoundVars += ii->second;
  943. notFoundVars += "\n";
  944. }
  945. cmSystemTools::Error("The following variables are used in this project, "
  946. "but they are set to NOTFOUND.\n"
  947. "Please set them or make sure they are set and "
  948. "tested correctly in the CMake files:\n",
  949. notFoundVars.c_str());
  950. }
  951. }
  952. int cmGlobalGenerator::TryCompile(const char *srcdir, const char *bindir,
  953. const char *projectName,
  954. const char *target, bool fast,
  955. std::string *output, cmMakefile *mf)
  956. {
  957. // if this is not set, then this is a first time configure
  958. // and there is a good chance that the try compile stuff will
  959. // take the bulk of the time, so try and guess some progress
  960. // by getting closer and closer to 100 without actually getting there.
  961. if (!this->CMakeInstance->GetCacheManager()->GetCacheValue
  962. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS"))
  963. {
  964. // If CMAKE_NUMBER_OF_LOCAL_GENERATORS is not set
  965. // we are in the first time progress and we have no
  966. // idea how long it will be. So, just move 1/10th of the way
  967. // there each time, and don't go over 95%
  968. this->FirstTimeProgress += ((1.0f - this->FirstTimeProgress) /30.0f);
  969. if(this->FirstTimeProgress > 0.95f)
  970. {
  971. this->FirstTimeProgress = 0.95f;
  972. }
  973. this->CMakeInstance->UpdateProgress("Configuring",
  974. this->FirstTimeProgress);
  975. }
  976. std::string makeCommand = this->CMakeInstance->
  977. GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  978. if(makeCommand.size() == 0)
  979. {
  980. cmSystemTools::Error(
  981. "Generator cannot find the appropriate make command.");
  982. return 1;
  983. }
  984. std::string newTarget;
  985. if (target && strlen(target))
  986. {
  987. newTarget += target;
  988. #if 0
  989. #if defined(_WIN32) || defined(__CYGWIN__)
  990. std::string tmp = target;
  991. // if the target does not already end in . something
  992. // then assume .exe
  993. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  994. {
  995. newTarget += ".exe";
  996. }
  997. #endif // WIN32
  998. #endif
  999. }
  1000. const char* config = mf->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  1001. return this->Build(srcdir,bindir,projectName,
  1002. newTarget.c_str(),
  1003. output,makeCommand.c_str(),config,false,fast,
  1004. this->TryCompileTimeout);
  1005. }
  1006. std::string cmGlobalGenerator
  1007. ::GenerateBuildCommand(const char* makeProgram, const char *projectName,
  1008. const char* additionalOptions, const char *targetName,
  1009. const char* config, bool ignoreErrors, bool)
  1010. {
  1011. // Project name and config are not used yet.
  1012. (void)projectName;
  1013. (void)config;
  1014. std::string makeCommand =
  1015. cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  1016. // Since we have full control over the invocation of nmake, let us
  1017. // make it quiet.
  1018. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  1019. {
  1020. makeCommand += " /NOLOGO ";
  1021. }
  1022. if ( ignoreErrors )
  1023. {
  1024. makeCommand += " -i";
  1025. }
  1026. if ( additionalOptions )
  1027. {
  1028. makeCommand += " ";
  1029. makeCommand += additionalOptions;
  1030. }
  1031. if ( targetName )
  1032. {
  1033. makeCommand += " ";
  1034. makeCommand += targetName;
  1035. }
  1036. return makeCommand;
  1037. }
  1038. int cmGlobalGenerator::Build(
  1039. const char *, const char *bindir,
  1040. const char *projectName, const char *target,
  1041. std::string *output,
  1042. const char *makeCommandCSTR,
  1043. const char *config,
  1044. bool clean, bool fast,
  1045. double timeout,
  1046. bool verbose,
  1047. const char* extraOptions,
  1048. std::vector<std::string> const& nativeOptions)
  1049. {
  1050. /**
  1051. * Run an executable command and put the stdout in output.
  1052. */
  1053. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1054. cmSystemTools::ChangeDirectory(bindir);
  1055. if(output)
  1056. {
  1057. *output += "Change Dir: ";
  1058. *output += bindir;
  1059. *output += "\n";
  1060. }
  1061. int retVal;
  1062. bool hideconsole = cmSystemTools::GetRunCommandHideConsole();
  1063. cmSystemTools::SetRunCommandHideConsole(true);
  1064. std::string outputBuffer;
  1065. std::string* outputPtr = 0;
  1066. if(output)
  1067. {
  1068. outputPtr = &outputBuffer;
  1069. }
  1070. // should we do a clean first?
  1071. if (clean)
  1072. {
  1073. std::string cleanCommand =
  1074. this->GenerateBuildCommand(makeCommandCSTR, projectName,
  1075. 0, "clean", config, false, fast);
  1076. if(output)
  1077. {
  1078. *output += "\nRun Clean Command:";
  1079. *output += cleanCommand;
  1080. *output += "\n";
  1081. }
  1082. if (!cmSystemTools::RunSingleCommand(cleanCommand.c_str(), outputPtr,
  1083. &retVal, 0, verbose, timeout))
  1084. {
  1085. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1086. cmSystemTools::Error("Generator: execution of make clean failed.");
  1087. if (output)
  1088. {
  1089. *output += *outputPtr;
  1090. *output += "\nGenerator: execution of make clean failed.\n";
  1091. }
  1092. // return to the original directory
  1093. cmSystemTools::ChangeDirectory(cwd.c_str());
  1094. return 1;
  1095. }
  1096. if (output)
  1097. {
  1098. *output += *outputPtr;
  1099. }
  1100. }
  1101. // now build
  1102. std::string makeCommand =
  1103. this->GenerateBuildCommand(makeCommandCSTR, projectName,
  1104. extraOptions, target,
  1105. config, false, fast);
  1106. if(output)
  1107. {
  1108. *output += "\nRun Build Command:";
  1109. *output += makeCommand;
  1110. *output += "\n";
  1111. }
  1112. std::vector<cmStdString> command =
  1113. cmSystemTools::ParseArguments(makeCommand.c_str());
  1114. for(std::vector<std::string>::const_iterator ni = nativeOptions.begin();
  1115. ni != nativeOptions.end(); ++ni)
  1116. {
  1117. command.push_back(*ni);
  1118. }
  1119. if (!cmSystemTools::RunSingleCommand(command, outputPtr,
  1120. &retVal, 0, verbose, timeout))
  1121. {
  1122. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1123. cmSystemTools::Error
  1124. ("Generator: execution of make failed. Make command was: ",
  1125. makeCommand.c_str());
  1126. if (output)
  1127. {
  1128. *output += *outputPtr;
  1129. *output += "\nGenerator: execution of make failed. Make command was: "
  1130. + makeCommand + "\n";
  1131. }
  1132. // return to the original directory
  1133. cmSystemTools::ChangeDirectory(cwd.c_str());
  1134. return 1;
  1135. }
  1136. if (output)
  1137. {
  1138. *output += *outputPtr;
  1139. }
  1140. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1141. // The SGI MipsPro 7.3 compiler does not return an error code when
  1142. // the source has a #error in it! This is a work-around for such
  1143. // compilers.
  1144. if((retVal == 0) && (output->find("#error") != std::string::npos))
  1145. {
  1146. retVal = 1;
  1147. }
  1148. cmSystemTools::ChangeDirectory(cwd.c_str());
  1149. return retVal;
  1150. }
  1151. void cmGlobalGenerator::AddLocalGenerator(cmLocalGenerator *lg)
  1152. {
  1153. this->LocalGenerators.push_back(lg);
  1154. // update progress
  1155. // estimate how many lg there will be
  1156. const char *numGenC =
  1157. this->CMakeInstance->GetCacheManager()->GetCacheValue
  1158. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS");
  1159. if (!numGenC)
  1160. {
  1161. // If CMAKE_NUMBER_OF_LOCAL_GENERATORS is not set
  1162. // we are in the first time progress and we have no
  1163. // idea how long it will be. So, just move half way
  1164. // there each time, and don't go over 95%
  1165. this->FirstTimeProgress += ((1.0f - this->FirstTimeProgress) /30.0f);
  1166. if(this->FirstTimeProgress > 0.95f)
  1167. {
  1168. this->FirstTimeProgress = 0.95f;
  1169. }
  1170. this->CMakeInstance->UpdateProgress("Configuring",
  1171. this->FirstTimeProgress);
  1172. return;
  1173. }
  1174. int numGen = atoi(numGenC);
  1175. float prog = 0.9f*static_cast<float>(this->LocalGenerators.size())/
  1176. static_cast<float>(numGen);
  1177. if (prog > 0.9f)
  1178. {
  1179. prog = 0.9f;
  1180. }
  1181. this->CMakeInstance->UpdateProgress("Configuring", prog);
  1182. }
  1183. void cmGlobalGenerator::AddInstallComponent(const char* component)
  1184. {
  1185. if(component && *component)
  1186. {
  1187. this->InstallComponents.insert(component);
  1188. }
  1189. }
  1190. void cmGlobalGenerator::AddTargetToExports(const char* exportSetName,
  1191. cmTarget* target,
  1192. cmInstallTargetGenerator* archive,
  1193. cmInstallTargetGenerator* runTime,
  1194. cmInstallTargetGenerator* library,
  1195. cmInstallTargetGenerator* framework,
  1196. cmInstallTargetGenerator* bundle,
  1197. cmInstallFilesGenerator* headers)
  1198. {
  1199. if ((exportSetName) && (*exportSetName) && (target))
  1200. {
  1201. cmTargetExport* te = new cmTargetExport(target, archive, runTime, library,
  1202. framework, bundle, headers);
  1203. this->ExportSets[exportSetName].push_back(te);
  1204. }
  1205. }
  1206. //----------------------------------------------------------------------------
  1207. void cmGlobalGenerator::ClearExportSets()
  1208. {
  1209. for(std::map<cmStdString, std::vector<cmTargetExport*> >::iterator
  1210. setIt = this->ExportSets.begin();
  1211. setIt != this->ExportSets.end(); ++setIt)
  1212. {
  1213. for(unsigned int i = 0; i < setIt->second.size(); ++i)
  1214. {
  1215. delete setIt->second[i];
  1216. }
  1217. }
  1218. this->ExportSets.clear();
  1219. }
  1220. const std::vector<cmTargetExport*>* cmGlobalGenerator::GetExportSet(
  1221. const char* name) const
  1222. {
  1223. std::map<cmStdString, std::vector<cmTargetExport*> >::const_iterator
  1224. exportSetIt = this->ExportSets.find(name);
  1225. if (exportSetIt != this->ExportSets.end())
  1226. {
  1227. return &exportSetIt->second;
  1228. }
  1229. return 0;
  1230. }
  1231. void cmGlobalGenerator::EnableInstallTarget()
  1232. {
  1233. this->InstallTargetEnabled = true;
  1234. }
  1235. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  1236. {
  1237. cmLocalGenerator *lg = new cmLocalGenerator;
  1238. lg->SetGlobalGenerator(this);
  1239. return lg;
  1240. }
  1241. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen,
  1242. cmMakefile* mf)
  1243. {
  1244. this->SetConfiguredFilesPath(gen);
  1245. this->TryCompileOuterMakefile = mf;
  1246. const char* make =
  1247. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  1248. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  1249. "make program",
  1250. cmCacheManager::FILEPATH);
  1251. // copy the enabled languages
  1252. this->LanguageEnabled = gen->LanguageEnabled;
  1253. this->LanguagesReady = gen->LanguagesReady;
  1254. this->ExtensionToLanguage = gen->ExtensionToLanguage;
  1255. this->IgnoreExtensions = gen->IgnoreExtensions;
  1256. this->LanguageToOutputExtension = gen->LanguageToOutputExtension;
  1257. this->LanguageToLinkerPreference = gen->LanguageToLinkerPreference;
  1258. this->OutputExtensions = gen->OutputExtensions;
  1259. }
  1260. //----------------------------------------------------------------------------
  1261. void cmGlobalGenerator::SetConfiguredFilesPath(cmGlobalGenerator* gen)
  1262. {
  1263. if(!gen->ConfiguredFilesPath.empty())
  1264. {
  1265. this->ConfiguredFilesPath = gen->ConfiguredFilesPath;
  1266. }
  1267. else
  1268. {
  1269. this->ConfiguredFilesPath = gen->CMakeInstance->GetHomeOutputDirectory();
  1270. this->ConfiguredFilesPath += cmake::GetCMakeFilesDirectory();
  1271. }
  1272. }
  1273. //----------------------------------------------------------------------------
  1274. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  1275. {
  1276. entry.Name = this->GetName();
  1277. entry.Brief = "";
  1278. entry.Full = "";
  1279. }
  1280. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1281. cmLocalGenerator* gen)
  1282. {
  1283. if(!gen || gen == root)
  1284. {
  1285. // No directory excludes itself.
  1286. return false;
  1287. }
  1288. if(gen->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1289. {
  1290. // This directory is excluded from its parent.
  1291. return true;
  1292. }
  1293. // This directory is included in its parent. Check whether the
  1294. // parent is excluded.
  1295. return this->IsExcluded(root, gen->GetParent());
  1296. }
  1297. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1298. cmTarget& target)
  1299. {
  1300. if(target.GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1301. {
  1302. // This target is excluded from its directory.
  1303. return true;
  1304. }
  1305. else
  1306. {
  1307. // This target is included in its directory. Check whether the
  1308. // directory is excluded.
  1309. return this->IsExcluded(root, target.GetMakefile()->GetLocalGenerator());
  1310. }
  1311. }
  1312. void cmGlobalGenerator::GetEnabledLanguages(std::vector<std::string>& lang)
  1313. {
  1314. for(std::map<cmStdString, bool>::iterator i =
  1315. this->LanguageEnabled.begin(); i != this->LanguageEnabled.end(); ++i)
  1316. {
  1317. lang.push_back(i->first);
  1318. }
  1319. }
  1320. int cmGlobalGenerator::GetLinkerPreference(const char* lang)
  1321. {
  1322. std::map<cmStdString, int>::const_iterator it =
  1323. this->LanguageToLinkerPreference.find(lang);
  1324. if (it != this->LanguageToLinkerPreference.end())
  1325. {
  1326. return it->second;
  1327. }
  1328. return 0;
  1329. }
  1330. void cmGlobalGenerator::FillProjectMap()
  1331. {
  1332. this->ProjectMap.clear(); // make sure we start with a clean map
  1333. unsigned int i;
  1334. for(i = 0; i < this->LocalGenerators.size(); ++i)
  1335. {
  1336. // for each local generator add all projects
  1337. cmLocalGenerator *lg = this->LocalGenerators[i];
  1338. std::string name;
  1339. do
  1340. {
  1341. if (name != lg->GetMakefile()->GetProjectName())
  1342. {
  1343. name = lg->GetMakefile()->GetProjectName();
  1344. this->ProjectMap[name].push_back(this->LocalGenerators[i]);
  1345. }
  1346. lg = lg->GetParent();
  1347. }
  1348. while (lg);
  1349. }
  1350. }
  1351. // Build a map that contains a the set of targets used by each local
  1352. // generator directory level.
  1353. void cmGlobalGenerator::FillLocalGeneratorToTargetMap()
  1354. {
  1355. this->LocalGeneratorToTargetMap.clear();
  1356. // Loop over all targets in all local generators.
  1357. for(std::vector<cmLocalGenerator*>::const_iterator
  1358. lgi = this->LocalGenerators.begin();
  1359. lgi != this->LocalGenerators.end(); ++lgi)
  1360. {
  1361. cmLocalGenerator* lg = *lgi;
  1362. cmMakefile* mf = lg->GetMakefile();
  1363. cmTargets& targets = mf->GetTargets();
  1364. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  1365. {
  1366. cmTarget& target = t->second;
  1367. // Consider the directory containing the target and all its
  1368. // parents until something excludes the target.
  1369. for(cmLocalGenerator* clg = lg; clg && !this->IsExcluded(clg, target);
  1370. clg = clg->GetParent())
  1371. {
  1372. // This local generator includes the target.
  1373. std::set<cmTarget*>& targetSet =
  1374. this->LocalGeneratorToTargetMap[clg];
  1375. targetSet.insert(&target);
  1376. // Add dependencies of the included target. An excluded
  1377. // target may still be included if it is a dependency of a
  1378. // non-excluded target.
  1379. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  1380. for(TargetDependSet::const_iterator ti = tgtdeps.begin();
  1381. ti != tgtdeps.end(); ++ti)
  1382. {
  1383. targetSet.insert(*ti);
  1384. }
  1385. }
  1386. }
  1387. }
  1388. }
  1389. ///! Find a local generator by its startdirectory
  1390. cmLocalGenerator* cmGlobalGenerator::FindLocalGenerator(const char* start_dir)
  1391. {
  1392. std::vector<cmLocalGenerator*>* gens = &this->LocalGenerators;
  1393. for(unsigned int i = 0; i < gens->size(); ++i)
  1394. {
  1395. std::string sd = (*gens)[i]->GetMakefile()->GetStartDirectory();
  1396. if (sd == start_dir)
  1397. {
  1398. return (*gens)[i];
  1399. }
  1400. }
  1401. return 0;
  1402. }
  1403. //----------------------------------------------------------------------------
  1404. cmTarget*
  1405. cmGlobalGenerator::FindTarget(const char* project, const char* name)
  1406. {
  1407. // if project specific
  1408. if(project)
  1409. {
  1410. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  1411. for(unsigned int i = 0; i < gens->size(); ++i)
  1412. {
  1413. cmTarget* ret = (*gens)[i]->GetMakefile()->FindTarget(name);
  1414. if(ret)
  1415. {
  1416. return ret;
  1417. }
  1418. }
  1419. }
  1420. // if all projects/directories
  1421. else
  1422. {
  1423. std::map<cmStdString,cmTarget *>::iterator i =
  1424. this->TotalTargets.find ( name );
  1425. if ( i != this->TotalTargets.end() )
  1426. {
  1427. return i->second;
  1428. }
  1429. }
  1430. return 0;
  1431. }
  1432. //----------------------------------------------------------------------------
  1433. bool cmGlobalGenerator::NameResolvesToFramework(const std::string& libname)
  1434. {
  1435. if(cmSystemTools::IsPathToFramework(libname.c_str()))
  1436. {
  1437. return true;
  1438. }
  1439. if(cmTarget* tgt = this->FindTarget(0, libname.c_str()))
  1440. {
  1441. if(tgt->IsFrameworkOnApple())
  1442. {
  1443. return true;
  1444. }
  1445. }
  1446. return false;
  1447. }
  1448. //----------------------------------------------------------------------------
  1449. inline std::string removeQuotes(const std::string& s)
  1450. {
  1451. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1452. {
  1453. return s.substr(1, s.size()-2);
  1454. }
  1455. return s;
  1456. }
  1457. void cmGlobalGenerator::SetCMakeInstance(cmake* cm)
  1458. {
  1459. // Store a pointer to the cmake object instance.
  1460. this->CMakeInstance = cm;
  1461. }
  1462. void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
  1463. {
  1464. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  1465. const char* cmakeCfgIntDir = this->GetCMakeCFGInitDirectory();
  1466. const char* cmakeCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1467. // CPack
  1468. std::string workingDir = mf->GetStartOutputDirectory();
  1469. cmCustomCommandLines cpackCommandLines;
  1470. std::vector<std::string> depends;
  1471. cmCustomCommandLine singleLine;
  1472. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1473. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1474. {
  1475. singleLine.push_back("-C");
  1476. singleLine.push_back(cmakeCfgIntDir);
  1477. }
  1478. singleLine.push_back("--config");
  1479. std::string configFile = mf->GetStartOutputDirectory();;
  1480. configFile += "/CPackConfig.cmake";
  1481. std::string relConfigFile = "./CPackConfig.cmake";
  1482. singleLine.push_back(relConfigFile);
  1483. cpackCommandLines.push_back(singleLine);
  1484. if ( this->GetPreinstallTargetName() )
  1485. {
  1486. depends.push_back(this->GetPreinstallTargetName());
  1487. }
  1488. else
  1489. {
  1490. const char* noPackageAll =
  1491. mf->GetDefinition("CMAKE_SKIP_PACKAGE_ALL_DEPENDENCY");
  1492. if(!noPackageAll || cmSystemTools::IsOff(noPackageAll))
  1493. {
  1494. depends.push_back(this->GetAllTargetName());
  1495. }
  1496. }
  1497. if(cmSystemTools::FileExists(configFile.c_str()))
  1498. {
  1499. (*targets)[this->GetPackageTargetName()]
  1500. = this->CreateGlobalTarget(this->GetPackageTargetName(),
  1501. "Run CPack packaging tool...",
  1502. &cpackCommandLines, depends,
  1503. workingDir.c_str());
  1504. }
  1505. // CPack source
  1506. const char* packageSourceTargetName = this->GetPackageSourceTargetName();
  1507. if ( packageSourceTargetName )
  1508. {
  1509. cpackCommandLines.erase(cpackCommandLines.begin(),
  1510. cpackCommandLines.end());
  1511. singleLine.erase(singleLine.begin(), singleLine.end());
  1512. depends.erase(depends.begin(), depends.end());
  1513. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1514. singleLine.push_back("--config");
  1515. configFile = mf->GetStartOutputDirectory();;
  1516. configFile += "/CPackSourceConfig.cmake";
  1517. relConfigFile = "./CPackSourceConfig.cmake";
  1518. singleLine.push_back(relConfigFile);
  1519. if(cmSystemTools::FileExists(configFile.c_str()))
  1520. {
  1521. singleLine.push_back(configFile);
  1522. cpackCommandLines.push_back(singleLine);
  1523. (*targets)[packageSourceTargetName]
  1524. = this->CreateGlobalTarget(packageSourceTargetName,
  1525. "Run CPack packaging tool for source...",
  1526. &cpackCommandLines, depends,
  1527. workingDir.c_str()
  1528. );
  1529. }
  1530. }
  1531. // Test
  1532. if(mf->IsOn("CMAKE_TESTING_ENABLED"))
  1533. {
  1534. cpackCommandLines.erase(cpackCommandLines.begin(),
  1535. cpackCommandLines.end());
  1536. singleLine.erase(singleLine.begin(), singleLine.end());
  1537. depends.erase(depends.begin(), depends.end());
  1538. singleLine.push_back(this->GetCMakeInstance()->GetCTestCommand());
  1539. singleLine.push_back("--force-new-ctest-process");
  1540. if(cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.')
  1541. {
  1542. singleLine.push_back("-C");
  1543. singleLine.push_back(cmakeCfgIntDir);
  1544. }
  1545. else // TODO: This is a hack. Should be something to do with the generator
  1546. {
  1547. singleLine.push_back("$(ARGS)");
  1548. }
  1549. cpackCommandLines.push_back(singleLine);
  1550. (*targets)[this->GetTestTargetName()]
  1551. = this->CreateGlobalTarget(this->GetTestTargetName(),
  1552. "Running tests...", &cpackCommandLines, depends, 0);
  1553. }
  1554. //Edit Cache
  1555. const char* editCacheTargetName = this->GetEditCacheTargetName();
  1556. if ( editCacheTargetName )
  1557. {
  1558. cpackCommandLines.erase(cpackCommandLines.begin(),
  1559. cpackCommandLines.end());
  1560. singleLine.erase(singleLine.begin(), singleLine.end());
  1561. depends.erase(depends.begin(), depends.end());
  1562. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  1563. // Otherwise default to the interactive command-line interface.
  1564. if(mf->GetDefinition("CMAKE_EDIT_COMMAND"))
  1565. {
  1566. singleLine.push_back(mf->GetDefinition("CMAKE_EDIT_COMMAND"));
  1567. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1568. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1569. cpackCommandLines.push_back(singleLine);
  1570. (*targets)[editCacheTargetName] =
  1571. this->CreateGlobalTarget(
  1572. editCacheTargetName, "Running CMake cache editor...",
  1573. &cpackCommandLines, depends, 0);
  1574. }
  1575. else
  1576. {
  1577. singleLine.push_back(cmakeCommand);
  1578. singleLine.push_back("-i");
  1579. singleLine.push_back(".");
  1580. cpackCommandLines.push_back(singleLine);
  1581. (*targets)[editCacheTargetName] =
  1582. this->CreateGlobalTarget(
  1583. editCacheTargetName,
  1584. "Running interactive CMake command-line interface...",
  1585. &cpackCommandLines, depends, 0);
  1586. }
  1587. }
  1588. //Rebuild Cache
  1589. const char* rebuildCacheTargetName = this->GetRebuildCacheTargetName();
  1590. if ( rebuildCacheTargetName )
  1591. {
  1592. cpackCommandLines.erase(cpackCommandLines.begin(),
  1593. cpackCommandLines.end());
  1594. singleLine.erase(singleLine.begin(), singleLine.end());
  1595. depends.erase(depends.begin(), depends.end());
  1596. singleLine.push_back(cmakeCommand);
  1597. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1598. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1599. cpackCommandLines.push_back(singleLine);
  1600. (*targets)[rebuildCacheTargetName] =
  1601. this->CreateGlobalTarget(
  1602. rebuildCacheTargetName, "Running CMake to regenerate build system...",
  1603. &cpackCommandLines, depends, 0);
  1604. }
  1605. //Install
  1606. if(this->InstallTargetEnabled)
  1607. {
  1608. if(!cmakeCfgIntDir || !*cmakeCfgIntDir || cmakeCfgIntDir[0] == '.')
  1609. {
  1610. std::set<cmStdString>* componentsSet = &this->InstallComponents;
  1611. cpackCommandLines.erase(cpackCommandLines.begin(),
  1612. cpackCommandLines.end());
  1613. depends.erase(depends.begin(), depends.end());
  1614. cmOStringStream ostr;
  1615. if ( componentsSet->size() > 0 )
  1616. {
  1617. ostr << "Available install components are:";
  1618. std::set<cmStdString>::iterator it;
  1619. for (
  1620. it = componentsSet->begin();
  1621. it != componentsSet->end();
  1622. ++ it )
  1623. {
  1624. ostr << " \"" << it->c_str() << "\"";
  1625. }
  1626. }
  1627. else
  1628. {
  1629. ostr << "Only default component available";
  1630. }
  1631. singleLine.push_back(ostr.str().c_str());
  1632. (*targets)["list_install_components"]
  1633. = this->CreateGlobalTarget("list_install_components",
  1634. ostr.str().c_str(),
  1635. &cpackCommandLines, depends, 0);
  1636. }
  1637. std::string cmd = cmakeCommand;
  1638. cpackCommandLines.erase(cpackCommandLines.begin(),
  1639. cpackCommandLines.end());
  1640. singleLine.erase(singleLine.begin(), singleLine.end());
  1641. depends.erase(depends.begin(), depends.end());
  1642. if ( this->GetPreinstallTargetName() )
  1643. {
  1644. depends.push_back(this->GetPreinstallTargetName());
  1645. }
  1646. else
  1647. {
  1648. const char* noall =
  1649. mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1650. if(!noall || cmSystemTools::IsOff(noall))
  1651. {
  1652. depends.push_back(this->GetAllTargetName());
  1653. }
  1654. }
  1655. if(mf->GetDefinition("CMake_BINARY_DIR"))
  1656. {
  1657. // We are building CMake itself. We cannot use the original
  1658. // executable to install over itself. The generator will
  1659. // automatically convert this name to the build-time location.
  1660. cmd = "cmake";
  1661. }
  1662. singleLine.push_back(cmd.c_str());
  1663. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1664. {
  1665. std::string cfgArg = "-DBUILD_TYPE=";
  1666. cfgArg += mf->GetDefinition("CMAKE_CFG_INTDIR");
  1667. singleLine.push_back(cfgArg);
  1668. }
  1669. singleLine.push_back("-P");
  1670. singleLine.push_back("cmake_install.cmake");
  1671. cpackCommandLines.push_back(singleLine);
  1672. (*targets)[this->GetInstallTargetName()] =
  1673. this->CreateGlobalTarget(
  1674. this->GetInstallTargetName(), "Install the project...",
  1675. &cpackCommandLines, depends, 0);
  1676. // install_local
  1677. if(const char* install_local = this->GetInstallLocalTargetName())
  1678. {
  1679. cmCustomCommandLine localCmdLine = singleLine;
  1680. localCmdLine.insert(localCmdLine.begin()+1,
  1681. "-DCMAKE_INSTALL_LOCAL_ONLY=1");
  1682. cpackCommandLines.erase(cpackCommandLines.begin(),
  1683. cpackCommandLines.end());
  1684. cpackCommandLines.push_back(localCmdLine);
  1685. (*targets)[install_local] =
  1686. this->CreateGlobalTarget(
  1687. install_local, "Installing only the local directory...",
  1688. &cpackCommandLines, depends, 0);
  1689. }
  1690. // install_strip
  1691. const char* install_strip = this->GetInstallStripTargetName();
  1692. if((install_strip !=0) && (mf->IsSet("CMAKE_STRIP")))
  1693. {
  1694. cmCustomCommandLine stripCmdLine = singleLine;
  1695. stripCmdLine.insert(stripCmdLine.begin()+1,"-DCMAKE_INSTALL_DO_STRIP=1");
  1696. cpackCommandLines.erase(cpackCommandLines.begin(),
  1697. cpackCommandLines.end());
  1698. cpackCommandLines.push_back(stripCmdLine);
  1699. (*targets)[install_strip] =
  1700. this->CreateGlobalTarget(
  1701. install_strip, "Installing the project stripped...",
  1702. &cpackCommandLines, depends, 0);
  1703. }
  1704. }
  1705. }
  1706. //----------------------------------------------------------------------------
  1707. const char* cmGlobalGenerator::GetPredefinedTargetsFolder()
  1708. {
  1709. const char* prop =
  1710. this->GetCMakeInstance()->GetProperty("PREDEFINED_TARGETS_FOLDER");
  1711. if (prop)
  1712. {
  1713. return prop;
  1714. }
  1715. return "CMakePredefinedTargets";
  1716. }
  1717. //----------------------------------------------------------------------------
  1718. bool cmGlobalGenerator::UseFolderProperty()
  1719. {
  1720. const char* prop = this->GetCMakeInstance()->GetProperty("USE_FOLDERS");
  1721. // If this property is defined, let the setter turn this on or off...
  1722. //
  1723. if (prop)
  1724. {
  1725. return cmSystemTools::IsOn(prop);
  1726. }
  1727. // By default, this feature is OFF, since it is not supported in the
  1728. // Visual Studio Express editions:
  1729. //
  1730. return false;
  1731. }
  1732. //----------------------------------------------------------------------------
  1733. cmTarget cmGlobalGenerator::CreateGlobalTarget(
  1734. const char* name, const char* message,
  1735. const cmCustomCommandLines* commandLines,
  1736. std::vector<std::string> depends,
  1737. const char* workingDirectory)
  1738. {
  1739. // Package
  1740. cmTarget target;
  1741. target.GetProperties().SetCMakeInstance(this->CMakeInstance);
  1742. target.SetType(cmTarget::GLOBAL_TARGET, name);
  1743. target.SetProperty("EXCLUDE_FROM_ALL","TRUE");
  1744. std::vector<std::string> no_outputs;
  1745. std::vector<std::string> no_depends;
  1746. // Store the custom command in the target.
  1747. cmCustomCommand cc(0, no_outputs, no_depends, *commandLines, 0,
  1748. workingDirectory);
  1749. target.GetPostBuildCommands().push_back(cc);
  1750. target.SetProperty("EchoString", message);
  1751. std::vector<std::string>::iterator dit;
  1752. for ( dit = depends.begin(); dit != depends.end(); ++ dit )
  1753. {
  1754. target.AddUtility(dit->c_str());
  1755. }
  1756. // Organize in the "predefined targets" folder:
  1757. //
  1758. if (this->UseFolderProperty())
  1759. {
  1760. target.SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  1761. }
  1762. return target;
  1763. }
  1764. //----------------------------------------------------------------------------
  1765. void cmGlobalGenerator::AppendDirectoryForConfig(const char*, const char*,
  1766. const char*, std::string&)
  1767. {
  1768. // Subclasses that support multiple configurations should implement
  1769. // this method to append the subdirectory for the given build
  1770. // configuration.
  1771. }
  1772. //----------------------------------------------------------------------------
  1773. cmGlobalGenerator::TargetDependSet const&
  1774. cmGlobalGenerator::GetTargetDirectDepends(cmTarget & target)
  1775. {
  1776. return this->TargetDependencies[&target];
  1777. }
  1778. void cmGlobalGenerator::AddTarget(cmTargets::value_type &v)
  1779. {
  1780. assert(!v.second.IsImported());
  1781. this->TotalTargets[v.first] = &v.second;
  1782. }
  1783. void cmGlobalGenerator::SetExternalMakefileProjectGenerator(
  1784. cmExternalMakefileProjectGenerator *extraGenerator)
  1785. {
  1786. this->ExtraGenerator = extraGenerator;
  1787. if (this->ExtraGenerator!=0)
  1788. {
  1789. this->ExtraGenerator->SetGlobalGenerator(this);
  1790. }
  1791. }
  1792. const char* cmGlobalGenerator::GetExtraGeneratorName() const
  1793. {
  1794. return this->ExtraGenerator==0 ? 0 : this->ExtraGenerator->GetName();
  1795. }
  1796. void cmGlobalGenerator::FileReplacedDuringGenerate(const std::string& filename)
  1797. {
  1798. this->FilesReplacedDuringGenerate.push_back(filename);
  1799. }
  1800. void
  1801. cmGlobalGenerator
  1802. ::GetFilesReplacedDuringGenerate(std::vector<std::string>& filenames)
  1803. {
  1804. filenames.clear();
  1805. std::copy(
  1806. this->FilesReplacedDuringGenerate.begin(),
  1807. this->FilesReplacedDuringGenerate.end(),
  1808. std::back_inserter(filenames));
  1809. }
  1810. //----------------------------------------------------------------------------
  1811. void cmGlobalGenerator::GetTargetSets(TargetDependSet& projectTargets,
  1812. TargetDependSet& originalTargets,
  1813. cmLocalGenerator* root,
  1814. GeneratorVector const& generators)
  1815. {
  1816. // loop over all local generators
  1817. for(std::vector<cmLocalGenerator*>::const_iterator i = generators.begin();
  1818. i != generators.end(); ++i)
  1819. {
  1820. // check to make sure generator is not excluded
  1821. if(this->IsExcluded(root, *i))
  1822. {
  1823. continue;
  1824. }
  1825. cmMakefile* mf = (*i)->GetMakefile();
  1826. // Get the targets in the makefile
  1827. cmTargets &tgts = mf->GetTargets();
  1828. // loop over all the targets
  1829. for (cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  1830. {
  1831. cmTarget* target = &l->second;
  1832. if(this->IsRootOnlyTarget(target) &&
  1833. target->GetMakefile() != root->GetMakefile())
  1834. {
  1835. continue;
  1836. }
  1837. // put the target in the set of original targets
  1838. originalTargets.insert(target);
  1839. // Get the set of targets that depend on target
  1840. this->AddTargetDepends(target, projectTargets);
  1841. }
  1842. }
  1843. }
  1844. //----------------------------------------------------------------------------
  1845. bool cmGlobalGenerator::IsRootOnlyTarget(cmTarget* target)
  1846. {
  1847. return (target->GetType() == cmTarget::GLOBAL_TARGET ||
  1848. strcmp(target->GetName(), this->GetAllTargetName()) == 0);
  1849. }
  1850. //----------------------------------------------------------------------------
  1851. void cmGlobalGenerator::AddTargetDepends(cmTarget* target,
  1852. TargetDependSet& projectTargets)
  1853. {
  1854. // add the target itself
  1855. if(projectTargets.insert(target).second)
  1856. {
  1857. // This is the first time we have encountered the target.
  1858. // Recursively follow its dependencies.
  1859. TargetDependSet const& ts = this->GetTargetDirectDepends(*target);
  1860. for(TargetDependSet::const_iterator i = ts.begin(); i != ts.end(); ++i)
  1861. {
  1862. cmTarget* dtarget = *i;
  1863. this->AddTargetDepends(dtarget, projectTargets);
  1864. }
  1865. }
  1866. }
  1867. //----------------------------------------------------------------------------
  1868. void cmGlobalGenerator::AddToManifest(const char* config,
  1869. std::string const& f)
  1870. {
  1871. // Add to the main manifest for this configuration.
  1872. this->TargetManifest[config].insert(f);
  1873. // Add to the content listing for the file's directory.
  1874. std::string dir = cmSystemTools::GetFilenamePath(f);
  1875. std::string file = cmSystemTools::GetFilenameName(f);
  1876. this->DirectoryContentMap[dir].insert(file);
  1877. }
  1878. //----------------------------------------------------------------------------
  1879. std::set<cmStdString> const&
  1880. cmGlobalGenerator::GetDirectoryContent(std::string const& dir, bool needDisk)
  1881. {
  1882. DirectoryContent& dc = this->DirectoryContentMap[dir];
  1883. if(needDisk && !dc.LoadedFromDisk)
  1884. {
  1885. // Load the directory content from disk.
  1886. cmsys::Directory d;
  1887. if(d.Load(dir.c_str()))
  1888. {
  1889. unsigned long n = d.GetNumberOfFiles();
  1890. for(unsigned long i = 0; i < n; ++i)
  1891. {
  1892. const char* f = d.GetFile(i);
  1893. if(strcmp(f, ".") != 0 && strcmp(f, "..") != 0)
  1894. {
  1895. dc.insert(f);
  1896. }
  1897. }
  1898. }
  1899. dc.LoadedFromDisk = true;
  1900. }
  1901. return dc;
  1902. }
  1903. //----------------------------------------------------------------------------
  1904. void
  1905. cmGlobalGenerator::AddRuleHash(const std::vector<std::string>& outputs,
  1906. std::string const& content)
  1907. {
  1908. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1909. // Ignore if there are no outputs.
  1910. if(outputs.empty())
  1911. {
  1912. return;
  1913. }
  1914. // Compute a hash of the rule.
  1915. RuleHash hash;
  1916. {
  1917. unsigned char const* data =
  1918. reinterpret_cast<unsigned char const*>(content.c_str());
  1919. int length = static_cast<int>(content.length());
  1920. cmsysMD5* sum = cmsysMD5_New();
  1921. cmsysMD5_Initialize(sum);
  1922. cmsysMD5_Append(sum, data, length);
  1923. cmsysMD5_FinalizeHex(sum, hash.Data);
  1924. cmsysMD5_Delete(sum);
  1925. }
  1926. // Shorten the output name (in expected use case).
  1927. cmLocalGenerator* lg = this->GetLocalGenerators()[0];
  1928. std::string fname = lg->Convert(outputs[0].c_str(),
  1929. cmLocalGenerator::HOME_OUTPUT);
  1930. // Associate the hash with this output.
  1931. this->RuleHashes[fname] = hash;
  1932. #else
  1933. (void)outputs;
  1934. (void)content;
  1935. #endif
  1936. }
  1937. //----------------------------------------------------------------------------
  1938. void cmGlobalGenerator::CheckRuleHashes()
  1939. {
  1940. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1941. std::string home = this->GetCMakeInstance()->GetHomeOutputDirectory();
  1942. std::string pfile = home;
  1943. pfile += this->GetCMakeInstance()->GetCMakeFilesDirectory();
  1944. pfile += "/CMakeRuleHashes.txt";
  1945. this->CheckRuleHashes(pfile, home);
  1946. this->WriteRuleHashes(pfile);
  1947. #endif
  1948. }
  1949. //----------------------------------------------------------------------------
  1950. void cmGlobalGenerator::CheckRuleHashes(std::string const& pfile,
  1951. std::string const& home)
  1952. {
  1953. #if defined(_WIN32) || defined(__CYGWIN__)
  1954. std::ifstream fin(pfile.c_str(), std::ios::in | std::ios::binary);
  1955. #else
  1956. std::ifstream fin(pfile.c_str(), std::ios::in);
  1957. #endif
  1958. if(!fin)
  1959. {
  1960. return;
  1961. }
  1962. std::string line;
  1963. std::string fname;
  1964. while(cmSystemTools::GetLineFromStream(fin, line))
  1965. {
  1966. // Line format is a 32-byte hex string followed by a space
  1967. // followed by a file name (with no escaping).
  1968. // Skip blank and comment lines.
  1969. if(line.size() < 34 || line[0] == '#')
  1970. {
  1971. continue;
  1972. }
  1973. // Get the filename.
  1974. fname = line.substr(33, line.npos);
  1975. // Look for a hash for this file's rule.
  1976. std::map<cmStdString, RuleHash>::const_iterator rhi =
  1977. this->RuleHashes.find(fname);
  1978. if(rhi != this->RuleHashes.end())
  1979. {
  1980. // Compare the rule hash in the file to that we were given.
  1981. if(strncmp(line.c_str(), rhi->second.Data, 32) != 0)
  1982. {
  1983. // The rule has changed. Delete the output so it will be
  1984. // built again.
  1985. fname = cmSystemTools::CollapseFullPath(fname.c_str(), home.c_str());
  1986. cmSystemTools::RemoveFile(fname.c_str());
  1987. }
  1988. }
  1989. else
  1990. {
  1991. // We have no hash for a rule previously listed. This may be a
  1992. // case where a user has turned off a build option and might
  1993. // want to turn it back on later, so do not delete the file.
  1994. // Instead, we keep the rule hash as long as the file exists so
  1995. // that if the feature is turned back on and the rule has
  1996. // changed the file is still rebuilt.
  1997. std::string fpath =
  1998. cmSystemTools::CollapseFullPath(fname.c_str(), home.c_str());
  1999. if(cmSystemTools::FileExists(fpath.c_str()))
  2000. {
  2001. RuleHash hash;
  2002. strncpy(hash.Data, line.c_str(), 32);
  2003. this->RuleHashes[fname] = hash;
  2004. }
  2005. }
  2006. }
  2007. }
  2008. //----------------------------------------------------------------------------
  2009. void cmGlobalGenerator::WriteRuleHashes(std::string const& pfile)
  2010. {
  2011. // Now generate a new persistence file with the current hashes.
  2012. if(this->RuleHashes.empty())
  2013. {
  2014. cmSystemTools::RemoveFile(pfile.c_str());
  2015. }
  2016. else
  2017. {
  2018. cmGeneratedFileStream fout(pfile.c_str());
  2019. fout << "# Hashes of file build rules.\n";
  2020. for(std::map<cmStdString, RuleHash>::const_iterator
  2021. rhi = this->RuleHashes.begin(); rhi != this->RuleHashes.end(); ++rhi)
  2022. {
  2023. fout.write(rhi->second.Data, 32);
  2024. fout << " " << rhi->first << "\n";
  2025. }
  2026. }
  2027. }
  2028. //----------------------------------------------------------------------------
  2029. void cmGlobalGenerator::WriteSummary()
  2030. {
  2031. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  2032. // Record all target directories in a central location.
  2033. std::string fname = mf->GetHomeOutputDirectory();
  2034. fname += cmake::GetCMakeFilesDirectory();
  2035. fname += "/TargetDirectories.txt";
  2036. cmGeneratedFileStream fout(fname.c_str());
  2037. // Generate summary information files for each target.
  2038. std::string dir;
  2039. for(std::map<cmStdString,cmTarget *>::const_iterator ti =
  2040. this->TotalTargets.begin(); ti != this->TotalTargets.end(); ++ti)
  2041. {
  2042. this->WriteSummary(ti->second);
  2043. fout << ti->second->GetSupportDirectory() << "\n";
  2044. }
  2045. }
  2046. //----------------------------------------------------------------------------
  2047. void cmGlobalGenerator::WriteSummary(cmTarget* target)
  2048. {
  2049. // Place the labels file in a per-target support directory.
  2050. std::string dir = target->GetSupportDirectory();
  2051. std::string file = dir;
  2052. file += "/Labels.txt";
  2053. // Check whether labels are enabled for this target.
  2054. if(const char* value = target->GetProperty("LABELS"))
  2055. {
  2056. cmSystemTools::MakeDirectory(dir.c_str());
  2057. cmGeneratedFileStream fout(file.c_str());
  2058. // List the target-wide labels. All sources in the target get
  2059. // these labels.
  2060. std::vector<std::string> labels;
  2061. cmSystemTools::ExpandListArgument(value, labels);
  2062. if(!labels.empty())
  2063. {
  2064. fout << "# Target labels\n";
  2065. for(std::vector<std::string>::const_iterator li = labels.begin();
  2066. li != labels.end(); ++li)
  2067. {
  2068. fout << " " << *li << "\n";
  2069. }
  2070. }
  2071. // List the source files with any per-source labels.
  2072. fout << "# Source files and their labels\n";
  2073. std::vector<cmSourceFile*> const& sources = target->GetSourceFiles();
  2074. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  2075. si != sources.end(); ++si)
  2076. {
  2077. cmSourceFile* sf = *si;
  2078. fout << sf->GetFullPath() << "\n";
  2079. if(const char* svalue = sf->GetProperty("LABELS"))
  2080. {
  2081. labels.clear();
  2082. cmSystemTools::ExpandListArgument(svalue, labels);
  2083. for(std::vector<std::string>::const_iterator li = labels.begin();
  2084. li != labels.end(); ++li)
  2085. {
  2086. fout << " " << *li << "\n";
  2087. }
  2088. }
  2089. }
  2090. }
  2091. else
  2092. {
  2093. cmSystemTools::RemoveFile(file.c_str());
  2094. }
  2095. }