cmLocalGenerator.cxx 84 KB

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