cmLocalGenerator.cxx 91 KB

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