cmLocalGenerator.cxx 107 KB

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