cmLocalGenerator.cxx 93 KB

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