cmLocalGenerator.cxx 96 KB

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