cmLocalGenerator.cxx 87 KB

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