cmLocalGenerator.cxx 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  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.Target)
  684. {
  685. if(variable == "TARGET_QUOTED")
  686. {
  687. std::string targetQuoted = replaceValues.Target;
  688. if(targetQuoted.size() && targetQuoted[0] != '\"')
  689. {
  690. targetQuoted = '\"';
  691. targetQuoted += replaceValues.Target;
  692. targetQuoted += '\"';
  693. return targetQuoted;
  694. }
  695. }
  696. if(variable == "LANGUAGE_COMPILE_FLAGS")
  697. {
  698. return replaceValues.LanguageCompileFlags;
  699. }
  700. if(variable == "TARGET")
  701. {
  702. return replaceValues.Target;
  703. }
  704. if(variable == "TARGET_IMPLIB")
  705. {
  706. return this->TargetImplib;
  707. }
  708. if(variable == "TARGET_BASE")
  709. {
  710. // Strip the last extension off the target name.
  711. std::string targetBase = replaceValues.Target;
  712. std::string::size_type pos = targetBase.rfind(".");
  713. if(pos != targetBase.npos)
  714. {
  715. return targetBase.substr(0, pos);
  716. }
  717. else
  718. {
  719. return targetBase;
  720. }
  721. }
  722. }
  723. if(replaceValues.TargetSOName)
  724. {
  725. if(variable == "TARGET_SONAME")
  726. {
  727. if(replaceValues.Language)
  728. {
  729. std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
  730. name += replaceValues.Language;
  731. name += "_FLAG";
  732. if(this->Makefile->GetDefinition(name.c_str()))
  733. {
  734. return replaceValues.TargetSOName;
  735. }
  736. }
  737. return "";
  738. }
  739. }
  740. if(replaceValues.TargetInstallNameDir)
  741. {
  742. if(variable == "TARGET_INSTALLNAME_DIR")
  743. {
  744. return replaceValues.TargetInstallNameDir;
  745. }
  746. }
  747. if(replaceValues.LinkLibraries)
  748. {
  749. if(variable == "LINK_LIBRARIES")
  750. {
  751. return replaceValues.LinkLibraries;
  752. }
  753. }
  754. std::vector<std::string> enabledLanguages;
  755. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  756. // loop over language specific replace variables
  757. int pos = 0;
  758. while(ruleReplaceVars[pos])
  759. {
  760. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  761. i != enabledLanguages.end(); ++i)
  762. {
  763. const char* lang = i->c_str();
  764. std::string actualReplace = ruleReplaceVars[pos];
  765. // If this is the compiler then look for the extra variable
  766. // _COMPILER_ARG1 which must be the first argument to the compiler
  767. const char* compilerArg1 = 0;
  768. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  769. {
  770. std::string arg1 = actualReplace + "_ARG1";
  771. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  772. compilerArg1 = this->Makefile->GetDefinition(arg1.c_str());
  773. }
  774. if(actualReplace.find("${LANG}") != actualReplace.npos)
  775. {
  776. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  777. }
  778. if(actualReplace == variable)
  779. {
  780. std::string replace = this->Makefile->GetSafeDefinition(variable.c_str());
  781. // if the variable is not a FLAG then treat it like a path
  782. if(variable.find("_FLAG") == variable.npos)
  783. {
  784. std::string ret = this->ConvertToOutputForExisting(replace.c_str());
  785. // if there is a required first argument to the compiler add it to the compiler string
  786. if(compilerArg1)
  787. {
  788. ret += " ";
  789. ret += compilerArg1;
  790. }
  791. return ret;
  792. }
  793. return replace;
  794. }
  795. }
  796. pos++;
  797. }
  798. return variable;
  799. }
  800. void
  801. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  802. const RuleVariables& replaceValues)
  803. {
  804. std::vector<std::string> enabledLanguages;
  805. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  806. std::string::size_type start = s.find('<');
  807. // no variables to expand
  808. if(start == s.npos)
  809. {
  810. return;
  811. }
  812. std::string::size_type pos = 0;
  813. std::string expandedInput;
  814. while(start != s.npos && start < s.size()-2)
  815. {
  816. std::string::size_type end = s.find('>', start);
  817. // if we find a < with no > we are done
  818. if(end == s.npos)
  819. {
  820. return;
  821. }
  822. char c = s[start+1];
  823. // if the next char after the < is not A-Za-z then
  824. // skip it and try to find the next < in the string
  825. if(!isalpha(c))
  826. {
  827. start = s.find('<', start+1);
  828. }
  829. else
  830. {
  831. // extract the var
  832. std::string var = s.substr(start+1, end - start-1);
  833. std::string replace = this->ExpandRuleVariable(var,
  834. replaceValues);
  835. expandedInput += s.substr(pos, start-pos);
  836. expandedInput += replace;
  837. // move to next one
  838. start = s.find('<', start+var.size()+2);
  839. pos = end+1;
  840. }
  841. }
  842. // add the rest of the input
  843. expandedInput += s.substr(pos, s.size()-pos);
  844. s = expandedInput;
  845. }
  846. std::string
  847. cmLocalGenerator::ConvertToOutputForExisting(const char* p)
  848. {
  849. std::string ret = this->Convert(p, START_OUTPUT, SHELL, true);
  850. // if there are spaces in the path, then get the short path version
  851. // if there is one
  852. if(ret.find(' ') != std::string::npos)
  853. {
  854. if(cmSystemTools::FileExists(p))
  855. {
  856. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  857. {
  858. ret = this->Convert(p,START_OUTPUT,MAKEFILE,true);
  859. }
  860. }
  861. }
  862. return ret;
  863. }
  864. const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
  865. {
  866. if(!lang)
  867. {
  868. return "";
  869. }
  870. if(this->LanguageToIncludeFlags.count(lang))
  871. {
  872. return this->LanguageToIncludeFlags[lang].c_str();
  873. }
  874. cmOStringStream includeFlags;
  875. std::vector<std::string> includes;
  876. this->GetIncludeDirectories(includes);
  877. std::vector<std::string>::iterator i;
  878. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  879. flagVar += lang;
  880. const char* includeFlag = this->Makefile->GetDefinition(flagVar.c_str());
  881. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  882. flagVar += lang;
  883. const char* sep = this->Makefile->GetDefinition(flagVar.c_str());
  884. bool quotePaths = false;
  885. if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
  886. {
  887. quotePaths = true;
  888. }
  889. bool repeatFlag = true; // should the include flag be repeated like ie. -IA -IB
  890. if(!sep)
  891. {
  892. sep = " ";
  893. }
  894. else
  895. {
  896. // if there is a separator then the flag is not repeated but is only given once
  897. // i.e. -classpath a:b:c
  898. repeatFlag = false;
  899. }
  900. bool flagUsed = false;
  901. std::set<cmStdString> emitted;
  902. for(i = includes.begin(); i != includes.end(); ++i)
  903. {
  904. #ifdef __APPLE__
  905. if(cmSystemTools::IsPathToFramework(i->c_str()))
  906. {
  907. std::string frameworkDir = *i;
  908. frameworkDir += "/../";
  909. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  910. if(emitted.insert(frameworkDir).second)
  911. {
  912. includeFlags << "-F" << this->ConvertToOutputForExisting(frameworkDir.c_str()) << " ";
  913. }
  914. continue;
  915. }
  916. #endif
  917. std::string include = *i;
  918. if(!flagUsed || repeatFlag)
  919. {
  920. includeFlags << includeFlag;
  921. flagUsed = true;
  922. }
  923. std::string includePath = this->ConvertToOutputForExisting(i->c_str());
  924. if(quotePaths && includePath.size() && includePath[0] != '\"')
  925. {
  926. includeFlags << "\"";
  927. }
  928. includeFlags << includePath;
  929. if(quotePaths && includePath.size() && includePath[0] != '\"')
  930. {
  931. includeFlags << "\"";
  932. }
  933. includeFlags << sep;
  934. }
  935. std::string flags = includeFlags.str();
  936. // remove trailing separators
  937. if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
  938. {
  939. flags[flags.size()-1] = ' ';
  940. }
  941. flags += this->Makefile->GetDefineFlags();
  942. this->LanguageToIncludeFlags[lang] = flags;
  943. // Use this temorary variable for the return value to work-around a
  944. // bogus GCC 2.95 warning.
  945. const char* ret = this->LanguageToIncludeFlags[lang].c_str();
  946. return ret;
  947. }
  948. //----------------------------------------------------------------------------
  949. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs)
  950. {
  951. // Need to decide whether to automatically include the source and
  952. // binary directories at the beginning of the include path.
  953. bool includeSourceDir = false;
  954. bool includeBinaryDir = false;
  955. // When automatic include directories are requested for a build then
  956. // include the source and binary directories at the beginning of the
  957. // include path to approximate include file behavior for an
  958. // in-source build. This does not account for the case of a source
  959. // file in a subdirectory of the current source directory but we
  960. // cannot fix this because not all native build tools support
  961. // per-source-file include paths.
  962. if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
  963. {
  964. includeSourceDir = true;
  965. includeBinaryDir = true;
  966. }
  967. // CMake versions below 2.0 would add the source tree to the -I path
  968. // automatically. Preserve compatibility.
  969. const char* versionValue =
  970. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  971. int major = 0;
  972. int minor = 0;
  973. if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
  974. {
  975. versionValue = 0;
  976. }
  977. if(versionValue && major < 2)
  978. {
  979. includeSourceDir = true;
  980. }
  981. // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
  982. // not set the backwards compatibility level automatically.
  983. const char* vtkSourceDir =
  984. this->Makefile->GetDefinition("VTK_SOURCE_DIR");
  985. if(vtkSourceDir)
  986. {
  987. const char* vtk_major = this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
  988. const char* vtk_minor = this->Makefile->GetDefinition("VTK_MINOR_VERSION");
  989. vtk_major = vtk_major? vtk_major : "4";
  990. vtk_minor = vtk_minor? vtk_minor : "4";
  991. int vmajor = 0;
  992. int vminor = 0;
  993. if(sscanf(vtk_major, "%d", &vmajor) && sscanf(vtk_minor, "%d", &vminor) &&
  994. vmajor == 4 && vminor <= 4)
  995. {
  996. includeSourceDir = true;
  997. }
  998. }
  999. // Do not repeat an include path.
  1000. std::set<cmStdString> emitted;
  1001. // Store the automatic include paths.
  1002. if(includeBinaryDir)
  1003. {
  1004. dirs.push_back(this->Makefile->GetStartOutputDirectory());
  1005. emitted.insert(this->Makefile->GetStartOutputDirectory());
  1006. }
  1007. if(includeSourceDir)
  1008. {
  1009. if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
  1010. {
  1011. dirs.push_back(this->Makefile->GetStartDirectory());
  1012. emitted.insert(this->Makefile->GetStartDirectory());
  1013. }
  1014. }
  1015. // Do not explicitly add the standard include path "/usr/include".
  1016. // This can cause problems with certain standard library
  1017. // implementations because the wrong headers may be found first.
  1018. emitted.insert("/usr/include");
  1019. if(const char* implicitIncludes =
  1020. this->Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  1021. {
  1022. std::vector<std::string> implicitIncludeVec;
  1023. cmSystemTools::ExpandListArgument(implicitIncludes, implicitIncludeVec);
  1024. for(unsigned int k = 0; k < implicitIncludeVec.size(); ++k)
  1025. {
  1026. emitted.insert(implicitIncludeVec[k]);
  1027. }
  1028. }
  1029. // Construct the ordered list.
  1030. std::vector<std::string>& includes = this->Makefile->GetIncludeDirectories();
  1031. for(std::vector<std::string>::iterator i = includes.begin();
  1032. i != includes.end(); ++i)
  1033. {
  1034. if(emitted.find(*i) == emitted.end())
  1035. {
  1036. dirs.push_back(*i);
  1037. emitted.insert(*i);
  1038. }
  1039. }
  1040. }
  1041. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1042. std::string& flags,
  1043. std::string& linkFlags,
  1044. cmTarget& target)
  1045. {
  1046. std::string buildType = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1047. buildType = cmSystemTools::UpperCase(buildType);
  1048. const char* libraryLinkVariable = "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1049. switch(target.GetType())
  1050. {
  1051. case cmTarget::STATIC_LIBRARY:
  1052. {
  1053. const char* targetLinkFlags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  1054. if(targetLinkFlags)
  1055. {
  1056. linkFlags += targetLinkFlags;
  1057. linkFlags += " ";
  1058. }
  1059. }
  1060. break;
  1061. case cmTarget::MODULE_LIBRARY:
  1062. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1063. case cmTarget::SHARED_LIBRARY:
  1064. {
  1065. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  1066. linkFlags += " ";
  1067. if(buildType.size())
  1068. {
  1069. std::string build = libraryLinkVariable;
  1070. build += "_";
  1071. build += buildType;
  1072. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1073. linkFlags += " ";
  1074. }
  1075. if(this->Makefile->IsOn("WIN32") && !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  1076. {
  1077. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1078. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1079. i != sources.end(); ++i)
  1080. {
  1081. if((*i)->GetSourceExtension() == "def")
  1082. {
  1083. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1084. linkFlags += this->Convert((*i)->GetFullPath().c_str(),START_OUTPUT,MAKEFILE);
  1085. linkFlags += " ";
  1086. }
  1087. }
  1088. }
  1089. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1090. if(targetLinkFlags)
  1091. {
  1092. linkFlags += targetLinkFlags;
  1093. linkFlags += " ";
  1094. std::string configLinkFlags = targetLinkFlags;
  1095. configLinkFlags += buildType;
  1096. targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
  1097. if(targetLinkFlags)
  1098. {
  1099. linkFlags += targetLinkFlags;
  1100. linkFlags += " ";
  1101. }
  1102. }
  1103. cmOStringStream linklibsStr;
  1104. this->OutputLinkLibraries(linklibsStr, target, false);
  1105. linkLibs = linklibsStr.str();
  1106. }
  1107. break;
  1108. case cmTarget::EXECUTABLE:
  1109. {
  1110. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1111. linkFlags += " ";
  1112. if(buildType.size())
  1113. {
  1114. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1115. build += buildType;
  1116. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1117. linkFlags += " ";
  1118. }
  1119. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  1120. if(!linkLanguage)
  1121. {
  1122. cmSystemTools::Error("CMake can not determine linker language for target:",
  1123. target.GetName());
  1124. return;
  1125. }
  1126. std::string langVar = "CMAKE_";
  1127. langVar += linkLanguage;
  1128. std::string flagsVar = langVar + "_FLAGS";
  1129. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  1130. sharedFlagsVar += linkLanguage;
  1131. sharedFlagsVar += "_FLAGS";
  1132. flags += this->Makefile->GetSafeDefinition(flagsVar.c_str());
  1133. flags += " ";
  1134. flags += this->Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  1135. flags += " ";
  1136. cmOStringStream linklibs;
  1137. this->OutputLinkLibraries(linklibs, target, false);
  1138. linkLibs = linklibs.str();
  1139. if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1140. {
  1141. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") + linkLanguage
  1142. + std::string("_FLAGS");
  1143. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
  1144. linkFlags += " ";
  1145. }
  1146. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1147. {
  1148. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1149. linkFlags += " ";
  1150. }
  1151. else
  1152. {
  1153. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1154. linkFlags += " ";
  1155. }
  1156. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1157. if(targetLinkFlags)
  1158. {
  1159. linkFlags += targetLinkFlags;
  1160. linkFlags += " ";
  1161. std::string configLinkFlags = targetLinkFlags;
  1162. configLinkFlags += buildType;
  1163. targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
  1164. if(targetLinkFlags)
  1165. {
  1166. linkFlags += targetLinkFlags;
  1167. linkFlags += " ";
  1168. }
  1169. }
  1170. }
  1171. break;
  1172. case cmTarget::UTILITY:
  1173. case cmTarget::GLOBAL_TARGET:
  1174. case cmTarget::INSTALL_FILES:
  1175. case cmTarget::INSTALL_PROGRAMS:
  1176. case cmTarget::INSTALL_DIRECTORY:
  1177. break;
  1178. }
  1179. }
  1180. /**
  1181. * Output the linking rules on a command line. For executables,
  1182. * targetLibrary should be a NULL pointer. For libraries, it should point
  1183. * to the name of the library. This will not link a library against itself.
  1184. */
  1185. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  1186. cmTarget& tgt,
  1187. bool relink)
  1188. {
  1189. // Try to emit each search path once
  1190. std::set<cmStdString> emitted;
  1191. // Embed runtime search paths if possible and if required.
  1192. bool outputRuntime = true;
  1193. std::string runtimeFlag;
  1194. std::string runtimeSep;
  1195. const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1196. const char* linkLanguage = tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1197. if(!linkLanguage)
  1198. {
  1199. cmSystemTools::
  1200. Error("CMake can not determine linker language for target:",
  1201. tgt.GetName());
  1202. return;
  1203. }
  1204. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1205. runTimeFlagVar += linkLanguage;
  1206. runTimeFlagVar += "_FLAG";
  1207. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1208. runtimeFlag = this->Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1209. runtimeSep = this->Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1210. // concatenate all paths or no?
  1211. bool runtimeConcatenate = ( runtimeSep!="" );
  1212. if(runtimeFlag == "" || this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
  1213. {
  1214. outputRuntime = false;
  1215. }
  1216. // Some search paths should never be emitted
  1217. emitted.insert("");
  1218. emitted.insert("/usr/lib");
  1219. std::string libPathFlag = this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1220. std::string libLinkFlag = this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1221. // collect all the flags needed for linking libraries
  1222. std::string linkLibs;
  1223. // Flags to link an executable to shared libraries.
  1224. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1225. linkFlagsVar += linkLanguage;
  1226. linkFlagsVar += "_FLAGS";
  1227. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1228. {
  1229. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1230. linkLibs += " ";
  1231. }
  1232. // Compute the link library and directory information.
  1233. std::vector<cmStdString> libNames;
  1234. std::vector<cmStdString> libDirs;
  1235. this->ComputeLinkInformation(tgt, config, libNames, libDirs);
  1236. // Select whether to generate an rpath for the install tree or the
  1237. // build tree.
  1238. bool linking_for_install =
  1239. relink || tgt.GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  1240. bool use_install_rpath =
  1241. outputRuntime && tgt.HaveInstallTreeRPATH() && linking_for_install;
  1242. bool use_build_rpath =
  1243. outputRuntime && tgt.HaveBuildTreeRPATH() && !linking_for_install;
  1244. // Construct the RPATH.
  1245. std::vector<std::string> runtimeDirs;
  1246. if(use_install_rpath)
  1247. {
  1248. const char* install_rpath = tgt.GetProperty("INSTALL_RPATH");
  1249. cmSystemTools::ExpandListArgument(install_rpath, runtimeDirs);
  1250. for(unsigned int i=0; i < runtimeDirs.size(); ++i)
  1251. {
  1252. runtimeDirs[i] =
  1253. this->Convert(runtimeDirs[i].c_str(), FULL, SHELL, false);
  1254. }
  1255. }
  1256. // Append the library search path flags.
  1257. for(std::vector<cmStdString>::const_iterator libDir = libDirs.begin();
  1258. libDir != libDirs.end(); ++libDir)
  1259. {
  1260. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1261. if(emitted.insert(libpath).second)
  1262. {
  1263. std::string fullLibPath;
  1264. if(!this->WindowsShell && this->UseRelativePaths)
  1265. {
  1266. fullLibPath = "\"`cd ";
  1267. }
  1268. fullLibPath += libpath;
  1269. if(!this->WindowsShell && this->UseRelativePaths)
  1270. {
  1271. fullLibPath += ";pwd`\"";
  1272. }
  1273. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  1274. if((pos == std::string::npos || pos > 0)
  1275. && libDir->find("${") == std::string::npos)
  1276. {
  1277. linkLibs += libPathFlag;
  1278. if(use_build_rpath)
  1279. {
  1280. runtimeDirs.push_back( fullLibPath );
  1281. }
  1282. }
  1283. linkLibs += fullLibPath;
  1284. linkLibs += " ";
  1285. }
  1286. }
  1287. // Append the link libraries.
  1288. for(std::vector<cmStdString>::iterator lib = libNames.begin();
  1289. lib != libNames.end(); ++lib)
  1290. {
  1291. linkLibs += *lib;
  1292. linkLibs += " ";
  1293. }
  1294. fout << linkLibs;
  1295. if(!runtimeDirs.empty())
  1296. {
  1297. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1298. // a "-R a -R b -R c" type link line
  1299. fout << runtimeFlag;
  1300. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1301. fout << *itr;
  1302. ++itr;
  1303. for( ; itr != runtimeDirs.end(); ++itr )
  1304. {
  1305. if(runtimeConcatenate)
  1306. {
  1307. fout << runtimeSep << *itr;
  1308. }
  1309. else
  1310. {
  1311. fout << " " << runtimeFlag << *itr;
  1312. }
  1313. }
  1314. fout << " ";
  1315. }
  1316. // Add standard libraries for this language.
  1317. std::string standardLibsVar = "CMAKE_";
  1318. standardLibsVar += linkLanguage;
  1319. standardLibsVar += "_STANDARD_LIBRARIES";
  1320. if(const char* stdLibs =
  1321. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1322. {
  1323. fout << stdLibs << " ";
  1324. }
  1325. }
  1326. //----------------------------------------------------------------------------
  1327. void
  1328. cmLocalGenerator::ComputeLinkInformation(cmTarget& target,
  1329. const char* config,
  1330. std::vector<cmStdString>& outLibs,
  1331. std::vector<cmStdString>& outDirs,
  1332. std::vector<cmStdString>* fullPathLibs)
  1333. {
  1334. // Compute which library configuration to link.
  1335. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  1336. if(config && cmSystemTools::UpperCase(config) == "DEBUG")
  1337. {
  1338. linkType = cmTarget::DEBUG;
  1339. }
  1340. // Get the list of libraries against which this target wants to link.
  1341. std::vector<std::string> linkLibraries;
  1342. const cmTarget::LinkLibraryVectorType& inLibs = target.GetLinkLibraries();
  1343. for(cmTarget::LinkLibraryVectorType::const_iterator j = inLibs.begin();
  1344. j != inLibs.end(); ++j)
  1345. {
  1346. // For backwards compatibility variables may have been expanded
  1347. // inside library names. Clean up the resulting name.
  1348. std::string lib = j->first;
  1349. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  1350. if(pos != lib.npos)
  1351. {
  1352. lib = lib.substr(pos, lib.npos);
  1353. }
  1354. pos = lib.find_last_not_of(" \t\r\n");
  1355. if(pos != lib.npos)
  1356. {
  1357. lib = lib.substr(0, pos+1);
  1358. }
  1359. if(lib.empty())
  1360. {
  1361. continue;
  1362. }
  1363. // Link to a library if it is not the same target and is meant for
  1364. // this configuration type.
  1365. if((target.GetType() == cmTarget::EXECUTABLE ||
  1366. lib != target.GetName()) &&
  1367. (j->second == cmTarget::GENERAL || j->second == linkType))
  1368. {
  1369. // Compute the proper name to use to link this library.
  1370. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, lib.c_str());
  1371. if(tgt)
  1372. {
  1373. // This is a CMake target. Ask the target for its real name.
  1374. // Pass the full path to the target file but purposely leave
  1375. // off the per-configuration subdirectory. The link directory
  1376. // ordering knows how to deal with this.
  1377. std::string linkItem = tgt->GetDirectory(0);
  1378. linkItem += "/";
  1379. linkItem += tgt->GetFullName(config);
  1380. linkLibraries.push_back(linkItem);
  1381. // For full path, use the true location.
  1382. if(fullPathLibs)
  1383. {
  1384. fullPathLibs->push_back(tgt->GetFullPath(config));
  1385. }
  1386. }
  1387. else
  1388. {
  1389. // This is not a CMake target. Use the name given.
  1390. linkLibraries.push_back(lib);
  1391. // Add to the list of full paths if this library is one.
  1392. if(fullPathLibs &&
  1393. cmSystemTools::FileIsFullPath(lib.c_str()) &&
  1394. !cmSystemTools::FileIsDirectory(lib.c_str()))
  1395. {
  1396. fullPathLibs->push_back(lib);
  1397. }
  1398. }
  1399. }
  1400. }
  1401. // Get the list of directories the target wants to search for libraries.
  1402. const std::vector<std::string>&
  1403. linkDirectories = target.GetLinkDirectories();
  1404. // Compute the link directory order needed to link the libraries.
  1405. cmOrderLinkDirectories orderLibs;
  1406. orderLibs.SetLinkPrefix(
  1407. this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
  1408. orderLibs.AddLinkExtension(
  1409. this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"));
  1410. orderLibs.AddLinkExtension(
  1411. this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"));
  1412. orderLibs.AddLinkExtension(
  1413. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"));
  1414. orderLibs.AddLinkExtension(
  1415. this->Makefile->GetDefinition("CMAKE_LINK_LIBRARY_SUFFIX"));
  1416. if(const char* linkSuffixes =
  1417. this->Makefile->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS"))
  1418. {
  1419. std::vector<std::string> linkSuffixVec;
  1420. cmSystemTools::ExpandListArgument(linkSuffixes, linkSuffixVec);
  1421. for(std::vector<std::string>::iterator i = linkSuffixVec.begin();
  1422. i != linkSuffixVec.end(); ++i)
  1423. {
  1424. orderLibs.AddLinkExtension(i->c_str());
  1425. }
  1426. }
  1427. std::string configSubdir;
  1428. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  1429. gg->AppendDirectoryForConfig("", config, "", configSubdir);
  1430. orderLibs.SetLinkInformation(target.GetName(),
  1431. linkLibraries,
  1432. linkDirectories,
  1433. gg->GetTargetManifest(),
  1434. configSubdir.c_str());
  1435. orderLibs.DetermineLibraryPathOrder();
  1436. std::vector<cmStdString> orderedLibs;
  1437. orderLibs.GetLinkerInformation(outDirs, orderedLibs);
  1438. // Make sure libraries are linked with the proper syntax.
  1439. std::string libLinkFlag =
  1440. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1441. std::string libLinkSuffix =
  1442. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1443. for(std::vector<cmStdString>::iterator l = orderedLibs.begin();
  1444. l != orderedLibs.end(); ++l)
  1445. {
  1446. std::string lib = *l;
  1447. if(lib[0] == '-' || lib[0] == '$' || lib[0] == '`')
  1448. {
  1449. // The library is linked with special syntax by the user.
  1450. outLibs.push_back(lib);
  1451. }
  1452. else
  1453. {
  1454. // Generate the proper link syntax.
  1455. lib = libLinkFlag;
  1456. lib += *l;
  1457. lib += libLinkSuffix;
  1458. outLibs.push_back(lib);
  1459. }
  1460. }
  1461. }
  1462. //----------------------------------------------------------------------------
  1463. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1464. const char* lang,
  1465. const char* config)
  1466. {
  1467. // Add language-specific flags.
  1468. std::string flagsVar = "CMAKE_";
  1469. flagsVar += lang;
  1470. flagsVar += "_FLAGS";
  1471. if(this->EmitUniversalBinaryFlags)
  1472. {
  1473. const char* osxArch =
  1474. this->Makefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  1475. const char* sysroot =
  1476. this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1477. if(osxArch && sysroot && lang && lang[0] =='C')
  1478. {
  1479. std::vector<std::string> archs;
  1480. cmSystemTools::ExpandListArgument(std::string(osxArch),
  1481. archs);
  1482. if(archs.size() > 1)
  1483. {
  1484. for( std::vector<std::string>::iterator i = archs.begin();
  1485. i != archs.end(); ++i)
  1486. {
  1487. flags += " -arch ";
  1488. flags += *i;
  1489. }
  1490. flags += " -isysroot ";
  1491. flags += sysroot;
  1492. }
  1493. }
  1494. }
  1495. this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
  1496. }
  1497. //----------------------------------------------------------------------------
  1498. std::string cmLocalGenerator::GetRealDependency(const char* inName,
  1499. const char* config)
  1500. {
  1501. // Older CMake code may specify the dependency using the target
  1502. // output file rather than the target name. Such code would have
  1503. // been written before there was support for target properties that
  1504. // modify the name so stripping down to just the file name should
  1505. // produce the target name in this case.
  1506. std::string name = cmSystemTools::GetFilenameName(inName);
  1507. if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
  1508. {
  1509. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1510. }
  1511. // Look for a CMake target with the given name.
  1512. if(cmTarget* target = this->GlobalGenerator->FindTarget(0, name.c_str()))
  1513. {
  1514. switch (target->GetType())
  1515. {
  1516. case cmTarget::EXECUTABLE:
  1517. case cmTarget::STATIC_LIBRARY:
  1518. case cmTarget::SHARED_LIBRARY:
  1519. case cmTarget::MODULE_LIBRARY:
  1520. {
  1521. // Get the location of the target's output file and depend on it.
  1522. if(const char* location = target->GetLocation(config))
  1523. {
  1524. return location;
  1525. }
  1526. }
  1527. break;
  1528. case cmTarget::UTILITY:
  1529. case cmTarget::GLOBAL_TARGET:
  1530. // Depending on a utility target may not work but just trust
  1531. // the user to have given a valid name.
  1532. return inName;
  1533. case cmTarget::INSTALL_FILES:
  1534. case cmTarget::INSTALL_PROGRAMS:
  1535. case cmTarget::INSTALL_DIRECTORY:
  1536. break;
  1537. }
  1538. }
  1539. // The name was not that of a CMake target. It must name a file.
  1540. if(cmSystemTools::FileIsFullPath(inName))
  1541. {
  1542. // This is a full path. Return it as given.
  1543. return inName;
  1544. }
  1545. // Treat the name as relative to the source directory in which it
  1546. // was given.
  1547. name = this->Makefile->GetCurrentDirectory();
  1548. name += "/";
  1549. name += inName;
  1550. return name;
  1551. }
  1552. //----------------------------------------------------------------------------
  1553. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1554. const char* lang,
  1555. bool shared)
  1556. {
  1557. std::string flagsVar;
  1558. // Add flags for dealing with shared libraries for this language.
  1559. if(shared)
  1560. {
  1561. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1562. flagsVar += lang;
  1563. flagsVar += "_FLAGS";
  1564. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1565. }
  1566. // Add flags specific to shared builds.
  1567. if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1568. {
  1569. flagsVar = "CMAKE_SHARED_BUILD_";
  1570. flagsVar += lang;
  1571. flagsVar += "_FLAGS";
  1572. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1573. }
  1574. }
  1575. //----------------------------------------------------------------------------
  1576. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1577. const char* var,
  1578. const char* config)
  1579. {
  1580. // Add the flags from the variable itself.
  1581. std::string flagsVar = var;
  1582. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1583. // Add the flags from the build-type specific variable.
  1584. if(config && *config)
  1585. {
  1586. flagsVar += "_";
  1587. flagsVar += cmSystemTools::UpperCase(config);
  1588. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1589. }
  1590. }
  1591. //----------------------------------------------------------------------------
  1592. void cmLocalGenerator::AppendFlags(std::string& flags,
  1593. const char* newFlags)
  1594. {
  1595. if(newFlags && *newFlags)
  1596. {
  1597. if(flags.size())
  1598. {
  1599. flags += " ";
  1600. }
  1601. flags += newFlags;
  1602. }
  1603. }
  1604. //----------------------------------------------------------------------------
  1605. std::string
  1606. cmLocalGenerator::ConstructScript(const cmCustomCommandLines& commandLines,
  1607. const char* workingDirectory,
  1608. const char* newline)
  1609. {
  1610. // Store the script in a string.
  1611. std::string script;
  1612. if(workingDirectory)
  1613. {
  1614. script += "cd ";
  1615. script += this->Convert(workingDirectory, START_OUTPUT, SHELL);
  1616. script += newline;
  1617. }
  1618. // Write each command on a single line.
  1619. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  1620. cl != commandLines.end(); ++cl)
  1621. {
  1622. // Start with the command name.
  1623. const cmCustomCommandLine& commandLine = *cl;
  1624. script += this->Convert(commandLine[0].c_str(),START_OUTPUT,SHELL);
  1625. // Add the arguments.
  1626. for(unsigned int j=1;j < commandLine.size(); ++j)
  1627. {
  1628. script += " ";
  1629. script += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  1630. }
  1631. // End the line.
  1632. script += newline;
  1633. }
  1634. return script;
  1635. }
  1636. //----------------------------------------------------------------------------
  1637. std::string
  1638. cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
  1639. const char* default_comment)
  1640. {
  1641. // Check for a comment provided with the command.
  1642. if(cc.GetComment() && *cc.GetComment())
  1643. {
  1644. return cc.GetComment();
  1645. }
  1646. // Construct a reasonable default comment if possible.
  1647. if(!cc.GetOutputs().empty())
  1648. {
  1649. std::string comment;
  1650. comment = "Generating ";
  1651. const char* sep = "";
  1652. for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
  1653. o != cc.GetOutputs().end(); ++o)
  1654. {
  1655. comment += sep;
  1656. comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
  1657. sep = ", ";
  1658. }
  1659. return comment;
  1660. }
  1661. // Otherwise use the provided default.
  1662. return default_comment;
  1663. }
  1664. //----------------------------------------------------------------------------
  1665. std::string
  1666. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  1667. {
  1668. return this->Convert(remote, START_OUTPUT, SHELL, true);
  1669. }
  1670. //----------------------------------------------------------------------------
  1671. std::string cmLocalGenerator::Convert(const char* source,
  1672. RelativeRoot relative,
  1673. OutputFormat output,
  1674. bool optional)
  1675. {
  1676. // Convert the path to a relative path.
  1677. std::string result = source;
  1678. if (!optional || this->UseRelativePaths)
  1679. {
  1680. switch (relative)
  1681. {
  1682. case HOME:
  1683. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1684. result = this->GlobalGenerator->
  1685. ConvertToRelativePath(this->HomeDirectoryComponents, result.c_str());
  1686. break;
  1687. case START:
  1688. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1689. result = this->GlobalGenerator->
  1690. ConvertToRelativePath(this->StartDirectoryComponents, result.c_str());
  1691. break;
  1692. case HOME_OUTPUT:
  1693. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1694. result = this->GlobalGenerator->
  1695. ConvertToRelativePath(this->HomeOutputDirectoryComponents, result.c_str());
  1696. break;
  1697. case START_OUTPUT:
  1698. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1699. result = this->GlobalGenerator->
  1700. ConvertToRelativePath(this->StartOutputDirectoryComponents, result.c_str());
  1701. break;
  1702. case FULL:
  1703. result = cmSystemTools::CollapseFullPath(result.c_str());
  1704. break;
  1705. case NONE:
  1706. break;
  1707. }
  1708. }
  1709. // Now convert it to an output path.
  1710. if (output == MAKEFILE)
  1711. {
  1712. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1713. }
  1714. if( output == SHELL)
  1715. {
  1716. // for shell commands if force unix is on, but this->WindowsShell
  1717. // is true, then turn off force unix paths for the output path
  1718. // so that the path is windows style and will work with windows
  1719. // cmd.exe.
  1720. bool forceOn = cmSystemTools::GetForceUnixPaths();
  1721. if(forceOn && this->WindowsShell)
  1722. {
  1723. cmSystemTools::SetForceUnixPaths(false);
  1724. }
  1725. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1726. if(forceOn && this->WindowsShell)
  1727. {
  1728. cmSystemTools::SetForceUnixPaths(true);
  1729. }
  1730. }
  1731. return result;
  1732. }
  1733. //----------------------------------------------------------------------------
  1734. void
  1735. cmLocalGenerator
  1736. ::GenerateTargetInstallRules(
  1737. std::ostream& os, const char* config,
  1738. std::vector<std::string> const& configurationTypes)
  1739. {
  1740. // Convert the old-style install specification from each target to
  1741. // an install generator and run it.
  1742. cmTargets& tgts = this->Makefile->GetTargets();
  1743. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  1744. {
  1745. // Include the user-specified pre-install script for this target.
  1746. if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
  1747. {
  1748. cmInstallScriptGenerator g(preinstall);
  1749. g.Generate(os, config, configurationTypes);
  1750. }
  1751. // Install this target if a destination is given.
  1752. if(l->second.GetInstallPath() != "")
  1753. {
  1754. // Compute the full install destination. Note that converting
  1755. // to unix slashes also removes any trailing slash.
  1756. std::string destination = "${CMAKE_INSTALL_PREFIX}";
  1757. destination += l->second.GetInstallPath();
  1758. cmSystemTools::ConvertToUnixSlashes(destination);
  1759. // Generate the proper install generator for this target type.
  1760. switch(l->second.GetType())
  1761. {
  1762. case cmTarget::EXECUTABLE:
  1763. case cmTarget::STATIC_LIBRARY:
  1764. case cmTarget::MODULE_LIBRARY:
  1765. {
  1766. // Use a target install generator.
  1767. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  1768. g.Generate(os, config, configurationTypes);
  1769. }
  1770. break;
  1771. case cmTarget::SHARED_LIBRARY:
  1772. {
  1773. #if defined(_WIN32) || defined(__CYGWIN__)
  1774. // Special code to handle DLL. Install the import library
  1775. // to the normal destination and the DLL to the runtime
  1776. // destination.
  1777. cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
  1778. g1.Generate(os, config, configurationTypes);
  1779. destination = "${CMAKE_INSTALL_PREFIX}";
  1780. destination += l->second.GetRuntimeInstallPath();
  1781. cmSystemTools::ConvertToUnixSlashes(destination);
  1782. cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
  1783. g2.Generate(os, config, configurationTypes);
  1784. #else
  1785. // Use a target install generator.
  1786. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  1787. g.Generate(os, config, configurationTypes);
  1788. #endif
  1789. }
  1790. break;
  1791. case cmTarget::INSTALL_FILES:
  1792. {
  1793. // Use a file install generator.
  1794. const char* no_permissions = "";
  1795. const char* no_rename = "";
  1796. const char* no_component = "";
  1797. cmInstallFilesGenerator g(l->second.GetSourceLists(),
  1798. destination.c_str(), false,
  1799. no_permissions, no_component, no_rename);
  1800. g.Generate(os, config, configurationTypes);
  1801. }
  1802. break;
  1803. case cmTarget::INSTALL_PROGRAMS:
  1804. {
  1805. // Use a file install generator.
  1806. const char* no_permissions = "";
  1807. const char* no_rename = "";
  1808. const char* no_component = "";
  1809. cmInstallFilesGenerator g(l->second.GetSourceLists(),
  1810. destination.c_str(), true,
  1811. no_permissions, no_component, no_rename);
  1812. g.Generate(os, config, configurationTypes);
  1813. }
  1814. break;
  1815. case cmTarget::UTILITY:
  1816. default:
  1817. break;
  1818. }
  1819. }
  1820. // Include the user-specified post-install script for this target.
  1821. if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
  1822. {
  1823. cmInstallScriptGenerator g(postinstall);
  1824. g.Generate(os, config, configurationTypes);
  1825. }
  1826. }
  1827. }