cmLocalGenerator.cxx 89 KB

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