cmLocalGenerator.cxx 92 KB

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