cmLocalGenerator.cxx 72 KB

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