cmLocalGenerator.cxx 88 KB

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