cmLocalGenerator.cxx 87 KB

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