cmLocalGenerator.cxx 109 KB

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