cmLocalGenerator.cxx 91 KB

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