cmLocalGenerator.cxx 72 KB

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