cmLocalGenerator.cxx 88 KB

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