cmLocalGenerator.cxx 112 KB

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