cmLocalGenerator.cxx 109 KB

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