cmLocalGenerator.cxx 91 KB

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