cmLocalGenerator.cxx 91 KB

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