cmLocalGenerator.cxx 93 KB

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