cmLocalGenerator.cxx 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963
  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. std::string defineFlags = this->Makefile->GetDefineFlags();
  1115. std::cout << defineFlags << "\n";
  1116. this->FixDefineFlags(defineFlags, lang);
  1117. flags += defineFlags;
  1118. this->LanguageToIncludeFlags[lang] = flags;
  1119. // Use this temorary variable for the return value to work-around a
  1120. // bogus GCC 2.95 warning.
  1121. const char* ret = this->LanguageToIncludeFlags[lang].c_str();
  1122. return ret;
  1123. }
  1124. //----------------------------------------------------------------------------
  1125. void cmLocalGenerator::FixDefineFlags(std::string& flags,
  1126. const char* lang)
  1127. {
  1128. std::string defineFlagVar = "CMAKE_DEFINE_FLAG_";
  1129. defineFlagVar += lang;
  1130. std::string defineFlag =
  1131. this->Makefile->GetSafeDefinition(defineFlagVar.c_str());
  1132. if(defineFlag.size() == 0)
  1133. {
  1134. return;
  1135. }
  1136. std::vector<std::string> args;
  1137. cmSystemTools::ParseWindowsCommandLine(flags.c_str(), args);
  1138. std::string fixedFlags;
  1139. const char* sep = 0;
  1140. for(std::vector<std::string>::iterator i = args.begin();
  1141. i != args.end(); ++i)
  1142. {
  1143. if(sep)
  1144. {
  1145. fixedFlags += sep;
  1146. }
  1147. else
  1148. {
  1149. sep = " ";
  1150. }
  1151. cmSystemTools::ReplaceString(*i, "-D", defineFlag.c_str());
  1152. fixedFlags += *i;
  1153. }
  1154. flags = fixedFlags;
  1155. }
  1156. //----------------------------------------------------------------------------
  1157. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  1158. bool filter_system_dirs)
  1159. {
  1160. // Need to decide whether to automatically include the source and
  1161. // binary directories at the beginning of the include path.
  1162. bool includeSourceDir = false;
  1163. bool includeBinaryDir = false;
  1164. // When automatic include directories are requested for a build then
  1165. // include the source and binary directories at the beginning of the
  1166. // include path to approximate include file behavior for an
  1167. // in-source build. This does not account for the case of a source
  1168. // file in a subdirectory of the current source directory but we
  1169. // cannot fix this because not all native build tools support
  1170. // per-source-file include paths.
  1171. if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
  1172. {
  1173. includeSourceDir = true;
  1174. includeBinaryDir = true;
  1175. }
  1176. // CMake versions below 2.0 would add the source tree to the -I path
  1177. // automatically. Preserve compatibility.
  1178. const char* versionValue =
  1179. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  1180. int major = 0;
  1181. int minor = 0;
  1182. if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
  1183. {
  1184. versionValue = 0;
  1185. }
  1186. if(versionValue && major < 2)
  1187. {
  1188. includeSourceDir = true;
  1189. }
  1190. // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
  1191. // not set the backwards compatibility level automatically.
  1192. const char* vtkSourceDir =
  1193. this->Makefile->GetDefinition("VTK_SOURCE_DIR");
  1194. if(vtkSourceDir)
  1195. {
  1196. const char* vtk_major =
  1197. this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
  1198. const char* vtk_minor =
  1199. this->Makefile->GetDefinition("VTK_MINOR_VERSION");
  1200. vtk_major = vtk_major? vtk_major : "4";
  1201. vtk_minor = vtk_minor? vtk_minor : "4";
  1202. int vmajor = 0;
  1203. int vminor = 0;
  1204. if(sscanf(vtk_major, "%d", &vmajor) &&
  1205. sscanf(vtk_minor, "%d", &vminor) && vmajor == 4 && vminor <= 4)
  1206. {
  1207. includeSourceDir = true;
  1208. }
  1209. }
  1210. // Do not repeat an include path.
  1211. std::set<cmStdString> emitted;
  1212. // Store the automatic include paths.
  1213. if(includeBinaryDir)
  1214. {
  1215. dirs.push_back(this->Makefile->GetStartOutputDirectory());
  1216. emitted.insert(this->Makefile->GetStartOutputDirectory());
  1217. }
  1218. if(includeSourceDir)
  1219. {
  1220. if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
  1221. {
  1222. dirs.push_back(this->Makefile->GetStartDirectory());
  1223. emitted.insert(this->Makefile->GetStartDirectory());
  1224. }
  1225. }
  1226. if(filter_system_dirs)
  1227. {
  1228. // Do not explicitly add the standard include path "/usr/include".
  1229. // This can cause problems with certain standard library
  1230. // implementations because the wrong headers may be found first.
  1231. emitted.insert("/usr/include");
  1232. if(const char* implicitIncludes = this->Makefile->GetDefinition
  1233. ("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  1234. {
  1235. std::vector<std::string> implicitIncludeVec;
  1236. cmSystemTools::ExpandListArgument(implicitIncludes, implicitIncludeVec);
  1237. for(unsigned int k = 0; k < implicitIncludeVec.size(); ++k)
  1238. {
  1239. emitted.insert(implicitIncludeVec[k]);
  1240. }
  1241. }
  1242. }
  1243. // Get the project-specified include directories.
  1244. std::vector<std::string>& includes =
  1245. this->Makefile->GetIncludeDirectories();
  1246. // Support putting all the in-project include directories first if
  1247. // it is requested by the project.
  1248. if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
  1249. {
  1250. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1251. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1252. for(std::vector<std::string>::iterator i = includes.begin();
  1253. i != includes.end(); ++i)
  1254. {
  1255. // Emit this directory only if it is a subdirectory of the
  1256. // top-level source or binary tree.
  1257. if(cmSystemTools::ComparePath(i->c_str(), topSourceDir) ||
  1258. cmSystemTools::ComparePath(i->c_str(), topBinaryDir) ||
  1259. cmSystemTools::IsSubDirectory(i->c_str(), topSourceDir) ||
  1260. cmSystemTools::IsSubDirectory(i->c_str(), topBinaryDir))
  1261. {
  1262. if(emitted.insert(*i).second)
  1263. {
  1264. dirs.push_back(*i);
  1265. }
  1266. }
  1267. }
  1268. }
  1269. // Construct the final ordered include directory list.
  1270. for(std::vector<std::string>::iterator i = includes.begin();
  1271. i != includes.end(); ++i)
  1272. {
  1273. if(emitted.insert(*i).second)
  1274. {
  1275. dirs.push_back(*i);
  1276. }
  1277. }
  1278. }
  1279. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1280. std::string& flags,
  1281. std::string& linkFlags,
  1282. cmTarget& target)
  1283. {
  1284. std::string buildType =
  1285. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1286. buildType = cmSystemTools::UpperCase(buildType);
  1287. const char* libraryLinkVariable =
  1288. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1289. switch(target.GetType())
  1290. {
  1291. case cmTarget::STATIC_LIBRARY:
  1292. {
  1293. const char* targetLinkFlags =
  1294. target.GetProperty("STATIC_LIBRARY_FLAGS");
  1295. if(targetLinkFlags)
  1296. {
  1297. linkFlags += targetLinkFlags;
  1298. linkFlags += " ";
  1299. }
  1300. }
  1301. break;
  1302. case cmTarget::MODULE_LIBRARY:
  1303. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1304. case cmTarget::SHARED_LIBRARY:
  1305. {
  1306. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  1307. linkFlags += " ";
  1308. if(buildType.size())
  1309. {
  1310. std::string build = libraryLinkVariable;
  1311. build += "_";
  1312. build += buildType;
  1313. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1314. linkFlags += " ";
  1315. }
  1316. if(this->Makefile->IsOn("WIN32") &&
  1317. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  1318. {
  1319. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1320. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1321. i != sources.end(); ++i)
  1322. {
  1323. cmSourceFile* sf = *i;
  1324. if(sf->GetExtension() == "def")
  1325. {
  1326. linkFlags +=
  1327. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1328. linkFlags += this->Convert(sf->GetFullPath().c_str(),
  1329. START_OUTPUT, SHELL);
  1330. linkFlags += " ";
  1331. }
  1332. }
  1333. }
  1334. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1335. if(targetLinkFlags)
  1336. {
  1337. linkFlags += targetLinkFlags;
  1338. linkFlags += " ";
  1339. std::string configLinkFlags = targetLinkFlags;
  1340. configLinkFlags += buildType;
  1341. targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
  1342. if(targetLinkFlags)
  1343. {
  1344. linkFlags += targetLinkFlags;
  1345. linkFlags += " ";
  1346. }
  1347. }
  1348. cmOStringStream linklibsStr;
  1349. this->OutputLinkLibraries(linklibsStr, target, false);
  1350. linkLibs = linklibsStr.str();
  1351. }
  1352. break;
  1353. case cmTarget::EXECUTABLE:
  1354. {
  1355. linkFlags +=
  1356. this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1357. linkFlags += " ";
  1358. if(buildType.size())
  1359. {
  1360. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1361. build += buildType;
  1362. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1363. linkFlags += " ";
  1364. }
  1365. const char* linkLanguage =
  1366. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1367. if(!linkLanguage)
  1368. {
  1369. cmSystemTools::Error
  1370. ("CMake can not determine linker language for target:",
  1371. target.GetName());
  1372. return;
  1373. }
  1374. std::string langVar = "CMAKE_";
  1375. langVar += linkLanguage;
  1376. std::string flagsVar = langVar + "_FLAGS";
  1377. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  1378. sharedFlagsVar += linkLanguage;
  1379. sharedFlagsVar += "_FLAGS";
  1380. flags += this->Makefile->GetSafeDefinition(flagsVar.c_str());
  1381. flags += " ";
  1382. flags += this->Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  1383. flags += " ";
  1384. cmOStringStream linklibs;
  1385. this->OutputLinkLibraries(linklibs, target, false);
  1386. linkLibs = linklibs.str();
  1387. if(cmSystemTools::IsOn
  1388. (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1389. {
  1390. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
  1391. + linkLanguage + std::string("_FLAGS");
  1392. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
  1393. linkFlags += " ";
  1394. }
  1395. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1396. {
  1397. linkFlags +=
  1398. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1399. linkFlags += " ";
  1400. }
  1401. else
  1402. {
  1403. linkFlags +=
  1404. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1405. linkFlags += " ";
  1406. }
  1407. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1408. if(targetLinkFlags)
  1409. {
  1410. linkFlags += targetLinkFlags;
  1411. linkFlags += " ";
  1412. std::string configLinkFlags = targetLinkFlags;
  1413. configLinkFlags += buildType;
  1414. targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
  1415. if(targetLinkFlags)
  1416. {
  1417. linkFlags += targetLinkFlags;
  1418. linkFlags += " ";
  1419. }
  1420. }
  1421. }
  1422. break;
  1423. case cmTarget::UTILITY:
  1424. case cmTarget::GLOBAL_TARGET:
  1425. case cmTarget::INSTALL_FILES:
  1426. case cmTarget::INSTALL_PROGRAMS:
  1427. case cmTarget::INSTALL_DIRECTORY:
  1428. break;
  1429. }
  1430. }
  1431. bool cmLocalGenerator::GetLinkerArgs(std::string& rpath,
  1432. std::string& linkLibs,
  1433. cmTarget& tgt,
  1434. bool relink,
  1435. unsigned int minRpathSize)
  1436. {
  1437. rpath = "";
  1438. // collect all the flags needed for linking libraries
  1439. linkLibs = "";
  1440. // Try to emit each search path once
  1441. std::set<cmStdString> emitted;
  1442. // Embed runtime search paths if possible and if required.
  1443. bool outputRuntime = true;
  1444. std::string runtimeFlag;
  1445. std::string runtimeSep;
  1446. const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1447. const char* linkLanguage =
  1448. tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1449. if(!linkLanguage)
  1450. {
  1451. cmSystemTools::
  1452. Error("CMake can not determine linker language for target:",
  1453. tgt.GetName());
  1454. return false;
  1455. }
  1456. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1457. runTimeFlagVar += linkLanguage;
  1458. runTimeFlagVar += "_FLAG";
  1459. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1460. runtimeFlag = this->Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1461. runtimeSep = this->Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1462. // concatenate all paths or no?
  1463. bool runtimeConcatenate = ( runtimeSep!="" );
  1464. if(runtimeFlag == "" || this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
  1465. {
  1466. outputRuntime = false;
  1467. }
  1468. // Some search paths should never be emitted
  1469. emitted.insert("");
  1470. if(const char* implicitLinks =
  1471. (this->Makefile->GetDefinition
  1472. ("CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES")))
  1473. {
  1474. std::vector<std::string> implicitLinkVec;
  1475. cmSystemTools::ExpandListArgument(implicitLinks, implicitLinkVec);
  1476. for(unsigned int k = 0; k < implicitLinkVec.size(); ++k)
  1477. {
  1478. emitted.insert(implicitLinkVec[k]);
  1479. }
  1480. }
  1481. std::string libPathFlag =
  1482. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1483. std::string libPathTerminator =
  1484. this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1485. std::string libLinkFlag =
  1486. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1487. // Flags to link an executable to shared libraries.
  1488. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1489. linkFlagsVar += linkLanguage;
  1490. linkFlagsVar += "_FLAGS";
  1491. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1492. {
  1493. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1494. linkLibs += " ";
  1495. }
  1496. // Compute the link library and directory information.
  1497. std::vector<cmStdString> libNames;
  1498. std::vector<cmStdString> libDirs;
  1499. this->ComputeLinkInformation(tgt, config, libNames, libDirs);
  1500. // Select whether to generate an rpath for the install tree or the
  1501. // build tree.
  1502. bool linking_for_install =
  1503. relink || tgt.GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  1504. bool use_install_rpath =
  1505. outputRuntime && tgt.HaveInstallTreeRPATH() && linking_for_install;
  1506. bool use_build_rpath =
  1507. outputRuntime && tgt.HaveBuildTreeRPATH() && !linking_for_install;
  1508. bool use_link_rpath =
  1509. outputRuntime && linking_for_install &&
  1510. tgt.GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
  1511. // Construct the RPATH.
  1512. std::vector<std::string> runtimeDirs;
  1513. if(use_install_rpath)
  1514. {
  1515. const char* install_rpath = tgt.GetProperty("INSTALL_RPATH");
  1516. cmSystemTools::ExpandListArgument(install_rpath, runtimeDirs);
  1517. for(unsigned int i=0; i < runtimeDirs.size(); ++i)
  1518. {
  1519. runtimeDirs[i] =
  1520. this->Convert(runtimeDirs[i].c_str(), FULL, SHELL, false);
  1521. }
  1522. }
  1523. // Append the library search path flags.
  1524. for(std::vector<cmStdString>::const_iterator libDir = libDirs.begin();
  1525. libDir != libDirs.end(); ++libDir)
  1526. {
  1527. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1528. if(emitted.insert(libpath).second)
  1529. {
  1530. std::string fullLibPath;
  1531. if(!this->WindowsShell && this->UseRelativePaths)
  1532. {
  1533. fullLibPath = "\"`cd ";
  1534. }
  1535. fullLibPath += libpath;
  1536. if(!this->WindowsShell && this->UseRelativePaths)
  1537. {
  1538. fullLibPath += ";pwd`\"";
  1539. }
  1540. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  1541. if((pos == std::string::npos || pos > 0)
  1542. && libDir->find("${") == std::string::npos)
  1543. {
  1544. linkLibs += libPathFlag;
  1545. linkLibs += fullLibPath;
  1546. linkLibs += libPathTerminator;
  1547. linkLibs += " ";
  1548. // Put this directory in the rpath if using build-tree rpath
  1549. // support or if using the link path as an rpath.
  1550. if(use_build_rpath)
  1551. {
  1552. runtimeDirs.push_back(fullLibPath);
  1553. }
  1554. else if(use_link_rpath)
  1555. {
  1556. // Do not add any path inside the source or build tree.
  1557. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1558. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1559. if(!cmSystemTools::ComparePath(libDir->c_str(), topSourceDir) &&
  1560. !cmSystemTools::ComparePath(libDir->c_str(), topBinaryDir) &&
  1561. !cmSystemTools::IsSubDirectory(libDir->c_str(), topSourceDir) &&
  1562. !cmSystemTools::IsSubDirectory(libDir->c_str(), topBinaryDir))
  1563. {
  1564. runtimeDirs.push_back(fullLibPath);
  1565. }
  1566. }
  1567. }
  1568. }
  1569. }
  1570. // Append the link libraries.
  1571. for(std::vector<cmStdString>::iterator lib = libNames.begin();
  1572. lib != libNames.end(); ++lib)
  1573. {
  1574. linkLibs += *lib;
  1575. linkLibs += " ";
  1576. }
  1577. if(!runtimeDirs.empty())
  1578. {
  1579. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1580. // a "-R a -R b -R c" type link line
  1581. rpath += runtimeFlag;
  1582. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1583. rpath += *itr;
  1584. ++itr;
  1585. for( ; itr != runtimeDirs.end(); ++itr )
  1586. {
  1587. if(runtimeConcatenate)
  1588. {
  1589. rpath += runtimeSep;
  1590. rpath += *itr;
  1591. }
  1592. else
  1593. {
  1594. rpath += " ";
  1595. rpath += runtimeFlag;
  1596. rpath += *itr;
  1597. }
  1598. }
  1599. }
  1600. while (rpath.size() < minRpathSize)
  1601. {
  1602. if (rpath.size()==0)
  1603. {
  1604. rpath += runtimeFlag;
  1605. }
  1606. rpath += runtimeSep;
  1607. }
  1608. return true;
  1609. }
  1610. /**
  1611. * Output the linking rules on a command line. For executables,
  1612. * targetLibrary should be a NULL pointer. For libraries, it should point
  1613. * to the name of the library. This will not link a library against itself.
  1614. */
  1615. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  1616. cmTarget& tgt,
  1617. bool relink)
  1618. {
  1619. std::string rpath;
  1620. std::string linkLibs;
  1621. unsigned int minBuildRpathSize = 0;
  1622. if ((relink==false)
  1623. && this->Makefile->IsOn("CMAKE_USE_CHRPATH")
  1624. && (tgt.IsChrpathAvailable()))
  1625. {
  1626. std::string installRpath;
  1627. std::string dummy;
  1628. this->GetLinkerArgs(installRpath, dummy, tgt, true, 0);
  1629. minBuildRpathSize = static_cast<unsigned int>(installRpath.size());
  1630. }
  1631. if (!this->GetLinkerArgs(rpath, linkLibs, tgt, relink, minBuildRpathSize))
  1632. {
  1633. return;
  1634. }
  1635. const char* linkLanguage =
  1636. tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1637. if(!linkLanguage)
  1638. {
  1639. cmSystemTools::
  1640. Error("CMake can not determine linker language for target:",
  1641. tgt.GetName());
  1642. return;
  1643. }
  1644. fout << linkLibs;
  1645. fout << rpath << " ";
  1646. // Add standard libraries for this language.
  1647. std::string standardLibsVar = "CMAKE_";
  1648. standardLibsVar += linkLanguage;
  1649. standardLibsVar += "_STANDARD_LIBRARIES";
  1650. if(const char* stdLibs =
  1651. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1652. {
  1653. fout << stdLibs << " ";
  1654. }
  1655. }
  1656. //----------------------------------------------------------------------------
  1657. void cmLocalGenerator
  1658. ::ComputeLinkInformation(cmTarget& target,
  1659. const char* config,
  1660. std::vector<cmStdString>& outLibs,
  1661. std::vector<cmStdString>& outDirs,
  1662. std::vector<cmStdString>* fullPathLibs)
  1663. {
  1664. // Compute which library configuration to link.
  1665. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  1666. if(config && cmSystemTools::UpperCase(config) == "DEBUG")
  1667. {
  1668. linkType = cmTarget::DEBUG;
  1669. }
  1670. // Get the language used for linking.
  1671. const char* linkLanguage =
  1672. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1673. // Check whether we should use an import library for linking a target.
  1674. bool implib =
  1675. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")?true:false;
  1676. // On platforms without import libraries there may be a special flag
  1677. // to use when creating a plugin (module) that obtains symbols from
  1678. // the program that will load it.
  1679. const char* loader_flag = 0;
  1680. if(!implib && target.GetType() == cmTarget::MODULE_LIBRARY)
  1681. {
  1682. if(!linkLanguage)
  1683. {
  1684. cmSystemTools::
  1685. Error("CMake can not determine linker language for target:",
  1686. target.GetName());
  1687. return;
  1688. }
  1689. std::string loader_flag_var = "CMAKE_SHARED_MODULE_LOADER_";
  1690. loader_flag_var += linkLanguage;
  1691. loader_flag_var += "_FLAG";
  1692. loader_flag = this->Makefile->GetDefinition(loader_flag_var.c_str());
  1693. }
  1694. // Get the list of libraries against which this target wants to link.
  1695. std::vector<std::string> linkLibraries;
  1696. const cmTarget::LinkLibraryVectorType& inLibs = target.GetLinkLibraries();
  1697. for(cmTarget::LinkLibraryVectorType::const_iterator j = inLibs.begin();
  1698. j != inLibs.end(); ++j)
  1699. {
  1700. // For backwards compatibility variables may have been expanded
  1701. // inside library names. Clean up the resulting name.
  1702. std::string lib = j->first;
  1703. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  1704. if(pos != lib.npos)
  1705. {
  1706. lib = lib.substr(pos, lib.npos);
  1707. }
  1708. pos = lib.find_last_not_of(" \t\r\n");
  1709. if(pos != lib.npos)
  1710. {
  1711. lib = lib.substr(0, pos+1);
  1712. }
  1713. if(lib.empty())
  1714. {
  1715. continue;
  1716. }
  1717. // Link to a library if it is not the same target and is meant for
  1718. // this configuration type.
  1719. if((target.GetType() == cmTarget::EXECUTABLE ||
  1720. lib != target.GetName()) &&
  1721. (j->second == cmTarget::GENERAL || j->second == linkType))
  1722. {
  1723. // Compute the proper name to use to link this library.
  1724. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, lib.c_str(), false);
  1725. bool impexe = (tgt &&
  1726. tgt->GetType() == cmTarget::EXECUTABLE &&
  1727. tgt->GetPropertyAsBool("ENABLE_EXPORTS"));
  1728. if(impexe && !implib && !loader_flag)
  1729. {
  1730. // Skip linking to executables on platforms with no import
  1731. // libraries or loader flags.
  1732. continue;
  1733. }
  1734. else if(tgt && (tgt->GetType() == cmTarget::STATIC_LIBRARY ||
  1735. tgt->GetType() == cmTarget::SHARED_LIBRARY ||
  1736. tgt->GetType() == cmTarget::MODULE_LIBRARY ||
  1737. impexe))
  1738. {
  1739. // This is a CMake target. Ask the target for its real name.
  1740. std::string linkItem;
  1741. if(impexe && loader_flag)
  1742. {
  1743. // This link item is an executable that may provide symbols
  1744. // used by this target. A special flag is needed on this
  1745. // platform. Add it now.
  1746. std::string exe = tgt->GetFullPath(config, implib);
  1747. linkItem += loader_flag;
  1748. linkItem += this->Convert(exe.c_str(), NONE, SHELL, false);
  1749. }
  1750. else
  1751. {
  1752. // Pass the full path to the target file but purposely leave
  1753. // off the per-configuration subdirectory. The link directory
  1754. // ordering knows how to deal with this.
  1755. linkItem += tgt->GetDirectory(0, implib);
  1756. // on apple if the FRAMEWORK prop is set, then
  1757. // do not add the target full name but just use the directory
  1758. // name
  1759. #ifdef __APPLE__
  1760. if (!(tgt->GetType() == cmTarget::SHARED_LIBRARY &&
  1761. tgt->GetPropertyAsBool("FRAMEWORK")))
  1762. #endif
  1763. {
  1764. linkItem += "/";
  1765. linkItem += tgt->GetFullName(config, implib);
  1766. }
  1767. }
  1768. linkLibraries.push_back(linkItem);
  1769. // For full path, use the true location.
  1770. if(fullPathLibs)
  1771. {
  1772. fullPathLibs->push_back(tgt->GetFullPath(config, implib));
  1773. }
  1774. }
  1775. else
  1776. {
  1777. // This is not a CMake target. Use the name given.
  1778. linkLibraries.push_back(lib);
  1779. // Add to the list of full paths if this library is one.
  1780. if(fullPathLibs &&
  1781. cmSystemTools::FileIsFullPath(lib.c_str()) &&
  1782. !cmSystemTools::FileIsDirectory(lib.c_str()))
  1783. {
  1784. fullPathLibs->push_back(lib);
  1785. }
  1786. }
  1787. }
  1788. }
  1789. // Get the list of directories the target wants to search for libraries.
  1790. const std::vector<std::string>&
  1791. linkDirectories = target.GetLinkDirectories();
  1792. // Lookup link type selection flags.
  1793. const char* static_link_type_flag = 0;
  1794. const char* shared_link_type_flag = 0;
  1795. const char* target_type_str = 0;
  1796. switch(target.GetType())
  1797. {
  1798. case cmTarget::EXECUTABLE: target_type_str = "EXE"; break;
  1799. case cmTarget::SHARED_LIBRARY: target_type_str = "SHARED_LIBRARY"; break;
  1800. case cmTarget::MODULE_LIBRARY: target_type_str = "SHARED_MODULE"; break;
  1801. default: break;
  1802. }
  1803. if(target_type_str)
  1804. {
  1805. if(!linkLanguage)
  1806. {
  1807. cmSystemTools::
  1808. Error("CMake can not determine linker language for target:",
  1809. target.GetName());
  1810. return;
  1811. }
  1812. std::string static_link_type_flag_var = "CMAKE_";
  1813. static_link_type_flag_var += target_type_str;
  1814. static_link_type_flag_var += "_LINK_STATIC_";
  1815. static_link_type_flag_var += linkLanguage;
  1816. static_link_type_flag_var += "_FLAGS";
  1817. static_link_type_flag =
  1818. this->Makefile->GetDefinition(static_link_type_flag_var.c_str());
  1819. std::string shared_link_type_flag_var = "CMAKE_";
  1820. shared_link_type_flag_var += target_type_str;
  1821. shared_link_type_flag_var += "_LINK_DYNAMIC_";
  1822. shared_link_type_flag_var += linkLanguage;
  1823. shared_link_type_flag_var += "_FLAGS";
  1824. shared_link_type_flag =
  1825. this->Makefile->GetDefinition(shared_link_type_flag_var.c_str());
  1826. }
  1827. // Compute the link directory order needed to link the libraries.
  1828. cmOrderLinkDirectories orderLibs;
  1829. orderLibs.SetLinkTypeInformation(cmOrderLinkDirectories::LinkShared,
  1830. static_link_type_flag,
  1831. shared_link_type_flag);
  1832. orderLibs.AddLinkPrefix(
  1833. this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
  1834. orderLibs.AddLinkPrefix(
  1835. this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_PREFIX"));
  1836. // Import library names should be matched and treated as shared
  1837. // libraries for the purposes of linking.
  1838. orderLibs.AddLinkExtension(
  1839. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"),
  1840. cmOrderLinkDirectories::LinkShared);
  1841. orderLibs.AddLinkExtension(
  1842. this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
  1843. cmOrderLinkDirectories::LinkStatic);
  1844. orderLibs.AddLinkExtension(
  1845. this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
  1846. cmOrderLinkDirectories::LinkShared);
  1847. orderLibs.AddLinkExtension(
  1848. this->Makefile->GetDefinition("CMAKE_LINK_LIBRARY_SUFFIX"));
  1849. if(const char* linkSuffixes =
  1850. this->Makefile->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS"))
  1851. {
  1852. std::vector<std::string> linkSuffixVec;
  1853. cmSystemTools::ExpandListArgument(linkSuffixes, linkSuffixVec);
  1854. for(std::vector<std::string>::iterator i = linkSuffixVec.begin();
  1855. i != linkSuffixVec.end(); ++i)
  1856. {
  1857. orderLibs.AddLinkExtension(i->c_str());
  1858. }
  1859. }
  1860. std::string configSubdir;
  1861. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  1862. gg->AppendDirectoryForConfig("", config, "", configSubdir);
  1863. orderLibs.SetLinkInformation(target.GetName(),
  1864. linkLibraries,
  1865. linkDirectories,
  1866. gg->GetTargetManifest(),
  1867. configSubdir.c_str());
  1868. orderLibs.DetermineLibraryPathOrder();
  1869. std::vector<cmStdString> orderedLibs;
  1870. orderLibs.GetLinkerInformation(outDirs, orderedLibs);
  1871. // Make sure libraries are linked with the proper syntax.
  1872. std::string libLinkFlag =
  1873. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1874. std::string libLinkSuffix =
  1875. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1876. for(std::vector<cmStdString>::iterator l = orderedLibs.begin();
  1877. l != orderedLibs.end(); ++l)
  1878. {
  1879. std::string lib = *l;
  1880. if(lib[0] == '-' || lib[0] == '$' || lib[0] == '`')
  1881. {
  1882. // The library is linked with special syntax by the user.
  1883. outLibs.push_back(lib);
  1884. }
  1885. else
  1886. {
  1887. // Generate the proper link syntax.
  1888. lib = libLinkFlag;
  1889. lib += *l;
  1890. lib += libLinkSuffix;
  1891. outLibs.push_back(lib);
  1892. }
  1893. }
  1894. }
  1895. //----------------------------------------------------------------------------
  1896. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1897. const char* lang,
  1898. const char* config)
  1899. {
  1900. // Add language-specific flags.
  1901. std::string flagsVar = "CMAKE_";
  1902. flagsVar += lang;
  1903. flagsVar += "_FLAGS";
  1904. if(this->EmitUniversalBinaryFlags)
  1905. {
  1906. const char* osxArch =
  1907. this->Makefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  1908. const char* sysroot =
  1909. this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1910. if(osxArch && sysroot && lang && lang[0] =='C')
  1911. {
  1912. std::vector<std::string> archs;
  1913. cmSystemTools::ExpandListArgument(std::string(osxArch),
  1914. archs);
  1915. bool addArchFlag = true;
  1916. if(archs.size() == 1)
  1917. {
  1918. const char* archOrig =
  1919. this->Makefile->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES_DEFAULT");
  1920. if(archs[0] == archOrig)
  1921. {
  1922. addArchFlag = false;
  1923. }
  1924. }
  1925. // if there is more than one arch add the -arch and
  1926. // -isysroot flags, or if there is one arch flag, but
  1927. // it is not the default -arch flag for the system, then
  1928. // add it. Otherwize do not add -arch and -isysroot
  1929. if(addArchFlag)
  1930. {
  1931. for( std::vector<std::string>::iterator i = archs.begin();
  1932. i != archs.end(); ++i)
  1933. {
  1934. flags += " -arch ";
  1935. flags += *i;
  1936. }
  1937. flags += " -isysroot ";
  1938. flags += sysroot;
  1939. }
  1940. }
  1941. }
  1942. this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
  1943. }
  1944. //----------------------------------------------------------------------------
  1945. std::string cmLocalGenerator::GetRealDependency(const char* inName,
  1946. const char* config)
  1947. {
  1948. // Older CMake code may specify the dependency using the target
  1949. // output file rather than the target name. Such code would have
  1950. // been written before there was support for target properties that
  1951. // modify the name so stripping down to just the file name should
  1952. // produce the target name in this case.
  1953. std::string name = cmSystemTools::GetFilenameName(inName);
  1954. if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
  1955. {
  1956. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1957. }
  1958. // Look for a CMake target with the given name.
  1959. if(cmTarget* target = this->GlobalGenerator->FindTarget(0,name.c_str(),true))
  1960. {
  1961. // make sure it is not just a coincidence that the target name
  1962. // found is part of the inName
  1963. if(cmSystemTools::FileIsFullPath(inName))
  1964. {
  1965. std::string tLocation = target->GetLocation(config);
  1966. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1967. std::string depLocation = cmSystemTools::GetFilenamePath(
  1968. std::string(inName));
  1969. depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
  1970. tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
  1971. if(depLocation != tLocation)
  1972. {
  1973. // it is a full path to a depend that has the same name
  1974. // as a target but is in a different location so do not use
  1975. // the target as the depend
  1976. return inName;
  1977. }
  1978. }
  1979. switch (target->GetType())
  1980. {
  1981. case cmTarget::EXECUTABLE:
  1982. case cmTarget::STATIC_LIBRARY:
  1983. case cmTarget::SHARED_LIBRARY:
  1984. case cmTarget::MODULE_LIBRARY:
  1985. {
  1986. // Get the location of the target's output file and depend on it.
  1987. if(const char* location = target->GetLocation(config))
  1988. {
  1989. return location;
  1990. }
  1991. }
  1992. break;
  1993. case cmTarget::UTILITY:
  1994. case cmTarget::GLOBAL_TARGET:
  1995. // Depending on a utility target may not work but just trust
  1996. // the user to have given a valid name.
  1997. return inName;
  1998. case cmTarget::INSTALL_FILES:
  1999. case cmTarget::INSTALL_PROGRAMS:
  2000. case cmTarget::INSTALL_DIRECTORY:
  2001. break;
  2002. }
  2003. }
  2004. // The name was not that of a CMake target. It must name a file.
  2005. if(cmSystemTools::FileIsFullPath(inName))
  2006. {
  2007. // This is a full path. Return it as given.
  2008. return inName;
  2009. }
  2010. // Treat the name as relative to the source directory in which it
  2011. // was given.
  2012. name = this->Makefile->GetCurrentDirectory();
  2013. name += "/";
  2014. name += inName;
  2015. return name;
  2016. }
  2017. //----------------------------------------------------------------------------
  2018. std::string cmLocalGenerator::GetRealLocation(const char* inName,
  2019. const char* config)
  2020. {
  2021. std::string outName=inName;
  2022. // Look for a CMake target with the given name, which is an executable
  2023. // and which can be run
  2024. cmTarget* target = this->GlobalGenerator->FindTarget(0, inName, true);
  2025. if ((target != 0)
  2026. && (target->GetType() == cmTarget::EXECUTABLE)
  2027. && ((this->Makefile->IsOn("CMAKE_CROSSCOMPILING") == false)
  2028. || (target->IsImported() == true)))
  2029. {
  2030. outName = target->GetLocation( config );
  2031. }
  2032. return outName;
  2033. }
  2034. //----------------------------------------------------------------------------
  2035. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  2036. const char* lang,
  2037. bool shared)
  2038. {
  2039. std::string flagsVar;
  2040. // Add flags for dealing with shared libraries for this language.
  2041. if(shared)
  2042. {
  2043. flagsVar = "CMAKE_SHARED_LIBRARY_";
  2044. flagsVar += lang;
  2045. flagsVar += "_FLAGS";
  2046. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  2047. }
  2048. // Add flags specific to shared builds.
  2049. if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2050. {
  2051. flagsVar = "CMAKE_SHARED_BUILD_";
  2052. flagsVar += lang;
  2053. flagsVar += "_FLAGS";
  2054. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  2055. }
  2056. }
  2057. //----------------------------------------------------------------------------
  2058. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  2059. const char* var,
  2060. const char* config)
  2061. {
  2062. // Add the flags from the variable itself.
  2063. std::string flagsVar = var;
  2064. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  2065. // Add the flags from the build-type specific variable.
  2066. if(config && *config)
  2067. {
  2068. flagsVar += "_";
  2069. flagsVar += cmSystemTools::UpperCase(config);
  2070. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  2071. }
  2072. }
  2073. //----------------------------------------------------------------------------
  2074. void cmLocalGenerator::AppendFlags(std::string& flags,
  2075. const char* newFlags)
  2076. {
  2077. if(newFlags && *newFlags)
  2078. {
  2079. std::string newf = newFlags;
  2080. if(flags.size())
  2081. {
  2082. flags += " ";
  2083. }
  2084. flags += newFlags;
  2085. }
  2086. }
  2087. //----------------------------------------------------------------------------
  2088. std::string
  2089. cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
  2090. const char* default_comment)
  2091. {
  2092. // Check for a comment provided with the command.
  2093. if(cc.GetComment())
  2094. {
  2095. return cc.GetComment();
  2096. }
  2097. // Construct a reasonable default comment if possible.
  2098. if(!cc.GetOutputs().empty())
  2099. {
  2100. std::string comment;
  2101. comment = "Generating ";
  2102. const char* sep = "";
  2103. for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
  2104. o != cc.GetOutputs().end(); ++o)
  2105. {
  2106. comment += sep;
  2107. comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
  2108. sep = ", ";
  2109. }
  2110. return comment;
  2111. }
  2112. // Otherwise use the provided default.
  2113. return default_comment;
  2114. }
  2115. //----------------------------------------------------------------------------
  2116. std::string
  2117. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  2118. {
  2119. return this->Convert(remote, START_OUTPUT, SHELL, true);
  2120. }
  2121. //----------------------------------------------------------------------------
  2122. std::string cmLocalGenerator::Convert(const char* source,
  2123. RelativeRoot relative,
  2124. OutputFormat output,
  2125. bool optional)
  2126. {
  2127. // Make sure the relative path conversion components are set.
  2128. if(!this->PathConversionsSetup)
  2129. {
  2130. this->SetupPathConversions();
  2131. this->PathConversionsSetup = true;
  2132. }
  2133. // Convert the path to a relative path.
  2134. std::string result = source;
  2135. if (!optional || this->UseRelativePaths)
  2136. {
  2137. switch (relative)
  2138. {
  2139. case HOME:
  2140. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2141. result = this->ConvertToRelativePath(this->HomeDirectoryComponents,
  2142. result.c_str());
  2143. break;
  2144. case START:
  2145. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2146. result = this->ConvertToRelativePath(this->StartDirectoryComponents,
  2147. result.c_str());
  2148. break;
  2149. case HOME_OUTPUT:
  2150. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2151. result =
  2152. this->ConvertToRelativePath(this->HomeOutputDirectoryComponents,
  2153. result.c_str());
  2154. break;
  2155. case START_OUTPUT:
  2156. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2157. result =
  2158. this->ConvertToRelativePath(this->StartOutputDirectoryComponents,
  2159. result.c_str());
  2160. break;
  2161. case FULL:
  2162. result = cmSystemTools::CollapseFullPath(result.c_str());
  2163. break;
  2164. case NONE:
  2165. break;
  2166. }
  2167. }
  2168. // Now convert it to an output path.
  2169. if (output == MAKEFILE)
  2170. {
  2171. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  2172. }
  2173. else if( output == SHELL)
  2174. {
  2175. // For the MSYS shell convert drive letters to posix paths, so
  2176. // that c:/some/path becomes /c/some/path. This is needed to
  2177. // avoid problems with the shell path translation.
  2178. if(this->MSYSShell)
  2179. {
  2180. if(result.size() > 2 && result[1] == ':')
  2181. {
  2182. result[1] = result[0];
  2183. result[0] = '/';
  2184. }
  2185. }
  2186. if(this->WindowsShell)
  2187. {
  2188. std::string::size_type pos = 0;
  2189. while((pos = result.find('/', pos)) != std::string::npos)
  2190. {
  2191. result[pos] = '\\';
  2192. pos++;
  2193. }
  2194. }
  2195. result = this->EscapeForShell(result.c_str(), true, false);
  2196. }
  2197. return result;
  2198. }
  2199. //----------------------------------------------------------------------------
  2200. std::string cmLocalGenerator::FindRelativePathTopSource()
  2201. {
  2202. // Relative path conversion within a single tree managed by CMake is
  2203. // safe. We can use our parent relative path top if and only if
  2204. // this is a subdirectory of that top.
  2205. if(cmLocalGenerator* parent = this->GetParent())
  2206. {
  2207. std::string parentTop = parent->FindRelativePathTopSource();
  2208. if(cmSystemTools::IsSubDirectory(
  2209. this->Makefile->GetStartDirectory(), parentTop.c_str()))
  2210. {
  2211. return parentTop;
  2212. }
  2213. }
  2214. // Otherwise this directory itself is the new top.
  2215. return this->Makefile->GetStartDirectory();
  2216. }
  2217. //----------------------------------------------------------------------------
  2218. std::string cmLocalGenerator::FindRelativePathTopBinary()
  2219. {
  2220. // Relative path conversion within a single tree managed by CMake is
  2221. // safe. We can use our parent relative path top if and only if
  2222. // this is a subdirectory of that top.
  2223. if(cmLocalGenerator* parent = this->GetParent())
  2224. {
  2225. std::string parentTop = parent->FindRelativePathTopBinary();
  2226. if(cmSystemTools::IsSubDirectory(
  2227. this->Makefile->GetStartOutputDirectory(), parentTop.c_str()))
  2228. {
  2229. return parentTop;
  2230. }
  2231. }
  2232. // Otherwise this directory itself is the new top.
  2233. return this->Makefile->GetStartOutputDirectory();
  2234. }
  2235. //----------------------------------------------------------------------------
  2236. void cmLocalGenerator::ConfigureRelativePaths()
  2237. {
  2238. // Relative path conversion inside the source tree is not used to
  2239. // construct relative paths passed to build tools so it is safe to
  2240. // even when the source is a network path.
  2241. std::string source = this->FindRelativePathTopSource();
  2242. this->RelativePathTopSource = source;
  2243. // The current working directory on Windows cannot be a network
  2244. // path. Therefore relative paths cannot work when the binary tree
  2245. // is a network path.
  2246. std::string binary = this->FindRelativePathTopBinary();
  2247. if(binary.size() < 2 || binary.substr(0, 2) != "//")
  2248. {
  2249. this->RelativePathTopBinary = binary;
  2250. }
  2251. else
  2252. {
  2253. this->RelativePathTopBinary = "";
  2254. }
  2255. }
  2256. //----------------------------------------------------------------------------
  2257. static bool cmLocalGeneratorNotAbove(const char* a, const char* b)
  2258. {
  2259. return (cmSystemTools::ComparePath(a, b) ||
  2260. cmSystemTools::IsSubDirectory(a, b));
  2261. }
  2262. //----------------------------------------------------------------------------
  2263. std::string
  2264. cmLocalGenerator::ConvertToRelativePath(const std::vector<std::string>& local,
  2265. const char* in_remote)
  2266. {
  2267. // The path should never be quoted.
  2268. assert(in_remote[0] != '\"');
  2269. // The local path should never have a trailing slash.
  2270. assert(local.size() > 0 && !(local[local.size()-1] == ""));
  2271. // If the path is already relative then just return the path.
  2272. if(!cmSystemTools::FileIsFullPath(in_remote))
  2273. {
  2274. return in_remote;
  2275. }
  2276. // Make sure relative path conversion is configured.
  2277. if(!this->RelativePathsConfigured)
  2278. {
  2279. this->ConfigureRelativePaths();
  2280. this->RelativePathsConfigured = true;
  2281. }
  2282. // Skip conversion if the path and local are not both in the source
  2283. // or both in the binary tree.
  2284. std::string local_path = cmSystemTools::JoinPath(local);
  2285. if(!((cmLocalGeneratorNotAbove(local_path.c_str(),
  2286. this->RelativePathTopBinary.c_str()) &&
  2287. cmLocalGeneratorNotAbove(in_remote,
  2288. this->RelativePathTopBinary.c_str())) ||
  2289. (cmLocalGeneratorNotAbove(local_path.c_str(),
  2290. this->RelativePathTopSource.c_str()) &&
  2291. cmLocalGeneratorNotAbove(in_remote,
  2292. this->RelativePathTopSource.c_str()))))
  2293. {
  2294. return in_remote;
  2295. }
  2296. // Identify the longest shared path component between the remote
  2297. // path and the local path.
  2298. std::vector<std::string> remote;
  2299. cmSystemTools::SplitPath(in_remote, remote);
  2300. unsigned int common=0;
  2301. while(common < remote.size() &&
  2302. common < local.size() &&
  2303. cmSystemTools::ComparePath(remote[common].c_str(),
  2304. local[common].c_str()))
  2305. {
  2306. ++common;
  2307. }
  2308. // If no part of the path is in common then return the full path.
  2309. if(common == 0)
  2310. {
  2311. return in_remote;
  2312. }
  2313. // If the entire path is in common then just return a ".".
  2314. if(common == remote.size() &&
  2315. common == local.size())
  2316. {
  2317. return ".";
  2318. }
  2319. // If the entire path is in common except for a trailing slash then
  2320. // just return a "./".
  2321. if(common+1 == remote.size() &&
  2322. remote[common].size() == 0 &&
  2323. common == local.size())
  2324. {
  2325. return "./";
  2326. }
  2327. // Construct the relative path.
  2328. std::string relative;
  2329. // First add enough ../ to get up to the level of the shared portion
  2330. // of the path. Leave off the trailing slash. Note that the last
  2331. // component of local will never be empty because local should never
  2332. // have a trailing slash.
  2333. for(unsigned int i=common; i < local.size(); ++i)
  2334. {
  2335. relative += "..";
  2336. if(i < local.size()-1)
  2337. {
  2338. relative += "/";
  2339. }
  2340. }
  2341. // Now add the portion of the destination path that is not included
  2342. // in the shared portion of the path. Add a slash the first time
  2343. // only if there was already something in the path. If there was a
  2344. // trailing slash in the input then the last iteration of the loop
  2345. // will add a slash followed by an empty string which will preserve
  2346. // the trailing slash in the output.
  2347. for(unsigned int i=common; i < remote.size(); ++i)
  2348. {
  2349. if(relative.size() > 0)
  2350. {
  2351. relative += "/";
  2352. }
  2353. relative += remote[i];
  2354. }
  2355. // Finally return the path.
  2356. return relative;
  2357. }
  2358. //----------------------------------------------------------------------------
  2359. void
  2360. cmLocalGenerator
  2361. ::GenerateTargetInstallRules(
  2362. std::ostream& os, const char* config,
  2363. std::vector<std::string> const& configurationTypes)
  2364. {
  2365. // Convert the old-style install specification from each target to
  2366. // an install generator and run it.
  2367. cmTargets& tgts = this->Makefile->GetTargets();
  2368. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  2369. {
  2370. // Include the user-specified pre-install script for this target.
  2371. if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
  2372. {
  2373. cmInstallScriptGenerator g(preinstall, false, 0);
  2374. g.Generate(os, config, configurationTypes);
  2375. }
  2376. // Install this target if a destination is given.
  2377. if(l->second.GetInstallPath() != "")
  2378. {
  2379. // Compute the full install destination. Note that converting
  2380. // to unix slashes also removes any trailing slash.
  2381. std::string destination = "${CMAKE_INSTALL_PREFIX}";
  2382. destination += l->second.GetInstallPath();
  2383. cmSystemTools::ConvertToUnixSlashes(destination);
  2384. // Generate the proper install generator for this target type.
  2385. switch(l->second.GetType())
  2386. {
  2387. case cmTarget::EXECUTABLE:
  2388. case cmTarget::STATIC_LIBRARY:
  2389. case cmTarget::MODULE_LIBRARY:
  2390. {
  2391. // Use a target install generator.
  2392. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2393. g.Generate(os, config, configurationTypes);
  2394. }
  2395. break;
  2396. case cmTarget::SHARED_LIBRARY:
  2397. {
  2398. #if defined(_WIN32) || defined(__CYGWIN__)
  2399. // Special code to handle DLL. Install the import library
  2400. // to the normal destination and the DLL to the runtime
  2401. // destination.
  2402. cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
  2403. g1.Generate(os, config, configurationTypes);
  2404. destination = "${CMAKE_INSTALL_PREFIX}";
  2405. destination += l->second.GetRuntimeInstallPath();
  2406. cmSystemTools::ConvertToUnixSlashes(destination);
  2407. cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
  2408. g2.Generate(os, config, configurationTypes);
  2409. #else
  2410. // Use a target install generator.
  2411. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2412. g.Generate(os, config, configurationTypes);
  2413. #endif
  2414. }
  2415. break;
  2416. default:
  2417. break;
  2418. }
  2419. }
  2420. // Include the user-specified post-install script for this target.
  2421. if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
  2422. {
  2423. cmInstallScriptGenerator g(postinstall, false, 0);
  2424. g.Generate(os, config, configurationTypes);
  2425. }
  2426. }
  2427. }
  2428. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2429. static std::string cmLocalGeneratorMD5(const char* input)
  2430. {
  2431. char md5out[32];
  2432. cmsysMD5* md5 = cmsysMD5_New();
  2433. cmsysMD5_Initialize(md5);
  2434. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
  2435. cmsysMD5_FinalizeHex(md5, md5out);
  2436. cmsysMD5_Delete(md5);
  2437. return std::string(md5out, 32);
  2438. }
  2439. static bool
  2440. cmLocalGeneratorShortenObjectName(std::string& objName,
  2441. std::string::size_type max_len)
  2442. {
  2443. // Replace the beginning of the path portion of the object name with
  2444. // its own md5 sum.
  2445. std::string::size_type pos = objName.find('/', objName.size()-max_len+32);
  2446. if(pos != objName.npos)
  2447. {
  2448. std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
  2449. md5name += objName.substr(pos);
  2450. objName = md5name;
  2451. // The object name is now short enough.
  2452. return true;
  2453. }
  2454. else
  2455. {
  2456. // The object name could not be shortened enough.
  2457. return false;
  2458. }
  2459. }
  2460. static bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2461. std::string::size_type dir_len)
  2462. {
  2463. // Choose a maximum file name length.
  2464. #if defined(_WIN32) || defined(__CYGWIN__)
  2465. std::string::size_type const max_total_len = 250;
  2466. #else
  2467. std::string::size_type const max_total_len = 1000;
  2468. #endif
  2469. // Enforce the maximum file name length if possible.
  2470. std::string::size_type max_obj_len = max_total_len;
  2471. if(dir_len < max_total_len)
  2472. {
  2473. max_obj_len = max_total_len - dir_len;
  2474. if(objName.size() > max_obj_len)
  2475. {
  2476. // The current object file name is too long. Try to shorten it.
  2477. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2478. }
  2479. else
  2480. {
  2481. // The object file name is short enough.
  2482. return true;
  2483. }
  2484. }
  2485. else
  2486. {
  2487. // The build directory in which the object will be stored is
  2488. // already too deep.
  2489. return false;
  2490. }
  2491. }
  2492. #endif
  2493. //----------------------------------------------------------------------------
  2494. std::string&
  2495. cmLocalGenerator
  2496. ::CreateSafeUniqueObjectFileName(const char* sin,
  2497. std::string::size_type dir_len)
  2498. {
  2499. // Look for an existing mapped name for this object file.
  2500. std::map<cmStdString,cmStdString>::iterator it =
  2501. this->UniqueObjectNamesMap.find(sin);
  2502. // If no entry exists create one.
  2503. if(it == this->UniqueObjectNamesMap.end())
  2504. {
  2505. // Start with the original name.
  2506. std::string ssin = sin;
  2507. // Avoid full paths by removing leading slashes.
  2508. std::string::size_type pos = 0;
  2509. for(;pos < ssin.size() && ssin[pos] == '/'; ++pos);
  2510. ssin = ssin.substr(pos);
  2511. // Avoid full paths by removing colons.
  2512. cmSystemTools::ReplaceString(ssin, ":", "_");
  2513. // Avoid relative paths that go up the tree.
  2514. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2515. // Avoid spaces.
  2516. cmSystemTools::ReplaceString(ssin, " ", "_");
  2517. // Mangle the name if necessary.
  2518. if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
  2519. {
  2520. bool done;
  2521. int cc = 0;
  2522. char rpstr[100];
  2523. sprintf(rpstr, "_p_");
  2524. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2525. std::string sssin = sin;
  2526. do
  2527. {
  2528. done = true;
  2529. for ( it = this->UniqueObjectNamesMap.begin();
  2530. it != this->UniqueObjectNamesMap.end();
  2531. ++ it )
  2532. {
  2533. if ( it->second == ssin )
  2534. {
  2535. done = false;
  2536. }
  2537. }
  2538. if ( done )
  2539. {
  2540. break;
  2541. }
  2542. sssin = ssin;
  2543. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2544. sprintf(rpstr, "_p%d_", cc++);
  2545. }
  2546. while ( !done );
  2547. }
  2548. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2549. cmLocalGeneratorCheckObjectName(ssin, dir_len);
  2550. #else
  2551. (void)dir_len;
  2552. #endif
  2553. // Insert the newly mapped object file name.
  2554. std::map<cmStdString, cmStdString>::value_type e(sin, ssin);
  2555. it = this->UniqueObjectNamesMap.insert(e).first;
  2556. }
  2557. // Return the map entry.
  2558. return it->second;
  2559. }
  2560. //----------------------------------------------------------------------------
  2561. std::string
  2562. cmLocalGenerator
  2563. ::GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  2564. std::string::size_type dir_len,
  2565. bool* hasSourceExtension)
  2566. {
  2567. // Construct the object file name using the full path to the source
  2568. // file which is its only unique identification.
  2569. const char* fullPath = source.GetFullPath().c_str();
  2570. // Try referencing the source relative to the source tree.
  2571. std::string relFromSource = this->Convert(fullPath, START);
  2572. assert(!relFromSource.empty());
  2573. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
  2574. bool subSource = relSource && relFromSource[0] != '.';
  2575. // Try referencing the source relative to the binary tree.
  2576. std::string relFromBinary = this->Convert(fullPath, START_OUTPUT);
  2577. assert(!relFromBinary.empty());
  2578. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
  2579. bool subBinary = relBinary && relFromBinary[0] != '.';
  2580. // Select a nice-looking reference to the source file to construct
  2581. // the object file name.
  2582. std::string objectName;
  2583. if((relSource && !relBinary) || (subSource && !subBinary))
  2584. {
  2585. objectName = relFromSource;
  2586. }
  2587. else if((relBinary && !relSource) || (subBinary && !subSource))
  2588. {
  2589. objectName = relFromBinary;
  2590. }
  2591. else if(relFromBinary.length() < relFromSource.length())
  2592. {
  2593. objectName = relFromBinary;
  2594. }
  2595. else
  2596. {
  2597. objectName = relFromSource;
  2598. }
  2599. // if it is still a full path check for the try compile case
  2600. // try compile never have in source sources, and should not
  2601. // have conflicting source file names in the same target
  2602. if(cmSystemTools::FileIsFullPath(objectName.c_str()))
  2603. {
  2604. if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
  2605. {
  2606. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2607. }
  2608. }
  2609. // Replace the original source file extension with the object file
  2610. // extension.
  2611. bool keptSourceExtension = true;
  2612. if(!source.GetPropertyAsBool("KEEP_EXTENSION"))
  2613. {
  2614. // Decide whether this language wants to replace the source
  2615. // extension with the object extension. For CMake 2.4
  2616. // compatibility do this by default.
  2617. bool replaceExt = this->NeedBackwardsCompatibility(2, 4);
  2618. if(!replaceExt)
  2619. {
  2620. std::string repVar = "CMAKE_";
  2621. repVar += source.GetLanguage();
  2622. repVar += "_OUTPUT_EXTENSION_REPLACE";
  2623. replaceExt = this->Makefile->IsOn(repVar.c_str());
  2624. }
  2625. // Remove the source extension if it is to be replaced.
  2626. if(replaceExt)
  2627. {
  2628. keptSourceExtension = false;
  2629. std::string::size_type dot_pos = objectName.rfind(".");
  2630. if(dot_pos != std::string::npos)
  2631. {
  2632. objectName = objectName.substr(0, dot_pos);
  2633. }
  2634. }
  2635. // Store the new extension.
  2636. objectName +=
  2637. this->GlobalGenerator->GetLanguageOutputExtension(source);
  2638. }
  2639. if(hasSourceExtension)
  2640. {
  2641. *hasSourceExtension = keptSourceExtension;
  2642. }
  2643. // Convert to a safe name.
  2644. return this->CreateSafeUniqueObjectFileName(objectName.c_str(), dir_len);
  2645. }
  2646. //----------------------------------------------------------------------------
  2647. const char*
  2648. cmLocalGenerator
  2649. ::GetSourceFileLanguage(const cmSourceFile& source)
  2650. {
  2651. return source.GetLanguage();
  2652. }
  2653. //----------------------------------------------------------------------------
  2654. std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
  2655. {
  2656. std::string result;
  2657. bool forceOn = cmSystemTools::GetForceUnixPaths();
  2658. if(forceOn && this->WindowsShell)
  2659. {
  2660. cmSystemTools::SetForceUnixPaths(false);
  2661. }
  2662. result = cmSystemTools::EscapeSpaces(str);
  2663. if(forceOn && this->WindowsShell)
  2664. {
  2665. cmSystemTools::SetForceUnixPaths(true);
  2666. }
  2667. return result;
  2668. }
  2669. //----------------------------------------------------------------------------
  2670. std::string cmLocalGenerator::EscapeForShell(const char* str, bool makeVars,
  2671. bool forEcho)
  2672. {
  2673. // Compute the flags for the target shell environment.
  2674. int flags = 0;
  2675. if(this->WindowsVSIDE)
  2676. {
  2677. flags |= cmsysSystem_Shell_Flag_VSIDE;
  2678. }
  2679. else
  2680. {
  2681. flags |= cmsysSystem_Shell_Flag_Make;
  2682. }
  2683. if(makeVars)
  2684. {
  2685. flags |= cmsysSystem_Shell_Flag_AllowMakeVariables;
  2686. }
  2687. if(forEcho)
  2688. {
  2689. flags |= cmsysSystem_Shell_Flag_EchoWindows;
  2690. }
  2691. if(this->WatcomWMake)
  2692. {
  2693. flags |= cmsysSystem_Shell_Flag_WatcomWMake;
  2694. }
  2695. if(this->MinGWMake)
  2696. {
  2697. flags |= cmsysSystem_Shell_Flag_MinGWMake;
  2698. }
  2699. // Compute the buffer size needed.
  2700. int size = (this->WindowsShell ?
  2701. cmsysSystem_Shell_GetArgumentSizeForWindows(str, flags) :
  2702. cmsysSystem_Shell_GetArgumentSizeForUnix(str, flags));
  2703. // Compute the shell argument itself.
  2704. std::vector<char> arg(size);
  2705. if(this->WindowsShell)
  2706. {
  2707. cmsysSystem_Shell_GetArgumentForWindows(str, &arg[0], flags);
  2708. }
  2709. else
  2710. {
  2711. cmsysSystem_Shell_GetArgumentForUnix(str, &arg[0], flags);
  2712. }
  2713. return std::string(&arg[0]);
  2714. }
  2715. //----------------------------------------------------------------------------
  2716. std::string
  2717. cmLocalGenerator::GetTargetDirectory(cmTarget const&) const
  2718. {
  2719. cmSystemTools::Error("GetTargetDirectory"
  2720. " called on cmLocalGenerator");
  2721. return "";
  2722. }
  2723. //----------------------------------------------------------------------------
  2724. void
  2725. cmLocalGenerator::GetTargetObjectFileDirectories(cmTarget* ,
  2726. std::vector<std::string>&
  2727. )
  2728. {
  2729. cmSystemTools::Error("GetTargetObjectFileDirectories"
  2730. " called on cmLocalGenerator");
  2731. }
  2732. //----------------------------------------------------------------------------
  2733. unsigned int cmLocalGenerator::GetBackwardsCompatibility()
  2734. {
  2735. // The computed version may change until the project is fully
  2736. // configured.
  2737. if(!this->BackwardsCompatibilityFinal)
  2738. {
  2739. unsigned int major = 0;
  2740. unsigned int minor = 0;
  2741. unsigned int patch = 0;
  2742. if(const char* value
  2743. = this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
  2744. {
  2745. switch(sscanf(value, "%u.%u.%u", &major, &minor, &patch))
  2746. {
  2747. case 2: patch = 0; break;
  2748. case 1: minor = 0; patch = 0; break;
  2749. default: break;
  2750. }
  2751. }
  2752. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  2753. this->BackwardsCompatibilityFinal = this->Configured;
  2754. }
  2755. return this->BackwardsCompatibility;
  2756. }
  2757. //----------------------------------------------------------------------------
  2758. bool cmLocalGenerator::NeedBackwardsCompatibility(unsigned int major,
  2759. unsigned int minor,
  2760. unsigned int patch)
  2761. {
  2762. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  2763. // equal to or lower than the given version.
  2764. unsigned int actual_compat = this->GetBackwardsCompatibility();
  2765. return (actual_compat &&
  2766. actual_compat <= CMake_VERSION_ENCODE(major, minor, patch));
  2767. }