cmLocalGenerator.cxx 112 KB

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