cmLocalGenerator.cxx 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmLocalGenerator.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmComputeLinkInformation.h"
  6. #include "cmCustomCommandGenerator.h"
  7. #include "cmGeneratedFileStream.h"
  8. #include "cmGeneratorExpressionEvaluationFile.h"
  9. #include "cmGeneratorTarget.h"
  10. #include "cmGlobalGenerator.h"
  11. #include "cmInstallGenerator.h"
  12. #include "cmInstallScriptGenerator.h"
  13. #include "cmInstallTargetGenerator.h"
  14. #include "cmLinkLineComputer.h"
  15. #include "cmMakefile.h"
  16. #include "cmRulePlaceholderExpander.h"
  17. #include "cmSourceFile.h"
  18. #include "cmState.h"
  19. #include "cmStateDirectory.h"
  20. #include "cmStateTypes.h"
  21. #include "cmSystemTools.h"
  22. #include "cmTarget.h"
  23. #include "cmTestGenerator.h"
  24. #include "cmVersion.h"
  25. #include "cmake.h"
  26. #if defined(CMAKE_BUILD_WITH_CMAKE)
  27. #define CM_LG_ENCODE_OBJECT_NAMES
  28. #include "cmCryptoHash.h"
  29. #endif
  30. #include "cmsys/RegularExpression.hxx"
  31. #include <algorithm>
  32. #include <assert.h>
  33. #include <iterator>
  34. #include <sstream>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <utility>
  38. #if defined(__HAIKU__)
  39. #include <FindDirectory.h>
  40. #include <StorageDefs.h>
  41. #endif
  42. // List of variables that are replaced when
  43. // rules are expanced. These variables are
  44. // replaced in the form <var> with GetSafeDefinition(var).
  45. // ${LANG} is replaced in the variable first with all enabled
  46. // languages.
  47. static const char* ruleReplaceVars[] = {
  48. "CMAKE_${LANG}_COMPILER",
  49. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  50. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  51. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  52. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  53. "CMAKE_${LANG}_LINK_FLAGS",
  54. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  55. "CMAKE_${LANG}_ARCHIVE",
  56. "CMAKE_AR",
  57. "CMAKE_CURRENT_SOURCE_DIR",
  58. "CMAKE_CURRENT_BINARY_DIR",
  59. "CMAKE_RANLIB",
  60. "CMAKE_LINKER",
  61. "CMAKE_CUDA_HOST_COMPILER",
  62. "CMAKE_CUDA_HOST_LINK_LAUNCHER",
  63. "CMAKE_CL_SHOWINCLUDES_PREFIX"
  64. };
  65. cmLocalGenerator::cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile)
  66. : cmOutputConverter(makefile->GetStateSnapshot())
  67. , StateSnapshot(makefile->GetStateSnapshot())
  68. , DirectoryBacktrace(makefile->GetBacktrace())
  69. {
  70. this->GlobalGenerator = gg;
  71. this->Makefile = makefile;
  72. this->AliasTargets = makefile->GetAliasTargets();
  73. this->EmitUniversalBinaryFlags = true;
  74. this->BackwardsCompatibility = 0;
  75. this->BackwardsCompatibilityFinal = false;
  76. this->ComputeObjectMaxPath();
  77. std::vector<std::string> enabledLanguages =
  78. this->GetState()->GetEnabledLanguages();
  79. if (const char* sysrootCompile =
  80. this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE")) {
  81. this->CompilerSysroot = sysrootCompile;
  82. } else {
  83. this->CompilerSysroot = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  84. }
  85. if (const char* sysrootLink =
  86. this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
  87. this->LinkerSysroot = sysrootLink;
  88. } else {
  89. this->LinkerSysroot = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  90. }
  91. for (std::vector<std::string>::iterator i = enabledLanguages.begin();
  92. i != enabledLanguages.end(); ++i) {
  93. std::string const& lang = *i;
  94. if (lang == "NONE") {
  95. continue;
  96. }
  97. this->Compilers["CMAKE_" + lang + "_COMPILER"] = lang;
  98. this->VariableMappings["CMAKE_" + lang + "_COMPILER"] =
  99. this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER");
  100. std::string const& compilerArg1 = "CMAKE_" + lang + "_COMPILER_ARG1";
  101. std::string const& compilerTarget = "CMAKE_" + lang + "_COMPILER_TARGET";
  102. std::string const& compilerOptionTarget =
  103. "CMAKE_" + lang + "_COMPILE_OPTIONS_TARGET";
  104. std::string const& compilerExternalToolchain =
  105. "CMAKE_" + lang + "_COMPILER_EXTERNAL_TOOLCHAIN";
  106. std::string const& compilerOptionExternalToolchain =
  107. "CMAKE_" + lang + "_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN";
  108. std::string const& compilerOptionSysroot =
  109. "CMAKE_" + lang + "_COMPILE_OPTIONS_SYSROOT";
  110. this->VariableMappings[compilerArg1] =
  111. this->Makefile->GetSafeDefinition(compilerArg1);
  112. this->VariableMappings[compilerTarget] =
  113. this->Makefile->GetSafeDefinition(compilerTarget);
  114. this->VariableMappings[compilerOptionTarget] =
  115. this->Makefile->GetSafeDefinition(compilerOptionTarget);
  116. this->VariableMappings[compilerExternalToolchain] =
  117. this->Makefile->GetSafeDefinition(compilerExternalToolchain);
  118. this->VariableMappings[compilerOptionExternalToolchain] =
  119. this->Makefile->GetSafeDefinition(compilerOptionExternalToolchain);
  120. this->VariableMappings[compilerOptionSysroot] =
  121. this->Makefile->GetSafeDefinition(compilerOptionSysroot);
  122. for (const char* const* replaceIter = cmArrayBegin(ruleReplaceVars);
  123. replaceIter != cmArrayEnd(ruleReplaceVars); ++replaceIter) {
  124. std::string actualReplace = *replaceIter;
  125. if (actualReplace.find("${LANG}") != std::string::npos) {
  126. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  127. }
  128. this->VariableMappings[actualReplace] =
  129. this->Makefile->GetSafeDefinition(actualReplace);
  130. }
  131. }
  132. }
  133. cmRulePlaceholderExpander* cmLocalGenerator::CreateRulePlaceholderExpander()
  134. const
  135. {
  136. return new cmRulePlaceholderExpander(this->Compilers, this->VariableMappings,
  137. this->CompilerSysroot,
  138. this->LinkerSysroot);
  139. }
  140. cmLocalGenerator::~cmLocalGenerator()
  141. {
  142. cmDeleteAll(this->GeneratorTargets);
  143. cmDeleteAll(this->OwnedImportedGeneratorTargets);
  144. }
  145. void cmLocalGenerator::IssueMessage(cmake::MessageType t,
  146. std::string const& text) const
  147. {
  148. this->GetCMakeInstance()->IssueMessage(t, text, this->DirectoryBacktrace);
  149. }
  150. void cmLocalGenerator::ComputeObjectMaxPath()
  151. {
  152. // Choose a maximum object file name length.
  153. #if defined(_WIN32) || defined(__CYGWIN__)
  154. this->ObjectPathMax = 250;
  155. #else
  156. this->ObjectPathMax = 1000;
  157. #endif
  158. const char* plen = this->Makefile->GetDefinition("CMAKE_OBJECT_PATH_MAX");
  159. if (plen && *plen) {
  160. unsigned int pmax;
  161. if (sscanf(plen, "%u", &pmax) == 1) {
  162. if (pmax >= 128) {
  163. this->ObjectPathMax = pmax;
  164. } else {
  165. std::ostringstream w;
  166. w << "CMAKE_OBJECT_PATH_MAX is set to " << pmax
  167. << ", which is less than the minimum of 128. "
  168. << "The value will be ignored.";
  169. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  170. }
  171. } else {
  172. std::ostringstream w;
  173. w << "CMAKE_OBJECT_PATH_MAX is set to \"" << plen
  174. << "\", which fails to parse as a positive integer. "
  175. << "The value will be ignored.";
  176. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  177. }
  178. }
  179. this->ObjectMaxPathViolations.clear();
  180. }
  181. void cmLocalGenerator::TraceDependencies()
  182. {
  183. std::vector<std::string> configs;
  184. this->Makefile->GetConfigurations(configs);
  185. if (configs.empty()) {
  186. configs.push_back("");
  187. }
  188. for (std::vector<std::string>::const_iterator ci = configs.begin();
  189. ci != configs.end(); ++ci) {
  190. this->GlobalGenerator->CreateEvaluationSourceFiles(*ci);
  191. }
  192. // Generate the rule files for each target.
  193. std::vector<cmGeneratorTarget*> targets = this->GetGeneratorTargets();
  194. for (std::vector<cmGeneratorTarget*>::iterator t = targets.begin();
  195. t != targets.end(); ++t) {
  196. if ((*t)->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  197. continue;
  198. }
  199. (*t)->TraceDependencies();
  200. }
  201. }
  202. void cmLocalGenerator::GenerateTestFiles()
  203. {
  204. if (!this->Makefile->IsOn("CMAKE_TESTING_ENABLED")) {
  205. return;
  206. }
  207. // Compute the set of configurations.
  208. std::vector<std::string> configurationTypes;
  209. const std::string& config =
  210. this->Makefile->GetConfigurations(configurationTypes, false);
  211. std::string file = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  212. file += "/";
  213. file += "CTestTestfile.cmake";
  214. cmGeneratedFileStream fout(file.c_str());
  215. fout.SetCopyIfDifferent(true);
  216. fout << "# CMake generated Testfile for " << std::endl
  217. << "# Source directory: "
  218. << this->StateSnapshot.GetDirectory().GetCurrentSource() << std::endl
  219. << "# Build directory: "
  220. << this->StateSnapshot.GetDirectory().GetCurrentBinary() << std::endl
  221. << "# " << std::endl
  222. << "# This file includes the relevant testing commands "
  223. << "required for " << std::endl
  224. << "# testing this directory and lists subdirectories to "
  225. << "be tested as well." << std::endl;
  226. const char* testIncludeFile =
  227. this->Makefile->GetProperty("TEST_INCLUDE_FILE");
  228. if (testIncludeFile) {
  229. fout << "include(\"" << testIncludeFile << "\")" << std::endl;
  230. }
  231. const char* testIncludeFiles =
  232. this->Makefile->GetProperty("TEST_INCLUDE_FILES");
  233. if (testIncludeFiles) {
  234. std::vector<std::string> includesList;
  235. cmSystemTools::ExpandListArgument(testIncludeFiles, includesList);
  236. for (std::vector<std::string>::const_iterator i = includesList.begin();
  237. i != includesList.end(); ++i) {
  238. fout << "include(\"" << *i << "\")" << std::endl;
  239. }
  240. }
  241. // Ask each test generator to write its code.
  242. std::vector<cmTestGenerator*> const& testers =
  243. this->Makefile->GetTestGenerators();
  244. for (std::vector<cmTestGenerator*>::const_iterator gi = testers.begin();
  245. gi != testers.end(); ++gi) {
  246. (*gi)->Compute(this);
  247. (*gi)->Generate(fout, config, configurationTypes);
  248. }
  249. typedef std::vector<cmStateSnapshot> vec_t;
  250. vec_t const& children = this->Makefile->GetStateSnapshot().GetChildren();
  251. std::string parentBinDir = this->GetCurrentBinaryDirectory();
  252. for (vec_t::const_iterator i = children.begin(); i != children.end(); ++i) {
  253. // TODO: Use add_subdirectory instead?
  254. std::string outP = i->GetDirectory().GetCurrentBinary();
  255. outP = this->ConvertToRelativePath(parentBinDir, outP);
  256. outP = cmOutputConverter::EscapeForCMake(outP);
  257. fout << "subdirs(" << outP << ")" << std::endl;
  258. }
  259. // Add directory labels property
  260. const char* directoryLabels =
  261. this->Makefile->GetDefinition("CMAKE_DIRECTORY_LABELS");
  262. const char* labels = this->Makefile->GetProperty("LABELS");
  263. if (labels || directoryLabels) {
  264. fout << "set_directory_properties(PROPERTIES LABELS ";
  265. if (labels) {
  266. fout << cmOutputConverter::EscapeForCMake(labels);
  267. }
  268. if (labels && directoryLabels) {
  269. fout << ";";
  270. }
  271. if (directoryLabels) {
  272. fout << cmOutputConverter::EscapeForCMake(directoryLabels);
  273. }
  274. fout << ")" << std::endl;
  275. }
  276. }
  277. void cmLocalGenerator::CreateEvaluationFileOutputs(std::string const& config)
  278. {
  279. std::vector<cmGeneratorExpressionEvaluationFile*> ef =
  280. this->Makefile->GetEvaluationFiles();
  281. for (std::vector<cmGeneratorExpressionEvaluationFile*>::const_iterator li =
  282. ef.begin();
  283. li != ef.end(); ++li) {
  284. (*li)->CreateOutputFile(this, config);
  285. }
  286. }
  287. void cmLocalGenerator::ProcessEvaluationFiles(
  288. std::vector<std::string>& generatedFiles)
  289. {
  290. std::vector<cmGeneratorExpressionEvaluationFile*> ef =
  291. this->Makefile->GetEvaluationFiles();
  292. for (std::vector<cmGeneratorExpressionEvaluationFile*>::const_iterator li =
  293. ef.begin();
  294. li != ef.end(); ++li) {
  295. (*li)->Generate(this);
  296. if (cmSystemTools::GetFatalErrorOccured()) {
  297. return;
  298. }
  299. std::vector<std::string> files = (*li)->GetFiles();
  300. std::sort(files.begin(), files.end());
  301. std::vector<std::string> intersection;
  302. std::set_intersection(files.begin(), files.end(), generatedFiles.begin(),
  303. generatedFiles.end(),
  304. std::back_inserter(intersection));
  305. if (!intersection.empty()) {
  306. cmSystemTools::Error("Files to be generated by multiple different "
  307. "commands: ",
  308. cmWrap('"', intersection, '"', " ").c_str());
  309. return;
  310. }
  311. generatedFiles.insert(generatedFiles.end(), files.begin(), files.end());
  312. std::vector<std::string>::iterator newIt =
  313. generatedFiles.end() - files.size();
  314. std::inplace_merge(generatedFiles.begin(), newIt, generatedFiles.end());
  315. }
  316. }
  317. void cmLocalGenerator::GenerateInstallRules()
  318. {
  319. // Compute the install prefix.
  320. const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  321. #if defined(_WIN32) && !defined(__CYGWIN__)
  322. std::string prefix_win32;
  323. if (!prefix) {
  324. if (!cmSystemTools::GetEnv("SystemDrive", prefix_win32)) {
  325. prefix_win32 = "C:";
  326. }
  327. const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
  328. if (project_name && project_name[0]) {
  329. prefix_win32 += "/Program Files/";
  330. prefix_win32 += project_name;
  331. } else {
  332. prefix_win32 += "/InstalledCMakeProject";
  333. }
  334. prefix = prefix_win32.c_str();
  335. }
  336. #elif defined(__HAIKU__)
  337. char dir[B_PATH_NAME_LENGTH];
  338. if (!prefix) {
  339. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  340. B_OK) {
  341. prefix = dir;
  342. } else {
  343. prefix = "/boot/system";
  344. }
  345. }
  346. #else
  347. if (!prefix) {
  348. prefix = "/usr/local";
  349. }
  350. #endif
  351. if (const char* stagingPrefix =
  352. this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX")) {
  353. prefix = stagingPrefix;
  354. }
  355. // Compute the set of configurations.
  356. std::vector<std::string> configurationTypes;
  357. const std::string& config =
  358. this->Makefile->GetConfigurations(configurationTypes, false);
  359. // Choose a default install configuration.
  360. std::string default_config = config;
  361. const char* default_order[] = { "RELEASE", "MINSIZEREL", "RELWITHDEBINFO",
  362. "DEBUG", CM_NULLPTR };
  363. for (const char** c = default_order; *c && default_config.empty(); ++c) {
  364. for (std::vector<std::string>::iterator i = configurationTypes.begin();
  365. i != configurationTypes.end(); ++i) {
  366. if (cmSystemTools::UpperCase(*i) == *c) {
  367. default_config = *i;
  368. }
  369. }
  370. }
  371. if (default_config.empty() && !configurationTypes.empty()) {
  372. default_config = configurationTypes[0];
  373. }
  374. // Create the install script file.
  375. std::string file = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  376. std::string homedir = this->GetState()->GetBinaryDirectory();
  377. int toplevel_install = 0;
  378. if (file == homedir) {
  379. toplevel_install = 1;
  380. }
  381. file += "/cmake_install.cmake";
  382. cmGeneratedFileStream fout(file.c_str());
  383. fout.SetCopyIfDifferent(true);
  384. // Write the header.
  385. fout << "# Install script for directory: "
  386. << this->StateSnapshot.GetDirectory().GetCurrentSource() << std::endl
  387. << std::endl;
  388. fout << "# Set the install prefix" << std::endl
  389. << "if(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  390. << " set(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
  391. << "endif()" << std::endl
  392. << "string(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
  393. << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
  394. << std::endl;
  395. // Write support code for generating per-configuration install rules.
  396. /* clang-format off */
  397. fout <<
  398. "# Set the install configuration name.\n"
  399. "if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  400. " if(BUILD_TYPE)\n"
  401. " string(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
  402. " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
  403. " else()\n"
  404. " set(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
  405. " endif()\n"
  406. " message(STATUS \"Install configuration: "
  407. "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
  408. "endif()\n"
  409. "\n";
  410. /* clang-format on */
  411. // Write support code for dealing with component-specific installs.
  412. /* clang-format off */
  413. fout <<
  414. "# Set the component getting installed.\n"
  415. "if(NOT CMAKE_INSTALL_COMPONENT)\n"
  416. " if(COMPONENT)\n"
  417. " message(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
  418. " set(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
  419. " else()\n"
  420. " set(CMAKE_INSTALL_COMPONENT)\n"
  421. " endif()\n"
  422. "endif()\n"
  423. "\n";
  424. /* clang-format on */
  425. // Copy user-specified install options to the install code.
  426. if (const char* so_no_exe =
  427. this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE")) {
  428. /* clang-format off */
  429. fout <<
  430. "# Install shared libraries without execute permission?\n"
  431. "if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  432. " set(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
  433. "endif()\n"
  434. "\n";
  435. /* clang-format on */
  436. }
  437. // Copy cmake cross compile state to install code.
  438. if (const char* crosscompiling =
  439. this->Makefile->GetDefinition("CMAKE_CROSSCOMPILING")) {
  440. /* clang-format off */
  441. fout <<
  442. "# Is this installation the result of a crosscompile?\n"
  443. "if(NOT DEFINED CMAKE_CROSSCOMPILING)\n"
  444. " set(CMAKE_CROSSCOMPILING \"" << crosscompiling << "\")\n"
  445. "endif()\n"
  446. "\n";
  447. /* clang-format on */
  448. }
  449. // Ask each install generator to write its code.
  450. std::vector<cmInstallGenerator*> const& installers =
  451. this->Makefile->GetInstallGenerators();
  452. for (std::vector<cmInstallGenerator*>::const_iterator gi =
  453. installers.begin();
  454. gi != installers.end(); ++gi) {
  455. (*gi)->Generate(fout, config, configurationTypes);
  456. }
  457. // Write rules from old-style specification stored in targets.
  458. this->GenerateTargetInstallRules(fout, config, configurationTypes);
  459. // Include install scripts from subdirectories.
  460. std::vector<cmStateSnapshot> children =
  461. this->Makefile->GetStateSnapshot().GetChildren();
  462. if (!children.empty()) {
  463. fout << "if(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
  464. fout << " # Include the install script for each subdirectory.\n";
  465. for (std::vector<cmStateSnapshot>::const_iterator ci = children.begin();
  466. ci != children.end(); ++ci) {
  467. if (!ci->GetDirectory().GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  468. std::string odir = ci->GetDirectory().GetCurrentBinary();
  469. cmSystemTools::ConvertToUnixSlashes(odir);
  470. fout << " include(\"" << odir << "/cmake_install.cmake\")"
  471. << std::endl;
  472. }
  473. }
  474. fout << "\n";
  475. fout << "endif()\n\n";
  476. }
  477. // Record the install manifest.
  478. if (toplevel_install) {
  479. /* clang-format off */
  480. fout <<
  481. "if(CMAKE_INSTALL_COMPONENT)\n"
  482. " set(CMAKE_INSTALL_MANIFEST \"install_manifest_"
  483. "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
  484. "else()\n"
  485. " set(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
  486. "endif()\n"
  487. "\n"
  488. "string(REPLACE \";\" \"\\n\" CMAKE_INSTALL_MANIFEST_CONTENT\n"
  489. " \"${CMAKE_INSTALL_MANIFEST_FILES}\")\n"
  490. "file(WRITE \"" << homedir << "/${CMAKE_INSTALL_MANIFEST}\"\n"
  491. " \"${CMAKE_INSTALL_MANIFEST_CONTENT}\")\n";
  492. /* clang-format on */
  493. }
  494. }
  495. void cmLocalGenerator::AddGeneratorTarget(cmGeneratorTarget* gt)
  496. {
  497. this->GeneratorTargets.push_back(gt);
  498. this->GlobalGenerator->IndexGeneratorTarget(gt);
  499. }
  500. void cmLocalGenerator::AddImportedGeneratorTarget(cmGeneratorTarget* gt)
  501. {
  502. this->ImportedGeneratorTargets.push_back(gt);
  503. this->GlobalGenerator->IndexGeneratorTarget(gt);
  504. }
  505. void cmLocalGenerator::AddOwnedImportedGeneratorTarget(cmGeneratorTarget* gt)
  506. {
  507. this->OwnedImportedGeneratorTargets.push_back(gt);
  508. }
  509. struct NamedGeneratorTargetFinder
  510. {
  511. NamedGeneratorTargetFinder(std::string const& name)
  512. : Name(name)
  513. {
  514. }
  515. bool operator()(cmGeneratorTarget* tgt)
  516. {
  517. return tgt->GetName() == this->Name;
  518. }
  519. private:
  520. std::string Name;
  521. };
  522. cmGeneratorTarget* cmLocalGenerator::FindLocalNonAliasGeneratorTarget(
  523. const std::string& name) const
  524. {
  525. std::vector<cmGeneratorTarget*>::const_iterator ti =
  526. std::find_if(this->GeneratorTargets.begin(), this->GeneratorTargets.end(),
  527. NamedGeneratorTargetFinder(name));
  528. if (ti != this->GeneratorTargets.end()) {
  529. return *ti;
  530. }
  531. return CM_NULLPTR;
  532. }
  533. void cmLocalGenerator::ComputeTargetManifest()
  534. {
  535. // Collect the set of configuration types.
  536. std::vector<std::string> configNames;
  537. this->Makefile->GetConfigurations(configNames);
  538. if (configNames.empty()) {
  539. configNames.push_back("");
  540. }
  541. // Add our targets to the manifest for each configuration.
  542. std::vector<cmGeneratorTarget*> targets = this->GetGeneratorTargets();
  543. for (std::vector<cmGeneratorTarget*>::iterator t = targets.begin();
  544. t != targets.end(); ++t) {
  545. cmGeneratorTarget* target = *t;
  546. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  547. continue;
  548. }
  549. for (std::vector<std::string>::iterator ci = configNames.begin();
  550. ci != configNames.end(); ++ci) {
  551. const char* config = ci->c_str();
  552. target->ComputeTargetManifest(config);
  553. }
  554. }
  555. }
  556. bool cmLocalGenerator::ComputeTargetCompileFeatures()
  557. {
  558. // Collect the set of configuration types.
  559. std::vector<std::string> configNames;
  560. this->Makefile->GetConfigurations(configNames);
  561. if (configNames.empty()) {
  562. configNames.push_back("");
  563. }
  564. // Process compile features of all targets.
  565. std::vector<cmGeneratorTarget*> targets = this->GetGeneratorTargets();
  566. for (std::vector<cmGeneratorTarget*>::iterator t = targets.begin();
  567. t != targets.end(); ++t) {
  568. cmGeneratorTarget* target = *t;
  569. for (std::vector<std::string>::iterator ci = configNames.begin();
  570. ci != configNames.end(); ++ci) {
  571. if (!target->ComputeCompileFeatures(*ci)) {
  572. return false;
  573. }
  574. }
  575. }
  576. return true;
  577. }
  578. bool cmLocalGenerator::IsRootMakefile() const
  579. {
  580. return !this->StateSnapshot.GetBuildsystemDirectoryParent().IsValid();
  581. }
  582. cmState* cmLocalGenerator::GetState() const
  583. {
  584. return this->GlobalGenerator->GetCMakeInstance()->GetState();
  585. }
  586. cmStateSnapshot cmLocalGenerator::GetStateSnapshot() const
  587. {
  588. return this->Makefile->GetStateSnapshot();
  589. }
  590. const char* cmLocalGenerator::GetRuleLauncher(cmGeneratorTarget* target,
  591. const std::string& prop)
  592. {
  593. if (target) {
  594. return target->GetProperty(prop);
  595. }
  596. return this->Makefile->GetProperty(prop);
  597. }
  598. std::string cmLocalGenerator::ConvertToIncludeReference(
  599. std::string const& path, OutputFormat format, bool forceFullPaths)
  600. {
  601. static_cast<void>(forceFullPaths);
  602. return this->ConvertToOutputForExisting(path, format);
  603. }
  604. std::string cmLocalGenerator::GetIncludeFlags(
  605. const std::vector<std::string>& includes, cmGeneratorTarget* target,
  606. const std::string& lang, bool forceFullPaths, bool forResponseFile,
  607. const std::string& config)
  608. {
  609. if (lang.empty()) {
  610. return "";
  611. }
  612. OutputFormat shellFormat = forResponseFile ? RESPONSE : SHELL;
  613. std::ostringstream includeFlags;
  614. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  615. flagVar += lang;
  616. const char* includeFlag = this->Makefile->GetSafeDefinition(flagVar);
  617. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  618. flagVar += lang;
  619. const char* sep = this->Makefile->GetDefinition(flagVar);
  620. bool quotePaths = false;
  621. if (this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS")) {
  622. quotePaths = true;
  623. }
  624. bool repeatFlag = true;
  625. // should the include flag be repeated like ie. -IA -IB
  626. if (!sep) {
  627. sep = " ";
  628. } else {
  629. // if there is a separator then the flag is not repeated but is only
  630. // given once i.e. -classpath a:b:c
  631. repeatFlag = false;
  632. }
  633. // Support special system include flag if it is available and the
  634. // normal flag is repeated for each directory.
  635. std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
  636. sysFlagVar += lang;
  637. const char* sysIncludeFlag = CM_NULLPTR;
  638. if (repeatFlag) {
  639. sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar);
  640. }
  641. std::string fwSearchFlagVar = "CMAKE_";
  642. fwSearchFlagVar += lang;
  643. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  644. const char* fwSearchFlag = this->Makefile->GetDefinition(fwSearchFlagVar);
  645. std::string sysFwSearchFlagVar = "CMAKE_";
  646. sysFwSearchFlagVar += lang;
  647. sysFwSearchFlagVar += "_SYSTEM_FRAMEWORK_SEARCH_FLAG";
  648. const char* sysFwSearchFlag =
  649. this->Makefile->GetDefinition(sysFwSearchFlagVar);
  650. bool flagUsed = false;
  651. std::set<std::string> emitted;
  652. #ifdef __APPLE__
  653. emitted.insert("/System/Library/Frameworks");
  654. #endif
  655. std::vector<std::string>::const_iterator i;
  656. for (i = includes.begin(); i != includes.end(); ++i) {
  657. if (fwSearchFlag && *fwSearchFlag && this->Makefile->IsOn("APPLE") &&
  658. cmSystemTools::IsPathToFramework(i->c_str())) {
  659. std::string frameworkDir = *i;
  660. frameworkDir += "/../";
  661. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  662. if (emitted.insert(frameworkDir).second) {
  663. if (sysFwSearchFlag && target &&
  664. target->IsSystemIncludeDirectory(*i, config)) {
  665. includeFlags << sysFwSearchFlag;
  666. } else {
  667. includeFlags << fwSearchFlag;
  668. }
  669. includeFlags << this->ConvertToOutputFormat(frameworkDir, shellFormat)
  670. << " ";
  671. }
  672. continue;
  673. }
  674. if (!flagUsed || repeatFlag) {
  675. if (sysIncludeFlag && target &&
  676. target->IsSystemIncludeDirectory(*i, config)) {
  677. includeFlags << sysIncludeFlag;
  678. } else {
  679. includeFlags << includeFlag;
  680. }
  681. flagUsed = true;
  682. }
  683. std::string includePath =
  684. this->ConvertToIncludeReference(*i, shellFormat, forceFullPaths);
  685. if (quotePaths && !includePath.empty() && includePath[0] != '\"') {
  686. includeFlags << "\"";
  687. }
  688. includeFlags << includePath;
  689. if (quotePaths && !includePath.empty() && includePath[0] != '\"') {
  690. includeFlags << "\"";
  691. }
  692. includeFlags << sep;
  693. }
  694. std::string flags = includeFlags.str();
  695. // remove trailing separators
  696. if ((sep[0] != ' ') && !flags.empty() && flags[flags.size() - 1] == sep[0]) {
  697. flags[flags.size() - 1] = ' ';
  698. }
  699. return flags;
  700. }
  701. void cmLocalGenerator::AddCompileDefinitions(std::set<std::string>& defines,
  702. cmGeneratorTarget const* target,
  703. const std::string& config,
  704. const std::string& lang) const
  705. {
  706. std::vector<std::string> targetDefines;
  707. target->GetCompileDefinitions(targetDefines, config, lang);
  708. this->AppendDefines(defines, targetDefines);
  709. }
  710. void cmLocalGenerator::AddCompileOptions(std::string& flags,
  711. cmGeneratorTarget* target,
  712. const std::string& lang,
  713. const std::string& config)
  714. {
  715. std::string langFlagRegexVar = std::string("CMAKE_") + lang + "_FLAG_REGEX";
  716. if (const char* langFlagRegexStr =
  717. this->Makefile->GetDefinition(langFlagRegexVar)) {
  718. // Filter flags acceptable to this language.
  719. cmsys::RegularExpression r(langFlagRegexStr);
  720. std::vector<std::string> opts;
  721. if (const char* targetFlags = target->GetProperty("COMPILE_FLAGS")) {
  722. cmSystemTools::ParseWindowsCommandLine(targetFlags, opts);
  723. }
  724. target->GetCompileOptions(opts, config, lang);
  725. for (std::vector<std::string>::const_iterator i = opts.begin();
  726. i != opts.end(); ++i) {
  727. if (r.find(i->c_str())) {
  728. // (Re-)Escape this flag. COMPILE_FLAGS were already parsed
  729. // as a command line above, and COMPILE_OPTIONS are escaped.
  730. this->AppendFlagEscape(flags, *i);
  731. }
  732. }
  733. } else {
  734. // Use all flags.
  735. if (const char* targetFlags = target->GetProperty("COMPILE_FLAGS")) {
  736. // COMPILE_FLAGS are not escaped for historical reasons.
  737. this->AppendFlags(flags, targetFlags);
  738. }
  739. std::vector<std::string> opts;
  740. target->GetCompileOptions(opts, config, lang);
  741. for (std::vector<std::string>::const_iterator i = opts.begin();
  742. i != opts.end(); ++i) {
  743. // COMPILE_OPTIONS are escaped.
  744. this->AppendFlagEscape(flags, *i);
  745. }
  746. }
  747. for (std::map<std::string, std::string>::const_iterator it =
  748. target->GetMaxLanguageStandards().begin();
  749. it != target->GetMaxLanguageStandards().end(); ++it) {
  750. const char* standard = target->GetProperty(it->first + "_STANDARD");
  751. if (!standard) {
  752. continue;
  753. }
  754. if (this->Makefile->IsLaterStandard(it->first, standard, it->second)) {
  755. std::ostringstream e;
  756. e << "The COMPILE_FEATURES property of target \"" << target->GetName()
  757. << "\" was evaluated when computing the link "
  758. "implementation, and the \""
  759. << it->first << "_STANDARD\" was \"" << it->second
  760. << "\" for that computation. Computing the "
  761. "COMPILE_FEATURES based on the link implementation resulted in a "
  762. "higher \""
  763. << it->first << "_STANDARD\" \"" << standard
  764. << "\". "
  765. "This is not permitted. The COMPILE_FEATURES may not both depend "
  766. "on "
  767. "and be depended on by the link implementation."
  768. << std::endl;
  769. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  770. return;
  771. }
  772. }
  773. this->AddCompilerRequirementFlag(flags, target, lang);
  774. }
  775. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  776. cmGeneratorTarget const* target,
  777. const std::string& lang,
  778. const std::string& config,
  779. bool stripImplicitInclDirs) const
  780. {
  781. // Need to decide whether to automatically include the source and
  782. // binary directories at the beginning of the include path.
  783. bool includeSourceDir = false;
  784. bool includeBinaryDir = false;
  785. // When automatic include directories are requested for a build then
  786. // include the source and binary directories at the beginning of the
  787. // include path to approximate include file behavior for an
  788. // in-source build. This does not account for the case of a source
  789. // file in a subdirectory of the current source directory but we
  790. // cannot fix this because not all native build tools support
  791. // per-source-file include paths.
  792. if (this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR")) {
  793. includeSourceDir = true;
  794. includeBinaryDir = true;
  795. }
  796. // Do not repeat an include path.
  797. std::set<std::string> emitted;
  798. // Store the automatic include paths.
  799. if (includeBinaryDir) {
  800. std::string binDir = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  801. if (emitted.find(binDir) == emitted.end()) {
  802. dirs.push_back(binDir);
  803. emitted.insert(binDir);
  804. }
  805. }
  806. if (includeSourceDir) {
  807. std::string srcDir = this->StateSnapshot.GetDirectory().GetCurrentSource();
  808. if (emitted.find(srcDir) == emitted.end()) {
  809. dirs.push_back(srcDir);
  810. emitted.insert(srcDir);
  811. }
  812. }
  813. if (!target) {
  814. return;
  815. }
  816. std::string rootPath;
  817. if (const char* sysrootCompile =
  818. this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE")) {
  819. rootPath = sysrootCompile;
  820. } else {
  821. rootPath = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  822. }
  823. std::vector<std::string> implicitDirs;
  824. // Load implicit include directories for this language.
  825. std::string impDirVar = "CMAKE_";
  826. impDirVar += lang;
  827. impDirVar += "_IMPLICIT_INCLUDE_DIRECTORIES";
  828. if (const char* value = this->Makefile->GetDefinition(impDirVar)) {
  829. std::vector<std::string> impDirVec;
  830. cmSystemTools::ExpandListArgument(value, impDirVec);
  831. for (std::vector<std::string>::const_iterator i = impDirVec.begin();
  832. i != impDirVec.end(); ++i) {
  833. std::string d = rootPath + *i;
  834. cmSystemTools::ConvertToUnixSlashes(d);
  835. emitted.insert(d);
  836. if (!stripImplicitInclDirs) {
  837. implicitDirs.push_back(*i);
  838. }
  839. }
  840. }
  841. // Get the target-specific include directories.
  842. std::vector<std::string> includes;
  843. includes = target->GetIncludeDirectories(config, lang);
  844. // Support putting all the in-project include directories first if
  845. // it is requested by the project.
  846. if (this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE")) {
  847. const char* topSourceDir = this->GetState()->GetSourceDirectory();
  848. const char* topBinaryDir = this->GetState()->GetBinaryDirectory();
  849. for (std::vector<std::string>::const_iterator i = includes.begin();
  850. i != includes.end(); ++i) {
  851. // Emit this directory only if it is a subdirectory of the
  852. // top-level source or binary tree.
  853. if (cmSystemTools::ComparePath(*i, topSourceDir) ||
  854. cmSystemTools::ComparePath(*i, topBinaryDir) ||
  855. cmSystemTools::IsSubDirectory(*i, topSourceDir) ||
  856. cmSystemTools::IsSubDirectory(*i, topBinaryDir)) {
  857. if (emitted.insert(*i).second) {
  858. dirs.push_back(*i);
  859. }
  860. }
  861. }
  862. }
  863. // Construct the final ordered include directory list.
  864. for (std::vector<std::string>::const_iterator i = includes.begin();
  865. i != includes.end(); ++i) {
  866. if (emitted.insert(*i).second) {
  867. dirs.push_back(*i);
  868. }
  869. }
  870. // Add standard include directories for this language.
  871. // We do not filter out implicit directories here.
  872. std::string const standardIncludesVar =
  873. "CMAKE_" + lang + "_STANDARD_INCLUDE_DIRECTORIES";
  874. std::string const standardIncludes =
  875. this->Makefile->GetSafeDefinition(standardIncludesVar);
  876. std::vector<std::string>::size_type const before = includes.size();
  877. cmSystemTools::ExpandListArgument(standardIncludes, includes);
  878. for (std::vector<std::string>::iterator i = includes.begin() + before;
  879. i != includes.end(); ++i) {
  880. cmSystemTools::ConvertToUnixSlashes(*i);
  881. dirs.push_back(*i);
  882. }
  883. for (std::vector<std::string>::const_iterator i = implicitDirs.begin();
  884. i != implicitDirs.end(); ++i) {
  885. if (std::find(includes.begin(), includes.end(), *i) != includes.end()) {
  886. dirs.push_back(*i);
  887. }
  888. }
  889. }
  890. void cmLocalGenerator::GetStaticLibraryFlags(std::string& flags,
  891. std::string const& config,
  892. cmGeneratorTarget* target)
  893. {
  894. this->AppendFlags(
  895. flags, this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS"));
  896. if (!config.empty()) {
  897. std::string name = "CMAKE_STATIC_LINKER_FLAGS_" + config;
  898. this->AppendFlags(flags, this->Makefile->GetSafeDefinition(name));
  899. }
  900. this->AppendFlags(flags, target->GetProperty("STATIC_LIBRARY_FLAGS"));
  901. if (!config.empty()) {
  902. std::string name = "STATIC_LIBRARY_FLAGS_" + config;
  903. this->AppendFlags(flags, target->GetProperty(name));
  904. }
  905. }
  906. void cmLocalGenerator::GetTargetFlags(
  907. cmLinkLineComputer* linkLineComputer, const std::string& config,
  908. std::string& linkLibs, std::string& flags, std::string& linkFlags,
  909. std::string& frameworkPath, std::string& linkPath, cmGeneratorTarget* target)
  910. {
  911. const std::string buildType = cmSystemTools::UpperCase(config);
  912. cmComputeLinkInformation* pcli = target->GetLinkInformation(config);
  913. const char* libraryLinkVariable =
  914. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  915. const std::string linkLanguage =
  916. linkLineComputer->GetLinkerLanguage(target, buildType);
  917. switch (target->GetType()) {
  918. case cmStateEnums::STATIC_LIBRARY:
  919. this->GetStaticLibraryFlags(linkFlags, buildType, target);
  920. break;
  921. case cmStateEnums::MODULE_LIBRARY:
  922. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  923. CM_FALLTHROUGH;
  924. case cmStateEnums::SHARED_LIBRARY: {
  925. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  926. linkFlags += " ";
  927. if (!buildType.empty()) {
  928. std::string build = libraryLinkVariable;
  929. build += "_";
  930. build += buildType;
  931. linkFlags += this->Makefile->GetSafeDefinition(build);
  932. linkFlags += " ";
  933. }
  934. if (this->Makefile->IsOn("WIN32") &&
  935. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW"))) {
  936. std::vector<cmSourceFile*> sources;
  937. target->GetSourceFiles(sources, buildType);
  938. std::string defFlag =
  939. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  940. for (std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  941. i != sources.end(); ++i) {
  942. cmSourceFile* sf = *i;
  943. if (sf->GetExtension() == "def") {
  944. linkFlags += defFlag;
  945. linkFlags += this->ConvertToOutputFormat(
  946. cmSystemTools::CollapseFullPath(sf->GetFullPath()), SHELL);
  947. linkFlags += " ";
  948. }
  949. }
  950. }
  951. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  952. if (targetLinkFlags) {
  953. linkFlags += targetLinkFlags;
  954. linkFlags += " ";
  955. }
  956. if (!buildType.empty()) {
  957. std::string configLinkFlags = "LINK_FLAGS_";
  958. configLinkFlags += buildType;
  959. targetLinkFlags = target->GetProperty(configLinkFlags);
  960. if (targetLinkFlags) {
  961. linkFlags += targetLinkFlags;
  962. linkFlags += " ";
  963. }
  964. }
  965. if (pcli) {
  966. this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
  967. frameworkPath, linkPath);
  968. }
  969. } break;
  970. case cmStateEnums::EXECUTABLE: {
  971. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  972. linkFlags += " ";
  973. if (!buildType.empty()) {
  974. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  975. build += buildType;
  976. linkFlags += this->Makefile->GetSafeDefinition(build);
  977. linkFlags += " ";
  978. }
  979. if (linkLanguage.empty()) {
  980. cmSystemTools::Error(
  981. "CMake can not determine linker language for target: ",
  982. target->GetName().c_str());
  983. return;
  984. }
  985. this->AddLanguageFlagsForLinking(flags, target, linkLanguage, buildType);
  986. if (pcli) {
  987. this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
  988. frameworkPath, linkPath);
  989. }
  990. if (cmSystemTools::IsOn(
  991. this->Makefile->GetDefinition("BUILD_SHARED_LIBS"))) {
  992. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") +
  993. linkLanguage + std::string("_FLAGS");
  994. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar);
  995. linkFlags += " ";
  996. }
  997. if (target->GetPropertyAsBool("WIN32_EXECUTABLE")) {
  998. linkFlags +=
  999. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1000. linkFlags += " ";
  1001. } else {
  1002. linkFlags +=
  1003. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1004. linkFlags += " ";
  1005. }
  1006. if (target->IsExecutableWithExports()) {
  1007. std::string exportFlagVar = "CMAKE_EXE_EXPORTS_";
  1008. exportFlagVar += linkLanguage;
  1009. exportFlagVar += "_FLAG";
  1010. linkFlags += this->Makefile->GetSafeDefinition(exportFlagVar);
  1011. linkFlags += " ";
  1012. }
  1013. std::string cmp0065Flags =
  1014. this->GetLinkLibsCMP0065(linkLanguage, *target);
  1015. if (!cmp0065Flags.empty()) {
  1016. linkFlags += cmp0065Flags;
  1017. linkFlags += " ";
  1018. }
  1019. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1020. if (targetLinkFlags) {
  1021. linkFlags += targetLinkFlags;
  1022. linkFlags += " ";
  1023. }
  1024. if (!buildType.empty()) {
  1025. std::string configLinkFlags = "LINK_FLAGS_";
  1026. configLinkFlags += buildType;
  1027. targetLinkFlags = target->GetProperty(configLinkFlags);
  1028. if (targetLinkFlags) {
  1029. linkFlags += targetLinkFlags;
  1030. linkFlags += " ";
  1031. }
  1032. }
  1033. } break;
  1034. default:
  1035. break;
  1036. }
  1037. this->AppendIPOLinkerFlags(linkFlags, target, config, linkLanguage);
  1038. }
  1039. void cmLocalGenerator::GetTargetCompileFlags(cmGeneratorTarget* target,
  1040. std::string const& config,
  1041. std::string const& lang,
  1042. std::string& flags)
  1043. {
  1044. cmMakefile* mf = this->GetMakefile();
  1045. // Add language-specific flags.
  1046. this->AddLanguageFlags(flags, target, lang, config);
  1047. this->AddArchitectureFlags(flags, target, lang, config);
  1048. if (lang == "Fortran") {
  1049. this->AppendFlags(flags, this->GetTargetFortranFlags(target, config));
  1050. }
  1051. this->AddCMP0018Flags(flags, target, lang, config);
  1052. this->AddVisibilityPresetFlags(flags, target, lang);
  1053. this->AppendFlags(flags, mf->GetDefineFlags());
  1054. this->AppendFlags(flags, this->GetFrameworkFlags(lang, config, target));
  1055. this->AddCompileOptions(flags, target, lang, config);
  1056. }
  1057. static std::string GetFrameworkFlags(const std::string& lang,
  1058. const std::string& config,
  1059. cmGeneratorTarget* target)
  1060. {
  1061. cmLocalGenerator* lg = target->GetLocalGenerator();
  1062. cmMakefile* mf = lg->GetMakefile();
  1063. if (!mf->IsOn("APPLE")) {
  1064. return std::string();
  1065. }
  1066. std::string fwSearchFlagVar = "CMAKE_" + lang + "_FRAMEWORK_SEARCH_FLAG";
  1067. const char* fwSearchFlag = mf->GetDefinition(fwSearchFlagVar);
  1068. if (!(fwSearchFlag && *fwSearchFlag)) {
  1069. return std::string();
  1070. }
  1071. std::set<std::string> emitted;
  1072. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1073. emitted.insert("/System/Library/Frameworks");
  1074. #endif
  1075. std::vector<std::string> includes;
  1076. lg->GetIncludeDirectories(includes, target, "C", config);
  1077. // check all include directories for frameworks as this
  1078. // will already have added a -F for the framework
  1079. for (std::vector<std::string>::iterator i = includes.begin();
  1080. i != includes.end(); ++i) {
  1081. if (lg->GetGlobalGenerator()->NameResolvesToFramework(*i)) {
  1082. std::string frameworkDir = *i;
  1083. frameworkDir += "/../";
  1084. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1085. emitted.insert(frameworkDir);
  1086. }
  1087. }
  1088. std::string flags;
  1089. if (cmComputeLinkInformation* cli = target->GetLinkInformation(config)) {
  1090. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1091. for (std::vector<std::string>::const_iterator i = frameworks.begin();
  1092. i != frameworks.end(); ++i) {
  1093. if (emitted.insert(*i).second) {
  1094. flags += fwSearchFlag;
  1095. flags += lg->ConvertToOutputFormat(*i, cmOutputConverter::SHELL);
  1096. flags += " ";
  1097. }
  1098. }
  1099. }
  1100. return flags;
  1101. }
  1102. std::string cmLocalGenerator::GetFrameworkFlags(std::string const& l,
  1103. std::string const& config,
  1104. cmGeneratorTarget* target)
  1105. {
  1106. return ::GetFrameworkFlags(l, config, target);
  1107. }
  1108. void cmLocalGenerator::GetTargetDefines(cmGeneratorTarget const* target,
  1109. std::string const& config,
  1110. std::string const& lang,
  1111. std::set<std::string>& defines) const
  1112. {
  1113. // Add the export symbol definition for shared library objects.
  1114. if (const char* exportMacro = target->GetExportMacro()) {
  1115. this->AppendDefines(defines, exportMacro);
  1116. }
  1117. // Add preprocessor definitions for this target and configuration.
  1118. this->AddCompileDefinitions(defines, target, config, lang);
  1119. }
  1120. std::string cmLocalGenerator::GetTargetFortranFlags(
  1121. cmGeneratorTarget const* /*unused*/, std::string const& /*unused*/)
  1122. {
  1123. // Implemented by specific generators that override this.
  1124. return std::string();
  1125. }
  1126. /**
  1127. * Output the linking rules on a command line. For executables,
  1128. * targetLibrary should be a NULL pointer. For libraries, it should point
  1129. * to the name of the library. This will not link a library against itself.
  1130. */
  1131. void cmLocalGenerator::OutputLinkLibraries(
  1132. cmComputeLinkInformation* pcli, cmLinkLineComputer* linkLineComputer,
  1133. std::string& linkLibraries, std::string& frameworkPath,
  1134. std::string& linkPath)
  1135. {
  1136. cmComputeLinkInformation& cli = *pcli;
  1137. std::string linkLanguage = cli.GetLinkLanguage();
  1138. std::string libPathFlag =
  1139. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1140. std::string libPathTerminator =
  1141. this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1142. // Add standard libraries for this language.
  1143. std::string standardLibsVar = "CMAKE_";
  1144. standardLibsVar += cli.GetLinkLanguage();
  1145. standardLibsVar += "_STANDARD_LIBRARIES";
  1146. std::string stdLibString;
  1147. if (const char* stdLibs = this->Makefile->GetDefinition(standardLibsVar)) {
  1148. stdLibString = stdLibs;
  1149. }
  1150. // Append the framework search path flags.
  1151. std::string fwSearchFlagVar = "CMAKE_";
  1152. fwSearchFlagVar += linkLanguage;
  1153. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  1154. std::string fwSearchFlag =
  1155. this->Makefile->GetSafeDefinition(fwSearchFlagVar);
  1156. frameworkPath = linkLineComputer->ComputeFrameworkPath(cli, fwSearchFlag);
  1157. linkPath =
  1158. linkLineComputer->ComputeLinkPath(cli, libPathFlag, libPathTerminator);
  1159. linkLibraries = linkLineComputer->ComputeLinkLibraries(cli, stdLibString);
  1160. }
  1161. std::string cmLocalGenerator::GetLinkLibsCMP0065(
  1162. std::string const& linkLanguage, cmGeneratorTarget& tgt) const
  1163. {
  1164. std::string linkFlags;
  1165. // Flags to link an executable to shared libraries.
  1166. if (tgt.GetType() == cmStateEnums::EXECUTABLE &&
  1167. this->StateSnapshot.GetState()->GetGlobalPropertyAsBool(
  1168. "TARGET_SUPPORTS_SHARED_LIBS")) {
  1169. bool add_shlib_flags = false;
  1170. switch (tgt.GetPolicyStatusCMP0065()) {
  1171. case cmPolicies::WARN:
  1172. if (!tgt.GetPropertyAsBool("ENABLE_EXPORTS") &&
  1173. this->Makefile->PolicyOptionalWarningEnabled(
  1174. "CMAKE_POLICY_WARNING_CMP0065")) {
  1175. std::ostringstream w;
  1176. /* clang-format off */
  1177. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0065) << "\n"
  1178. "For compatibility with older versions of CMake, "
  1179. "additional flags may be added to export symbols on all "
  1180. "executables regardless of their ENABLE_EXPORTS property.";
  1181. /* clang-format on */
  1182. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  1183. }
  1184. CM_FALLTHROUGH;
  1185. case cmPolicies::OLD:
  1186. // OLD behavior is to always add the flags
  1187. add_shlib_flags = true;
  1188. break;
  1189. case cmPolicies::REQUIRED_IF_USED:
  1190. case cmPolicies::REQUIRED_ALWAYS:
  1191. this->IssueMessage(
  1192. cmake::FATAL_ERROR,
  1193. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0065));
  1194. CM_FALLTHROUGH;
  1195. case cmPolicies::NEW:
  1196. // NEW behavior is to only add the flags if ENABLE_EXPORTS is on
  1197. add_shlib_flags = tgt.GetPropertyAsBool("ENABLE_EXPORTS");
  1198. break;
  1199. }
  1200. if (add_shlib_flags) {
  1201. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1202. linkFlagsVar += linkLanguage;
  1203. linkFlagsVar += "_FLAGS";
  1204. linkFlags = this->Makefile->GetSafeDefinition(linkFlagsVar);
  1205. }
  1206. }
  1207. return linkFlags;
  1208. }
  1209. void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
  1210. cmGeneratorTarget const* target,
  1211. const std::string& lang,
  1212. const std::string& config)
  1213. {
  1214. // Only add Mac OS X specific flags on Darwin platforms (OSX and iphone):
  1215. if (this->Makefile->IsOn("APPLE") && this->EmitUniversalBinaryFlags) {
  1216. std::vector<std::string> archs;
  1217. target->GetAppleArchs(config, archs);
  1218. const char* sysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1219. if (sysroot && sysroot[0] == '/' && !sysroot[1]) {
  1220. sysroot = CM_NULLPTR;
  1221. }
  1222. std::string sysrootFlagVar =
  1223. std::string("CMAKE_") + lang + "_SYSROOT_FLAG";
  1224. const char* sysrootFlag = this->Makefile->GetDefinition(sysrootFlagVar);
  1225. const char* deploymentTarget =
  1226. this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  1227. std::string deploymentTargetFlagVar =
  1228. std::string("CMAKE_") + lang + "_OSX_DEPLOYMENT_TARGET_FLAG";
  1229. const char* deploymentTargetFlag =
  1230. this->Makefile->GetDefinition(deploymentTargetFlagVar);
  1231. if (!archs.empty() && !lang.empty() &&
  1232. (lang[0] == 'C' || lang[0] == 'F')) {
  1233. for (std::vector<std::string>::iterator i = archs.begin();
  1234. i != archs.end(); ++i) {
  1235. flags += " -arch ";
  1236. flags += *i;
  1237. }
  1238. }
  1239. if (sysrootFlag && *sysrootFlag && sysroot && *sysroot) {
  1240. flags += " ";
  1241. flags += sysrootFlag;
  1242. flags += " ";
  1243. flags += this->ConvertToOutputFormat(sysroot, SHELL);
  1244. }
  1245. if (deploymentTargetFlag && *deploymentTargetFlag && deploymentTarget &&
  1246. *deploymentTarget) {
  1247. flags += " ";
  1248. flags += deploymentTargetFlag;
  1249. flags += deploymentTarget;
  1250. }
  1251. }
  1252. }
  1253. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1254. cmGeneratorTarget const* target,
  1255. const std::string& lang,
  1256. const std::string& config)
  1257. {
  1258. // Add language-specific flags.
  1259. std::string flagsVar = "CMAKE_";
  1260. flagsVar += lang;
  1261. flagsVar += "_FLAGS";
  1262. this->AddConfigVariableFlags(flags, flagsVar, config);
  1263. if (target->IsIPOEnabled(lang, config)) {
  1264. this->AppendFeatureOptions(flags, lang, "IPO");
  1265. }
  1266. }
  1267. void cmLocalGenerator::AddLanguageFlagsForLinking(
  1268. std::string& flags, cmGeneratorTarget const* target, const std::string& lang,
  1269. const std::string& config)
  1270. {
  1271. if (this->Makefile->IsOn("CMAKE_" + lang +
  1272. "_LINK_WITH_STANDARD_COMPILE_OPTION")) {
  1273. // This toolchain requires use of the language standard flag
  1274. // when linking in order to use the matching standard library.
  1275. // FIXME: If CMake gains an abstraction for standard library
  1276. // selection, this will have to be reconciled with it.
  1277. this->AddCompilerRequirementFlag(flags, target, lang);
  1278. }
  1279. this->AddLanguageFlags(flags, target, lang, config);
  1280. }
  1281. cmGeneratorTarget* cmLocalGenerator::FindGeneratorTargetToUse(
  1282. const std::string& name) const
  1283. {
  1284. std::vector<cmGeneratorTarget*>::const_iterator imported = std::find_if(
  1285. this->ImportedGeneratorTargets.begin(),
  1286. this->ImportedGeneratorTargets.end(), NamedGeneratorTargetFinder(name));
  1287. if (imported != this->ImportedGeneratorTargets.end()) {
  1288. return *imported;
  1289. }
  1290. if (cmGeneratorTarget* t = this->FindLocalNonAliasGeneratorTarget(name)) {
  1291. return t;
  1292. }
  1293. return this->GetGlobalGenerator()->FindGeneratorTarget(name);
  1294. }
  1295. bool cmLocalGenerator::GetRealDependency(const std::string& inName,
  1296. const std::string& config,
  1297. std::string& dep)
  1298. {
  1299. // Older CMake code may specify the dependency using the target
  1300. // output file rather than the target name. Such code would have
  1301. // been written before there was support for target properties that
  1302. // modify the name so stripping down to just the file name should
  1303. // produce the target name in this case.
  1304. std::string name = cmSystemTools::GetFilenameName(inName);
  1305. // If the input name is the empty string, there is no real
  1306. // dependency. Short-circuit the other checks:
  1307. if (name == "") {
  1308. return false;
  1309. }
  1310. if (cmSystemTools::GetFilenameLastExtension(name) == ".exe") {
  1311. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1312. }
  1313. // Look for a CMake target with the given name.
  1314. if (cmGeneratorTarget* target = this->FindGeneratorTargetToUse(name)) {
  1315. // make sure it is not just a coincidence that the target name
  1316. // found is part of the inName
  1317. if (cmSystemTools::FileIsFullPath(inName.c_str())) {
  1318. std::string tLocation;
  1319. if (target->GetType() >= cmStateEnums::EXECUTABLE &&
  1320. target->GetType() <= cmStateEnums::MODULE_LIBRARY) {
  1321. tLocation = target->GetLocation(config);
  1322. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1323. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  1324. }
  1325. std::string depLocation =
  1326. cmSystemTools::GetFilenamePath(std::string(inName));
  1327. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  1328. if (depLocation != tLocation) {
  1329. // it is a full path to a depend that has the same name
  1330. // as a target but is in a different location so do not use
  1331. // the target as the depend
  1332. dep = inName;
  1333. return true;
  1334. }
  1335. }
  1336. switch (target->GetType()) {
  1337. case cmStateEnums::EXECUTABLE:
  1338. case cmStateEnums::STATIC_LIBRARY:
  1339. case cmStateEnums::SHARED_LIBRARY:
  1340. case cmStateEnums::MODULE_LIBRARY:
  1341. case cmStateEnums::UNKNOWN_LIBRARY:
  1342. dep = target->GetLocation(config);
  1343. return true;
  1344. case cmStateEnums::OBJECT_LIBRARY:
  1345. // An object library has no single file on which to depend.
  1346. // This was listed to get the target-level dependency.
  1347. return false;
  1348. case cmStateEnums::INTERFACE_LIBRARY:
  1349. // An interface library has no file on which to depend.
  1350. // This was listed to get the target-level dependency.
  1351. return false;
  1352. case cmStateEnums::UTILITY:
  1353. case cmStateEnums::GLOBAL_TARGET:
  1354. // A utility target has no file on which to depend. This was listed
  1355. // only to get the target-level dependency.
  1356. return false;
  1357. }
  1358. }
  1359. // The name was not that of a CMake target. It must name a file.
  1360. if (cmSystemTools::FileIsFullPath(inName.c_str())) {
  1361. // This is a full path. Return it as given.
  1362. dep = inName;
  1363. return true;
  1364. }
  1365. // Check for a source file in this directory that matches the
  1366. // dependency.
  1367. if (cmSourceFile* sf = this->Makefile->GetSource(inName)) {
  1368. dep = sf->GetFullPath();
  1369. return true;
  1370. }
  1371. // Treat the name as relative to the source directory in which it
  1372. // was given.
  1373. dep = this->StateSnapshot.GetDirectory().GetCurrentSource();
  1374. dep += "/";
  1375. dep += inName;
  1376. return true;
  1377. }
  1378. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1379. const std::string& lang, bool shared)
  1380. {
  1381. std::string flagsVar;
  1382. // Add flags for dealing with shared libraries for this language.
  1383. if (shared) {
  1384. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1385. flagsVar += lang;
  1386. flagsVar += "_FLAGS";
  1387. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1388. }
  1389. }
  1390. void cmLocalGenerator::AddCompilerRequirementFlag(
  1391. std::string& flags, cmGeneratorTarget const* target, const std::string& lang)
  1392. {
  1393. if (lang.empty()) {
  1394. return;
  1395. }
  1396. const char* defaultStd =
  1397. this->Makefile->GetDefinition("CMAKE_" + lang + "_STANDARD_DEFAULT");
  1398. if (!defaultStd || !*defaultStd) {
  1399. // This compiler has no notion of language standard levels.
  1400. return;
  1401. }
  1402. std::string extProp = lang + "_EXTENSIONS";
  1403. bool ext = true;
  1404. if (const char* extPropValue = target->GetProperty(extProp)) {
  1405. if (cmSystemTools::IsOff(extPropValue)) {
  1406. ext = false;
  1407. }
  1408. }
  1409. std::string stdProp = lang + "_STANDARD";
  1410. const char* standardProp = target->GetProperty(stdProp);
  1411. if (!standardProp) {
  1412. if (ext) {
  1413. // No language standard is specified and extensions are not disabled.
  1414. // Check if this compiler needs a flag to enable extensions.
  1415. std::string const option_flag =
  1416. "CMAKE_" + lang + "_EXTENSION_COMPILE_OPTION";
  1417. if (const char* opt =
  1418. target->Target->GetMakefile()->GetDefinition(option_flag)) {
  1419. this->AppendFlagEscape(flags, opt);
  1420. }
  1421. }
  1422. return;
  1423. }
  1424. std::string const type = ext ? "EXTENSION" : "STANDARD";
  1425. if (target->GetPropertyAsBool(lang + "_STANDARD_REQUIRED")) {
  1426. std::string option_flag =
  1427. "CMAKE_" + lang + standardProp + "_" + type + "_COMPILE_OPTION";
  1428. const char* opt =
  1429. target->Target->GetMakefile()->GetDefinition(option_flag);
  1430. if (!opt) {
  1431. std::ostringstream e;
  1432. e << "Target \"" << target->GetName() << "\" requires the language "
  1433. "dialect \""
  1434. << lang << standardProp << "\" "
  1435. << (ext ? "(with compiler extensions)" : "")
  1436. << ", but CMake "
  1437. "does not know the compile flags to use to enable it.";
  1438. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  1439. } else {
  1440. std::vector<std::string> optVec;
  1441. cmSystemTools::ExpandListArgument(opt, optVec);
  1442. for (size_t i = 0; i < optVec.size(); ++i) {
  1443. this->AppendFlagEscape(flags, optVec[i]);
  1444. }
  1445. }
  1446. return;
  1447. }
  1448. static std::map<std::string, std::vector<std::string> > langStdMap;
  1449. if (langStdMap.empty()) {
  1450. // Maintain sorted order, most recent first.
  1451. langStdMap["CXX"].push_back("17");
  1452. langStdMap["CXX"].push_back("14");
  1453. langStdMap["CXX"].push_back("11");
  1454. langStdMap["CXX"].push_back("98");
  1455. langStdMap["C"].push_back("11");
  1456. langStdMap["C"].push_back("99");
  1457. langStdMap["C"].push_back("90");
  1458. langStdMap["CUDA"].push_back("14");
  1459. langStdMap["CUDA"].push_back("11");
  1460. langStdMap["CUDA"].push_back("98");
  1461. }
  1462. std::string standard(standardProp);
  1463. std::vector<std::string>& stds = langStdMap[lang];
  1464. std::vector<std::string>::const_iterator stdIt =
  1465. std::find(stds.begin(), stds.end(), standard);
  1466. if (stdIt == stds.end()) {
  1467. std::string e =
  1468. lang + "_STANDARD is set to invalid value '" + standard + "'";
  1469. this->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  1470. cmake::FATAL_ERROR, e, target->GetBacktrace());
  1471. return;
  1472. }
  1473. std::vector<std::string>::const_iterator defaultStdIt =
  1474. std::find(stds.begin(), stds.end(), defaultStd);
  1475. if (defaultStdIt == stds.end()) {
  1476. std::string e = "CMAKE_" + lang +
  1477. "_STANDARD_DEFAULT is set to invalid value '" + std::string(defaultStd) +
  1478. "'";
  1479. this->IssueMessage(cmake::INTERNAL_ERROR, e);
  1480. return;
  1481. }
  1482. // Greater or equal because the standards are stored in
  1483. // backward chronological order.
  1484. if (stdIt >= defaultStdIt) {
  1485. std::string option_flag =
  1486. "CMAKE_" + lang + *stdIt + "_" + type + "_COMPILE_OPTION";
  1487. const char* opt =
  1488. target->Target->GetMakefile()->GetRequiredDefinition(option_flag);
  1489. this->AppendFlagEscape(flags, opt);
  1490. return;
  1491. }
  1492. for (; stdIt < defaultStdIt; ++stdIt) {
  1493. std::string option_flag =
  1494. "CMAKE_" + lang + *stdIt + "_" + type + "_COMPILE_OPTION";
  1495. if (const char* opt =
  1496. target->Target->GetMakefile()->GetDefinition(option_flag)) {
  1497. this->AppendFlagEscape(flags, opt);
  1498. return;
  1499. }
  1500. }
  1501. }
  1502. static void AddVisibilityCompileOption(std::string& flags,
  1503. cmGeneratorTarget const* target,
  1504. cmLocalGenerator* lg,
  1505. const std::string& lang,
  1506. std::string* warnCMP0063)
  1507. {
  1508. std::string compileOption = "CMAKE_" + lang + "_COMPILE_OPTIONS_VISIBILITY";
  1509. const char* opt = lg->GetMakefile()->GetDefinition(compileOption);
  1510. if (!opt) {
  1511. return;
  1512. }
  1513. std::string flagDefine = lang + "_VISIBILITY_PRESET";
  1514. const char* prop = target->GetProperty(flagDefine);
  1515. if (!prop) {
  1516. return;
  1517. }
  1518. if (warnCMP0063) {
  1519. *warnCMP0063 += " " + flagDefine + "\n";
  1520. return;
  1521. }
  1522. if (strcmp(prop, "hidden") != 0 && strcmp(prop, "default") != 0 &&
  1523. strcmp(prop, "protected") != 0 && strcmp(prop, "internal") != 0) {
  1524. std::ostringstream e;
  1525. e << "Target " << target->GetName() << " uses unsupported value \"" << prop
  1526. << "\" for " << flagDefine << ".";
  1527. cmSystemTools::Error(e.str().c_str());
  1528. return;
  1529. }
  1530. std::string option = std::string(opt) + prop;
  1531. lg->AppendFlags(flags, option);
  1532. }
  1533. static void AddInlineVisibilityCompileOption(std::string& flags,
  1534. cmGeneratorTarget const* target,
  1535. cmLocalGenerator* lg,
  1536. std::string* warnCMP0063)
  1537. {
  1538. std::string compileOption =
  1539. "CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN";
  1540. const char* opt = lg->GetMakefile()->GetDefinition(compileOption);
  1541. if (!opt) {
  1542. return;
  1543. }
  1544. bool prop = target->GetPropertyAsBool("VISIBILITY_INLINES_HIDDEN");
  1545. if (!prop) {
  1546. return;
  1547. }
  1548. if (warnCMP0063) {
  1549. *warnCMP0063 += " VISIBILITY_INLINES_HIDDEN\n";
  1550. return;
  1551. }
  1552. lg->AppendFlags(flags, opt);
  1553. }
  1554. void cmLocalGenerator::AddVisibilityPresetFlags(
  1555. std::string& flags, cmGeneratorTarget const* target, const std::string& lang)
  1556. {
  1557. if (lang.empty()) {
  1558. return;
  1559. }
  1560. std::string warnCMP0063;
  1561. std::string* pWarnCMP0063 = CM_NULLPTR;
  1562. if (target->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1563. target->GetType() != cmStateEnums::MODULE_LIBRARY &&
  1564. !target->IsExecutableWithExports()) {
  1565. switch (target->GetPolicyStatusCMP0063()) {
  1566. case cmPolicies::OLD:
  1567. return;
  1568. case cmPolicies::WARN:
  1569. pWarnCMP0063 = &warnCMP0063;
  1570. break;
  1571. default:
  1572. break;
  1573. }
  1574. }
  1575. AddVisibilityCompileOption(flags, target, this, lang, pWarnCMP0063);
  1576. if (lang == "CXX") {
  1577. AddInlineVisibilityCompileOption(flags, target, this, pWarnCMP0063);
  1578. }
  1579. if (!warnCMP0063.empty() && this->WarnCMP0063.insert(target).second) {
  1580. std::ostringstream w;
  1581. /* clang-format off */
  1582. w <<
  1583. cmPolicies::GetPolicyWarning(cmPolicies::CMP0063) << "\n"
  1584. "Target \"" << target->GetName() << "\" of "
  1585. "type \"" << cmState::GetTargetTypeName(target->GetType()) << "\" "
  1586. "has the following visibility properties set for " << lang << ":\n" <<
  1587. warnCMP0063 <<
  1588. "For compatibility CMake is not honoring them for this target.";
  1589. /* clang-format on */
  1590. target->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1591. cmake::AUTHOR_WARNING, w.str(), target->GetBacktrace());
  1592. }
  1593. }
  1594. void cmLocalGenerator::AddCMP0018Flags(std::string& flags,
  1595. cmGeneratorTarget const* target,
  1596. std::string const& lang,
  1597. const std::string& config)
  1598. {
  1599. int targetType = target->GetType();
  1600. bool shared = ((targetType == cmStateEnums::SHARED_LIBRARY) ||
  1601. (targetType == cmStateEnums::MODULE_LIBRARY));
  1602. if (this->GetShouldUseOldFlags(shared, lang)) {
  1603. this->AddSharedFlags(flags, lang, shared);
  1604. } else {
  1605. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1606. if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE")) {
  1607. this->AddPositionIndependentFlags(flags, lang, targetType);
  1608. }
  1609. return;
  1610. }
  1611. if (target->GetLinkInterfaceDependentBoolProperty(
  1612. "POSITION_INDEPENDENT_CODE", config)) {
  1613. this->AddPositionIndependentFlags(flags, lang, targetType);
  1614. }
  1615. if (shared) {
  1616. this->AppendFeatureOptions(flags, lang, "DLL");
  1617. }
  1618. }
  1619. }
  1620. bool cmLocalGenerator::GetShouldUseOldFlags(bool shared,
  1621. const std::string& lang) const
  1622. {
  1623. std::string originalFlags =
  1624. this->GlobalGenerator->GetSharedLibFlagsForLanguage(lang);
  1625. if (shared) {
  1626. std::string flagsVar = "CMAKE_SHARED_LIBRARY_";
  1627. flagsVar += lang;
  1628. flagsVar += "_FLAGS";
  1629. const char* flags = this->Makefile->GetSafeDefinition(flagsVar);
  1630. if (flags && flags != originalFlags) {
  1631. switch (this->GetPolicyStatus(cmPolicies::CMP0018)) {
  1632. case cmPolicies::WARN: {
  1633. std::ostringstream e;
  1634. e << "Variable " << flagsVar
  1635. << " has been modified. CMake "
  1636. "will ignore the POSITION_INDEPENDENT_CODE target property for "
  1637. "shared libraries and will use the "
  1638. << flagsVar
  1639. << " variable "
  1640. "instead. This may cause errors if the original content of "
  1641. << flagsVar << " was removed.\n"
  1642. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0018);
  1643. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  1644. CM_FALLTHROUGH;
  1645. }
  1646. case cmPolicies::OLD:
  1647. return true;
  1648. case cmPolicies::REQUIRED_IF_USED:
  1649. case cmPolicies::REQUIRED_ALWAYS:
  1650. case cmPolicies::NEW:
  1651. return false;
  1652. }
  1653. }
  1654. }
  1655. return false;
  1656. }
  1657. void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
  1658. std::string const& lang,
  1659. int targetType)
  1660. {
  1661. const char* picFlags = CM_NULLPTR;
  1662. if (targetType == cmStateEnums::EXECUTABLE) {
  1663. std::string flagsVar = "CMAKE_";
  1664. flagsVar += lang;
  1665. flagsVar += "_COMPILE_OPTIONS_PIE";
  1666. picFlags = this->Makefile->GetSafeDefinition(flagsVar);
  1667. }
  1668. if (!picFlags) {
  1669. std::string flagsVar = "CMAKE_";
  1670. flagsVar += lang;
  1671. flagsVar += "_COMPILE_OPTIONS_PIC";
  1672. picFlags = this->Makefile->GetSafeDefinition(flagsVar);
  1673. }
  1674. if (picFlags) {
  1675. std::vector<std::string> options;
  1676. cmSystemTools::ExpandListArgument(picFlags, options);
  1677. for (std::vector<std::string>::const_iterator oi = options.begin();
  1678. oi != options.end(); ++oi) {
  1679. this->AppendFlagEscape(flags, *oi);
  1680. }
  1681. }
  1682. }
  1683. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1684. const std::string& var,
  1685. const std::string& config)
  1686. {
  1687. // Add the flags from the variable itself.
  1688. std::string flagsVar = var;
  1689. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1690. // Add the flags from the build-type specific variable.
  1691. if (!config.empty()) {
  1692. flagsVar += "_";
  1693. flagsVar += cmSystemTools::UpperCase(config);
  1694. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1695. }
  1696. }
  1697. void cmLocalGenerator::AppendFlags(std::string& flags,
  1698. const std::string& newFlags)
  1699. {
  1700. if (!newFlags.empty()) {
  1701. if (!flags.empty()) {
  1702. flags += " ";
  1703. }
  1704. flags += newFlags;
  1705. }
  1706. }
  1707. void cmLocalGenerator::AppendFlags(std::string& flags, const char* newFlags)
  1708. {
  1709. if (newFlags && *newFlags) {
  1710. this->AppendFlags(flags, std::string(newFlags));
  1711. }
  1712. }
  1713. void cmLocalGenerator::AppendFlagEscape(std::string& flags,
  1714. const std::string& rawFlag)
  1715. {
  1716. this->AppendFlags(flags, this->EscapeForShell(rawFlag));
  1717. }
  1718. void cmLocalGenerator::AppendIPOLinkerFlags(std::string& flags,
  1719. cmGeneratorTarget* target,
  1720. const std::string& config,
  1721. const std::string& lang)
  1722. {
  1723. if (!target->IsIPOEnabled(lang, config)) {
  1724. return;
  1725. }
  1726. switch (target->GetType()) {
  1727. case cmStateEnums::EXECUTABLE:
  1728. case cmStateEnums::SHARED_LIBRARY:
  1729. case cmStateEnums::MODULE_LIBRARY:
  1730. break;
  1731. default:
  1732. return;
  1733. }
  1734. const std::string name = "CMAKE_" + lang + "_LINK_OPTIONS_IPO";
  1735. const char* rawFlagsList = this->Makefile->GetDefinition(name);
  1736. if (rawFlagsList == CM_NULLPTR) {
  1737. return;
  1738. }
  1739. std::vector<std::string> flagsList;
  1740. cmSystemTools::ExpandListArgument(rawFlagsList, flagsList);
  1741. for (std::vector<std::string>::const_iterator oi = flagsList.begin();
  1742. oi != flagsList.end(); ++oi) {
  1743. this->AppendFlagEscape(flags, *oi);
  1744. }
  1745. }
  1746. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  1747. const char* defines_list) const
  1748. {
  1749. // Short-circuit if there are no definitions.
  1750. if (!defines_list) {
  1751. return;
  1752. }
  1753. // Expand the list of definitions.
  1754. std::vector<std::string> defines_vec;
  1755. cmSystemTools::ExpandListArgument(defines_list, defines_vec);
  1756. this->AppendDefines(defines, defines_vec);
  1757. }
  1758. void cmLocalGenerator::AppendDefines(
  1759. std::set<std::string>& defines,
  1760. const std::vector<std::string>& defines_vec) const
  1761. {
  1762. for (std::vector<std::string>::const_iterator di = defines_vec.begin();
  1763. di != defines_vec.end(); ++di) {
  1764. // Skip unsupported definitions.
  1765. if (!this->CheckDefinition(*di)) {
  1766. continue;
  1767. }
  1768. defines.insert(*di);
  1769. }
  1770. }
  1771. void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines,
  1772. std::string& definesString,
  1773. const std::string& lang)
  1774. {
  1775. // Lookup the define flag for the current language.
  1776. std::string dflag = "-D";
  1777. if (!lang.empty()) {
  1778. std::string defineFlagVar = "CMAKE_";
  1779. defineFlagVar += lang;
  1780. defineFlagVar += "_DEFINE_FLAG";
  1781. const char* df = this->Makefile->GetDefinition(defineFlagVar);
  1782. if (df && *df) {
  1783. dflag = df;
  1784. }
  1785. }
  1786. std::set<std::string>::const_iterator defineIt = defines.begin();
  1787. const std::set<std::string>::const_iterator defineEnd = defines.end();
  1788. const char* itemSeparator = definesString.empty() ? "" : " ";
  1789. for (; defineIt != defineEnd; ++defineIt) {
  1790. // Append the definition with proper escaping.
  1791. std::string def = dflag;
  1792. if (this->GetState()->UseWatcomWMake()) {
  1793. // The Watcom compiler does its own command line parsing instead
  1794. // of using the windows shell rules. Definitions are one of
  1795. // -DNAME
  1796. // -DNAME=<cpp-token>
  1797. // -DNAME="c-string with spaces and other characters(?@#$)"
  1798. //
  1799. // Watcom will properly parse each of these cases from the
  1800. // command line without any escapes. However we still have to
  1801. // get the '$' and '#' characters through WMake as '$$' and
  1802. // '$#'.
  1803. for (const char* c = defineIt->c_str(); *c; ++c) {
  1804. if (*c == '$' || *c == '#') {
  1805. def += '$';
  1806. }
  1807. def += *c;
  1808. }
  1809. } else {
  1810. // Make the definition appear properly on the command line. Use
  1811. // -DNAME="value" instead of -D"NAME=value" for historical reasons.
  1812. std::string::size_type eq = defineIt->find("=");
  1813. def += defineIt->substr(0, eq);
  1814. if (eq != std::string::npos) {
  1815. def += "=";
  1816. def += this->EscapeForShell(defineIt->c_str() + eq + 1, true);
  1817. }
  1818. }
  1819. definesString += itemSeparator;
  1820. itemSeparator = " ";
  1821. definesString += def;
  1822. }
  1823. }
  1824. void cmLocalGenerator::AppendFeatureOptions(std::string& flags,
  1825. const std::string& lang,
  1826. const char* feature)
  1827. {
  1828. std::string optVar = "CMAKE_";
  1829. optVar += lang;
  1830. optVar += "_COMPILE_OPTIONS_";
  1831. optVar += feature;
  1832. if (const char* optionList = this->Makefile->GetDefinition(optVar)) {
  1833. std::vector<std::string> options;
  1834. cmSystemTools::ExpandListArgument(optionList, options);
  1835. for (std::vector<std::string>::const_iterator oi = options.begin();
  1836. oi != options.end(); ++oi) {
  1837. this->AppendFlagEscape(flags, *oi);
  1838. }
  1839. }
  1840. }
  1841. const char* cmLocalGenerator::GetFeature(const std::string& feature,
  1842. const std::string& config)
  1843. {
  1844. std::string featureName = feature;
  1845. // TODO: Define accumulation policy for features (prepend, append, replace).
  1846. // Currently we always replace.
  1847. if (!config.empty()) {
  1848. featureName += "_";
  1849. featureName += cmSystemTools::UpperCase(config);
  1850. }
  1851. cmStateSnapshot snp = this->StateSnapshot;
  1852. while (snp.IsValid()) {
  1853. if (const char* value = snp.GetDirectory().GetProperty(featureName)) {
  1854. return value;
  1855. }
  1856. snp = snp.GetBuildsystemDirectoryParent();
  1857. }
  1858. return CM_NULLPTR;
  1859. }
  1860. std::string cmLocalGenerator::GetProjectName() const
  1861. {
  1862. return this->StateSnapshot.GetProjectName();
  1863. }
  1864. std::string cmLocalGenerator::ConstructComment(
  1865. cmCustomCommandGenerator const& ccg, const char* default_comment)
  1866. {
  1867. // Check for a comment provided with the command.
  1868. if (ccg.GetComment()) {
  1869. return ccg.GetComment();
  1870. }
  1871. // Construct a reasonable default comment if possible.
  1872. if (!ccg.GetOutputs().empty()) {
  1873. std::string comment;
  1874. comment = "Generating ";
  1875. const char* sep = "";
  1876. std::string currentBinaryDir = this->GetCurrentBinaryDirectory();
  1877. for (std::vector<std::string>::const_iterator o = ccg.GetOutputs().begin();
  1878. o != ccg.GetOutputs().end(); ++o) {
  1879. comment += sep;
  1880. comment += this->ConvertToRelativePath(currentBinaryDir, *o);
  1881. sep = ", ";
  1882. }
  1883. return comment;
  1884. }
  1885. // Otherwise use the provided default.
  1886. return default_comment;
  1887. }
  1888. class cmInstallTargetGeneratorLocal : public cmInstallTargetGenerator
  1889. {
  1890. public:
  1891. cmInstallTargetGeneratorLocal(cmLocalGenerator* lg, std::string const& t,
  1892. const char* dest, bool implib)
  1893. : cmInstallTargetGenerator(
  1894. t, dest, implib, "", std::vector<std::string>(), "Unspecified",
  1895. cmInstallGenerator::SelectMessageLevel(lg->GetMakefile()), false,
  1896. false)
  1897. {
  1898. this->Compute(lg);
  1899. }
  1900. };
  1901. void cmLocalGenerator::GenerateTargetInstallRules(
  1902. std::ostream& os, const std::string& config,
  1903. std::vector<std::string> const& configurationTypes)
  1904. {
  1905. // Convert the old-style install specification from each target to
  1906. // an install generator and run it.
  1907. std::vector<cmGeneratorTarget*> tgts = this->GetGeneratorTargets();
  1908. for (std::vector<cmGeneratorTarget*>::iterator l = tgts.begin();
  1909. l != tgts.end(); ++l) {
  1910. if ((*l)->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  1911. continue;
  1912. }
  1913. // Include the user-specified pre-install script for this target.
  1914. if (const char* preinstall = (*l)->GetProperty("PRE_INSTALL_SCRIPT")) {
  1915. cmInstallScriptGenerator g(preinstall, false, CM_NULLPTR, false);
  1916. g.Generate(os, config, configurationTypes);
  1917. }
  1918. // Install this target if a destination is given.
  1919. if ((*l)->Target->GetInstallPath() != "") {
  1920. // Compute the full install destination. Note that converting
  1921. // to unix slashes also removes any trailing slash.
  1922. // We also skip over the leading slash given by the user.
  1923. std::string destination = (*l)->Target->GetInstallPath().substr(1);
  1924. cmSystemTools::ConvertToUnixSlashes(destination);
  1925. if (destination.empty()) {
  1926. destination = ".";
  1927. }
  1928. // Generate the proper install generator for this target type.
  1929. switch ((*l)->GetType()) {
  1930. case cmStateEnums::EXECUTABLE:
  1931. case cmStateEnums::STATIC_LIBRARY:
  1932. case cmStateEnums::MODULE_LIBRARY: {
  1933. // Use a target install generator.
  1934. cmInstallTargetGeneratorLocal g(this, (*l)->GetName(),
  1935. destination.c_str(), false);
  1936. g.Generate(os, config, configurationTypes);
  1937. } break;
  1938. case cmStateEnums::SHARED_LIBRARY: {
  1939. #if defined(_WIN32) || defined(__CYGWIN__)
  1940. // Special code to handle DLL. Install the import library
  1941. // to the normal destination and the DLL to the runtime
  1942. // destination.
  1943. cmInstallTargetGeneratorLocal g1(this, (*l)->GetName(),
  1944. destination.c_str(), true);
  1945. g1.Generate(os, config, configurationTypes);
  1946. // We also skip over the leading slash given by the user.
  1947. destination = (*l)->Target->GetRuntimeInstallPath().substr(1);
  1948. cmSystemTools::ConvertToUnixSlashes(destination);
  1949. cmInstallTargetGeneratorLocal g2(this, (*l)->GetName(),
  1950. destination.c_str(), false);
  1951. g2.Generate(os, config, configurationTypes);
  1952. #else
  1953. // Use a target install generator.
  1954. cmInstallTargetGeneratorLocal g(this, (*l)->GetName(),
  1955. destination.c_str(), false);
  1956. g.Generate(os, config, configurationTypes);
  1957. #endif
  1958. } break;
  1959. default:
  1960. break;
  1961. }
  1962. }
  1963. // Include the user-specified post-install script for this target.
  1964. if (const char* postinstall = (*l)->GetProperty("POST_INSTALL_SCRIPT")) {
  1965. cmInstallScriptGenerator g(postinstall, false, CM_NULLPTR, false);
  1966. g.Generate(os, config, configurationTypes);
  1967. }
  1968. }
  1969. }
  1970. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  1971. static bool cmLocalGeneratorShortenObjectName(std::string& objName,
  1972. std::string::size_type max_len)
  1973. {
  1974. // Replace the beginning of the path portion of the object name with
  1975. // its own md5 sum.
  1976. std::string::size_type pos =
  1977. objName.find('/', objName.size() - max_len + 32);
  1978. if (pos != std::string::npos) {
  1979. cmCryptoHash md5(cmCryptoHash::AlgoMD5);
  1980. std::string md5name = md5.HashString(objName.substr(0, pos));
  1981. md5name += objName.substr(pos);
  1982. objName = md5name;
  1983. // The object name is now short enough.
  1984. return true;
  1985. }
  1986. // The object name could not be shortened enough.
  1987. return false;
  1988. }
  1989. bool cmLocalGeneratorCheckObjectName(std::string& objName,
  1990. std::string::size_type dir_len,
  1991. std::string::size_type max_total_len)
  1992. {
  1993. // Enforce the maximum file name length if possible.
  1994. std::string::size_type max_obj_len = max_total_len;
  1995. if (dir_len < max_total_len) {
  1996. max_obj_len = max_total_len - dir_len;
  1997. if (objName.size() > max_obj_len) {
  1998. // The current object file name is too long. Try to shorten it.
  1999. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2000. }
  2001. // The object file name is short enough.
  2002. return true;
  2003. }
  2004. // The build directory in which the object will be stored is
  2005. // already too deep.
  2006. return false;
  2007. }
  2008. #endif
  2009. std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName(
  2010. const std::string& sin, std::string const& dir_max)
  2011. {
  2012. // Look for an existing mapped name for this object file.
  2013. std::map<std::string, std::string>::iterator it =
  2014. this->UniqueObjectNamesMap.find(sin);
  2015. // If no entry exists create one.
  2016. if (it == this->UniqueObjectNamesMap.end()) {
  2017. // Start with the original name.
  2018. std::string ssin = sin;
  2019. // Avoid full paths by removing leading slashes.
  2020. ssin.erase(0, ssin.find_first_not_of('/'));
  2021. // Avoid full paths by removing colons.
  2022. std::replace(ssin.begin(), ssin.end(), ':', '_');
  2023. // Avoid relative paths that go up the tree.
  2024. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2025. // Avoid spaces.
  2026. std::replace(ssin.begin(), ssin.end(), ' ', '_');
  2027. // Mangle the name if necessary.
  2028. if (this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES")) {
  2029. bool done;
  2030. int cc = 0;
  2031. char rpstr[100];
  2032. sprintf(rpstr, "_p_");
  2033. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2034. std::string sssin = sin;
  2035. do {
  2036. done = true;
  2037. for (it = this->UniqueObjectNamesMap.begin();
  2038. it != this->UniqueObjectNamesMap.end(); ++it) {
  2039. if (it->second == ssin) {
  2040. done = false;
  2041. }
  2042. }
  2043. if (done) {
  2044. break;
  2045. }
  2046. sssin = ssin;
  2047. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2048. sprintf(rpstr, "_p%d_", cc++);
  2049. } while (!done);
  2050. }
  2051. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2052. if (!cmLocalGeneratorCheckObjectName(ssin, dir_max.size(),
  2053. this->ObjectPathMax)) {
  2054. // Warn if this is the first time the path has been seen.
  2055. if (this->ObjectMaxPathViolations.insert(dir_max).second) {
  2056. std::ostringstream m;
  2057. /* clang-format off */
  2058. m << "The object file directory\n"
  2059. << " " << dir_max << "\n"
  2060. << "has " << dir_max.size() << " characters. "
  2061. << "The maximum full path to an object file is "
  2062. << this->ObjectPathMax << " characters "
  2063. << "(see CMAKE_OBJECT_PATH_MAX). "
  2064. << "Object file\n"
  2065. << " " << ssin << "\n"
  2066. << "cannot be safely placed under this directory. "
  2067. << "The build may not work correctly.";
  2068. /* clang-format on */
  2069. this->IssueMessage(cmake::WARNING, m.str());
  2070. }
  2071. }
  2072. #else
  2073. (void)dir_max;
  2074. #endif
  2075. // Insert the newly mapped object file name.
  2076. std::map<std::string, std::string>::value_type e(sin, ssin);
  2077. it = this->UniqueObjectNamesMap.insert(e).first;
  2078. }
  2079. // Return the map entry.
  2080. return it->second;
  2081. }
  2082. void cmLocalGenerator::ComputeObjectFilenames(
  2083. std::map<cmSourceFile const*, std::string>& /*unused*/,
  2084. cmGeneratorTarget const* /*unused*/)
  2085. {
  2086. }
  2087. bool cmLocalGenerator::IsWindowsShell() const
  2088. {
  2089. return this->GetState()->UseWindowsShell();
  2090. }
  2091. bool cmLocalGenerator::IsWatcomWMake() const
  2092. {
  2093. return this->GetState()->UseWatcomWMake();
  2094. }
  2095. bool cmLocalGenerator::IsMinGWMake() const
  2096. {
  2097. return this->GetState()->UseMinGWMake();
  2098. }
  2099. bool cmLocalGenerator::IsNMake() const
  2100. {
  2101. return this->GetState()->UseNMake();
  2102. }
  2103. std::string cmLocalGenerator::GetObjectFileNameWithoutTarget(
  2104. const cmSourceFile& source, std::string const& dir_max,
  2105. bool* hasSourceExtension, char const* customOutputExtension)
  2106. {
  2107. // Construct the object file name using the full path to the source
  2108. // file which is its only unique identification.
  2109. std::string const& fullPath = source.GetFullPath();
  2110. // Try referencing the source relative to the source tree.
  2111. std::string relFromSource =
  2112. this->ConvertToRelativePath(this->GetCurrentSourceDirectory(), fullPath);
  2113. assert(!relFromSource.empty());
  2114. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
  2115. bool subSource = relSource && relFromSource[0] != '.';
  2116. // Try referencing the source relative to the binary tree.
  2117. std::string relFromBinary =
  2118. this->ConvertToRelativePath(this->GetCurrentBinaryDirectory(), fullPath);
  2119. assert(!relFromBinary.empty());
  2120. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
  2121. bool subBinary = relBinary && relFromBinary[0] != '.';
  2122. // Select a nice-looking reference to the source file to construct
  2123. // the object file name.
  2124. std::string objectName;
  2125. if ((relSource && !relBinary) || (subSource && !subBinary)) {
  2126. objectName = relFromSource;
  2127. } else if ((relBinary && !relSource) || (subBinary && !subSource)) {
  2128. objectName = relFromBinary;
  2129. } else if (relFromBinary.length() < relFromSource.length()) {
  2130. objectName = relFromBinary;
  2131. } else {
  2132. objectName = relFromSource;
  2133. }
  2134. // if it is still a full path check for the try compile case
  2135. // try compile never have in source sources, and should not
  2136. // have conflicting source file names in the same target
  2137. if (cmSystemTools::FileIsFullPath(objectName.c_str())) {
  2138. if (this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile()) {
  2139. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2140. }
  2141. }
  2142. // Replace the original source file extension with the object file
  2143. // extension.
  2144. bool keptSourceExtension = true;
  2145. if (!source.GetPropertyAsBool("KEEP_EXTENSION")) {
  2146. // Decide whether this language wants to replace the source
  2147. // extension with the object extension. For CMake 2.4
  2148. // compatibility do this by default.
  2149. bool replaceExt = this->NeedBackwardsCompatibility_2_4();
  2150. if (!replaceExt) {
  2151. std::string lang = source.GetLanguage();
  2152. if (!lang.empty()) {
  2153. std::string repVar = "CMAKE_";
  2154. repVar += lang;
  2155. repVar += "_OUTPUT_EXTENSION_REPLACE";
  2156. replaceExt = this->Makefile->IsOn(repVar);
  2157. }
  2158. }
  2159. // Remove the source extension if it is to be replaced.
  2160. if (replaceExt || customOutputExtension) {
  2161. keptSourceExtension = false;
  2162. std::string::size_type dot_pos = objectName.rfind('.');
  2163. if (dot_pos != std::string::npos) {
  2164. objectName = objectName.substr(0, dot_pos);
  2165. }
  2166. }
  2167. // Store the new extension.
  2168. if (customOutputExtension) {
  2169. objectName += customOutputExtension;
  2170. } else {
  2171. objectName += this->GlobalGenerator->GetLanguageOutputExtension(source);
  2172. }
  2173. }
  2174. if (hasSourceExtension) {
  2175. *hasSourceExtension = keptSourceExtension;
  2176. }
  2177. // Convert to a safe name.
  2178. return this->CreateSafeUniqueObjectFileName(objectName, dir_max);
  2179. }
  2180. std::string cmLocalGenerator::GetSourceFileLanguage(const cmSourceFile& source)
  2181. {
  2182. return source.GetLanguage();
  2183. }
  2184. cmake* cmLocalGenerator::GetCMakeInstance() const
  2185. {
  2186. return this->GlobalGenerator->GetCMakeInstance();
  2187. }
  2188. const char* cmLocalGenerator::GetSourceDirectory() const
  2189. {
  2190. return this->GetCMakeInstance()->GetHomeDirectory();
  2191. }
  2192. const char* cmLocalGenerator::GetBinaryDirectory() const
  2193. {
  2194. return this->GetCMakeInstance()->GetHomeOutputDirectory();
  2195. }
  2196. const char* cmLocalGenerator::GetCurrentBinaryDirectory() const
  2197. {
  2198. return this->StateSnapshot.GetDirectory().GetCurrentBinary();
  2199. }
  2200. const char* cmLocalGenerator::GetCurrentSourceDirectory() const
  2201. {
  2202. return this->StateSnapshot.GetDirectory().GetCurrentSource();
  2203. }
  2204. std::string cmLocalGenerator::GetTargetDirectory(
  2205. const cmGeneratorTarget* /*unused*/) const
  2206. {
  2207. cmSystemTools::Error("GetTargetDirectory"
  2208. " called on cmLocalGenerator");
  2209. return "";
  2210. }
  2211. KWIML_INT_uint64_t cmLocalGenerator::GetBackwardsCompatibility()
  2212. {
  2213. // The computed version may change until the project is fully
  2214. // configured.
  2215. if (!this->BackwardsCompatibilityFinal) {
  2216. unsigned int major = 0;
  2217. unsigned int minor = 0;
  2218. unsigned int patch = 0;
  2219. if (const char* value =
  2220. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY")) {
  2221. switch (sscanf(value, "%u.%u.%u", &major, &minor, &patch)) {
  2222. case 2:
  2223. patch = 0;
  2224. break;
  2225. case 1:
  2226. minor = 0;
  2227. patch = 0;
  2228. break;
  2229. default:
  2230. break;
  2231. }
  2232. }
  2233. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  2234. this->BackwardsCompatibilityFinal = true;
  2235. }
  2236. return this->BackwardsCompatibility;
  2237. }
  2238. bool cmLocalGenerator::NeedBackwardsCompatibility_2_4()
  2239. {
  2240. // Check the policy to decide whether to pay attention to this
  2241. // variable.
  2242. switch (this->GetPolicyStatus(cmPolicies::CMP0001)) {
  2243. case cmPolicies::WARN:
  2244. // WARN is just OLD without warning because user code does not
  2245. // always affect whether this check is done.
  2246. case cmPolicies::OLD:
  2247. // Old behavior is to check the variable.
  2248. break;
  2249. case cmPolicies::NEW:
  2250. // New behavior is to ignore the variable.
  2251. return false;
  2252. case cmPolicies::REQUIRED_IF_USED:
  2253. case cmPolicies::REQUIRED_ALWAYS:
  2254. // This will never be the case because the only way to require
  2255. // the setting is to require the user to specify version policy
  2256. // 2.6 or higher. Once we add that requirement then this whole
  2257. // method can be removed anyway.
  2258. return false;
  2259. }
  2260. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  2261. // equal to or lower than the given version.
  2262. KWIML_INT_uint64_t actual_compat = this->GetBackwardsCompatibility();
  2263. return (actual_compat && actual_compat <= CMake_VERSION_ENCODE(2, 4, 255));
  2264. }
  2265. cmPolicies::PolicyStatus cmLocalGenerator::GetPolicyStatus(
  2266. cmPolicies::PolicyID id) const
  2267. {
  2268. return this->Makefile->GetPolicyStatus(id);
  2269. }
  2270. bool cmLocalGenerator::CheckDefinition(std::string const& define) const
  2271. {
  2272. // Many compilers do not support -DNAME(arg)=sdf so we disable it.
  2273. std::string::size_type pos = define.find_first_of("(=");
  2274. if (pos != std::string::npos) {
  2275. if (define[pos] == '(') {
  2276. std::ostringstream e;
  2277. /* clang-format off */
  2278. e << "WARNING: Function-style preprocessor definitions may not be "
  2279. << "passed on the compiler command line because many compilers "
  2280. << "do not support it.\n"
  2281. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2282. << "Consider defining the macro in a (configured) header file.\n";
  2283. /* clang-format on */
  2284. cmSystemTools::Message(e.str().c_str());
  2285. return false;
  2286. }
  2287. }
  2288. // Many compilers do not support # in the value so we disable it.
  2289. if (define.find_first_of('#') != std::string::npos) {
  2290. std::ostringstream e;
  2291. /* clang-format off */
  2292. e << "WARNING: Preprocessor definitions containing '#' may not be "
  2293. << "passed on the compiler command line because many compilers "
  2294. << "do not support it.\n"
  2295. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2296. << "Consider defining the macro in a (configured) header file.\n";
  2297. /* clang-format on */
  2298. cmSystemTools::Message(e.str().c_str());
  2299. return false;
  2300. }
  2301. // Assume it is supported.
  2302. return true;
  2303. }
  2304. static void cmLGInfoProp(cmMakefile* mf, cmGeneratorTarget* target,
  2305. const std::string& prop)
  2306. {
  2307. if (const char* val = target->GetProperty(prop)) {
  2308. mf->AddDefinition(prop, val);
  2309. }
  2310. }
  2311. void cmLocalGenerator::GenerateAppleInfoPList(cmGeneratorTarget* target,
  2312. const std::string& targetName,
  2313. const char* fname)
  2314. {
  2315. // Find the Info.plist template.
  2316. const char* in = target->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
  2317. std::string inFile = (in && *in) ? in : "MacOSXBundleInfo.plist.in";
  2318. if (!cmSystemTools::FileIsFullPath(inFile.c_str())) {
  2319. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  2320. if (!inMod.empty()) {
  2321. inFile = inMod;
  2322. }
  2323. }
  2324. if (!cmSystemTools::FileExists(inFile.c_str(), true)) {
  2325. std::ostringstream e;
  2326. e << "Target " << target->GetName() << " Info.plist template \"" << inFile
  2327. << "\" could not be found.";
  2328. cmSystemTools::Error(e.str().c_str());
  2329. return;
  2330. }
  2331. // Convert target properties to variables in an isolated makefile
  2332. // scope to configure the file. If properties are set they will
  2333. // override user make variables. If not the configuration will fall
  2334. // back to the directory-level values set by the user.
  2335. cmMakefile* mf = this->Makefile;
  2336. cmMakefile::ScopePushPop varScope(mf);
  2337. mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName.c_str());
  2338. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
  2339. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
  2340. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
  2341. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
  2342. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_NAME");
  2343. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
  2344. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
  2345. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
  2346. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  2347. }
  2348. void cmLocalGenerator::GenerateFrameworkInfoPList(
  2349. cmGeneratorTarget* target, const std::string& targetName, const char* fname)
  2350. {
  2351. // Find the Info.plist template.
  2352. const char* in = target->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
  2353. std::string inFile = (in && *in) ? in : "MacOSXFrameworkInfo.plist.in";
  2354. if (!cmSystemTools::FileIsFullPath(inFile.c_str())) {
  2355. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  2356. if (!inMod.empty()) {
  2357. inFile = inMod;
  2358. }
  2359. }
  2360. if (!cmSystemTools::FileExists(inFile.c_str(), true)) {
  2361. std::ostringstream e;
  2362. e << "Target " << target->GetName() << " Info.plist template \"" << inFile
  2363. << "\" could not be found.";
  2364. cmSystemTools::Error(e.str().c_str());
  2365. return;
  2366. }
  2367. // Convert target properties to variables in an isolated makefile
  2368. // scope to configure the file. If properties are set they will
  2369. // override user make variables. If not the configuration will fall
  2370. // back to the directory-level values set by the user.
  2371. cmMakefile* mf = this->Makefile;
  2372. cmMakefile::ScopePushPop varScope(mf);
  2373. mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName.c_str());
  2374. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
  2375. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
  2376. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
  2377. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
  2378. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  2379. }