cmLocalGenerator.cxx 87 KB

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