cmGlobalGenerator.cxx 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #if defined(_WIN32) && !defined(__CYGWIN__)
  4. #include "windows.h" // this must be first to define GetCurrentDirectory
  5. #if defined(_MSC_VER) && _MSC_VER >= 1800
  6. #define KWSYS_WINDOWS_DEPRECATED_GetVersionEx
  7. #endif
  8. #endif
  9. #include "cmGlobalGenerator.h"
  10. #include "cmAlgorithms.h"
  11. #include "cmCPackPropertiesGenerator.h"
  12. #include "cmComputeTargetDepends.h"
  13. #include "cmCustomCommand.h"
  14. #include "cmCustomCommandLines.h"
  15. #include "cmExportBuildFileGenerator.h"
  16. #include "cmExternalMakefileProjectGenerator.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmGeneratorExpression.h"
  19. #include "cmGeneratorTarget.h"
  20. #include "cmInstallGenerator.h"
  21. #include "cmLinkLineComputer.h"
  22. #include "cmLocalGenerator.h"
  23. #include "cmMSVC60LinkLineComputer.h"
  24. #include "cmMakefile.h"
  25. #include "cmOutputConverter.h"
  26. #include "cmPolicies.h"
  27. #include "cmQtAutoGeneratorInitializer.h"
  28. #include "cmSourceFile.h"
  29. #include "cmState.h"
  30. #include "cmVersion.h"
  31. #include "cmake.h"
  32. #include <algorithm>
  33. #include <assert.h>
  34. #include <cmsys/Directory.hxx>
  35. #include <cmsys/FStream.hxx>
  36. #include <iterator>
  37. #include <sstream>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #if defined(CMAKE_BUILD_WITH_CMAKE)
  42. #include <cm_jsoncpp_value.h>
  43. #include <cm_jsoncpp_writer.h>
  44. #include <cmsys/MD5.h>
  45. #endif
  46. class cmInstalledFile;
  47. bool cmTarget::StrictTargetComparison::operator()(cmTarget const* t1,
  48. cmTarget const* t2) const
  49. {
  50. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  51. if (nameResult == 0) {
  52. return strcmp(t1->GetMakefile()->GetCurrentBinaryDirectory(),
  53. t2->GetMakefile()->GetCurrentBinaryDirectory()) < 0;
  54. }
  55. return nameResult < 0;
  56. }
  57. cmGlobalGenerator::cmGlobalGenerator(cmake* cm)
  58. : CMakeInstance(cm)
  59. {
  60. // By default the .SYMBOLIC dependency is not needed on symbolic rules.
  61. this->NeedSymbolicMark = false;
  62. // by default use the native paths
  63. this->ForceUnixPaths = false;
  64. // By default do not try to support color.
  65. this->ToolSupportsColor = false;
  66. // By default do not use link scripts.
  67. this->UseLinkScript = false;
  68. // Whether an install target is needed.
  69. this->InstallTargetEnabled = false;
  70. // how long to let try compiles run
  71. this->TryCompileTimeout = 0;
  72. this->ExtraGenerator = CM_NULLPTR;
  73. this->CurrentMakefile = CM_NULLPTR;
  74. this->TryCompileOuterMakefile = CM_NULLPTR;
  75. this->ConfigureDoneCMP0026AndCMP0024 = false;
  76. this->FirstTimeProgress = 0.0f;
  77. cm->GetState()->SetMinGWMake(false);
  78. cm->GetState()->SetMSYSShell(false);
  79. cm->GetState()->SetNMake(false);
  80. cm->GetState()->SetWatcomWMake(false);
  81. cm->GetState()->SetWindowsShell(false);
  82. cm->GetState()->SetWindowsVSIDE(false);
  83. }
  84. cmGlobalGenerator::~cmGlobalGenerator()
  85. {
  86. this->ClearGeneratorMembers();
  87. delete this->ExtraGenerator;
  88. }
  89. bool cmGlobalGenerator::SetGeneratorPlatform(std::string const& p,
  90. cmMakefile* mf)
  91. {
  92. if (p.empty()) {
  93. return true;
  94. }
  95. std::ostringstream e;
  96. /* clang-format off */
  97. e <<
  98. "Generator\n"
  99. " " << this->GetName() << "\n"
  100. "does not support platform specification, but platform\n"
  101. " " << p << "\n"
  102. "was specified.";
  103. /* clang-format on */
  104. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  105. return false;
  106. }
  107. bool cmGlobalGenerator::SetGeneratorToolset(std::string const& ts,
  108. cmMakefile* mf)
  109. {
  110. if (ts.empty()) {
  111. return true;
  112. }
  113. std::ostringstream e;
  114. /* clang-format off */
  115. e <<
  116. "Generator\n"
  117. " " << this->GetName() << "\n"
  118. "does not support toolset specification, but toolset\n"
  119. " " << ts << "\n"
  120. "was specified.";
  121. /* clang-format on */
  122. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  123. return false;
  124. }
  125. std::string cmGlobalGenerator::SelectMakeProgram(
  126. const std::string& inMakeProgram, const std::string& makeDefault) const
  127. {
  128. std::string makeProgram = inMakeProgram;
  129. if (cmSystemTools::IsOff(makeProgram.c_str())) {
  130. const char* makeProgramCSTR =
  131. this->CMakeInstance->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  132. if (cmSystemTools::IsOff(makeProgramCSTR)) {
  133. makeProgram = makeDefault;
  134. } else {
  135. makeProgram = makeProgramCSTR;
  136. }
  137. if (cmSystemTools::IsOff(makeProgram.c_str()) && !makeProgram.empty()) {
  138. makeProgram = "CMAKE_MAKE_PROGRAM-NOTFOUND";
  139. }
  140. }
  141. return makeProgram;
  142. }
  143. void cmGlobalGenerator::ResolveLanguageCompiler(const std::string& lang,
  144. cmMakefile* mf,
  145. bool optional) const
  146. {
  147. std::string langComp = "CMAKE_";
  148. langComp += lang;
  149. langComp += "_COMPILER";
  150. if (!mf->GetDefinition(langComp)) {
  151. if (!optional) {
  152. cmSystemTools::Error(langComp.c_str(), " not set, after EnableLanguage");
  153. }
  154. return;
  155. }
  156. const char* name = mf->GetRequiredDefinition(langComp);
  157. std::string path;
  158. if (!cmSystemTools::FileIsFullPath(name)) {
  159. path = cmSystemTools::FindProgram(name);
  160. } else {
  161. path = name;
  162. }
  163. if (!optional && (path.empty() || !cmSystemTools::FileExists(path))) {
  164. return;
  165. }
  166. const char* cname =
  167. this->GetCMakeInstance()->GetState()->GetInitializedCacheValue(langComp);
  168. std::string changeVars;
  169. if (cname && !optional) {
  170. std::string cnameString;
  171. if (!cmSystemTools::FileIsFullPath(cname)) {
  172. cnameString = cmSystemTools::FindProgram(cname);
  173. } else {
  174. cnameString = cname;
  175. }
  176. std::string pathString = path;
  177. // get rid of potentially multiple slashes:
  178. cmSystemTools::ConvertToUnixSlashes(cnameString);
  179. cmSystemTools::ConvertToUnixSlashes(pathString);
  180. if (cnameString != pathString) {
  181. const char* cvars =
  182. this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  183. "__CMAKE_DELETE_CACHE_CHANGE_VARS_");
  184. if (cvars) {
  185. changeVars += cvars;
  186. changeVars += ";";
  187. }
  188. changeVars += langComp;
  189. changeVars += ";";
  190. changeVars += cname;
  191. this->GetCMakeInstance()->GetState()->SetGlobalProperty(
  192. "__CMAKE_DELETE_CACHE_CHANGE_VARS_", changeVars.c_str());
  193. }
  194. }
  195. }
  196. void cmGlobalGenerator::AddBuildExportSet(cmExportBuildFileGenerator* gen)
  197. {
  198. this->BuildExportSets[gen->GetMainExportFileName()] = gen;
  199. }
  200. void cmGlobalGenerator::AddBuildExportExportSet(
  201. cmExportBuildFileGenerator* gen)
  202. {
  203. this->BuildExportSets[gen->GetMainExportFileName()] = gen;
  204. this->BuildExportExportSets[gen->GetMainExportFileName()] = gen;
  205. }
  206. bool cmGlobalGenerator::GenerateImportFile(const std::string& file)
  207. {
  208. std::map<std::string, cmExportBuildFileGenerator*>::iterator it =
  209. this->BuildExportSets.find(file);
  210. if (it != this->BuildExportSets.end()) {
  211. bool result = it->second->GenerateImportFile();
  212. if (!this->ConfigureDoneCMP0026AndCMP0024) {
  213. for (std::vector<cmMakefile*>::const_iterator mit =
  214. this->Makefiles.begin();
  215. mit != this->Makefiles.end(); ++mit) {
  216. (*mit)->RemoveExportBuildFileGeneratorCMP0024(it->second);
  217. }
  218. }
  219. delete it->second;
  220. it->second = CM_NULLPTR;
  221. this->BuildExportSets.erase(it);
  222. return result;
  223. }
  224. return false;
  225. }
  226. void cmGlobalGenerator::ForceLinkerLanguages()
  227. {
  228. }
  229. bool cmGlobalGenerator::IsExportedTargetsFile(
  230. const std::string& filename) const
  231. {
  232. const std::map<std::string, cmExportBuildFileGenerator*>::const_iterator it =
  233. this->BuildExportSets.find(filename);
  234. if (it == this->BuildExportSets.end()) {
  235. return false;
  236. }
  237. return this->BuildExportExportSets.find(filename) ==
  238. this->BuildExportExportSets.end();
  239. }
  240. // Find the make program for the generator, required for try compiles
  241. void cmGlobalGenerator::FindMakeProgram(cmMakefile* mf)
  242. {
  243. if (this->FindMakeProgramFile.empty()) {
  244. cmSystemTools::Error(
  245. "Generator implementation error, "
  246. "all generators must specify this->FindMakeProgramFile");
  247. }
  248. if (!mf->GetDefinition("CMAKE_MAKE_PROGRAM") ||
  249. cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM"))) {
  250. std::string setMakeProgram =
  251. mf->GetModulesFile(this->FindMakeProgramFile.c_str());
  252. if (!setMakeProgram.empty()) {
  253. mf->ReadListFile(setMakeProgram.c_str());
  254. }
  255. }
  256. if (!mf->GetDefinition("CMAKE_MAKE_PROGRAM") ||
  257. cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM"))) {
  258. std::ostringstream err;
  259. err << "CMake was unable to find a build program corresponding to \""
  260. << this->GetName() << "\". CMAKE_MAKE_PROGRAM is not set. You "
  261. << "probably need to select a different build tool.";
  262. cmSystemTools::Error(err.str().c_str());
  263. cmSystemTools::SetFatalErrorOccured();
  264. return;
  265. }
  266. std::string makeProgram = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  267. // if there are spaces in the make program use short path
  268. // but do not short path the actual program name, as
  269. // this can cause trouble with VSExpress
  270. if (makeProgram.find(' ') != makeProgram.npos) {
  271. std::string dir;
  272. std::string file;
  273. cmSystemTools::SplitProgramPath(makeProgram, dir, file);
  274. std::string saveFile = file;
  275. cmSystemTools::GetShortPath(makeProgram, makeProgram);
  276. cmSystemTools::SplitProgramPath(makeProgram, dir, file);
  277. makeProgram = dir;
  278. makeProgram += "/";
  279. makeProgram += saveFile;
  280. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM", makeProgram.c_str(),
  281. "make program", cmState::FILEPATH);
  282. }
  283. }
  284. bool cmGlobalGenerator::CheckLanguages(
  285. std::vector<std::string> const& /* languages */, cmMakefile* /* mf */) const
  286. {
  287. return true;
  288. }
  289. // enable the given language
  290. //
  291. // The following files are loaded in this order:
  292. //
  293. // First figure out what OS we are running on:
  294. //
  295. // CMakeSystem.cmake - configured file created by CMakeDetermineSystem.cmake
  296. // CMakeDetermineSystem.cmake - figure out os info and create
  297. // CMakeSystem.cmake IF CMAKE_SYSTEM
  298. // not set
  299. // CMakeSystem.cmake - configured file created by
  300. // CMakeDetermineSystem.cmake IF CMAKE_SYSTEM_LOADED
  301. // CMakeSystemSpecificInitialize.cmake
  302. // - includes Platform/${CMAKE_SYSTEM_NAME}-Initialize.cmake
  303. // Next try and enable all languages found in the languages vector
  304. //
  305. // FOREACH LANG in languages
  306. // CMake(LANG)Compiler.cmake - configured file create by
  307. // CMakeDetermine(LANG)Compiler.cmake
  308. // CMakeDetermine(LANG)Compiler.cmake - Finds compiler for LANG and
  309. // creates CMake(LANG)Compiler.cmake
  310. // CMake(LANG)Compiler.cmake - configured file created by
  311. // CMakeDetermine(LANG)Compiler.cmake
  312. //
  313. // CMakeSystemSpecificInformation.cmake
  314. // - includes Platform/${CMAKE_SYSTEM_NAME}.cmake
  315. // may use compiler stuff
  316. // FOREACH LANG in languages
  317. // CMake(LANG)Information.cmake
  318. // - loads Platform/${CMAKE_SYSTEM_NAME}-${COMPILER}.cmake
  319. // CMakeTest(LANG)Compiler.cmake
  320. // - Make sure the compiler works with a try compile if
  321. // CMakeDetermine(LANG) was loaded
  322. //
  323. // Now load a few files that can override values set in any of the above
  324. // (PROJECTNAME)Compatibility.cmake
  325. // - load any backwards compatibility stuff for current project
  326. // ${CMAKE_USER_MAKE_RULES_OVERRIDE}
  327. // - allow users a chance to override system variables
  328. //
  329. //
  330. void cmGlobalGenerator::EnableLanguage(
  331. std::vector<std::string> const& languages, cmMakefile* mf, bool optional)
  332. {
  333. if (languages.empty()) {
  334. cmSystemTools::Error("EnableLanguage must have a lang specified!");
  335. cmSystemTools::SetFatalErrorOccured();
  336. return;
  337. }
  338. std::set<std::string> cur_languages(languages.begin(), languages.end());
  339. for (std::set<std::string>::iterator li = cur_languages.begin();
  340. li != cur_languages.end(); ++li) {
  341. if (!this->LanguagesInProgress.insert(*li).second) {
  342. std::ostringstream e;
  343. e << "Language '" << *li << "' is currently being enabled. "
  344. "Recursive call not allowed.";
  345. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  346. cmSystemTools::SetFatalErrorOccured();
  347. return;
  348. }
  349. }
  350. if (this->TryCompileOuterMakefile) {
  351. // In a try-compile we can only enable languages provided by caller.
  352. for (std::vector<std::string>::const_iterator li = languages.begin();
  353. li != languages.end(); ++li) {
  354. if (*li == "NONE") {
  355. this->SetLanguageEnabled("NONE", mf);
  356. } else {
  357. const char* lang = li->c_str();
  358. if (this->LanguagesReady.find(lang) == this->LanguagesReady.end()) {
  359. std::ostringstream e;
  360. e << "The test project needs language " << lang
  361. << " which is not enabled.";
  362. this->TryCompileOuterMakefile->IssueMessage(cmake::FATAL_ERROR,
  363. e.str());
  364. cmSystemTools::SetFatalErrorOccured();
  365. return;
  366. }
  367. }
  368. }
  369. }
  370. bool fatalError = false;
  371. mf->AddDefinition("RUN_CONFIGURE", true);
  372. std::string rootBin = this->CMakeInstance->GetHomeOutputDirectory();
  373. rootBin += cmake::GetCMakeFilesDirectory();
  374. // If the configuration files path has been set,
  375. // then we are in a try compile and need to copy the enable language
  376. // files from the parent cmake bin dir, into the try compile bin dir
  377. if (!this->ConfiguredFilesPath.empty()) {
  378. rootBin = this->ConfiguredFilesPath;
  379. }
  380. rootBin += "/";
  381. rootBin += cmVersion::GetCMakeVersion();
  382. // set the dir for parent files so they can be used by modules
  383. mf->AddDefinition("CMAKE_PLATFORM_INFO_DIR", rootBin.c_str());
  384. // find and make sure CMAKE_MAKE_PROGRAM is defined
  385. this->FindMakeProgram(mf);
  386. if (!this->CheckLanguages(languages, mf)) {
  387. return;
  388. }
  389. // try and load the CMakeSystem.cmake if it is there
  390. std::string fpath = rootBin;
  391. bool const readCMakeSystem = !mf->GetDefinition("CMAKE_SYSTEM_LOADED");
  392. if (readCMakeSystem) {
  393. fpath += "/CMakeSystem.cmake";
  394. if (cmSystemTools::FileExists(fpath.c_str())) {
  395. mf->ReadListFile(fpath.c_str());
  396. }
  397. }
  398. // Load the CMakeDetermineSystem.cmake file and find out
  399. // what platform we are running on
  400. if (!mf->GetDefinition("CMAKE_SYSTEM")) {
  401. #if defined(_WIN32) && !defined(__CYGWIN__)
  402. /* Windows version number data. */
  403. OSVERSIONINFOEXW osviex;
  404. ZeroMemory(&osviex, sizeof(osviex));
  405. osviex.dwOSVersionInfoSize = sizeof(osviex);
  406. #ifdef KWSYS_WINDOWS_DEPRECATED_GetVersionEx
  407. #pragma warning(push)
  408. #pragma warning(disable : 4996)
  409. #endif
  410. GetVersionExW((OSVERSIONINFOW*)&osviex);
  411. #ifdef KWSYS_WINDOWS_DEPRECATED_GetVersionEx
  412. #pragma warning(pop)
  413. #endif
  414. std::ostringstream windowsVersionString;
  415. windowsVersionString << osviex.dwMajorVersion << "."
  416. << osviex.dwMinorVersion << "."
  417. << osviex.dwBuildNumber;
  418. windowsVersionString.str();
  419. mf->AddDefinition("CMAKE_HOST_SYSTEM_VERSION",
  420. windowsVersionString.str().c_str());
  421. #endif
  422. // Read the DetermineSystem file
  423. std::string systemFile = mf->GetModulesFile("CMakeDetermineSystem.cmake");
  424. mf->ReadListFile(systemFile.c_str());
  425. // load the CMakeSystem.cmake from the binary directory
  426. // this file is configured by the CMakeDetermineSystem.cmake file
  427. fpath = rootBin;
  428. fpath += "/CMakeSystem.cmake";
  429. mf->ReadListFile(fpath.c_str());
  430. }
  431. if (readCMakeSystem) {
  432. // Tell the generator about the target system.
  433. std::string system = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  434. if (!this->SetSystemName(system, mf)) {
  435. cmSystemTools::SetFatalErrorOccured();
  436. return;
  437. }
  438. // Tell the generator about the platform, if any.
  439. std::string platform = mf->GetSafeDefinition("CMAKE_GENERATOR_PLATFORM");
  440. if (!this->SetGeneratorPlatform(platform, mf)) {
  441. cmSystemTools::SetFatalErrorOccured();
  442. return;
  443. }
  444. // Tell the generator about the toolset, if any.
  445. std::string toolset = mf->GetSafeDefinition("CMAKE_GENERATOR_TOOLSET");
  446. if (!this->SetGeneratorToolset(toolset, mf)) {
  447. cmSystemTools::SetFatalErrorOccured();
  448. return;
  449. }
  450. }
  451. // **** Load the system specific initialization if not yet loaded
  452. if (!mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INITIALIZE_LOADED")) {
  453. fpath = mf->GetModulesFile("CMakeSystemSpecificInitialize.cmake");
  454. if (!mf->ReadListFile(fpath.c_str())) {
  455. cmSystemTools::Error("Could not find cmake module file: "
  456. "CMakeSystemSpecificInitialize.cmake");
  457. }
  458. }
  459. std::map<std::string, bool> needTestLanguage;
  460. std::map<std::string, bool> needSetLanguageEnabledMaps;
  461. // foreach language
  462. // load the CMakeDetermine(LANG)Compiler.cmake file to find
  463. // the compiler
  464. for (std::vector<std::string>::const_iterator l = languages.begin();
  465. l != languages.end(); ++l) {
  466. const char* lang = l->c_str();
  467. needSetLanguageEnabledMaps[lang] = false;
  468. if (*l == "NONE") {
  469. this->SetLanguageEnabled("NONE", mf);
  470. continue;
  471. }
  472. std::string loadedLang = "CMAKE_";
  473. loadedLang += lang;
  474. loadedLang += "_COMPILER_LOADED";
  475. if (!mf->GetDefinition(loadedLang)) {
  476. fpath = rootBin;
  477. fpath += "/CMake";
  478. fpath += lang;
  479. fpath += "Compiler.cmake";
  480. // If the existing build tree was already configured with this
  481. // version of CMake then try to load the configured file first
  482. // to avoid duplicate compiler tests.
  483. if (cmSystemTools::FileExists(fpath.c_str())) {
  484. if (!mf->ReadListFile(fpath.c_str())) {
  485. cmSystemTools::Error("Could not find cmake module file: ",
  486. fpath.c_str());
  487. }
  488. // if this file was found then the language was already determined
  489. // to be working
  490. needTestLanguage[lang] = false;
  491. this->SetLanguageEnabledFlag(lang, mf);
  492. needSetLanguageEnabledMaps[lang] = true;
  493. // this can only be called after loading CMake(LANG)Compiler.cmake
  494. }
  495. }
  496. if (!this->GetLanguageEnabled(lang)) {
  497. if (this->CMakeInstance->GetIsInTryCompile()) {
  498. cmSystemTools::Error("This should not have happened. "
  499. "If you see this message, you are probably "
  500. "using a broken CMakeLists.txt file or a "
  501. "problematic release of CMake");
  502. }
  503. // if the CMake(LANG)Compiler.cmake file was not found then
  504. // load CMakeDetermine(LANG)Compiler.cmake
  505. std::string determineCompiler = "CMakeDetermine";
  506. determineCompiler += lang;
  507. determineCompiler += "Compiler.cmake";
  508. std::string determineFile =
  509. mf->GetModulesFile(determineCompiler.c_str());
  510. if (!mf->ReadListFile(determineFile.c_str())) {
  511. cmSystemTools::Error("Could not find cmake module file: ",
  512. determineCompiler.c_str());
  513. }
  514. if (cmSystemTools::GetFatalErrorOccured()) {
  515. return;
  516. }
  517. needTestLanguage[lang] = true;
  518. // Some generators like visual studio should not use the env variables
  519. // So the global generator can specify that in this variable
  520. if (!mf->GetDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV")) {
  521. // put ${CMake_(LANG)_COMPILER_ENV_VAR}=${CMAKE_(LANG)_COMPILER
  522. // into the environment, in case user scripts want to run
  523. // configure, or sub cmakes
  524. std::string compilerName = "CMAKE_";
  525. compilerName += lang;
  526. compilerName += "_COMPILER";
  527. std::string compilerEnv = "CMAKE_";
  528. compilerEnv += lang;
  529. compilerEnv += "_COMPILER_ENV_VAR";
  530. std::string envVar = mf->GetRequiredDefinition(compilerEnv);
  531. std::string envVarValue = mf->GetRequiredDefinition(compilerName);
  532. std::string env = envVar;
  533. env += "=";
  534. env += envVarValue;
  535. cmSystemTools::PutEnv(env);
  536. }
  537. // if determineLanguage was called then load the file it
  538. // configures CMake(LANG)Compiler.cmake
  539. fpath = rootBin;
  540. fpath += "/CMake";
  541. fpath += lang;
  542. fpath += "Compiler.cmake";
  543. if (!mf->ReadListFile(fpath.c_str())) {
  544. cmSystemTools::Error("Could not find cmake module file: ",
  545. fpath.c_str());
  546. }
  547. this->SetLanguageEnabledFlag(lang, mf);
  548. needSetLanguageEnabledMaps[lang] = true;
  549. // this can only be called after loading CMake(LANG)Compiler.cmake
  550. // the language must be enabled for try compile to work, but we do
  551. // not know if it is a working compiler yet so set the test language
  552. // flag
  553. needTestLanguage[lang] = true;
  554. } // end if(!this->GetLanguageEnabled(lang) )
  555. } // end loop over languages
  556. // **** Load the system specific information if not yet loaded
  557. if (!mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED")) {
  558. fpath = mf->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  559. if (!mf->ReadListFile(fpath.c_str())) {
  560. cmSystemTools::Error("Could not find cmake module file: "
  561. "CMakeSystemSpecificInformation.cmake");
  562. }
  563. }
  564. // loop over languages again loading CMake(LANG)Information.cmake
  565. //
  566. for (std::vector<std::string>::const_iterator l = languages.begin();
  567. l != languages.end(); ++l) {
  568. const char* lang = l->c_str();
  569. if (*l == "NONE") {
  570. this->SetLanguageEnabled("NONE", mf);
  571. continue;
  572. }
  573. // Check that the compiler was found.
  574. std::string compilerName = "CMAKE_";
  575. compilerName += lang;
  576. compilerName += "_COMPILER";
  577. std::string compilerEnv = "CMAKE_";
  578. compilerEnv += lang;
  579. compilerEnv += "_COMPILER_ENV_VAR";
  580. std::ostringstream noCompiler;
  581. const char* compilerFile = mf->GetDefinition(compilerName);
  582. if (!compilerFile || !*compilerFile ||
  583. cmSystemTools::IsNOTFOUND(compilerFile)) {
  584. /* clang-format off */
  585. noCompiler <<
  586. "No " << compilerName << " could be found.\n"
  587. ;
  588. /* clang-format on */
  589. } else if (strcmp(lang, "RC") != 0 && strcmp(lang, "ASM_MASM") != 0) {
  590. if (!cmSystemTools::FileIsFullPath(compilerFile)) {
  591. /* clang-format off */
  592. noCompiler <<
  593. "The " << compilerName << ":\n"
  594. " " << compilerFile << "\n"
  595. "is not a full path and was not found in the PATH.\n"
  596. ;
  597. /* clang-format on */
  598. } else if (!cmSystemTools::FileExists(compilerFile)) {
  599. /* clang-format off */
  600. noCompiler <<
  601. "The " << compilerName << ":\n"
  602. " " << compilerFile << "\n"
  603. "is not a full path to an existing compiler tool.\n"
  604. ;
  605. /* clang-format on */
  606. }
  607. }
  608. if (!noCompiler.str().empty()) {
  609. // Skip testing this language since the compiler is not found.
  610. needTestLanguage[lang] = false;
  611. if (!optional) {
  612. // The compiler was not found and it is not optional. Remove
  613. // CMake(LANG)Compiler.cmake so we try again next time CMake runs.
  614. std::string compilerLangFile = rootBin;
  615. compilerLangFile += "/CMake";
  616. compilerLangFile += lang;
  617. compilerLangFile += "Compiler.cmake";
  618. cmSystemTools::RemoveFile(compilerLangFile);
  619. if (!this->CMakeInstance->GetIsInTryCompile()) {
  620. this->PrintCompilerAdvice(noCompiler, lang,
  621. mf->GetDefinition(compilerEnv));
  622. mf->IssueMessage(cmake::FATAL_ERROR, noCompiler.str());
  623. fatalError = true;
  624. }
  625. }
  626. }
  627. std::string langLoadedVar = "CMAKE_";
  628. langLoadedVar += lang;
  629. langLoadedVar += "_INFORMATION_LOADED";
  630. if (!mf->GetDefinition(langLoadedVar)) {
  631. fpath = "CMake";
  632. fpath += lang;
  633. fpath += "Information.cmake";
  634. std::string informationFile = mf->GetModulesFile(fpath.c_str());
  635. if (informationFile.empty()) {
  636. cmSystemTools::Error("Could not find cmake module file: ",
  637. fpath.c_str());
  638. } else if (!mf->ReadListFile(informationFile.c_str())) {
  639. cmSystemTools::Error("Could not process cmake module file: ",
  640. informationFile.c_str());
  641. }
  642. }
  643. if (needSetLanguageEnabledMaps[lang]) {
  644. this->SetLanguageEnabledMaps(lang, mf);
  645. }
  646. this->LanguagesReady.insert(lang);
  647. // Test the compiler for the language just setup
  648. // (but only if a compiler has been actually found)
  649. // At this point we should have enough info for a try compile
  650. // which is used in the backward stuff
  651. // If the language is untested then test it now with a try compile.
  652. if (needTestLanguage[lang]) {
  653. if (!this->CMakeInstance->GetIsInTryCompile()) {
  654. std::string testLang = "CMakeTest";
  655. testLang += lang;
  656. testLang += "Compiler.cmake";
  657. std::string ifpath = mf->GetModulesFile(testLang.c_str());
  658. if (!mf->ReadListFile(ifpath.c_str())) {
  659. cmSystemTools::Error("Could not find cmake module file: ",
  660. testLang.c_str());
  661. }
  662. std::string compilerWorks = "CMAKE_";
  663. compilerWorks += lang;
  664. compilerWorks += "_COMPILER_WORKS";
  665. // if the compiler did not work, then remove the
  666. // CMake(LANG)Compiler.cmake file so that it will get tested the
  667. // next time cmake is run
  668. if (!mf->IsOn(compilerWorks)) {
  669. std::string compilerLangFile = rootBin;
  670. compilerLangFile += "/CMake";
  671. compilerLangFile += lang;
  672. compilerLangFile += "Compiler.cmake";
  673. cmSystemTools::RemoveFile(compilerLangFile);
  674. }
  675. } // end if in try compile
  676. } // end need test language
  677. // Store the shared library flags so that we can satisfy CMP0018
  678. std::string sharedLibFlagsVar = "CMAKE_SHARED_LIBRARY_";
  679. sharedLibFlagsVar += lang;
  680. sharedLibFlagsVar += "_FLAGS";
  681. const char* sharedLibFlags = mf->GetSafeDefinition(sharedLibFlagsVar);
  682. if (sharedLibFlags) {
  683. this->LanguageToOriginalSharedLibFlags[lang] = sharedLibFlags;
  684. }
  685. // Translate compiler ids for compatibility.
  686. this->CheckCompilerIdCompatibility(mf, lang);
  687. } // end for each language
  688. // Now load files that can override any settings on the platform or for
  689. // the project First load the project compatibility file if it is in
  690. // cmake
  691. std::string projectCompatibility = cmSystemTools::GetCMakeRoot();
  692. projectCompatibility += "/Modules/";
  693. projectCompatibility += mf->GetSafeDefinition("PROJECT_NAME");
  694. projectCompatibility += "Compatibility.cmake";
  695. if (cmSystemTools::FileExists(projectCompatibility.c_str())) {
  696. mf->ReadListFile(projectCompatibility.c_str());
  697. }
  698. // Inform any extra generator of the new language.
  699. if (this->ExtraGenerator) {
  700. this->ExtraGenerator->EnableLanguage(languages, mf, false);
  701. }
  702. if (fatalError) {
  703. cmSystemTools::SetFatalErrorOccured();
  704. }
  705. for (std::set<std::string>::iterator li = cur_languages.begin();
  706. li != cur_languages.end(); ++li) {
  707. this->LanguagesInProgress.erase(*li);
  708. }
  709. }
  710. void cmGlobalGenerator::PrintCompilerAdvice(std::ostream& os,
  711. std::string const& lang,
  712. const char* envVar) const
  713. {
  714. // Subclasses override this method if they do not support this advice.
  715. os << "Tell CMake where to find the compiler by setting ";
  716. if (envVar) {
  717. os << "either the environment variable \"" << envVar << "\" or ";
  718. }
  719. os << "the CMake cache entry CMAKE_" << lang
  720. << "_COMPILER "
  721. "to the full path to the compiler, or to the compiler name "
  722. "if it is in the PATH.";
  723. }
  724. void cmGlobalGenerator::CheckCompilerIdCompatibility(
  725. cmMakefile* mf, std::string const& lang) const
  726. {
  727. std::string compilerIdVar = "CMAKE_" + lang + "_COMPILER_ID";
  728. const char* compilerId = mf->GetDefinition(compilerIdVar);
  729. if (!compilerId) {
  730. return;
  731. }
  732. if (strcmp(compilerId, "AppleClang") == 0) {
  733. switch (mf->GetPolicyStatus(cmPolicies::CMP0025)) {
  734. case cmPolicies::WARN:
  735. if (!this->CMakeInstance->GetIsInTryCompile() &&
  736. mf->PolicyOptionalWarningEnabled("CMAKE_POLICY_WARNING_CMP0025")) {
  737. std::ostringstream w;
  738. /* clang-format off */
  739. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0025) << "\n"
  740. "Converting " << lang <<
  741. " compiler id \"AppleClang\" to \"Clang\" for compatibility."
  742. ;
  743. /* clang-format on */
  744. mf->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  745. }
  746. case cmPolicies::OLD:
  747. // OLD behavior is to convert AppleClang to Clang.
  748. mf->AddDefinition(compilerIdVar, "Clang");
  749. break;
  750. case cmPolicies::REQUIRED_IF_USED:
  751. case cmPolicies::REQUIRED_ALWAYS:
  752. mf->IssueMessage(
  753. cmake::FATAL_ERROR,
  754. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0025));
  755. case cmPolicies::NEW:
  756. // NEW behavior is to keep AppleClang.
  757. break;
  758. }
  759. }
  760. if (strcmp(compilerId, "QCC") == 0) {
  761. switch (mf->GetPolicyStatus(cmPolicies::CMP0047)) {
  762. case cmPolicies::WARN:
  763. if (!this->CMakeInstance->GetIsInTryCompile() &&
  764. mf->PolicyOptionalWarningEnabled("CMAKE_POLICY_WARNING_CMP0047")) {
  765. std::ostringstream w;
  766. /* clang-format off */
  767. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0047) << "\n"
  768. "Converting " << lang <<
  769. " compiler id \"QCC\" to \"GNU\" for compatibility."
  770. ;
  771. /* clang-format on */
  772. mf->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  773. }
  774. case cmPolicies::OLD:
  775. // OLD behavior is to convert QCC to GNU.
  776. mf->AddDefinition(compilerIdVar, "GNU");
  777. if (lang == "C") {
  778. mf->AddDefinition("CMAKE_COMPILER_IS_GNUCC", "1");
  779. } else if (lang == "CXX") {
  780. mf->AddDefinition("CMAKE_COMPILER_IS_GNUCXX", "1");
  781. }
  782. break;
  783. case cmPolicies::REQUIRED_IF_USED:
  784. case cmPolicies::REQUIRED_ALWAYS:
  785. mf->IssueMessage(
  786. cmake::FATAL_ERROR,
  787. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0047));
  788. case cmPolicies::NEW:
  789. // NEW behavior is to keep QCC.
  790. break;
  791. }
  792. }
  793. }
  794. std::string cmGlobalGenerator::GetLanguageOutputExtension(
  795. cmSourceFile const& source) const
  796. {
  797. const std::string& lang = source.GetLanguage();
  798. if (!lang.empty()) {
  799. std::map<std::string, std::string>::const_iterator it =
  800. this->LanguageToOutputExtension.find(lang);
  801. if (it != this->LanguageToOutputExtension.end()) {
  802. return it->second;
  803. }
  804. } else {
  805. // if no language is found then check to see if it is already an
  806. // ouput extension for some language. In that case it should be ignored
  807. // and in this map, so it will not be compiled but will just be used.
  808. std::string const& ext = source.GetExtension();
  809. if (!ext.empty()) {
  810. if (this->OutputExtensions.count(ext)) {
  811. return ext;
  812. }
  813. }
  814. }
  815. return "";
  816. }
  817. std::string cmGlobalGenerator::GetLanguageFromExtension(const char* ext) const
  818. {
  819. // if there is an extension and it starts with . then move past the
  820. // . because the extensions are not stored with a . in the map
  821. if (ext && *ext == '.') {
  822. ++ext;
  823. }
  824. std::map<std::string, std::string>::const_iterator it =
  825. this->ExtensionToLanguage.find(ext);
  826. if (it != this->ExtensionToLanguage.end()) {
  827. return it->second;
  828. }
  829. return "";
  830. }
  831. /* SetLanguageEnabled() is now split in two parts:
  832. at first the enabled-flag is set. This can then be used in EnabledLanguage()
  833. for checking whether the language is already enabled. After setting this
  834. flag still the values from the cmake variables have to be copied into the
  835. internal maps, this is done in SetLanguageEnabledMaps() which is called
  836. after the system- and compiler specific files have been loaded.
  837. This split was done originally so that compiler-specific configuration
  838. files could change the object file extension
  839. (CMAKE_<LANG>_OUTPUT_EXTENSION) before the CMake variables were copied
  840. to the C++ maps.
  841. */
  842. void cmGlobalGenerator::SetLanguageEnabled(const std::string& l,
  843. cmMakefile* mf)
  844. {
  845. this->SetLanguageEnabledFlag(l, mf);
  846. this->SetLanguageEnabledMaps(l, mf);
  847. }
  848. void cmGlobalGenerator::SetLanguageEnabledFlag(const std::string& l,
  849. cmMakefile* mf)
  850. {
  851. this->CMakeInstance->GetState()->SetLanguageEnabled(l);
  852. // Fill the language-to-extension map with the current variable
  853. // settings to make sure it is available for the try_compile()
  854. // command source file signature. In SetLanguageEnabledMaps this
  855. // will be done again to account for any compiler- or
  856. // platform-specific entries.
  857. this->FillExtensionToLanguageMap(l, mf);
  858. }
  859. void cmGlobalGenerator::SetLanguageEnabledMaps(const std::string& l,
  860. cmMakefile* mf)
  861. {
  862. // use LanguageToLinkerPreference to detect whether this functions has
  863. // run before
  864. if (this->LanguageToLinkerPreference.find(l) !=
  865. this->LanguageToLinkerPreference.end()) {
  866. return;
  867. }
  868. std::string linkerPrefVar =
  869. std::string("CMAKE_") + std::string(l) + std::string("_LINKER_PREFERENCE");
  870. const char* linkerPref = mf->GetDefinition(linkerPrefVar);
  871. int preference = 0;
  872. if (linkerPref) {
  873. if (sscanf(linkerPref, "%d", &preference) != 1) {
  874. // backward compatibility: before 2.6 LINKER_PREFERENCE
  875. // was either "None" or "Preferred", and only the first character was
  876. // tested. So if there is a custom language out there and it is
  877. // "Preferred", set its preference high
  878. if (linkerPref[0] == 'P') {
  879. preference = 100;
  880. } else {
  881. preference = 0;
  882. }
  883. }
  884. }
  885. if (preference < 0) {
  886. std::string msg = linkerPrefVar;
  887. msg += " is negative, adjusting it to 0";
  888. cmSystemTools::Message(msg.c_str(), "Warning");
  889. preference = 0;
  890. }
  891. this->LanguageToLinkerPreference[l] = preference;
  892. std::string outputExtensionVar =
  893. std::string("CMAKE_") + std::string(l) + std::string("_OUTPUT_EXTENSION");
  894. const char* outputExtension = mf->GetDefinition(outputExtensionVar);
  895. if (outputExtension) {
  896. this->LanguageToOutputExtension[l] = outputExtension;
  897. this->OutputExtensions[outputExtension] = outputExtension;
  898. if (outputExtension[0] == '.') {
  899. this->OutputExtensions[outputExtension + 1] = outputExtension + 1;
  900. }
  901. }
  902. // The map was originally filled by SetLanguageEnabledFlag, but
  903. // since then the compiler- and platform-specific files have been
  904. // loaded which might have added more entries.
  905. this->FillExtensionToLanguageMap(l, mf);
  906. std::string ignoreExtensionsVar =
  907. std::string("CMAKE_") + std::string(l) + std::string("_IGNORE_EXTENSIONS");
  908. std::string ignoreExts = mf->GetSafeDefinition(ignoreExtensionsVar);
  909. std::vector<std::string> extensionList;
  910. cmSystemTools::ExpandListArgument(ignoreExts, extensionList);
  911. for (std::vector<std::string>::iterator i = extensionList.begin();
  912. i != extensionList.end(); ++i) {
  913. this->IgnoreExtensions[*i] = true;
  914. }
  915. }
  916. void cmGlobalGenerator::FillExtensionToLanguageMap(const std::string& l,
  917. cmMakefile* mf)
  918. {
  919. std::string extensionsVar = std::string("CMAKE_") + std::string(l) +
  920. std::string("_SOURCE_FILE_EXTENSIONS");
  921. std::string exts = mf->GetSafeDefinition(extensionsVar);
  922. std::vector<std::string> extensionList;
  923. cmSystemTools::ExpandListArgument(exts, extensionList);
  924. for (std::vector<std::string>::iterator i = extensionList.begin();
  925. i != extensionList.end(); ++i) {
  926. this->ExtensionToLanguage[*i] = l;
  927. }
  928. }
  929. const char* cmGlobalGenerator::GetGlobalSetting(std::string const& name) const
  930. {
  931. assert(!this->Makefiles.empty());
  932. return this->Makefiles[0]->GetDefinition(name);
  933. }
  934. bool cmGlobalGenerator::GlobalSettingIsOn(std::string const& name) const
  935. {
  936. assert(!this->Makefiles.empty());
  937. return this->Makefiles[0]->IsOn(name);
  938. }
  939. const char* cmGlobalGenerator::GetSafeGlobalSetting(
  940. std::string const& name) const
  941. {
  942. assert(!this->Makefiles.empty());
  943. return this->Makefiles[0]->GetSafeDefinition(name);
  944. }
  945. bool cmGlobalGenerator::IgnoreFile(const char* ext) const
  946. {
  947. if (!this->GetLanguageFromExtension(ext).empty()) {
  948. return false;
  949. }
  950. return (this->IgnoreExtensions.count(ext) > 0);
  951. }
  952. bool cmGlobalGenerator::GetLanguageEnabled(const std::string& l) const
  953. {
  954. return this->CMakeInstance->GetState()->GetLanguageEnabled(l);
  955. }
  956. void cmGlobalGenerator::ClearEnabledLanguages()
  957. {
  958. return this->CMakeInstance->GetState()->ClearEnabledLanguages();
  959. }
  960. void cmGlobalGenerator::CreateLocalGenerators()
  961. {
  962. cmDeleteAll(this->LocalGenerators);
  963. this->LocalGenerators.clear();
  964. this->LocalGenerators.reserve(this->Makefiles.size());
  965. for (std::vector<cmMakefile*>::const_iterator it = this->Makefiles.begin();
  966. it != this->Makefiles.end(); ++it) {
  967. this->LocalGenerators.push_back(this->CreateLocalGenerator(*it));
  968. }
  969. }
  970. void cmGlobalGenerator::Configure()
  971. {
  972. this->FirstTimeProgress = 0.0f;
  973. this->ClearGeneratorMembers();
  974. cmState::Snapshot snapshot = this->CMakeInstance->GetCurrentSnapshot();
  975. snapshot.GetDirectory().SetCurrentSource(
  976. this->CMakeInstance->GetHomeDirectory());
  977. snapshot.GetDirectory().SetCurrentBinary(
  978. this->CMakeInstance->GetHomeOutputDirectory());
  979. cmMakefile* dirMf = new cmMakefile(this, snapshot);
  980. this->Makefiles.push_back(dirMf);
  981. this->IndexMakefile(dirMf);
  982. this->BinaryDirectories.insert(
  983. this->CMakeInstance->GetHomeOutputDirectory());
  984. // now do it
  985. this->ConfigureDoneCMP0026AndCMP0024 = false;
  986. dirMf->Configure();
  987. dirMf->EnforceDirectoryLevelRules();
  988. this->ConfigureDoneCMP0026AndCMP0024 = true;
  989. // Put a copy of each global target in every directory.
  990. std::vector<GlobalTargetInfo> globalTargets;
  991. this->CreateDefaultGlobalTargets(globalTargets);
  992. for (unsigned int i = 0; i < this->Makefiles.size(); ++i) {
  993. cmMakefile* mf = this->Makefiles[i];
  994. cmTargets* targets = &(mf->GetTargets());
  995. for (std::vector<GlobalTargetInfo>::iterator gti = globalTargets.begin();
  996. gti != globalTargets.end(); ++gti) {
  997. targets->insert(
  998. cmTargets::value_type(gti->Name, this->CreateGlobalTarget(*gti, mf)));
  999. }
  1000. }
  1001. // update the cache entry for the number of local generators, this is used
  1002. // for progress
  1003. char num[100];
  1004. sprintf(num, "%d", static_cast<int>(this->Makefiles.size()));
  1005. this->GetCMakeInstance()->AddCacheEntry("CMAKE_NUMBER_OF_MAKEFILES", num,
  1006. "number of local generators",
  1007. cmState::INTERNAL);
  1008. // check for link libraries and include directories containing "NOTFOUND"
  1009. // and for infinite loops
  1010. this->CheckTargetProperties();
  1011. if (this->CMakeInstance->GetWorkingMode() == cmake::NORMAL_MODE) {
  1012. std::ostringstream msg;
  1013. if (cmSystemTools::GetErrorOccuredFlag()) {
  1014. msg << "Configuring incomplete, errors occurred!";
  1015. const char* logs[] = { "CMakeOutput.log", "CMakeError.log", CM_NULLPTR };
  1016. for (const char** log = logs; *log; ++log) {
  1017. std::string f = this->CMakeInstance->GetHomeOutputDirectory();
  1018. f += this->CMakeInstance->GetCMakeFilesDirectory();
  1019. f += "/";
  1020. f += *log;
  1021. if (cmSystemTools::FileExists(f.c_str())) {
  1022. msg << "\nSee also \"" << f << "\".";
  1023. }
  1024. }
  1025. } else {
  1026. msg << "Configuring done";
  1027. }
  1028. this->CMakeInstance->UpdateProgress(msg.str().c_str(), -1);
  1029. }
  1030. }
  1031. void cmGlobalGenerator::CreateGenerationObjects(TargetTypes targetTypes)
  1032. {
  1033. this->CreateLocalGenerators();
  1034. this->CreateGeneratorTargets(targetTypes);
  1035. this->ComputeBuildFileGenerators();
  1036. }
  1037. void cmGlobalGenerator::CreateImportedGenerationObjects(
  1038. cmMakefile* mf, const std::vector<std::string>& targets,
  1039. std::vector<const cmGeneratorTarget*>& exports)
  1040. {
  1041. this->CreateGenerationObjects(ImportedOnly);
  1042. std::vector<cmMakefile*>::iterator mfit =
  1043. std::find(this->Makefiles.begin(), this->Makefiles.end(), mf);
  1044. cmLocalGenerator* lg =
  1045. this->LocalGenerators[std::distance(this->Makefiles.begin(), mfit)];
  1046. for (std::vector<std::string>::const_iterator it = targets.begin();
  1047. it != targets.end(); ++it) {
  1048. cmGeneratorTarget* gt = lg->FindGeneratorTargetToUse(*it);
  1049. if (gt) {
  1050. exports.push_back(gt);
  1051. }
  1052. }
  1053. }
  1054. cmExportBuildFileGenerator* cmGlobalGenerator::GetExportedTargetsFile(
  1055. const std::string& filename) const
  1056. {
  1057. std::map<std::string, cmExportBuildFileGenerator*>::const_iterator it =
  1058. this->BuildExportSets.find(filename);
  1059. return it == this->BuildExportSets.end() ? CM_NULLPTR : it->second;
  1060. }
  1061. void cmGlobalGenerator::AddCMP0042WarnTarget(const std::string& target)
  1062. {
  1063. this->CMP0042WarnTargets.insert(target);
  1064. }
  1065. bool cmGlobalGenerator::CheckALLOW_DUPLICATE_CUSTOM_TARGETS() const
  1066. {
  1067. // If the property is not enabled then okay.
  1068. if (!this->CMakeInstance->GetState()->GetGlobalPropertyAsBool(
  1069. "ALLOW_DUPLICATE_CUSTOM_TARGETS")) {
  1070. return true;
  1071. }
  1072. // This generator does not support duplicate custom targets.
  1073. std::ostringstream e;
  1074. e << "This project has enabled the ALLOW_DUPLICATE_CUSTOM_TARGETS "
  1075. << "global property. "
  1076. << "The \"" << this->GetName() << "\" generator does not support "
  1077. << "duplicate custom targets. "
  1078. << "Consider using a Makefiles generator or fix the project to not "
  1079. << "use duplicate target names.";
  1080. cmSystemTools::Error(e.str().c_str());
  1081. return false;
  1082. }
  1083. void cmGlobalGenerator::ComputeBuildFileGenerators()
  1084. {
  1085. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i) {
  1086. std::vector<cmExportBuildFileGenerator*> gens =
  1087. this->Makefiles[i]->GetExportBuildFileGenerators();
  1088. for (std::vector<cmExportBuildFileGenerator*>::const_iterator it =
  1089. gens.begin();
  1090. it != gens.end(); ++it) {
  1091. (*it)->Compute(this->LocalGenerators[i]);
  1092. }
  1093. }
  1094. }
  1095. bool cmGlobalGenerator::Compute()
  1096. {
  1097. // Some generators track files replaced during the Generate.
  1098. // Start with an empty vector:
  1099. this->FilesReplacedDuringGenerate.clear();
  1100. // clear targets to issue warning CMP0042 for
  1101. this->CMP0042WarnTargets.clear();
  1102. // Check whether this generator is allowed to run.
  1103. if (!this->CheckALLOW_DUPLICATE_CUSTOM_TARGETS()) {
  1104. return false;
  1105. }
  1106. this->FinalizeTargetCompileInfo();
  1107. this->CreateGenerationObjects();
  1108. // at this point this->LocalGenerators has been filled,
  1109. // so create the map from project name to vector of local generators
  1110. this->FillProjectMap();
  1111. #ifdef CMAKE_BUILD_WITH_CMAKE
  1112. // Iterate through all targets and set up automoc for those which have
  1113. // the AUTOMOC, AUTOUIC or AUTORCC property set
  1114. std::vector<cmGeneratorTarget const*> autogenTargets =
  1115. this->CreateQtAutoGeneratorsTargets();
  1116. #endif
  1117. unsigned int i;
  1118. // Add generator specific helper commands
  1119. for (i = 0; i < this->LocalGenerators.size(); ++i) {
  1120. this->LocalGenerators[i]->AddHelperCommands();
  1121. }
  1122. #ifdef CMAKE_BUILD_WITH_CMAKE
  1123. for (std::vector<cmGeneratorTarget const*>::iterator it =
  1124. autogenTargets.begin();
  1125. it != autogenTargets.end(); ++it) {
  1126. cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(*it);
  1127. }
  1128. #endif
  1129. for (i = 0; i < this->LocalGenerators.size(); ++i) {
  1130. cmMakefile* mf = this->LocalGenerators[i]->GetMakefile();
  1131. std::vector<cmInstallGenerator*>& gens = mf->GetInstallGenerators();
  1132. for (std::vector<cmInstallGenerator*>::const_iterator git = gens.begin();
  1133. git != gens.end(); ++git) {
  1134. (*git)->Compute(this->LocalGenerators[i]);
  1135. }
  1136. }
  1137. this->AddExtraIDETargets();
  1138. // Trace the dependencies, after that no custom commands should be added
  1139. // because their dependencies might not be handled correctly
  1140. for (i = 0; i < this->LocalGenerators.size(); ++i) {
  1141. this->LocalGenerators[i]->TraceDependencies();
  1142. }
  1143. this->ForceLinkerLanguages();
  1144. // Compute the manifest of main targets generated.
  1145. for (i = 0; i < this->LocalGenerators.size(); ++i) {
  1146. this->LocalGenerators[i]->ComputeTargetManifest();
  1147. }
  1148. // Compute the inter-target dependencies.
  1149. if (!this->ComputeTargetDepends()) {
  1150. return false;
  1151. }
  1152. for (i = 0; i < this->LocalGenerators.size(); ++i) {
  1153. this->LocalGenerators[i]->ComputeHomeRelativeOutputPath();
  1154. }
  1155. return true;
  1156. }
  1157. void cmGlobalGenerator::Generate()
  1158. {
  1159. // Create a map from local generator to the complete set of targets
  1160. // it builds by default.
  1161. this->InitializeProgressMarks();
  1162. this->ProcessEvaluationFiles();
  1163. // Generate project files
  1164. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i) {
  1165. this->SetCurrentMakefile(this->LocalGenerators[i]->GetMakefile());
  1166. this->LocalGenerators[i]->Generate();
  1167. if (!this->LocalGenerators[i]->GetMakefile()->IsOn(
  1168. "CMAKE_SKIP_INSTALL_RULES")) {
  1169. this->LocalGenerators[i]->GenerateInstallRules();
  1170. }
  1171. this->LocalGenerators[i]->GenerateTestFiles();
  1172. this->CMakeInstance->UpdateProgress(
  1173. "Generating", (static_cast<float>(i) + 1.0f) /
  1174. static_cast<float>(this->LocalGenerators.size()));
  1175. }
  1176. this->SetCurrentMakefile(CM_NULLPTR);
  1177. if (!this->GenerateCPackPropertiesFile()) {
  1178. this->GetCMakeInstance()->IssueMessage(
  1179. cmake::FATAL_ERROR, "Could not write CPack properties file.");
  1180. }
  1181. for (std::map<std::string, cmExportBuildFileGenerator*>::iterator it =
  1182. this->BuildExportSets.begin();
  1183. it != this->BuildExportSets.end(); ++it) {
  1184. if (!it->second->GenerateImportFile() &&
  1185. !cmSystemTools::GetErrorOccuredFlag()) {
  1186. this->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR,
  1187. "Could not write export file.");
  1188. return;
  1189. }
  1190. }
  1191. // Update rule hashes.
  1192. this->CheckRuleHashes();
  1193. this->WriteSummary();
  1194. if (this->ExtraGenerator != CM_NULLPTR) {
  1195. this->ExtraGenerator->Generate();
  1196. }
  1197. if (!this->CMP0042WarnTargets.empty()) {
  1198. std::ostringstream w;
  1199. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0042) << "\n";
  1200. w << "MACOSX_RPATH is not specified for"
  1201. " the following targets:\n";
  1202. for (std::set<std::string>::iterator iter =
  1203. this->CMP0042WarnTargets.begin();
  1204. iter != this->CMP0042WarnTargets.end(); ++iter) {
  1205. w << " " << *iter << "\n";
  1206. }
  1207. this->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  1208. }
  1209. this->CMakeInstance->UpdateProgress("Generating done", -1);
  1210. }
  1211. bool cmGlobalGenerator::ComputeTargetDepends()
  1212. {
  1213. cmComputeTargetDepends ctd(this);
  1214. if (!ctd.Compute()) {
  1215. return false;
  1216. }
  1217. std::vector<cmGeneratorTarget const*> const& targets = ctd.GetTargets();
  1218. for (std::vector<cmGeneratorTarget const*>::const_iterator ti =
  1219. targets.begin();
  1220. ti != targets.end(); ++ti) {
  1221. ctd.GetTargetDirectDepends(*ti, this->TargetDependencies[*ti]);
  1222. }
  1223. return true;
  1224. }
  1225. std::vector<const cmGeneratorTarget*>
  1226. cmGlobalGenerator::CreateQtAutoGeneratorsTargets()
  1227. {
  1228. std::vector<const cmGeneratorTarget*> autogenTargets;
  1229. #ifdef CMAKE_BUILD_WITH_CMAKE
  1230. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i) {
  1231. std::vector<cmGeneratorTarget*> targets =
  1232. this->LocalGenerators[i]->GetGeneratorTargets();
  1233. std::vector<cmGeneratorTarget*> filteredTargets;
  1234. filteredTargets.reserve(targets.size());
  1235. for (std::vector<cmGeneratorTarget*>::iterator ti = targets.begin();
  1236. ti != targets.end(); ++ti) {
  1237. if ((*ti)->GetType() == cmState::GLOBAL_TARGET) {
  1238. continue;
  1239. }
  1240. if ((*ti)->GetType() != cmState::EXECUTABLE &&
  1241. (*ti)->GetType() != cmState::STATIC_LIBRARY &&
  1242. (*ti)->GetType() != cmState::SHARED_LIBRARY &&
  1243. (*ti)->GetType() != cmState::MODULE_LIBRARY &&
  1244. (*ti)->GetType() != cmState::OBJECT_LIBRARY) {
  1245. continue;
  1246. }
  1247. if ((!(*ti)->GetPropertyAsBool("AUTOMOC") &&
  1248. !(*ti)->GetPropertyAsBool("AUTOUIC") &&
  1249. !(*ti)->GetPropertyAsBool("AUTORCC")) ||
  1250. (*ti)->IsImported()) {
  1251. continue;
  1252. }
  1253. // don't do anything if there is no Qt4 or Qt5Core (which contains moc):
  1254. cmMakefile* mf = (*ti)->Target->GetMakefile();
  1255. std::string qtMajorVersion = mf->GetSafeDefinition("QT_VERSION_MAJOR");
  1256. if (qtMajorVersion == "") {
  1257. qtMajorVersion = mf->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  1258. }
  1259. if (qtMajorVersion != "4" && qtMajorVersion != "5") {
  1260. continue;
  1261. }
  1262. cmGeneratorTarget* gt = *ti;
  1263. cmQtAutoGeneratorInitializer::InitializeAutogenSources(gt);
  1264. filteredTargets.push_back(gt);
  1265. }
  1266. for (std::vector<cmGeneratorTarget*>::iterator ti =
  1267. filteredTargets.begin();
  1268. ti != filteredTargets.end(); ++ti) {
  1269. cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  1270. this->LocalGenerators[i], *ti);
  1271. autogenTargets.push_back(*ti);
  1272. }
  1273. }
  1274. #endif
  1275. return autogenTargets;
  1276. }
  1277. cmLinkLineComputer* cmGlobalGenerator::CreateLinkLineComputer(
  1278. cmOutputConverter* outputConverter, cmState::Directory stateDir) const
  1279. {
  1280. return new cmLinkLineComputer(outputConverter, stateDir);
  1281. }
  1282. cmLinkLineComputer* cmGlobalGenerator::CreateMSVC60LinkLineComputer(
  1283. cmOutputConverter* outputConverter, cmState::Directory stateDir) const
  1284. {
  1285. return new cmMSVC60LinkLineComputer(outputConverter, stateDir);
  1286. }
  1287. void cmGlobalGenerator::FinalizeTargetCompileInfo()
  1288. {
  1289. std::vector<std::string> const langs =
  1290. this->CMakeInstance->GetState()->GetEnabledLanguages();
  1291. // Construct per-target generator information.
  1292. for (unsigned int i = 0; i < this->Makefiles.size(); ++i) {
  1293. cmMakefile* mf = this->Makefiles[i];
  1294. const cmStringRange noconfig_compile_definitions =
  1295. mf->GetCompileDefinitionsEntries();
  1296. const cmBacktraceRange noconfig_compile_definitions_bts =
  1297. mf->GetCompileDefinitionsBacktraces();
  1298. cmTargets& targets = mf->GetTargets();
  1299. for (cmTargets::iterator ti = targets.begin(); ti != targets.end(); ++ti) {
  1300. cmTarget* t = &ti->second;
  1301. if (t->GetType() == cmState::GLOBAL_TARGET) {
  1302. continue;
  1303. }
  1304. t->AppendBuildInterfaceIncludes();
  1305. if (t->GetType() == cmState::INTERFACE_LIBRARY) {
  1306. continue;
  1307. }
  1308. cmBacktraceRange::const_iterator btIt =
  1309. noconfig_compile_definitions_bts.begin();
  1310. for (cmStringRange::const_iterator
  1311. it = noconfig_compile_definitions.begin();
  1312. it != noconfig_compile_definitions.end(); ++it, ++btIt) {
  1313. t->InsertCompileDefinition(*it, *btIt);
  1314. }
  1315. cmPolicies::PolicyStatus polSt =
  1316. mf->GetPolicyStatus(cmPolicies::CMP0043);
  1317. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1318. std::vector<std::string> configs;
  1319. mf->GetConfigurations(configs);
  1320. for (std::vector<std::string>::const_iterator ci = configs.begin();
  1321. ci != configs.end(); ++ci) {
  1322. std::string defPropName = "COMPILE_DEFINITIONS_";
  1323. defPropName += cmSystemTools::UpperCase(*ci);
  1324. t->AppendProperty(defPropName, mf->GetProperty(defPropName));
  1325. }
  1326. }
  1327. }
  1328. // The standard include directories for each language
  1329. // should be treated as system include directories.
  1330. std::set<std::string> standardIncludesSet;
  1331. for (std::vector<std::string>::const_iterator li = langs.begin();
  1332. li != langs.end(); ++li) {
  1333. std::string const standardIncludesVar =
  1334. "CMAKE_" + *li + "_STANDARD_INCLUDE_DIRECTORIES";
  1335. std::string const standardIncludesStr =
  1336. mf->GetSafeDefinition(standardIncludesVar);
  1337. std::vector<std::string> standardIncludesVec;
  1338. cmSystemTools::ExpandListArgument(standardIncludesStr,
  1339. standardIncludesVec);
  1340. standardIncludesSet.insert(standardIncludesVec.begin(),
  1341. standardIncludesVec.end());
  1342. }
  1343. mf->AddSystemIncludeDirectories(standardIncludesSet);
  1344. }
  1345. }
  1346. void cmGlobalGenerator::CreateGeneratorTargets(
  1347. TargetTypes targetTypes, cmMakefile* mf, cmLocalGenerator* lg,
  1348. std::map<cmTarget*, cmGeneratorTarget*> const& importedMap)
  1349. {
  1350. if (targetTypes == AllTargets) {
  1351. cmTargets& targets = mf->GetTargets();
  1352. for (cmTargets::iterator ti = targets.begin(); ti != targets.end(); ++ti) {
  1353. cmTarget* t = &ti->second;
  1354. cmGeneratorTarget* gt = new cmGeneratorTarget(t, lg);
  1355. lg->AddGeneratorTarget(gt);
  1356. }
  1357. }
  1358. std::vector<cmTarget*> itgts = mf->GetImportedTargets();
  1359. for (std::vector<cmTarget*>::const_iterator j = itgts.begin();
  1360. j != itgts.end(); ++j) {
  1361. lg->AddImportedGeneratorTarget(importedMap.find(*j)->second);
  1362. }
  1363. }
  1364. void cmGlobalGenerator::CreateGeneratorTargets(TargetTypes targetTypes)
  1365. {
  1366. std::map<cmTarget*, cmGeneratorTarget*> importedMap;
  1367. for (unsigned int i = 0; i < this->Makefiles.size(); ++i) {
  1368. cmMakefile* mf = this->Makefiles[i];
  1369. for (std::vector<cmTarget*>::const_iterator j =
  1370. mf->GetOwnedImportedTargets().begin();
  1371. j != mf->GetOwnedImportedTargets().end(); ++j) {
  1372. cmLocalGenerator* lg = this->LocalGenerators[i];
  1373. cmGeneratorTarget* gt = new cmGeneratorTarget(*j, lg);
  1374. lg->AddOwnedImportedGeneratorTarget(gt);
  1375. importedMap[*j] = gt;
  1376. }
  1377. }
  1378. // Construct per-target generator information.
  1379. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i) {
  1380. this->CreateGeneratorTargets(targetTypes, this->Makefiles[i],
  1381. this->LocalGenerators[i], importedMap);
  1382. }
  1383. }
  1384. void cmGlobalGenerator::ClearGeneratorMembers()
  1385. {
  1386. cmDeleteAll(this->BuildExportSets);
  1387. this->BuildExportSets.clear();
  1388. cmDeleteAll(this->Makefiles);
  1389. this->Makefiles.clear();
  1390. cmDeleteAll(this->LocalGenerators);
  1391. this->LocalGenerators.clear();
  1392. this->ExportSets.clear();
  1393. this->TargetDependencies.clear();
  1394. this->TargetSearchIndex.clear();
  1395. this->GeneratorTargetSearchIndex.clear();
  1396. this->MakefileSearchIndex.clear();
  1397. this->ProjectMap.clear();
  1398. this->RuleHashes.clear();
  1399. this->DirectoryContentMap.clear();
  1400. this->BinaryDirectories.clear();
  1401. }
  1402. void cmGlobalGenerator::ComputeTargetObjectDirectory(
  1403. cmGeneratorTarget* /*unused*/) const
  1404. {
  1405. }
  1406. void cmGlobalGenerator::CheckTargetProperties()
  1407. {
  1408. std::map<std::string, std::string> notFoundMap;
  1409. // std::set<std::string> notFoundMap;
  1410. // after it is all done do a ConfigureFinalPass
  1411. cmState* state = this->GetCMakeInstance()->GetState();
  1412. for (unsigned int i = 0; i < this->Makefiles.size(); ++i) {
  1413. this->Makefiles[i]->ConfigureFinalPass();
  1414. cmTargets& targets = this->Makefiles[i]->GetTargets();
  1415. for (cmTargets::iterator l = targets.begin(); l != targets.end(); l++) {
  1416. if (l->second.GetType() == cmState::INTERFACE_LIBRARY) {
  1417. continue;
  1418. }
  1419. const cmTarget::LinkLibraryVectorType& libs =
  1420. l->second.GetOriginalLinkLibraries();
  1421. for (cmTarget::LinkLibraryVectorType::const_iterator lib = libs.begin();
  1422. lib != libs.end(); ++lib) {
  1423. if (lib->first.size() > 9 &&
  1424. cmSystemTools::IsNOTFOUND(lib->first.c_str())) {
  1425. std::string varName = lib->first.substr(0, lib->first.size() - 9);
  1426. if (state->GetCacheEntryPropertyAsBool(varName, "ADVANCED")) {
  1427. varName += " (ADVANCED)";
  1428. }
  1429. std::string text = notFoundMap[varName];
  1430. text += "\n linked by target \"";
  1431. text += l->second.GetName();
  1432. text += "\" in directory ";
  1433. text += this->Makefiles[i]->GetCurrentSourceDirectory();
  1434. notFoundMap[varName] = text;
  1435. }
  1436. }
  1437. std::vector<std::string> incs;
  1438. const char* incDirProp = l->second.GetProperty("INCLUDE_DIRECTORIES");
  1439. if (!incDirProp) {
  1440. continue;
  1441. }
  1442. std::string incDirs = cmGeneratorExpression::Preprocess(
  1443. incDirProp, cmGeneratorExpression::StripAllGeneratorExpressions);
  1444. cmSystemTools::ExpandListArgument(incDirs, incs);
  1445. for (std::vector<std::string>::const_iterator incDir = incs.begin();
  1446. incDir != incs.end(); ++incDir) {
  1447. if (incDir->size() > 9 && cmSystemTools::IsNOTFOUND(incDir->c_str())) {
  1448. std::string varName = incDir->substr(0, incDir->size() - 9);
  1449. if (state->GetCacheEntryPropertyAsBool(varName, "ADVANCED")) {
  1450. varName += " (ADVANCED)";
  1451. }
  1452. std::string text = notFoundMap[varName];
  1453. text += "\n used as include directory in directory ";
  1454. text += this->Makefiles[i]->GetCurrentSourceDirectory();
  1455. notFoundMap[varName] = text;
  1456. }
  1457. }
  1458. }
  1459. this->CMakeInstance->UpdateProgress(
  1460. "Configuring", 0.9f +
  1461. 0.1f * (static_cast<float>(i) + 1.0f) /
  1462. static_cast<float>(this->Makefiles.size()));
  1463. }
  1464. if (!notFoundMap.empty()) {
  1465. std::string notFoundVars;
  1466. for (std::map<std::string, std::string>::const_iterator ii =
  1467. notFoundMap.begin();
  1468. ii != notFoundMap.end(); ++ii) {
  1469. notFoundVars += ii->first;
  1470. notFoundVars += ii->second;
  1471. notFoundVars += "\n";
  1472. }
  1473. cmSystemTools::Error("The following variables are used in this project, "
  1474. "but they are set to NOTFOUND.\n"
  1475. "Please set them or make sure they are set and "
  1476. "tested correctly in the CMake files:\n",
  1477. notFoundVars.c_str());
  1478. }
  1479. }
  1480. int cmGlobalGenerator::TryCompile(const std::string& srcdir,
  1481. const std::string& bindir,
  1482. const std::string& projectName,
  1483. const std::string& target, bool fast,
  1484. std::string& output, cmMakefile* mf)
  1485. {
  1486. // if this is not set, then this is a first time configure
  1487. // and there is a good chance that the try compile stuff will
  1488. // take the bulk of the time, so try and guess some progress
  1489. // by getting closer and closer to 100 without actually getting there.
  1490. if (!this->CMakeInstance->GetState()->GetInitializedCacheValue(
  1491. "CMAKE_NUMBER_OF_MAKEFILES")) {
  1492. // If CMAKE_NUMBER_OF_MAKEFILES is not set
  1493. // we are in the first time progress and we have no
  1494. // idea how long it will be. So, just move 1/10th of the way
  1495. // there each time, and don't go over 95%
  1496. this->FirstTimeProgress += ((1.0f - this->FirstTimeProgress) / 30.0f);
  1497. if (this->FirstTimeProgress > 0.95f) {
  1498. this->FirstTimeProgress = 0.95f;
  1499. }
  1500. this->CMakeInstance->UpdateProgress("Configuring",
  1501. this->FirstTimeProgress);
  1502. }
  1503. std::string newTarget;
  1504. if (!target.empty()) {
  1505. newTarget += target;
  1506. #if 0
  1507. #if defined(_WIN32) || defined(__CYGWIN__)
  1508. std::string tmp = target;
  1509. // if the target does not already end in . something
  1510. // then assume .exe
  1511. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  1512. {
  1513. newTarget += ".exe";
  1514. }
  1515. #endif // WIN32
  1516. #endif
  1517. }
  1518. std::string config =
  1519. mf->GetSafeDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  1520. return this->Build(srcdir, bindir, projectName, newTarget, output, "",
  1521. config, false, fast, false, this->TryCompileTimeout);
  1522. }
  1523. void cmGlobalGenerator::GenerateBuildCommand(
  1524. std::vector<std::string>& makeCommand, const std::string& /*unused*/,
  1525. const std::string& /*unused*/, const std::string& /*unused*/,
  1526. const std::string& /*unused*/, const std::string& /*unused*/,
  1527. bool /*unused*/, bool /*unused*/, std::vector<std::string> const& /*unused*/)
  1528. {
  1529. makeCommand.push_back(
  1530. "cmGlobalGenerator::GenerateBuildCommand not implemented");
  1531. }
  1532. int cmGlobalGenerator::Build(const std::string& /*unused*/,
  1533. const std::string& bindir,
  1534. const std::string& projectName,
  1535. const std::string& target, std::string& output,
  1536. const std::string& makeCommandCSTR,
  1537. const std::string& config, bool clean, bool fast,
  1538. bool verbose, double timeout,
  1539. cmSystemTools::OutputOption outputflag,
  1540. std::vector<std::string> const& nativeOptions)
  1541. {
  1542. /**
  1543. * Run an executable command and put the stdout in output.
  1544. */
  1545. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1546. cmSystemTools::ChangeDirectory(bindir);
  1547. output += "Change Dir: ";
  1548. output += bindir;
  1549. output += "\n";
  1550. int retVal;
  1551. bool hideconsole = cmSystemTools::GetRunCommandHideConsole();
  1552. cmSystemTools::SetRunCommandHideConsole(true);
  1553. std::string outputBuffer;
  1554. std::string* outputPtr = &outputBuffer;
  1555. std::vector<std::string> makeCommand;
  1556. this->GenerateBuildCommand(makeCommand, makeCommandCSTR, projectName, bindir,
  1557. target, config, fast, verbose, nativeOptions);
  1558. // Workaround to convince VCExpress.exe to produce output.
  1559. if (outputflag == cmSystemTools::OUTPUT_PASSTHROUGH &&
  1560. !makeCommand.empty() &&
  1561. cmSystemTools::LowerCase(
  1562. cmSystemTools::GetFilenameName(makeCommand[0])) == "vcexpress.exe") {
  1563. outputflag = cmSystemTools::OUTPUT_FORWARD;
  1564. }
  1565. // should we do a clean first?
  1566. if (clean) {
  1567. std::vector<std::string> cleanCommand;
  1568. this->GenerateBuildCommand(cleanCommand, makeCommandCSTR, projectName,
  1569. bindir, "clean", config, fast, verbose);
  1570. output += "\nRun Clean Command:";
  1571. output += cmSystemTools::PrintSingleCommand(cleanCommand);
  1572. output += "\n";
  1573. if (!cmSystemTools::RunSingleCommand(cleanCommand, outputPtr, outputPtr,
  1574. &retVal, CM_NULLPTR, outputflag,
  1575. timeout)) {
  1576. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1577. cmSystemTools::Error("Generator: execution of make clean failed.");
  1578. output += *outputPtr;
  1579. output += "\nGenerator: execution of make clean failed.\n";
  1580. // return to the original directory
  1581. cmSystemTools::ChangeDirectory(cwd);
  1582. return 1;
  1583. }
  1584. output += *outputPtr;
  1585. }
  1586. // now build
  1587. std::string makeCommandStr = cmSystemTools::PrintSingleCommand(makeCommand);
  1588. output += "\nRun Build Command:";
  1589. output += makeCommandStr;
  1590. output += "\n";
  1591. if (!cmSystemTools::RunSingleCommand(makeCommand, outputPtr, outputPtr,
  1592. &retVal, CM_NULLPTR, outputflag,
  1593. timeout)) {
  1594. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1595. cmSystemTools::Error(
  1596. "Generator: execution of make failed. Make command was: ",
  1597. makeCommandStr.c_str());
  1598. output += *outputPtr;
  1599. output += "\nGenerator: execution of make failed. Make command was: " +
  1600. makeCommandStr + "\n";
  1601. // return to the original directory
  1602. cmSystemTools::ChangeDirectory(cwd);
  1603. return 1;
  1604. }
  1605. output += *outputPtr;
  1606. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1607. // The SGI MipsPro 7.3 compiler does not return an error code when
  1608. // the source has a #error in it! This is a work-around for such
  1609. // compilers.
  1610. if ((retVal == 0) && (output.find("#error") != std::string::npos)) {
  1611. retVal = 1;
  1612. }
  1613. cmSystemTools::ChangeDirectory(cwd);
  1614. return retVal;
  1615. }
  1616. std::string cmGlobalGenerator::GenerateCMakeBuildCommand(
  1617. const std::string& target, const std::string& config,
  1618. const std::string& native, bool ignoreErrors)
  1619. {
  1620. std::string makeCommand = cmSystemTools::GetCMakeCommand();
  1621. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  1622. makeCommand += " --build .";
  1623. if (!config.empty()) {
  1624. makeCommand += " --config \"";
  1625. makeCommand += config;
  1626. makeCommand += "\"";
  1627. }
  1628. if (!target.empty()) {
  1629. makeCommand += " --target \"";
  1630. makeCommand += target;
  1631. makeCommand += "\"";
  1632. }
  1633. const char* sep = " -- ";
  1634. if (ignoreErrors) {
  1635. const char* iflag = this->GetBuildIgnoreErrorsFlag();
  1636. if (iflag && *iflag) {
  1637. makeCommand += sep;
  1638. makeCommand += iflag;
  1639. sep = " ";
  1640. }
  1641. }
  1642. if (!native.empty()) {
  1643. makeCommand += sep;
  1644. makeCommand += native;
  1645. }
  1646. return makeCommand;
  1647. }
  1648. void cmGlobalGenerator::AddMakefile(cmMakefile* mf)
  1649. {
  1650. this->Makefiles.push_back(mf);
  1651. this->IndexMakefile(mf);
  1652. // update progress
  1653. // estimate how many lg there will be
  1654. const char* numGenC =
  1655. this->CMakeInstance->GetState()->GetInitializedCacheValue(
  1656. "CMAKE_NUMBER_OF_MAKEFILES");
  1657. if (!numGenC) {
  1658. // If CMAKE_NUMBER_OF_MAKEFILES is not set
  1659. // we are in the first time progress and we have no
  1660. // idea how long it will be. So, just move half way
  1661. // there each time, and don't go over 95%
  1662. this->FirstTimeProgress += ((1.0f - this->FirstTimeProgress) / 30.0f);
  1663. if (this->FirstTimeProgress > 0.95f) {
  1664. this->FirstTimeProgress = 0.95f;
  1665. }
  1666. this->CMakeInstance->UpdateProgress("Configuring",
  1667. this->FirstTimeProgress);
  1668. return;
  1669. }
  1670. int numGen = atoi(numGenC);
  1671. float prog = 0.9f * static_cast<float>(this->Makefiles.size()) /
  1672. static_cast<float>(numGen);
  1673. if (prog > 0.9f) {
  1674. prog = 0.9f;
  1675. }
  1676. this->CMakeInstance->UpdateProgress("Configuring", prog);
  1677. }
  1678. void cmGlobalGenerator::AddInstallComponent(const char* component)
  1679. {
  1680. if (component && *component) {
  1681. this->InstallComponents.insert(component);
  1682. }
  1683. }
  1684. void cmGlobalGenerator::EnableInstallTarget()
  1685. {
  1686. this->InstallTargetEnabled = true;
  1687. }
  1688. cmLocalGenerator* cmGlobalGenerator::CreateLocalGenerator(cmMakefile* mf)
  1689. {
  1690. return new cmLocalGenerator(this, mf);
  1691. }
  1692. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator* gen,
  1693. cmMakefile* mf)
  1694. {
  1695. this->SetConfiguredFilesPath(gen);
  1696. this->TryCompileOuterMakefile = mf;
  1697. const char* make =
  1698. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  1699. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  1700. "make program", cmState::FILEPATH);
  1701. // copy the enabled languages
  1702. this->GetCMakeInstance()->GetState()->SetEnabledLanguages(
  1703. gen->GetCMakeInstance()->GetState()->GetEnabledLanguages());
  1704. this->LanguagesReady = gen->LanguagesReady;
  1705. this->ExtensionToLanguage = gen->ExtensionToLanguage;
  1706. this->IgnoreExtensions = gen->IgnoreExtensions;
  1707. this->LanguageToOutputExtension = gen->LanguageToOutputExtension;
  1708. this->LanguageToLinkerPreference = gen->LanguageToLinkerPreference;
  1709. this->OutputExtensions = gen->OutputExtensions;
  1710. }
  1711. void cmGlobalGenerator::SetConfiguredFilesPath(cmGlobalGenerator* gen)
  1712. {
  1713. if (!gen->ConfiguredFilesPath.empty()) {
  1714. this->ConfiguredFilesPath = gen->ConfiguredFilesPath;
  1715. } else {
  1716. this->ConfiguredFilesPath = gen->CMakeInstance->GetHomeOutputDirectory();
  1717. this->ConfiguredFilesPath += cmake::GetCMakeFilesDirectory();
  1718. }
  1719. }
  1720. bool cmGlobalGenerator::IsExcluded(cmState::Snapshot const& rootSnp,
  1721. cmState::Snapshot const& snp_) const
  1722. {
  1723. cmState::Snapshot snp = snp_;
  1724. while (snp.IsValid()) {
  1725. if (snp == rootSnp) {
  1726. // No directory excludes itself.
  1727. return false;
  1728. }
  1729. if (snp.GetDirectory().GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  1730. // This directory is excluded from its parent.
  1731. return true;
  1732. }
  1733. snp = snp.GetBuildsystemDirectoryParent();
  1734. }
  1735. return false;
  1736. }
  1737. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1738. cmLocalGenerator* gen) const
  1739. {
  1740. assert(gen);
  1741. cmState::Snapshot rootSnp = root->GetStateSnapshot();
  1742. cmState::Snapshot snp = gen->GetStateSnapshot();
  1743. return this->IsExcluded(rootSnp, snp);
  1744. }
  1745. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1746. cmGeneratorTarget* target) const
  1747. {
  1748. if (target->GetType() == cmState::INTERFACE_LIBRARY ||
  1749. target->GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  1750. // This target is excluded from its directory.
  1751. return true;
  1752. }
  1753. // This target is included in its directory. Check whether the
  1754. // directory is excluded.
  1755. return this->IsExcluded(root, target->GetLocalGenerator());
  1756. }
  1757. void cmGlobalGenerator::GetEnabledLanguages(
  1758. std::vector<std::string>& lang) const
  1759. {
  1760. lang = this->CMakeInstance->GetState()->GetEnabledLanguages();
  1761. }
  1762. int cmGlobalGenerator::GetLinkerPreference(const std::string& lang) const
  1763. {
  1764. std::map<std::string, int>::const_iterator it =
  1765. this->LanguageToLinkerPreference.find(lang);
  1766. if (it != this->LanguageToLinkerPreference.end()) {
  1767. return it->second;
  1768. }
  1769. return 0;
  1770. }
  1771. void cmGlobalGenerator::FillProjectMap()
  1772. {
  1773. this->ProjectMap.clear(); // make sure we start with a clean map
  1774. unsigned int i;
  1775. for (i = 0; i < this->LocalGenerators.size(); ++i) {
  1776. // for each local generator add all projects
  1777. cmState::Snapshot snp = this->LocalGenerators[i]->GetStateSnapshot();
  1778. std::string name;
  1779. do {
  1780. std::string snpProjName = snp.GetProjectName();
  1781. if (name != snpProjName) {
  1782. name = snpProjName;
  1783. this->ProjectMap[name].push_back(this->LocalGenerators[i]);
  1784. }
  1785. snp = snp.GetBuildsystemDirectoryParent();
  1786. } while (snp.IsValid());
  1787. }
  1788. }
  1789. cmMakefile* cmGlobalGenerator::FindMakefile(const std::string& start_dir) const
  1790. {
  1791. MakefileMap::const_iterator i = this->MakefileSearchIndex.find(start_dir);
  1792. if (i != this->MakefileSearchIndex.end()) {
  1793. return i->second;
  1794. }
  1795. return CM_NULLPTR;
  1796. }
  1797. ///! Find a local generator by its startdirectory
  1798. cmLocalGenerator* cmGlobalGenerator::FindLocalGenerator(
  1799. const std::string& start_dir) const
  1800. {
  1801. for (std::vector<cmLocalGenerator*>::const_iterator it =
  1802. this->LocalGenerators.begin();
  1803. it != this->LocalGenerators.end(); ++it) {
  1804. std::string sd = (*it)->GetCurrentSourceDirectory();
  1805. if (sd == start_dir) {
  1806. return *it;
  1807. }
  1808. }
  1809. return CM_NULLPTR;
  1810. }
  1811. void cmGlobalGenerator::AddAlias(const std::string& name,
  1812. std::string const& tgtName)
  1813. {
  1814. this->AliasTargets[name] = tgtName;
  1815. }
  1816. bool cmGlobalGenerator::IsAlias(const std::string& name) const
  1817. {
  1818. return this->AliasTargets.find(name) != this->AliasTargets.end();
  1819. }
  1820. void cmGlobalGenerator::IndexTarget(cmTarget* t)
  1821. {
  1822. if (!t->IsImported() || t->IsImportedGloballyVisible()) {
  1823. this->TargetSearchIndex[t->GetName()] = t;
  1824. }
  1825. }
  1826. void cmGlobalGenerator::IndexGeneratorTarget(cmGeneratorTarget* gt)
  1827. {
  1828. if (!gt->IsImported() || gt->IsImportedGloballyVisible()) {
  1829. this->GeneratorTargetSearchIndex[gt->GetName()] = gt;
  1830. }
  1831. }
  1832. void cmGlobalGenerator::IndexMakefile(cmMakefile* mf)
  1833. {
  1834. // FIXME: add_subdirectory supports multiple build directories
  1835. // sharing the same source directory. We currently index only the
  1836. // first one, because that is what FindMakefile has always returned.
  1837. // All of its callers will need to be modified to support looking
  1838. // up directories by build directory path.
  1839. this->MakefileSearchIndex.insert(
  1840. MakefileMap::value_type(mf->GetCurrentSourceDirectory(), mf));
  1841. }
  1842. cmTarget* cmGlobalGenerator::FindTargetImpl(std::string const& name) const
  1843. {
  1844. TargetMap::const_iterator i = this->TargetSearchIndex.find(name);
  1845. if (i != this->TargetSearchIndex.end()) {
  1846. return i->second;
  1847. }
  1848. return CM_NULLPTR;
  1849. }
  1850. cmGeneratorTarget* cmGlobalGenerator::FindGeneratorTargetImpl(
  1851. std::string const& name) const
  1852. {
  1853. GeneratorTargetMap::const_iterator i =
  1854. this->GeneratorTargetSearchIndex.find(name);
  1855. if (i != this->GeneratorTargetSearchIndex.end()) {
  1856. return i->second;
  1857. }
  1858. return CM_NULLPTR;
  1859. }
  1860. cmTarget* cmGlobalGenerator::FindTarget(const std::string& name,
  1861. bool excludeAliases) const
  1862. {
  1863. if (!excludeAliases) {
  1864. std::map<std::string, std::string>::const_iterator ai =
  1865. this->AliasTargets.find(name);
  1866. if (ai != this->AliasTargets.end()) {
  1867. return this->FindTargetImpl(ai->second);
  1868. }
  1869. }
  1870. return this->FindTargetImpl(name);
  1871. }
  1872. cmGeneratorTarget* cmGlobalGenerator::FindGeneratorTarget(
  1873. const std::string& name) const
  1874. {
  1875. std::map<std::string, std::string>::const_iterator ai =
  1876. this->AliasTargets.find(name);
  1877. if (ai != this->AliasTargets.end()) {
  1878. return this->FindGeneratorTargetImpl(ai->second);
  1879. }
  1880. return this->FindGeneratorTargetImpl(name);
  1881. }
  1882. bool cmGlobalGenerator::NameResolvesToFramework(
  1883. const std::string& libname) const
  1884. {
  1885. if (cmSystemTools::IsPathToFramework(libname.c_str())) {
  1886. return true;
  1887. }
  1888. if (cmTarget* tgt = this->FindTarget(libname)) {
  1889. if (tgt->IsFrameworkOnApple()) {
  1890. return true;
  1891. }
  1892. }
  1893. return false;
  1894. }
  1895. inline std::string removeQuotes(const std::string& s)
  1896. {
  1897. if (s[0] == '\"' && s[s.size() - 1] == '\"') {
  1898. return s.substr(1, s.size() - 2);
  1899. }
  1900. return s;
  1901. }
  1902. void cmGlobalGenerator::CreateDefaultGlobalTargets(
  1903. std::vector<GlobalTargetInfo>& targets)
  1904. {
  1905. this->AddGlobalTarget_Package(targets);
  1906. this->AddGlobalTarget_PackageSource(targets);
  1907. this->AddGlobalTarget_Test(targets);
  1908. this->AddGlobalTarget_EditCache(targets);
  1909. this->AddGlobalTarget_RebuildCache(targets);
  1910. this->AddGlobalTarget_Install(targets);
  1911. }
  1912. void cmGlobalGenerator::AddGlobalTarget_Package(
  1913. std::vector<GlobalTargetInfo>& targets)
  1914. {
  1915. cmMakefile* mf = this->Makefiles[0];
  1916. const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir();
  1917. GlobalTargetInfo gti;
  1918. gti.Name = this->GetPackageTargetName();
  1919. gti.Message = "Run CPack packaging tool...";
  1920. gti.UsesTerminal = true;
  1921. gti.WorkingDir = mf->GetCurrentBinaryDirectory();
  1922. cmCustomCommandLine singleLine;
  1923. singleLine.push_back(cmSystemTools::GetCPackCommand());
  1924. if (cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.') {
  1925. singleLine.push_back("-C");
  1926. singleLine.push_back(cmakeCfgIntDir);
  1927. }
  1928. singleLine.push_back("--config");
  1929. std::string configFile = mf->GetCurrentBinaryDirectory();
  1930. configFile += "/CPackConfig.cmake";
  1931. std::string relConfigFile = "./CPackConfig.cmake";
  1932. singleLine.push_back(relConfigFile);
  1933. gti.CommandLines.push_back(singleLine);
  1934. if (this->GetPreinstallTargetName()) {
  1935. gti.Depends.push_back(this->GetPreinstallTargetName());
  1936. } else {
  1937. const char* noPackageAll =
  1938. mf->GetDefinition("CMAKE_SKIP_PACKAGE_ALL_DEPENDENCY");
  1939. if (!noPackageAll || cmSystemTools::IsOff(noPackageAll)) {
  1940. gti.Depends.push_back(this->GetAllTargetName());
  1941. }
  1942. }
  1943. if (cmSystemTools::FileExists(configFile.c_str())) {
  1944. targets.push_back(gti);
  1945. }
  1946. }
  1947. void cmGlobalGenerator::AddGlobalTarget_PackageSource(
  1948. std::vector<GlobalTargetInfo>& targets)
  1949. {
  1950. cmMakefile* mf = this->Makefiles[0];
  1951. const char* packageSourceTargetName = this->GetPackageSourceTargetName();
  1952. if (packageSourceTargetName) {
  1953. GlobalTargetInfo gti;
  1954. gti.Name = packageSourceTargetName;
  1955. gti.Message = "Run CPack packaging tool for source...";
  1956. gti.WorkingDir = mf->GetCurrentBinaryDirectory();
  1957. gti.UsesTerminal = true;
  1958. cmCustomCommandLine singleLine;
  1959. singleLine.push_back(cmSystemTools::GetCPackCommand());
  1960. singleLine.push_back("--config");
  1961. std::string configFile = mf->GetCurrentBinaryDirectory();
  1962. configFile += "/CPackSourceConfig.cmake";
  1963. std::string relConfigFile = "./CPackSourceConfig.cmake";
  1964. singleLine.push_back(relConfigFile);
  1965. if (cmSystemTools::FileExists(configFile.c_str())) {
  1966. singleLine.push_back(configFile);
  1967. gti.CommandLines.push_back(singleLine);
  1968. targets.push_back(gti);
  1969. }
  1970. }
  1971. }
  1972. void cmGlobalGenerator::AddGlobalTarget_Test(
  1973. std::vector<GlobalTargetInfo>& targets)
  1974. {
  1975. cmMakefile* mf = this->Makefiles[0];
  1976. const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir();
  1977. if (mf->IsOn("CMAKE_TESTING_ENABLED")) {
  1978. GlobalTargetInfo gti;
  1979. gti.Name = this->GetTestTargetName();
  1980. gti.Message = "Running tests...";
  1981. gti.UsesTerminal = true;
  1982. cmCustomCommandLine singleLine;
  1983. singleLine.push_back(cmSystemTools::GetCTestCommand());
  1984. singleLine.push_back("--force-new-ctest-process");
  1985. if (cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.') {
  1986. singleLine.push_back("-C");
  1987. singleLine.push_back(cmakeCfgIntDir);
  1988. } else // TODO: This is a hack. Should be something to do with the
  1989. // generator
  1990. {
  1991. singleLine.push_back("$(ARGS)");
  1992. }
  1993. gti.CommandLines.push_back(singleLine);
  1994. targets.push_back(gti);
  1995. }
  1996. }
  1997. void cmGlobalGenerator::AddGlobalTarget_EditCache(
  1998. std::vector<GlobalTargetInfo>& targets)
  1999. {
  2000. const char* editCacheTargetName = this->GetEditCacheTargetName();
  2001. if (editCacheTargetName) {
  2002. GlobalTargetInfo gti;
  2003. gti.Name = editCacheTargetName;
  2004. cmCustomCommandLine singleLine;
  2005. // Use generator preference for the edit_cache rule if it is defined.
  2006. std::string edit_cmd = this->GetEditCacheCommand();
  2007. if (!edit_cmd.empty()) {
  2008. singleLine.push_back(edit_cmd);
  2009. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  2010. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  2011. gti.Message = "Running CMake cache editor...";
  2012. gti.UsesTerminal = true;
  2013. gti.CommandLines.push_back(singleLine);
  2014. } else {
  2015. singleLine.push_back(cmSystemTools::GetCMakeCommand());
  2016. singleLine.push_back("-E");
  2017. singleLine.push_back("echo");
  2018. singleLine.push_back("No interactive CMake dialog available.");
  2019. gti.Message = "No interactive CMake dialog available...";
  2020. gti.UsesTerminal = false;
  2021. gti.CommandLines.push_back(singleLine);
  2022. }
  2023. targets.push_back(gti);
  2024. }
  2025. }
  2026. void cmGlobalGenerator::AddGlobalTarget_RebuildCache(
  2027. std::vector<GlobalTargetInfo>& targets)
  2028. {
  2029. const char* rebuildCacheTargetName = this->GetRebuildCacheTargetName();
  2030. if (rebuildCacheTargetName) {
  2031. GlobalTargetInfo gti;
  2032. gti.Name = rebuildCacheTargetName;
  2033. gti.Message = "Running CMake to regenerate build system...";
  2034. gti.UsesTerminal = true;
  2035. cmCustomCommandLine singleLine;
  2036. singleLine.push_back(cmSystemTools::GetCMakeCommand());
  2037. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  2038. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  2039. gti.CommandLines.push_back(singleLine);
  2040. targets.push_back(gti);
  2041. }
  2042. }
  2043. void cmGlobalGenerator::AddGlobalTarget_Install(
  2044. std::vector<GlobalTargetInfo>& targets)
  2045. {
  2046. cmMakefile* mf = this->Makefiles[0];
  2047. const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir();
  2048. bool skipInstallRules = mf->IsOn("CMAKE_SKIP_INSTALL_RULES");
  2049. if (this->InstallTargetEnabled && skipInstallRules) {
  2050. this->CMakeInstance->IssueMessage(
  2051. cmake::WARNING, "CMAKE_SKIP_INSTALL_RULES was enabled even though "
  2052. "installation rules have been specified",
  2053. mf->GetBacktrace());
  2054. } else if (this->InstallTargetEnabled && !skipInstallRules) {
  2055. if (!cmakeCfgIntDir || !*cmakeCfgIntDir || cmakeCfgIntDir[0] == '.') {
  2056. std::set<std::string>* componentsSet = &this->InstallComponents;
  2057. std::ostringstream ostr;
  2058. if (!componentsSet->empty()) {
  2059. ostr << "Available install components are: ";
  2060. ostr << cmWrap('"', *componentsSet, '"', " ");
  2061. } else {
  2062. ostr << "Only default component available";
  2063. }
  2064. GlobalTargetInfo gti;
  2065. gti.Name = "list_install_components";
  2066. gti.Message = ostr.str();
  2067. gti.UsesTerminal = false;
  2068. targets.push_back(gti);
  2069. }
  2070. std::string cmd = cmSystemTools::GetCMakeCommand();
  2071. GlobalTargetInfo gti;
  2072. gti.Name = this->GetInstallTargetName();
  2073. gti.Message = "Install the project...";
  2074. gti.UsesTerminal = true;
  2075. cmCustomCommandLine singleLine;
  2076. if (this->GetPreinstallTargetName()) {
  2077. gti.Depends.push_back(this->GetPreinstallTargetName());
  2078. } else {
  2079. const char* noall =
  2080. mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  2081. if (!noall || cmSystemTools::IsOff(noall)) {
  2082. gti.Depends.push_back(this->GetAllTargetName());
  2083. }
  2084. }
  2085. if (mf->GetDefinition("CMake_BINARY_DIR") &&
  2086. !mf->IsOn("CMAKE_CROSSCOMPILING")) {
  2087. // We are building CMake itself. We cannot use the original
  2088. // executable to install over itself. The generator will
  2089. // automatically convert this name to the build-time location.
  2090. cmd = "cmake";
  2091. }
  2092. singleLine.push_back(cmd);
  2093. if (cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.') {
  2094. std::string cfgArg = "-DBUILD_TYPE=";
  2095. bool iosPlatform = mf->PlatformIsAppleIos();
  2096. if (iosPlatform) {
  2097. cfgArg += "$(CONFIGURATION)";
  2098. singleLine.push_back(cfgArg);
  2099. cfgArg = "-DEFFECTIVE_PLATFORM_NAME=$(EFFECTIVE_PLATFORM_NAME)";
  2100. } else {
  2101. cfgArg += mf->GetDefinition("CMAKE_CFG_INTDIR");
  2102. }
  2103. singleLine.push_back(cfgArg);
  2104. }
  2105. singleLine.push_back("-P");
  2106. singleLine.push_back("cmake_install.cmake");
  2107. gti.CommandLines.push_back(singleLine);
  2108. targets.push_back(gti);
  2109. // install_local
  2110. if (const char* install_local = this->GetInstallLocalTargetName()) {
  2111. gti.Name = install_local;
  2112. gti.Message = "Installing only the local directory...";
  2113. gti.UsesTerminal = true;
  2114. gti.CommandLines.clear();
  2115. cmCustomCommandLine localCmdLine = singleLine;
  2116. localCmdLine.insert(localCmdLine.begin() + 1,
  2117. "-DCMAKE_INSTALL_LOCAL_ONLY=1");
  2118. gti.CommandLines.push_back(localCmdLine);
  2119. targets.push_back(gti);
  2120. }
  2121. // install_strip
  2122. const char* install_strip = this->GetInstallStripTargetName();
  2123. if ((install_strip != CM_NULLPTR) && (mf->IsSet("CMAKE_STRIP"))) {
  2124. gti.Name = install_strip;
  2125. gti.Message = "Installing the project stripped...";
  2126. gti.UsesTerminal = true;
  2127. gti.CommandLines.clear();
  2128. cmCustomCommandLine stripCmdLine = singleLine;
  2129. stripCmdLine.insert(stripCmdLine.begin() + 1,
  2130. "-DCMAKE_INSTALL_DO_STRIP=1");
  2131. gti.CommandLines.push_back(stripCmdLine);
  2132. targets.push_back(gti);
  2133. }
  2134. }
  2135. }
  2136. const char* cmGlobalGenerator::GetPredefinedTargetsFolder()
  2137. {
  2138. const char* prop = this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  2139. "PREDEFINED_TARGETS_FOLDER");
  2140. if (prop) {
  2141. return prop;
  2142. }
  2143. return "CMakePredefinedTargets";
  2144. }
  2145. bool cmGlobalGenerator::UseFolderProperty() const
  2146. {
  2147. const char* prop =
  2148. this->GetCMakeInstance()->GetState()->GetGlobalProperty("USE_FOLDERS");
  2149. // If this property is defined, let the setter turn this on or off...
  2150. //
  2151. if (prop) {
  2152. return cmSystemTools::IsOn(prop);
  2153. }
  2154. // By default, this feature is OFF, since it is not supported in the
  2155. // Visual Studio Express editions until VS11:
  2156. //
  2157. return false;
  2158. }
  2159. cmTarget cmGlobalGenerator::CreateGlobalTarget(GlobalTargetInfo const& gti,
  2160. cmMakefile* mf)
  2161. {
  2162. // Package
  2163. cmTarget target(gti.Name, cmState::GLOBAL_TARGET, cmTarget::VisibilityNormal,
  2164. mf);
  2165. target.SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  2166. std::vector<std::string> no_outputs;
  2167. std::vector<std::string> no_byproducts;
  2168. std::vector<std::string> no_depends;
  2169. // Store the custom command in the target.
  2170. cmCustomCommand cc(CM_NULLPTR, no_outputs, no_byproducts, no_depends,
  2171. gti.CommandLines, CM_NULLPTR, gti.WorkingDir.c_str());
  2172. cc.SetUsesTerminal(gti.UsesTerminal);
  2173. target.AddPostBuildCommand(cc);
  2174. if (!gti.Message.empty()) {
  2175. target.SetProperty("EchoString", gti.Message.c_str());
  2176. }
  2177. for (std::vector<std::string>::const_iterator dit = gti.Depends.begin();
  2178. dit != gti.Depends.end(); ++dit) {
  2179. target.AddUtility(*dit);
  2180. }
  2181. // Organize in the "predefined targets" folder:
  2182. //
  2183. if (this->UseFolderProperty()) {
  2184. target.SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  2185. }
  2186. return target;
  2187. }
  2188. std::string cmGlobalGenerator::GenerateRuleFile(
  2189. std::string const& output) const
  2190. {
  2191. std::string ruleFile = output;
  2192. ruleFile += ".rule";
  2193. const char* dir = this->GetCMakeCFGIntDir();
  2194. if (dir && dir[0] == '$') {
  2195. cmSystemTools::ReplaceString(ruleFile, dir,
  2196. cmake::GetCMakeFilesDirectory());
  2197. }
  2198. return ruleFile;
  2199. }
  2200. std::string cmGlobalGenerator::GetSharedLibFlagsForLanguage(
  2201. std::string const& l) const
  2202. {
  2203. std::map<std::string, std::string>::const_iterator it =
  2204. this->LanguageToOriginalSharedLibFlags.find(l);
  2205. if (it != this->LanguageToOriginalSharedLibFlags.end()) {
  2206. return it->second;
  2207. }
  2208. return "";
  2209. }
  2210. void cmGlobalGenerator::AppendDirectoryForConfig(const std::string& /*unused*/,
  2211. const std::string& /*unused*/,
  2212. const std::string& /*unused*/,
  2213. std::string& /*unused*/)
  2214. {
  2215. // Subclasses that support multiple configurations should implement
  2216. // this method to append the subdirectory for the given build
  2217. // configuration.
  2218. }
  2219. cmGlobalGenerator::TargetDependSet const&
  2220. cmGlobalGenerator::GetTargetDirectDepends(cmGeneratorTarget const* target)
  2221. {
  2222. return this->TargetDependencies[target];
  2223. }
  2224. bool cmGlobalGenerator::IsReservedTarget(std::string const& name)
  2225. {
  2226. // The following is a list of targets reserved
  2227. // by one or more of the cmake generators.
  2228. // Adding additional targets to this list will require a policy!
  2229. const char* reservedTargets[] = {
  2230. "all", "ALL_BUILD", "help", "install", "INSTALL",
  2231. "preinstall", "clean", "edit_cache", "rebuild_cache", "test",
  2232. "RUN_TESTS", "package", "PACKAGE", "package_source", "ZERO_CHECK"
  2233. };
  2234. return std::find(cmArrayBegin(reservedTargets), cmArrayEnd(reservedTargets),
  2235. name) != cmArrayEnd(reservedTargets);
  2236. }
  2237. void cmGlobalGenerator::SetExternalMakefileProjectGenerator(
  2238. cmExternalMakefileProjectGenerator* extraGenerator)
  2239. {
  2240. this->ExtraGenerator = extraGenerator;
  2241. if (this->ExtraGenerator != CM_NULLPTR) {
  2242. this->ExtraGenerator->SetGlobalGenerator(this);
  2243. }
  2244. }
  2245. std::string cmGlobalGenerator::GetExtraGeneratorName() const
  2246. {
  2247. return this->ExtraGenerator ? this->ExtraGenerator->GetName()
  2248. : std::string();
  2249. }
  2250. void cmGlobalGenerator::FileReplacedDuringGenerate(const std::string& filename)
  2251. {
  2252. this->FilesReplacedDuringGenerate.push_back(filename);
  2253. }
  2254. void cmGlobalGenerator::GetFilesReplacedDuringGenerate(
  2255. std::vector<std::string>& filenames)
  2256. {
  2257. filenames.clear();
  2258. std::copy(this->FilesReplacedDuringGenerate.begin(),
  2259. this->FilesReplacedDuringGenerate.end(),
  2260. std::back_inserter(filenames));
  2261. }
  2262. void cmGlobalGenerator::GetTargetSets(TargetDependSet& projectTargets,
  2263. TargetDependSet& originalTargets,
  2264. cmLocalGenerator* root,
  2265. GeneratorVector const& generators)
  2266. {
  2267. // loop over all local generators
  2268. for (std::vector<cmLocalGenerator*>::const_iterator i = generators.begin();
  2269. i != generators.end(); ++i) {
  2270. // check to make sure generator is not excluded
  2271. if (this->IsExcluded(root, *i)) {
  2272. continue;
  2273. }
  2274. // Get the targets in the makefile
  2275. std::vector<cmGeneratorTarget*> tgts = (*i)->GetGeneratorTargets();
  2276. // loop over all the targets
  2277. for (std::vector<cmGeneratorTarget*>::iterator l = tgts.begin();
  2278. l != tgts.end(); ++l) {
  2279. cmGeneratorTarget* target = *l;
  2280. if (this->IsRootOnlyTarget(target) &&
  2281. target->GetLocalGenerator() != root) {
  2282. continue;
  2283. }
  2284. // put the target in the set of original targets
  2285. originalTargets.insert(target);
  2286. // Get the set of targets that depend on target
  2287. this->AddTargetDepends(target, projectTargets);
  2288. }
  2289. }
  2290. }
  2291. bool cmGlobalGenerator::IsRootOnlyTarget(cmGeneratorTarget* target) const
  2292. {
  2293. return (target->GetType() == cmState::GLOBAL_TARGET ||
  2294. target->GetName() == this->GetAllTargetName());
  2295. }
  2296. void cmGlobalGenerator::AddTargetDepends(cmGeneratorTarget const* target,
  2297. TargetDependSet& projectTargets)
  2298. {
  2299. // add the target itself
  2300. if (projectTargets.insert(target).second) {
  2301. // This is the first time we have encountered the target.
  2302. // Recursively follow its dependencies.
  2303. TargetDependSet const& ts = this->GetTargetDirectDepends(target);
  2304. for (TargetDependSet::const_iterator i = ts.begin(); i != ts.end(); ++i) {
  2305. this->AddTargetDepends(*i, projectTargets);
  2306. }
  2307. }
  2308. }
  2309. void cmGlobalGenerator::AddToManifest(std::string const& f)
  2310. {
  2311. // Add to the content listing for the file's directory.
  2312. std::string dir = cmSystemTools::GetFilenamePath(f);
  2313. std::string file = cmSystemTools::GetFilenameName(f);
  2314. DirectoryContent& dc = this->DirectoryContentMap[dir];
  2315. dc.Generated.insert(file);
  2316. dc.All.insert(file);
  2317. }
  2318. std::set<std::string> const& cmGlobalGenerator::GetDirectoryContent(
  2319. std::string const& dir, bool needDisk)
  2320. {
  2321. DirectoryContent& dc = this->DirectoryContentMap[dir];
  2322. if (needDisk) {
  2323. long mt = cmSystemTools::ModifiedTime(dir);
  2324. if (mt != dc.LastDiskTime) {
  2325. // Reset to non-loaded directory content.
  2326. dc.All = dc.Generated;
  2327. // Load the directory content from disk.
  2328. cmsys::Directory d;
  2329. if (d.Load(dir)) {
  2330. unsigned long n = d.GetNumberOfFiles();
  2331. for (unsigned long i = 0; i < n; ++i) {
  2332. const char* f = d.GetFile(i);
  2333. if (strcmp(f, ".") != 0 && strcmp(f, "..") != 0) {
  2334. dc.All.insert(f);
  2335. }
  2336. }
  2337. }
  2338. dc.LastDiskTime = mt;
  2339. }
  2340. }
  2341. return dc.All;
  2342. }
  2343. void cmGlobalGenerator::AddRuleHash(const std::vector<std::string>& outputs,
  2344. std::string const& content)
  2345. {
  2346. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2347. // Ignore if there are no outputs.
  2348. if (outputs.empty()) {
  2349. return;
  2350. }
  2351. // Compute a hash of the rule.
  2352. RuleHash hash;
  2353. {
  2354. unsigned char const* data =
  2355. reinterpret_cast<unsigned char const*>(content.c_str());
  2356. int length = static_cast<int>(content.length());
  2357. cmsysMD5* sum = cmsysMD5_New();
  2358. cmsysMD5_Initialize(sum);
  2359. cmsysMD5_Append(sum, data, length);
  2360. cmsysMD5_FinalizeHex(sum, hash.Data);
  2361. cmsysMD5_Delete(sum);
  2362. }
  2363. // Shorten the output name (in expected use case).
  2364. cmOutputConverter converter(this->GetMakefiles()[0]->GetStateSnapshot());
  2365. std::string fname = converter.ConvertToRelativePath(
  2366. this->GetMakefiles()[0]->GetState()->GetBinaryDirectory(), outputs[0]);
  2367. // Associate the hash with this output.
  2368. this->RuleHashes[fname] = hash;
  2369. #else
  2370. (void)outputs;
  2371. (void)content;
  2372. #endif
  2373. }
  2374. void cmGlobalGenerator::CheckRuleHashes()
  2375. {
  2376. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2377. std::string home = this->GetCMakeInstance()->GetHomeOutputDirectory();
  2378. std::string pfile = home;
  2379. pfile += this->GetCMakeInstance()->GetCMakeFilesDirectory();
  2380. pfile += "/CMakeRuleHashes.txt";
  2381. this->CheckRuleHashes(pfile, home);
  2382. this->WriteRuleHashes(pfile);
  2383. #endif
  2384. }
  2385. void cmGlobalGenerator::CheckRuleHashes(std::string const& pfile,
  2386. std::string const& home)
  2387. {
  2388. #if defined(_WIN32) || defined(__CYGWIN__)
  2389. cmsys::ifstream fin(pfile.c_str(), std::ios::in | std::ios::binary);
  2390. #else
  2391. cmsys::ifstream fin(pfile.c_str());
  2392. #endif
  2393. if (!fin) {
  2394. return;
  2395. }
  2396. std::string line;
  2397. std::string fname;
  2398. while (cmSystemTools::GetLineFromStream(fin, line)) {
  2399. // Line format is a 32-byte hex string followed by a space
  2400. // followed by a file name (with no escaping).
  2401. // Skip blank and comment lines.
  2402. if (line.size() < 34 || line[0] == '#') {
  2403. continue;
  2404. }
  2405. // Get the filename.
  2406. fname = line.substr(33, line.npos);
  2407. // Look for a hash for this file's rule.
  2408. std::map<std::string, RuleHash>::const_iterator rhi =
  2409. this->RuleHashes.find(fname);
  2410. if (rhi != this->RuleHashes.end()) {
  2411. // Compare the rule hash in the file to that we were given.
  2412. if (strncmp(line.c_str(), rhi->second.Data, 32) != 0) {
  2413. // The rule has changed. Delete the output so it will be
  2414. // built again.
  2415. fname = cmSystemTools::CollapseFullPath(fname, home.c_str());
  2416. cmSystemTools::RemoveFile(fname);
  2417. }
  2418. } else {
  2419. // We have no hash for a rule previously listed. This may be a
  2420. // case where a user has turned off a build option and might
  2421. // want to turn it back on later, so do not delete the file.
  2422. // Instead, we keep the rule hash as long as the file exists so
  2423. // that if the feature is turned back on and the rule has
  2424. // changed the file is still rebuilt.
  2425. std::string fpath = cmSystemTools::CollapseFullPath(fname, home.c_str());
  2426. if (cmSystemTools::FileExists(fpath.c_str())) {
  2427. RuleHash hash;
  2428. strncpy(hash.Data, line.c_str(), 32);
  2429. this->RuleHashes[fname] = hash;
  2430. }
  2431. }
  2432. }
  2433. }
  2434. void cmGlobalGenerator::WriteRuleHashes(std::string const& pfile)
  2435. {
  2436. // Now generate a new persistence file with the current hashes.
  2437. if (this->RuleHashes.empty()) {
  2438. cmSystemTools::RemoveFile(pfile);
  2439. } else {
  2440. cmGeneratedFileStream fout(pfile.c_str());
  2441. fout << "# Hashes of file build rules.\n";
  2442. for (std::map<std::string, RuleHash>::const_iterator rhi =
  2443. this->RuleHashes.begin();
  2444. rhi != this->RuleHashes.end(); ++rhi) {
  2445. fout.write(rhi->second.Data, 32);
  2446. fout << " " << rhi->first << "\n";
  2447. }
  2448. }
  2449. }
  2450. void cmGlobalGenerator::WriteSummary()
  2451. {
  2452. // Record all target directories in a central location.
  2453. std::string fname = this->CMakeInstance->GetHomeOutputDirectory();
  2454. fname += cmake::GetCMakeFilesDirectory();
  2455. fname += "/TargetDirectories.txt";
  2456. cmGeneratedFileStream fout(fname.c_str());
  2457. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i) {
  2458. std::vector<cmGeneratorTarget*> tgts =
  2459. this->LocalGenerators[i]->GetGeneratorTargets();
  2460. for (std::vector<cmGeneratorTarget*>::iterator it = tgts.begin();
  2461. it != tgts.end(); ++it) {
  2462. if ((*it)->GetType() == cmState::INTERFACE_LIBRARY) {
  2463. continue;
  2464. }
  2465. this->WriteSummary(*it);
  2466. fout << (*it)->GetSupportDirectory() << "\n";
  2467. }
  2468. }
  2469. }
  2470. void cmGlobalGenerator::WriteSummary(cmGeneratorTarget* target)
  2471. {
  2472. // Place the labels file in a per-target support directory.
  2473. std::string dir = target->GetSupportDirectory();
  2474. std::string file = dir;
  2475. file += "/Labels.txt";
  2476. std::string json_file = dir + "/Labels.json";
  2477. #ifdef CMAKE_BUILD_WITH_CMAKE
  2478. // Check whether labels are enabled for this target.
  2479. if (const char* value = target->GetProperty("LABELS")) {
  2480. Json::Value lj_root(Json::objectValue);
  2481. Json::Value& lj_target = lj_root["target"] = Json::objectValue;
  2482. lj_target["name"] = target->GetName();
  2483. Json::Value& lj_target_labels = lj_target["labels"] = Json::arrayValue;
  2484. Json::Value& lj_sources = lj_root["sources"] = Json::arrayValue;
  2485. cmSystemTools::MakeDirectory(dir.c_str());
  2486. cmGeneratedFileStream fout(file.c_str());
  2487. // List the target-wide labels. All sources in the target get
  2488. // these labels.
  2489. std::vector<std::string> labels;
  2490. cmSystemTools::ExpandListArgument(value, labels);
  2491. if (!labels.empty()) {
  2492. fout << "# Target labels\n";
  2493. for (std::vector<std::string>::const_iterator li = labels.begin();
  2494. li != labels.end(); ++li) {
  2495. fout << " " << *li << "\n";
  2496. lj_target_labels.append(*li);
  2497. }
  2498. }
  2499. // List the source files with any per-source labels.
  2500. fout << "# Source files and their labels\n";
  2501. std::vector<cmSourceFile*> sources;
  2502. std::vector<std::string> configs;
  2503. target->Target->GetMakefile()->GetConfigurations(configs);
  2504. if (configs.empty()) {
  2505. configs.push_back("");
  2506. }
  2507. for (std::vector<std::string>::const_iterator ci = configs.begin();
  2508. ci != configs.end(); ++ci) {
  2509. target->GetSourceFiles(sources, *ci);
  2510. }
  2511. std::vector<cmSourceFile*>::const_iterator sourcesEnd =
  2512. cmRemoveDuplicates(sources);
  2513. for (std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  2514. si != sourcesEnd; ++si) {
  2515. Json::Value& lj_source = lj_sources.append(Json::objectValue);
  2516. cmSourceFile* sf = *si;
  2517. std::string const& sfp = sf->GetFullPath();
  2518. fout << sfp << "\n";
  2519. lj_source["file"] = sfp;
  2520. if (const char* svalue = sf->GetProperty("LABELS")) {
  2521. labels.clear();
  2522. Json::Value& lj_source_labels = lj_source["labels"] = Json::arrayValue;
  2523. cmSystemTools::ExpandListArgument(svalue, labels);
  2524. for (std::vector<std::string>::const_iterator li = labels.begin();
  2525. li != labels.end(); ++li) {
  2526. fout << " " << *li << "\n";
  2527. lj_source_labels.append(*li);
  2528. }
  2529. }
  2530. }
  2531. cmGeneratedFileStream json_fout(json_file.c_str());
  2532. json_fout << lj_root;
  2533. } else
  2534. #endif
  2535. {
  2536. cmSystemTools::RemoveFile(file);
  2537. cmSystemTools::RemoveFile(json_file);
  2538. }
  2539. }
  2540. // static
  2541. std::string cmGlobalGenerator::EscapeJSON(const std::string& s)
  2542. {
  2543. std::string result;
  2544. for (std::string::size_type i = 0; i < s.size(); ++i) {
  2545. if (s[i] == '"' || s[i] == '\\') {
  2546. result += '\\';
  2547. }
  2548. result += s[i];
  2549. }
  2550. return result;
  2551. }
  2552. void cmGlobalGenerator::SetFilenameTargetDepends(
  2553. cmSourceFile* sf, std::set<cmGeneratorTarget const*> const& tgts)
  2554. {
  2555. this->FilenameTargetDepends[sf] = tgts;
  2556. }
  2557. std::set<cmGeneratorTarget const*> const&
  2558. cmGlobalGenerator::GetFilenameTargetDepends(cmSourceFile* sf) const
  2559. {
  2560. return this->FilenameTargetDepends[sf];
  2561. }
  2562. void cmGlobalGenerator::CreateEvaluationSourceFiles(
  2563. std::string const& config) const
  2564. {
  2565. unsigned int i;
  2566. for (i = 0; i < this->LocalGenerators.size(); ++i) {
  2567. this->LocalGenerators[i]->CreateEvaluationFileOutputs(config);
  2568. }
  2569. }
  2570. void cmGlobalGenerator::ProcessEvaluationFiles()
  2571. {
  2572. std::vector<std::string> generatedFiles;
  2573. unsigned int i;
  2574. for (i = 0; i < this->LocalGenerators.size(); ++i) {
  2575. this->LocalGenerators[i]->ProcessEvaluationFiles(generatedFiles);
  2576. }
  2577. }
  2578. std::string cmGlobalGenerator::ExpandCFGIntDir(
  2579. const std::string& str, const std::string& /*config*/) const
  2580. {
  2581. return str;
  2582. }
  2583. bool cmGlobalGenerator::GenerateCPackPropertiesFile()
  2584. {
  2585. cmake::InstalledFilesMap const& installedFiles =
  2586. this->CMakeInstance->GetInstalledFiles();
  2587. cmLocalGenerator* lg = this->LocalGenerators[0];
  2588. cmMakefile* mf = lg->GetMakefile();
  2589. std::vector<std::string> configs;
  2590. std::string config = mf->GetConfigurations(configs, false);
  2591. std::string path = this->CMakeInstance->GetHomeOutputDirectory();
  2592. path += "/CPackProperties.cmake";
  2593. if (!cmSystemTools::FileExists(path.c_str()) && installedFiles.empty()) {
  2594. return true;
  2595. }
  2596. cmGeneratedFileStream file(path.c_str());
  2597. file << "# CPack properties\n";
  2598. for (cmake::InstalledFilesMap::const_iterator i = installedFiles.begin();
  2599. i != installedFiles.end(); ++i) {
  2600. cmInstalledFile const& installedFile = i->second;
  2601. cmCPackPropertiesGenerator cpackPropertiesGenerator(lg, installedFile,
  2602. configs);
  2603. cpackPropertiesGenerator.Generate(file, config, configs);
  2604. }
  2605. return true;
  2606. }