cmLocalGenerator.cxx 65 KB

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