cmLocalGenerator.cxx 74 KB

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