cmLocalGenerator.cxx 93 KB

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