cmLocalGenerator.cxx 110 KB

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