cmLocalGenerator.cxx 93 KB

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