cmLocalGenerator.cxx 80 KB

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