cmLocalGenerator.cxx 62 KB

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