cmLocalGenerator.cxx 109 KB

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