cmLocalGenerator.cxx 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015
  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 "cmGeneratorExpressionEvaluationFile.h"
  17. #include "cmInstallScriptGenerator.h"
  18. #include "cmInstallTargetGenerator.h"
  19. #include "cmMakefile.h"
  20. #include "cmSourceFile.h"
  21. #include "cmTest.h"
  22. #include "cmTestGenerator.h"
  23. #include "cmCustomCommandGenerator.h"
  24. #include "cmVersion.h"
  25. #include "cmake.h"
  26. #include "cmAlgorithms.h"
  27. #if defined(CMAKE_BUILD_WITH_CMAKE)
  28. # define CM_LG_ENCODE_OBJECT_NAMES
  29. # include <cmsys/MD5.h>
  30. #endif
  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. : cmOutputConverter(snapshot), 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->EmitUniversalBinaryFlags = true;
  51. this->BackwardsCompatibility = 0;
  52. this->BackwardsCompatibilityFinal = false;
  53. }
  54. cmLocalGenerator::~cmLocalGenerator()
  55. {
  56. delete this->Makefile;
  57. }
  58. void cmLocalGenerator::IssueMessage(cmake::MessageType t,
  59. std::string const& text) const
  60. {
  61. cmListFileContext lfc;
  62. lfc.FilePath = this->StateSnapshot.GetDirectory().GetCurrentSource();
  63. lfc.FilePath += "/CMakeLists.txt";
  64. if(!this->GlobalGenerator->GetCMakeInstance()->GetIsInTryCompile())
  65. {
  66. cmOutputConverter converter(this->StateSnapshot);
  67. lfc.FilePath = converter.Convert(lfc.FilePath, cmLocalGenerator::HOME);
  68. }
  69. lfc.Line = 0;
  70. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(t, text, lfc);
  71. }
  72. //----------------------------------------------------------------------------
  73. void cmLocalGenerator::ComputeObjectMaxPath()
  74. {
  75. // Choose a maximum object file name length.
  76. #if defined(_WIN32) || defined(__CYGWIN__)
  77. this->ObjectPathMax = 250;
  78. #else
  79. this->ObjectPathMax = 1000;
  80. #endif
  81. const char* plen = this->Makefile->GetDefinition("CMAKE_OBJECT_PATH_MAX");
  82. if(plen && *plen)
  83. {
  84. unsigned int pmax;
  85. if(sscanf(plen, "%u", &pmax) == 1)
  86. {
  87. if(pmax >= 128)
  88. {
  89. this->ObjectPathMax = pmax;
  90. }
  91. else
  92. {
  93. std::ostringstream w;
  94. w << "CMAKE_OBJECT_PATH_MAX is set to " << pmax
  95. << ", which is less than the minimum of 128. "
  96. << "The value will be ignored.";
  97. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  98. }
  99. }
  100. else
  101. {
  102. std::ostringstream w;
  103. w << "CMAKE_OBJECT_PATH_MAX is set to \"" << plen
  104. << "\", which fails to parse as a positive integer. "
  105. << "The value will be ignored.";
  106. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  107. }
  108. }
  109. this->ObjectMaxPathViolations.clear();
  110. }
  111. void cmLocalGenerator::TraceDependencies()
  112. {
  113. std::vector<std::string> configs;
  114. this->Makefile->GetConfigurations(configs);
  115. if (configs.empty())
  116. {
  117. configs.push_back("");
  118. }
  119. for(std::vector<std::string>::const_iterator ci = configs.begin();
  120. ci != configs.end(); ++ci)
  121. {
  122. this->GlobalGenerator->CreateEvaluationSourceFiles(*ci);
  123. }
  124. // Generate the rule files for each target.
  125. cmGeneratorTargetsType targets = this->Makefile->GetGeneratorTargets();
  126. for(cmGeneratorTargetsType::iterator t = targets.begin();
  127. t != targets.end(); ++t)
  128. {
  129. if (t->second->Target->IsImported()
  130. || t->second->Target->GetType() == cmTarget::INTERFACE_LIBRARY)
  131. {
  132. continue;
  133. }
  134. t->second->TraceDependencies();
  135. }
  136. }
  137. void cmLocalGenerator::GenerateTestFiles()
  138. {
  139. if ( !this->Makefile->IsOn("CMAKE_TESTING_ENABLED") )
  140. {
  141. return;
  142. }
  143. // Compute the set of configurations.
  144. std::vector<std::string> configurationTypes;
  145. const std::string& config =
  146. this->Makefile->GetConfigurations(configurationTypes, false);
  147. std::string file =
  148. this->StateSnapshot.GetDirectory().GetCurrentBinary();
  149. file += "/";
  150. file += "CTestTestfile.cmake";
  151. cmGeneratedFileStream fout(file.c_str());
  152. fout.SetCopyIfDifferent(true);
  153. fout << "# CMake generated Testfile for " << std::endl
  154. << "# Source directory: "
  155. << this->StateSnapshot.GetDirectory().GetCurrentSource()
  156. << std::endl
  157. << "# Build directory: "
  158. << this->StateSnapshot.GetDirectory().GetCurrentBinary()
  159. << std::endl
  160. << "# " << std::endl
  161. << "# This file includes the relevant testing commands "
  162. << "required for " << std::endl
  163. << "# testing this directory and lists subdirectories to "
  164. << "be tested as well." << std::endl;
  165. const char* testIncludeFile =
  166. this->Makefile->GetProperty("TEST_INCLUDE_FILE");
  167. if ( testIncludeFile )
  168. {
  169. fout << "include(\"" << testIncludeFile << "\")" << std::endl;
  170. }
  171. // Ask each test generator to write its code.
  172. std::vector<cmTestGenerator*> const&
  173. testers = this->Makefile->GetTestGenerators();
  174. for(std::vector<cmTestGenerator*>::const_iterator gi = testers.begin();
  175. gi != testers.end(); ++gi)
  176. {
  177. (*gi)->Compute(this);
  178. (*gi)->Generate(fout, config, configurationTypes);
  179. }
  180. if (!this->Children.empty())
  181. {
  182. size_t i;
  183. for(i = 0; i < this->Children.size(); ++i)
  184. {
  185. // TODO: Use add_subdirectory instead?
  186. fout << "subdirs(";
  187. std::string outP =
  188. this->Children[i]->GetMakefile()->GetCurrentBinaryDirectory();
  189. fout << this->Convert(outP,START_OUTPUT);
  190. fout << ")" << std::endl;
  191. }
  192. }
  193. }
  194. void cmLocalGenerator::CreateEvaluationFileOutputs(std::string const& config)
  195. {
  196. std::vector<cmGeneratorExpressionEvaluationFile*> ef =
  197. this->Makefile->GetEvaluationFiles();
  198. for(std::vector<cmGeneratorExpressionEvaluationFile*>::const_iterator
  199. li = ef.begin(); li != ef.end(); ++li)
  200. {
  201. (*li)->CreateOutputFile(this, config);
  202. }
  203. }
  204. void cmLocalGenerator::ProcessEvaluationFiles(
  205. std::vector<std::string>& generatedFiles)
  206. {
  207. std::vector<cmGeneratorExpressionEvaluationFile*> ef =
  208. this->Makefile->GetEvaluationFiles();
  209. for(std::vector<cmGeneratorExpressionEvaluationFile*>::const_iterator
  210. li = ef.begin();
  211. li != ef.end();
  212. ++li)
  213. {
  214. (*li)->Generate(this);
  215. if (cmSystemTools::GetFatalErrorOccured())
  216. {
  217. return;
  218. }
  219. std::vector<std::string> files = (*li)->GetFiles();
  220. std::sort(files.begin(), files.end());
  221. std::vector<std::string> intersection;
  222. std::set_intersection(files.begin(), files.end(),
  223. generatedFiles.begin(), generatedFiles.end(),
  224. std::back_inserter(intersection));
  225. if (!intersection.empty())
  226. {
  227. cmSystemTools::Error("Files to be generated by multiple different "
  228. "commands: ", cmWrap('"', intersection, '"', " ").c_str());
  229. return;
  230. }
  231. generatedFiles.insert(generatedFiles.end(), files.begin(), files.end());
  232. std::vector<std::string>::iterator newIt =
  233. generatedFiles.end() - files.size();
  234. std::inplace_merge(generatedFiles.begin(), newIt, generatedFiles.end());
  235. }
  236. }
  237. //----------------------------------------------------------------------------
  238. void cmLocalGenerator::GenerateInstallRules()
  239. {
  240. // Compute the install prefix.
  241. const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  242. #if defined(_WIN32) && !defined(__CYGWIN__)
  243. std::string prefix_win32;
  244. if(!prefix)
  245. {
  246. if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32))
  247. {
  248. prefix_win32 = "C:";
  249. }
  250. const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
  251. if(project_name && project_name[0])
  252. {
  253. prefix_win32 += "/Program Files/";
  254. prefix_win32 += project_name;
  255. }
  256. else
  257. {
  258. prefix_win32 += "/InstalledCMakeProject";
  259. }
  260. prefix = prefix_win32.c_str();
  261. }
  262. #elif defined(__HAIKU__)
  263. char dir[B_PATH_NAME_LENGTH];
  264. if (!prefix)
  265. {
  266. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir))
  267. == B_OK)
  268. {
  269. prefix = dir;
  270. }
  271. else
  272. {
  273. prefix = "/boot/system";
  274. }
  275. }
  276. #else
  277. if (!prefix)
  278. {
  279. prefix = "/usr/local";
  280. }
  281. #endif
  282. if (const char *stagingPrefix
  283. = this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX"))
  284. {
  285. prefix = stagingPrefix;
  286. }
  287. // Compute the set of configurations.
  288. std::vector<std::string> configurationTypes;
  289. const std::string& config =
  290. this->Makefile->GetConfigurations(configurationTypes, false);
  291. // Choose a default install configuration.
  292. std::string default_config = config;
  293. const char* default_order[] = {"RELEASE", "MINSIZEREL",
  294. "RELWITHDEBINFO", "DEBUG", 0};
  295. for(const char** c = default_order; *c && default_config.empty(); ++c)
  296. {
  297. for(std::vector<std::string>::iterator i = configurationTypes.begin();
  298. i != configurationTypes.end(); ++i)
  299. {
  300. if(cmSystemTools::UpperCase(*i) == *c)
  301. {
  302. default_config = *i;
  303. }
  304. }
  305. }
  306. if(default_config.empty() && !configurationTypes.empty())
  307. {
  308. default_config = configurationTypes[0];
  309. }
  310. // Create the install script file.
  311. std::string file =
  312. this->StateSnapshot.GetDirectory().GetCurrentBinary();
  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.GetDirectory().GetCurrentSource()
  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. // List of variables that are replaced when
  458. // rules are expanced. These variables are
  459. // replaced in the form <var> with GetSafeDefinition(var).
  460. // ${LANG} is replaced in the variable first with all enabled
  461. // languages.
  462. static const char* ruleReplaceVars[] =
  463. {
  464. "CMAKE_${LANG}_COMPILER",
  465. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  466. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  467. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  468. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  469. "CMAKE_${LANG}_LINK_FLAGS",
  470. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  471. "CMAKE_${LANG}_ARCHIVE",
  472. "CMAKE_AR",
  473. "CMAKE_CURRENT_SOURCE_DIR",
  474. "CMAKE_CURRENT_BINARY_DIR",
  475. "CMAKE_RANLIB",
  476. "CMAKE_LINKER",
  477. "CMAKE_CL_SHOWINCLUDES_PREFIX",
  478. 0
  479. };
  480. std::string
  481. cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
  482. const RuleVariables& replaceValues)
  483. {
  484. if(replaceValues.LinkFlags)
  485. {
  486. if(variable == "LINK_FLAGS")
  487. {
  488. return replaceValues.LinkFlags;
  489. }
  490. }
  491. if(replaceValues.Flags)
  492. {
  493. if(variable == "FLAGS")
  494. {
  495. return replaceValues.Flags;
  496. }
  497. }
  498. if(replaceValues.Source)
  499. {
  500. if(variable == "SOURCE")
  501. {
  502. return replaceValues.Source;
  503. }
  504. }
  505. if(replaceValues.PreprocessedSource)
  506. {
  507. if(variable == "PREPROCESSED_SOURCE")
  508. {
  509. return replaceValues.PreprocessedSource;
  510. }
  511. }
  512. if(replaceValues.AssemblySource)
  513. {
  514. if(variable == "ASSEMBLY_SOURCE")
  515. {
  516. return replaceValues.AssemblySource;
  517. }
  518. }
  519. if(replaceValues.Object)
  520. {
  521. if(variable == "OBJECT")
  522. {
  523. return replaceValues.Object;
  524. }
  525. }
  526. if(replaceValues.ObjectDir)
  527. {
  528. if(variable == "OBJECT_DIR")
  529. {
  530. return replaceValues.ObjectDir;
  531. }
  532. }
  533. if(replaceValues.ObjectFileDir)
  534. {
  535. if(variable == "OBJECT_FILE_DIR")
  536. {
  537. return replaceValues.ObjectFileDir;
  538. }
  539. }
  540. if(replaceValues.Objects)
  541. {
  542. if(variable == "OBJECTS")
  543. {
  544. return replaceValues.Objects;
  545. }
  546. }
  547. if(replaceValues.ObjectsQuoted)
  548. {
  549. if(variable == "OBJECTS_QUOTED")
  550. {
  551. return replaceValues.ObjectsQuoted;
  552. }
  553. }
  554. if(replaceValues.Defines && variable == "DEFINES")
  555. {
  556. return replaceValues.Defines;
  557. }
  558. if(replaceValues.Includes && variable == "INCLUDES")
  559. {
  560. return replaceValues.Includes;
  561. }
  562. if(replaceValues.TargetPDB )
  563. {
  564. if(variable == "TARGET_PDB")
  565. {
  566. return replaceValues.TargetPDB;
  567. }
  568. }
  569. if(replaceValues.TargetCompilePDB)
  570. {
  571. if(variable == "TARGET_COMPILE_PDB")
  572. {
  573. return replaceValues.TargetCompilePDB;
  574. }
  575. }
  576. if(replaceValues.DependencyFile )
  577. {
  578. if(variable == "DEP_FILE")
  579. {
  580. return replaceValues.DependencyFile;
  581. }
  582. }
  583. if(replaceValues.Target)
  584. {
  585. if(variable == "TARGET_QUOTED")
  586. {
  587. std::string targetQuoted = replaceValues.Target;
  588. if(!targetQuoted.empty() && targetQuoted[0] != '\"')
  589. {
  590. targetQuoted = '\"';
  591. targetQuoted += replaceValues.Target;
  592. targetQuoted += '\"';
  593. }
  594. return targetQuoted;
  595. }
  596. if(variable == "TARGET_UNQUOTED")
  597. {
  598. std::string unquoted = replaceValues.Target;
  599. std::string::size_type sz = unquoted.size();
  600. if(sz > 2 && unquoted[0] == '\"' && unquoted[sz-1] == '\"')
  601. {
  602. unquoted = unquoted.substr(1, sz-2);
  603. }
  604. return unquoted;
  605. }
  606. if(replaceValues.LanguageCompileFlags)
  607. {
  608. if(variable == "LANGUAGE_COMPILE_FLAGS")
  609. {
  610. return replaceValues.LanguageCompileFlags;
  611. }
  612. }
  613. if(replaceValues.Target)
  614. {
  615. if(variable == "TARGET")
  616. {
  617. return replaceValues.Target;
  618. }
  619. }
  620. if(variable == "TARGET_IMPLIB")
  621. {
  622. return this->TargetImplib;
  623. }
  624. if(variable == "TARGET_VERSION_MAJOR")
  625. {
  626. if(replaceValues.TargetVersionMajor)
  627. {
  628. return replaceValues.TargetVersionMajor;
  629. }
  630. else
  631. {
  632. return "0";
  633. }
  634. }
  635. if(variable == "TARGET_VERSION_MINOR")
  636. {
  637. if(replaceValues.TargetVersionMinor)
  638. {
  639. return replaceValues.TargetVersionMinor;
  640. }
  641. else
  642. {
  643. return "0";
  644. }
  645. }
  646. if(replaceValues.Target)
  647. {
  648. if(variable == "TARGET_BASE")
  649. {
  650. // Strip the last extension off the target name.
  651. std::string targetBase = replaceValues.Target;
  652. std::string::size_type pos = targetBase.rfind(".");
  653. if(pos != targetBase.npos)
  654. {
  655. return targetBase.substr(0, pos);
  656. }
  657. else
  658. {
  659. return targetBase;
  660. }
  661. }
  662. }
  663. }
  664. if(variable == "TARGET_SONAME" || variable == "SONAME_FLAG" ||
  665. variable == "TARGET_INSTALLNAME_DIR")
  666. {
  667. // All these variables depend on TargetSOName
  668. if(replaceValues.TargetSOName)
  669. {
  670. if(variable == "TARGET_SONAME")
  671. {
  672. return replaceValues.TargetSOName;
  673. }
  674. if(variable == "SONAME_FLAG" && replaceValues.SONameFlag)
  675. {
  676. return replaceValues.SONameFlag;
  677. }
  678. if(replaceValues.TargetInstallNameDir &&
  679. variable == "TARGET_INSTALLNAME_DIR")
  680. {
  681. return replaceValues.TargetInstallNameDir;
  682. }
  683. }
  684. return "";
  685. }
  686. if(replaceValues.LinkLibraries)
  687. {
  688. if(variable == "LINK_LIBRARIES")
  689. {
  690. return replaceValues.LinkLibraries;
  691. }
  692. }
  693. if(replaceValues.Language)
  694. {
  695. if(variable == "LANGUAGE")
  696. {
  697. return replaceValues.Language;
  698. }
  699. }
  700. if(replaceValues.CMTarget)
  701. {
  702. if(variable == "TARGET_NAME")
  703. {
  704. return replaceValues.CMTarget->GetName();
  705. }
  706. if(variable == "TARGET_TYPE")
  707. {
  708. return cmTarget::GetTargetTypeName(replaceValues.CMTarget->GetType());
  709. }
  710. }
  711. if(replaceValues.Output)
  712. {
  713. if(variable == "OUTPUT")
  714. {
  715. return replaceValues.Output;
  716. }
  717. }
  718. if(variable == "CMAKE_COMMAND")
  719. {
  720. return this->Convert(cmSystemTools::GetCMakeCommand(), FULL, SHELL);
  721. }
  722. std::vector<std::string> enabledLanguages =
  723. this->GetState()->GetEnabledLanguages();
  724. // loop over language specific replace variables
  725. int pos = 0;
  726. while(ruleReplaceVars[pos])
  727. {
  728. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  729. i != enabledLanguages.end(); ++i)
  730. {
  731. const char* lang = i->c_str();
  732. std::string actualReplace = ruleReplaceVars[pos];
  733. // If this is the compiler then look for the extra variable
  734. // _COMPILER_ARG1 which must be the first argument to the compiler
  735. const char* compilerArg1 = 0;
  736. const char* compilerTarget = 0;
  737. const char* compilerOptionTarget = 0;
  738. const char* compilerExternalToolchain = 0;
  739. const char* compilerOptionExternalToolchain = 0;
  740. const char* compilerSysroot = 0;
  741. const char* compilerOptionSysroot = 0;
  742. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  743. {
  744. std::string arg1 = actualReplace + "_ARG1";
  745. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  746. compilerArg1 = this->Makefile->GetDefinition(arg1);
  747. compilerTarget
  748. = this->Makefile->GetDefinition(
  749. std::string("CMAKE_") + lang + "_COMPILER_TARGET");
  750. compilerOptionTarget
  751. = this->Makefile->GetDefinition(
  752. std::string("CMAKE_") + lang +
  753. "_COMPILE_OPTIONS_TARGET");
  754. compilerExternalToolchain
  755. = this->Makefile->GetDefinition(
  756. std::string("CMAKE_") + lang +
  757. "_COMPILER_EXTERNAL_TOOLCHAIN");
  758. compilerOptionExternalToolchain
  759. = this->Makefile->GetDefinition(
  760. std::string("CMAKE_") + lang +
  761. "_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN");
  762. compilerSysroot
  763. = this->Makefile->GetDefinition("CMAKE_SYSROOT");
  764. compilerOptionSysroot
  765. = this->Makefile->GetDefinition(
  766. std::string("CMAKE_") + lang +
  767. "_COMPILE_OPTIONS_SYSROOT");
  768. }
  769. if(actualReplace.find("${LANG}") != actualReplace.npos)
  770. {
  771. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  772. }
  773. if(actualReplace == variable)
  774. {
  775. std::string replace =
  776. this->Makefile->GetSafeDefinition(variable);
  777. // if the variable is not a FLAG then treat it like a path
  778. if(variable.find("_FLAG") == variable.npos)
  779. {
  780. std::string ret = this->ConvertToOutputForExisting(replace);
  781. // if there is a required first argument to the compiler add it
  782. // to the compiler string
  783. if(compilerArg1)
  784. {
  785. ret += " ";
  786. ret += compilerArg1;
  787. }
  788. if (compilerTarget && compilerOptionTarget)
  789. {
  790. ret += " ";
  791. ret += compilerOptionTarget;
  792. ret += compilerTarget;
  793. }
  794. if (compilerExternalToolchain && compilerOptionExternalToolchain)
  795. {
  796. ret += " ";
  797. ret += compilerOptionExternalToolchain;
  798. ret += this->EscapeForShell(compilerExternalToolchain, true);
  799. }
  800. if (compilerSysroot && compilerOptionSysroot)
  801. {
  802. ret += " ";
  803. ret += compilerOptionSysroot;
  804. ret += this->EscapeForShell(compilerSysroot, true);
  805. }
  806. return ret;
  807. }
  808. return replace;
  809. }
  810. }
  811. pos++;
  812. }
  813. return variable;
  814. }
  815. void
  816. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  817. const RuleVariables& replaceValues)
  818. {
  819. if(replaceValues.RuleLauncher)
  820. {
  821. this->InsertRuleLauncher(s, replaceValues.CMTarget,
  822. replaceValues.RuleLauncher);
  823. }
  824. std::string::size_type start = s.find('<');
  825. // no variables to expand
  826. if(start == s.npos)
  827. {
  828. return;
  829. }
  830. std::string::size_type pos = 0;
  831. std::string expandedInput;
  832. while(start != s.npos && start < s.size()-2)
  833. {
  834. std::string::size_type end = s.find('>', start);
  835. // if we find a < with no > we are done
  836. if(end == s.npos)
  837. {
  838. return;
  839. }
  840. char c = s[start+1];
  841. // if the next char after the < is not A-Za-z then
  842. // skip it and try to find the next < in the string
  843. if(!isalpha(c))
  844. {
  845. start = s.find('<', start+1);
  846. }
  847. else
  848. {
  849. // extract the var
  850. std::string var = s.substr(start+1, end - start-1);
  851. std::string replace = this->ExpandRuleVariable(var,
  852. replaceValues);
  853. expandedInput += s.substr(pos, start-pos);
  854. expandedInput += replace;
  855. // move to next one
  856. start = s.find('<', start+var.size()+2);
  857. pos = end+1;
  858. }
  859. }
  860. // add the rest of the input
  861. expandedInput += s.substr(pos, s.size()-pos);
  862. s = expandedInput;
  863. }
  864. //----------------------------------------------------------------------------
  865. const char* cmLocalGenerator::GetRuleLauncher(cmTarget* target,
  866. const std::string& prop)
  867. {
  868. if(target)
  869. {
  870. return target->GetProperty(prop);
  871. }
  872. else
  873. {
  874. return this->Makefile->GetProperty(prop);
  875. }
  876. }
  877. //----------------------------------------------------------------------------
  878. void cmLocalGenerator::InsertRuleLauncher(std::string& s, cmTarget* target,
  879. const std::string& prop)
  880. {
  881. if(const char* val = this->GetRuleLauncher(target, prop))
  882. {
  883. std::ostringstream wrapped;
  884. wrapped << val << " " << s;
  885. s = wrapped.str();
  886. }
  887. }
  888. //----------------------------------------------------------------------------
  889. std::string
  890. cmLocalGenerator::ConvertToIncludeReference(std::string const& path,
  891. OutputFormat format,
  892. bool forceFullPaths)
  893. {
  894. return this->ConvertToOutputForExisting(
  895. path, forceFullPaths? FULL : START_OUTPUT, format);
  896. }
  897. //----------------------------------------------------------------------------
  898. std::string cmLocalGenerator::GetIncludeFlags(
  899. const std::vector<std::string> &includes,
  900. cmGeneratorTarget* target,
  901. const std::string& lang,
  902. bool forceFullPaths,
  903. bool forResponseFile,
  904. const std::string& config)
  905. {
  906. if(lang.empty())
  907. {
  908. return "";
  909. }
  910. OutputFormat shellFormat = forResponseFile? RESPONSE : SHELL;
  911. std::ostringstream includeFlags;
  912. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  913. flagVar += lang;
  914. const char* includeFlag =
  915. this->Makefile->GetSafeDefinition(flagVar);
  916. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  917. flagVar += lang;
  918. const char* sep = this->Makefile->GetDefinition(flagVar);
  919. bool quotePaths = false;
  920. if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
  921. {
  922. quotePaths = true;
  923. }
  924. bool repeatFlag = true;
  925. // should the include flag be repeated like ie. -IA -IB
  926. if(!sep)
  927. {
  928. sep = " ";
  929. }
  930. else
  931. {
  932. // if there is a separator then the flag is not repeated but is only
  933. // given once i.e. -classpath a:b:c
  934. repeatFlag = false;
  935. }
  936. // Support special system include flag if it is available and the
  937. // normal flag is repeated for each directory.
  938. std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
  939. sysFlagVar += lang;
  940. const char* sysIncludeFlag = 0;
  941. if(repeatFlag)
  942. {
  943. sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar);
  944. }
  945. std::string fwSearchFlagVar = "CMAKE_";
  946. fwSearchFlagVar += lang;
  947. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  948. const char* fwSearchFlag =
  949. this->Makefile->GetDefinition(fwSearchFlagVar);
  950. std::string sysFwSearchFlagVar = "CMAKE_";
  951. sysFwSearchFlagVar += lang;
  952. sysFwSearchFlagVar += "_SYSTEM_FRAMEWORK_SEARCH_FLAG";
  953. const char* sysFwSearchFlag =
  954. this->Makefile->GetDefinition(sysFwSearchFlagVar);
  955. bool flagUsed = false;
  956. std::set<std::string> emitted;
  957. #ifdef __APPLE__
  958. emitted.insert("/System/Library/Frameworks");
  959. #endif
  960. std::vector<std::string>::const_iterator i;
  961. for(i = includes.begin(); i != includes.end(); ++i)
  962. {
  963. if(fwSearchFlag && *fwSearchFlag && this->Makefile->IsOn("APPLE")
  964. && cmSystemTools::IsPathToFramework(i->c_str()))
  965. {
  966. std::string frameworkDir = *i;
  967. frameworkDir += "/../";
  968. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  969. if(emitted.insert(frameworkDir).second)
  970. {
  971. if (sysFwSearchFlag && target &&
  972. target->IsSystemIncludeDirectory(*i, config))
  973. {
  974. includeFlags << sysFwSearchFlag;
  975. }
  976. else
  977. {
  978. includeFlags << fwSearchFlag;
  979. }
  980. includeFlags << this->ConvertToOutputFormat(frameworkDir, shellFormat)
  981. << " ";
  982. }
  983. continue;
  984. }
  985. if(!flagUsed || repeatFlag)
  986. {
  987. if(sysIncludeFlag && target &&
  988. target->IsSystemIncludeDirectory(*i, config))
  989. {
  990. includeFlags << sysIncludeFlag;
  991. }
  992. else
  993. {
  994. includeFlags << includeFlag;
  995. }
  996. flagUsed = true;
  997. }
  998. std::string includePath =
  999. this->ConvertToIncludeReference(*i, shellFormat, forceFullPaths);
  1000. if(quotePaths && !includePath.empty() && includePath[0] != '\"')
  1001. {
  1002. includeFlags << "\"";
  1003. }
  1004. includeFlags << includePath;
  1005. if(quotePaths && !includePath.empty() && includePath[0] != '\"')
  1006. {
  1007. includeFlags << "\"";
  1008. }
  1009. includeFlags << sep;
  1010. }
  1011. std::string flags = includeFlags.str();
  1012. // remove trailing separators
  1013. if((sep[0] != ' ') && !flags.empty() && flags[flags.size()-1] == sep[0])
  1014. {
  1015. flags[flags.size()-1] = ' ';
  1016. }
  1017. return flags;
  1018. }
  1019. //----------------------------------------------------------------------------
  1020. void cmLocalGenerator::AddCompileDefinitions(std::set<std::string>& defines,
  1021. cmTarget const* target,
  1022. const std::string& config,
  1023. const std::string& lang)
  1024. {
  1025. std::vector<std::string> targetDefines;
  1026. target->GetCompileDefinitions(targetDefines, config, lang);
  1027. this->AppendDefines(defines, targetDefines);
  1028. }
  1029. //----------------------------------------------------------------------------
  1030. void cmLocalGenerator::AddCompileOptions(
  1031. std::string& flags, cmTarget* target,
  1032. const std::string& lang, const std::string& config
  1033. )
  1034. {
  1035. std::string langFlagRegexVar = std::string("CMAKE_")+lang+"_FLAG_REGEX";
  1036. if(const char* langFlagRegexStr =
  1037. this->Makefile->GetDefinition(langFlagRegexVar))
  1038. {
  1039. // Filter flags acceptable to this language.
  1040. cmsys::RegularExpression r(langFlagRegexStr);
  1041. std::vector<std::string> opts;
  1042. if(const char* targetFlags = target->GetProperty("COMPILE_FLAGS"))
  1043. {
  1044. cmSystemTools::ParseWindowsCommandLine(targetFlags, opts);
  1045. }
  1046. target->GetCompileOptions(opts, config, lang);
  1047. for(std::vector<std::string>::const_iterator i = opts.begin();
  1048. i != opts.end(); ++i)
  1049. {
  1050. if(r.find(i->c_str()))
  1051. {
  1052. // (Re-)Escape this flag. COMPILE_FLAGS were already parsed
  1053. // as a command line above, and COMPILE_OPTIONS are escaped.
  1054. this->AppendFlagEscape(flags, *i);
  1055. }
  1056. }
  1057. }
  1058. else
  1059. {
  1060. // Use all flags.
  1061. if(const char* targetFlags = target->GetProperty("COMPILE_FLAGS"))
  1062. {
  1063. // COMPILE_FLAGS are not escaped for historical reasons.
  1064. this->AppendFlags(flags, targetFlags);
  1065. }
  1066. std::vector<std::string> opts;
  1067. target->GetCompileOptions(opts, config, lang);
  1068. for(std::vector<std::string>::const_iterator i = opts.begin();
  1069. i != opts.end(); ++i)
  1070. {
  1071. // COMPILE_OPTIONS are escaped.
  1072. this->AppendFlagEscape(flags, *i);
  1073. }
  1074. }
  1075. std::vector<std::string> features;
  1076. target->GetCompileFeatures(features, config);
  1077. for(std::vector<std::string>::const_iterator it = features.begin();
  1078. it != features.end(); ++it)
  1079. {
  1080. if (!this->Makefile->AddRequiredTargetFeature(target, *it))
  1081. {
  1082. return;
  1083. }
  1084. }
  1085. for(std::map<std::string, std::string>::const_iterator it
  1086. = target->GetMaxLanguageStandards().begin();
  1087. it != target->GetMaxLanguageStandards().end(); ++it)
  1088. {
  1089. const char* standard = target->GetProperty(it->first + "_STANDARD");
  1090. if(!standard)
  1091. {
  1092. continue;
  1093. }
  1094. if (this->Makefile->IsLaterStandard(it->first, standard, it->second))
  1095. {
  1096. std::ostringstream e;
  1097. e << "The COMPILE_FEATURES property of target \""
  1098. << target->GetName() << "\" was evaluated when computing the link "
  1099. "implementation, and the \"" << it->first << "_STANDARD\" was \""
  1100. << it->second << "\" for that computation. Computing the "
  1101. "COMPILE_FEATURES based on the link implementation resulted in a "
  1102. "higher \"" << it->first << "_STANDARD\" \"" << standard << "\". "
  1103. "This is not permitted. The COMPILE_FEATURES may not both depend on "
  1104. "and be depended on by the link implementation." << std::endl;
  1105. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  1106. return;
  1107. }
  1108. }
  1109. this->AddCompilerRequirementFlag(flags, target, lang);
  1110. }
  1111. //----------------------------------------------------------------------------
  1112. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  1113. cmGeneratorTarget* target,
  1114. const std::string& lang,
  1115. const std::string& config,
  1116. bool stripImplicitInclDirs
  1117. ) const
  1118. {
  1119. // Need to decide whether to automatically include the source and
  1120. // binary directories at the beginning of the include path.
  1121. bool includeSourceDir = false;
  1122. bool includeBinaryDir = false;
  1123. // When automatic include directories are requested for a build then
  1124. // include the source and binary directories at the beginning of the
  1125. // include path to approximate include file behavior for an
  1126. // in-source build. This does not account for the case of a source
  1127. // file in a subdirectory of the current source directory but we
  1128. // cannot fix this because not all native build tools support
  1129. // per-source-file include paths.
  1130. if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
  1131. {
  1132. includeSourceDir = true;
  1133. includeBinaryDir = true;
  1134. }
  1135. // Do not repeat an include path.
  1136. std::set<std::string> emitted;
  1137. // Store the automatic include paths.
  1138. if(includeBinaryDir)
  1139. {
  1140. std::string binDir = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  1141. if(emitted.find(binDir) == emitted.end())
  1142. {
  1143. dirs.push_back(binDir);
  1144. emitted.insert(binDir);
  1145. }
  1146. }
  1147. if(includeSourceDir)
  1148. {
  1149. std::string srcDir = this->StateSnapshot.GetDirectory().GetCurrentSource();
  1150. if(emitted.find(srcDir) == emitted.end())
  1151. {
  1152. dirs.push_back(srcDir);
  1153. emitted.insert(srcDir);
  1154. }
  1155. }
  1156. if(!target)
  1157. {
  1158. return;
  1159. }
  1160. std::string rootPath = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  1161. std::vector<std::string> implicitDirs;
  1162. // Load implicit include directories for this language.
  1163. std::string impDirVar = "CMAKE_";
  1164. impDirVar += lang;
  1165. impDirVar += "_IMPLICIT_INCLUDE_DIRECTORIES";
  1166. if(const char* value = this->Makefile->GetDefinition(impDirVar))
  1167. {
  1168. std::vector<std::string> impDirVec;
  1169. cmSystemTools::ExpandListArgument(value, impDirVec);
  1170. for(std::vector<std::string>::const_iterator i = impDirVec.begin();
  1171. i != impDirVec.end(); ++i)
  1172. {
  1173. std::string d = rootPath + *i;
  1174. cmSystemTools::ConvertToUnixSlashes(d);
  1175. emitted.insert(d);
  1176. if (!stripImplicitInclDirs)
  1177. {
  1178. implicitDirs.push_back(*i);
  1179. }
  1180. }
  1181. }
  1182. // Get the target-specific include directories.
  1183. std::vector<std::string> includes;
  1184. includes = target->GetIncludeDirectories(config, lang);
  1185. // Support putting all the in-project include directories first if
  1186. // it is requested by the project.
  1187. if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
  1188. {
  1189. const char* topSourceDir = this->GetState()->GetSourceDirectory();
  1190. const char* topBinaryDir = this->GetState()->GetBinaryDirectory();
  1191. for(std::vector<std::string>::const_iterator i = includes.begin();
  1192. i != includes.end(); ++i)
  1193. {
  1194. // Emit this directory only if it is a subdirectory of the
  1195. // top-level source or binary tree.
  1196. if(cmSystemTools::ComparePath(*i, topSourceDir) ||
  1197. cmSystemTools::ComparePath(*i, topBinaryDir) ||
  1198. cmSystemTools::IsSubDirectory(*i, topSourceDir) ||
  1199. cmSystemTools::IsSubDirectory(*i, topBinaryDir))
  1200. {
  1201. if(emitted.insert(*i).second)
  1202. {
  1203. dirs.push_back(*i);
  1204. }
  1205. }
  1206. }
  1207. }
  1208. // Construct the final ordered include directory list.
  1209. for(std::vector<std::string>::const_iterator i = includes.begin();
  1210. i != includes.end(); ++i)
  1211. {
  1212. if(emitted.insert(*i).second)
  1213. {
  1214. dirs.push_back(*i);
  1215. }
  1216. }
  1217. for(std::vector<std::string>::const_iterator i = implicitDirs.begin();
  1218. i != implicitDirs.end(); ++i)
  1219. {
  1220. if(std::find(includes.begin(), includes.end(), *i) != includes.end())
  1221. {
  1222. dirs.push_back(*i);
  1223. }
  1224. }
  1225. }
  1226. void cmLocalGenerator::GetStaticLibraryFlags(std::string& flags,
  1227. std::string const& config,
  1228. cmTarget* target)
  1229. {
  1230. this->AppendFlags(flags,
  1231. this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS"));
  1232. if(!config.empty())
  1233. {
  1234. std::string name = "CMAKE_STATIC_LINKER_FLAGS_" + config;
  1235. this->AppendFlags(flags, this->Makefile->GetSafeDefinition(name));
  1236. }
  1237. this->AppendFlags(flags, target->GetProperty("STATIC_LIBRARY_FLAGS"));
  1238. if(!config.empty())
  1239. {
  1240. std::string name = "STATIC_LIBRARY_FLAGS_" + config;
  1241. this->AppendFlags(flags, target->GetProperty(name));
  1242. }
  1243. }
  1244. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1245. std::string& flags,
  1246. std::string& linkFlags,
  1247. std::string& frameworkPath,
  1248. std::string& linkPath,
  1249. cmGeneratorTarget* target,
  1250. bool useWatcomQuote)
  1251. {
  1252. std::string buildType =
  1253. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1254. buildType = cmSystemTools::UpperCase(buildType);
  1255. const char* libraryLinkVariable =
  1256. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1257. switch(target->GetType())
  1258. {
  1259. case cmTarget::STATIC_LIBRARY:
  1260. this->GetStaticLibraryFlags(linkFlags, buildType, target->Target);
  1261. break;
  1262. case cmTarget::MODULE_LIBRARY:
  1263. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1264. case cmTarget::SHARED_LIBRARY:
  1265. {
  1266. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  1267. linkFlags += " ";
  1268. if(!buildType.empty())
  1269. {
  1270. std::string build = libraryLinkVariable;
  1271. build += "_";
  1272. build += buildType;
  1273. linkFlags += this->Makefile->GetSafeDefinition(build);
  1274. linkFlags += " ";
  1275. }
  1276. if(this->Makefile->IsOn("WIN32") &&
  1277. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  1278. {
  1279. std::vector<cmSourceFile*> sources;
  1280. target->GetSourceFiles(sources, buildType);
  1281. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1282. i != sources.end(); ++i)
  1283. {
  1284. cmSourceFile* sf = *i;
  1285. if(sf->GetExtension() == "def")
  1286. {
  1287. linkFlags +=
  1288. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1289. linkFlags += this->Convert(sf->GetFullPath(),
  1290. FULL, SHELL);
  1291. linkFlags += " ";
  1292. }
  1293. }
  1294. }
  1295. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1296. if(targetLinkFlags)
  1297. {
  1298. linkFlags += targetLinkFlags;
  1299. linkFlags += " ";
  1300. }
  1301. if(!buildType.empty())
  1302. {
  1303. std::string configLinkFlags = "LINK_FLAGS_";
  1304. configLinkFlags += buildType;
  1305. targetLinkFlags = target->GetProperty(configLinkFlags);
  1306. if(targetLinkFlags)
  1307. {
  1308. linkFlags += targetLinkFlags;
  1309. linkFlags += " ";
  1310. }
  1311. }
  1312. this->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1313. *target, false, false, useWatcomQuote);
  1314. }
  1315. break;
  1316. case cmTarget::EXECUTABLE:
  1317. {
  1318. linkFlags +=
  1319. this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1320. linkFlags += " ";
  1321. if(!buildType.empty())
  1322. {
  1323. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1324. build += buildType;
  1325. linkFlags += this->Makefile->GetSafeDefinition(build);
  1326. linkFlags += " ";
  1327. }
  1328. std::string linkLanguage = target->GetLinkerLanguage(buildType);
  1329. if(linkLanguage.empty())
  1330. {
  1331. cmSystemTools::Error
  1332. ("CMake can not determine linker language for target: ",
  1333. target->Target->GetName().c_str());
  1334. return;
  1335. }
  1336. this->AddLanguageFlags(flags, linkLanguage, buildType);
  1337. this->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1338. *target, false, false, useWatcomQuote);
  1339. if(cmSystemTools::IsOn
  1340. (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1341. {
  1342. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
  1343. + linkLanguage + std::string("_FLAGS");
  1344. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar);
  1345. linkFlags += " ";
  1346. }
  1347. if ( target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1348. {
  1349. linkFlags +=
  1350. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1351. linkFlags += " ";
  1352. }
  1353. else
  1354. {
  1355. linkFlags +=
  1356. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1357. linkFlags += " ";
  1358. }
  1359. if (target->Target->IsExecutableWithExports())
  1360. {
  1361. std::string exportFlagVar = "CMAKE_EXE_EXPORTS_";
  1362. exportFlagVar += linkLanguage;
  1363. exportFlagVar += "_FLAG";
  1364. linkFlags +=
  1365. this->Makefile->GetSafeDefinition(exportFlagVar);
  1366. linkFlags += " ";
  1367. }
  1368. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1369. if(targetLinkFlags)
  1370. {
  1371. linkFlags += targetLinkFlags;
  1372. linkFlags += " ";
  1373. }
  1374. if(!buildType.empty())
  1375. {
  1376. std::string configLinkFlags = "LINK_FLAGS_";
  1377. configLinkFlags += buildType;
  1378. targetLinkFlags = target->GetProperty(configLinkFlags);
  1379. if(targetLinkFlags)
  1380. {
  1381. linkFlags += targetLinkFlags;
  1382. linkFlags += " ";
  1383. }
  1384. }
  1385. }
  1386. break;
  1387. default:
  1388. break;
  1389. }
  1390. }
  1391. std::string cmLocalGenerator::ConvertToLinkReference(std::string const& lib,
  1392. OutputFormat format)
  1393. {
  1394. #if defined(_WIN32) && !defined(__CYGWIN__)
  1395. // Work-ardound command line parsing limitations in MSVC 6.0
  1396. if(this->Makefile->IsOn("MSVC60"))
  1397. {
  1398. // Search for the last space.
  1399. std::string::size_type pos = lib.rfind(' ');
  1400. if(pos != lib.npos)
  1401. {
  1402. // Find the slash after the last space, if any.
  1403. pos = lib.find('/', pos);
  1404. // Convert the portion of the path with a space to a short path.
  1405. std::string sp;
  1406. if(cmSystemTools::GetShortPath(lib.substr(0, pos).c_str(), sp))
  1407. {
  1408. // Append the rest of the path with no space.
  1409. sp += lib.substr(pos);
  1410. // Convert to an output path.
  1411. return this->Convert(sp.c_str(), NONE, format);
  1412. }
  1413. }
  1414. }
  1415. #endif
  1416. // Normal behavior.
  1417. return this->Convert(lib, START_OUTPUT, format);
  1418. }
  1419. /**
  1420. * Output the linking rules on a command line. For executables,
  1421. * targetLibrary should be a NULL pointer. For libraries, it should point
  1422. * to the name of the library. This will not link a library against itself.
  1423. */
  1424. void cmLocalGenerator::OutputLinkLibraries(std::string& linkLibraries,
  1425. std::string& frameworkPath,
  1426. std::string& linkPath,
  1427. cmGeneratorTarget &tgt,
  1428. bool relink,
  1429. bool forResponseFile,
  1430. bool useWatcomQuote)
  1431. {
  1432. OutputFormat shellFormat = (forResponseFile) ? RESPONSE :
  1433. ((useWatcomQuote) ? WATCOMQUOTE : SHELL);
  1434. bool escapeAllowMakeVars = !forResponseFile;
  1435. std::ostringstream fout;
  1436. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1437. cmComputeLinkInformation* pcli = tgt.GetLinkInformation(config);
  1438. if(!pcli)
  1439. {
  1440. return;
  1441. }
  1442. cmComputeLinkInformation& cli = *pcli;
  1443. // Collect library linking flags command line options.
  1444. std::string linkLibs;
  1445. std::string linkLanguage = cli.GetLinkLanguage();
  1446. std::string libPathFlag =
  1447. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1448. std::string libPathTerminator =
  1449. this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1450. // Flags to link an executable to shared libraries.
  1451. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1452. linkFlagsVar += linkLanguage;
  1453. linkFlagsVar += "_FLAGS";
  1454. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1455. {
  1456. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar);
  1457. linkLibs += " ";
  1458. }
  1459. // Append the framework search path flags.
  1460. std::string fwSearchFlagVar = "CMAKE_";
  1461. fwSearchFlagVar += linkLanguage;
  1462. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  1463. const char* fwSearchFlag =
  1464. this->Makefile->GetDefinition(fwSearchFlagVar);
  1465. if(fwSearchFlag && *fwSearchFlag)
  1466. {
  1467. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  1468. for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  1469. fdi != fwDirs.end(); ++fdi)
  1470. {
  1471. frameworkPath += fwSearchFlag;
  1472. frameworkPath += this->Convert(*fdi, NONE, shellFormat);
  1473. frameworkPath += " ";
  1474. }
  1475. }
  1476. // Append the library search path flags.
  1477. std::vector<std::string> const& libDirs = cli.GetDirectories();
  1478. for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
  1479. libDir != libDirs.end(); ++libDir)
  1480. {
  1481. std::string libpath = this->ConvertToOutputForExisting(*libDir,
  1482. START_OUTPUT,
  1483. shellFormat);
  1484. linkPath += " " + libPathFlag;
  1485. linkPath += libpath;
  1486. linkPath += libPathTerminator;
  1487. linkPath += " ";
  1488. }
  1489. // Append the link items.
  1490. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1491. ItemVector const& items = cli.GetItems();
  1492. for(ItemVector::const_iterator li = items.begin(); li != items.end(); ++li)
  1493. {
  1494. if(li->Target && li->Target->GetType() == cmTarget::INTERFACE_LIBRARY)
  1495. {
  1496. continue;
  1497. }
  1498. if(li->IsPath)
  1499. {
  1500. linkLibs += this->ConvertToLinkReference(li->Value, shellFormat);
  1501. }
  1502. else
  1503. {
  1504. linkLibs += li->Value;
  1505. }
  1506. linkLibs += " ";
  1507. }
  1508. // Write the library flags to the build rule.
  1509. fout << linkLibs;
  1510. // Check what kind of rpath flags to use.
  1511. if(cli.GetRuntimeSep().empty())
  1512. {
  1513. // Each rpath entry gets its own option ("-R a -R b -R c")
  1514. std::vector<std::string> runtimeDirs;
  1515. cli.GetRPath(runtimeDirs, relink);
  1516. std::string rpath;
  1517. for(std::vector<std::string>::iterator ri = runtimeDirs.begin();
  1518. ri != runtimeDirs.end(); ++ri)
  1519. {
  1520. rpath += cli.GetRuntimeFlag();
  1521. rpath += this->Convert(*ri, NONE, shellFormat);
  1522. rpath += " ";
  1523. }
  1524. fout << rpath;
  1525. }
  1526. else
  1527. {
  1528. // All rpath entries are combined ("-Wl,-rpath,a:b:c").
  1529. std::string rpath = cli.GetRPathString(relink);
  1530. // Store the rpath option in the stream.
  1531. if(!rpath.empty())
  1532. {
  1533. fout << cli.GetRuntimeFlag();
  1534. fout << this->EscapeForShell(rpath, escapeAllowMakeVars);
  1535. fout << " ";
  1536. }
  1537. }
  1538. // Add the linker runtime search path if any.
  1539. std::string rpath_link = cli.GetRPathLinkString();
  1540. if(!cli.GetRPathLinkFlag().empty() && !rpath_link.empty())
  1541. {
  1542. fout << cli.GetRPathLinkFlag();
  1543. fout << this->EscapeForShell(rpath_link, escapeAllowMakeVars);
  1544. fout << " ";
  1545. }
  1546. // Add standard libraries for this language.
  1547. std::string standardLibsVar = "CMAKE_";
  1548. standardLibsVar += cli.GetLinkLanguage();
  1549. standardLibsVar += "_STANDARD_LIBRARIES";
  1550. if(const char* stdLibs =
  1551. this->Makefile->GetDefinition(standardLibsVar))
  1552. {
  1553. fout << stdLibs << " ";
  1554. }
  1555. linkLibraries = fout.str();
  1556. }
  1557. //----------------------------------------------------------------------------
  1558. void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
  1559. cmGeneratorTarget const* target,
  1560. const std::string& lang,
  1561. const std::string& config)
  1562. {
  1563. // Only add Mac OS X specific flags on Darwin platforms (OSX and iphone):
  1564. if(!this->Makefile->IsOn("APPLE"))
  1565. {
  1566. return;
  1567. }
  1568. if(this->EmitUniversalBinaryFlags)
  1569. {
  1570. std::vector<std::string> archs;
  1571. target->GetAppleArchs(config, archs);
  1572. const char* sysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1573. if(sysroot && sysroot[0] == '/' && !sysroot[1])
  1574. { sysroot = 0; }
  1575. std::string sysrootFlagVar =
  1576. std::string("CMAKE_") + lang + "_SYSROOT_FLAG";
  1577. const char* sysrootFlag =
  1578. this->Makefile->GetDefinition(sysrootFlagVar);
  1579. const char* deploymentTarget =
  1580. this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  1581. std::string deploymentTargetFlagVar =
  1582. std::string("CMAKE_") + lang + "_OSX_DEPLOYMENT_TARGET_FLAG";
  1583. const char* deploymentTargetFlag =
  1584. this->Makefile->GetDefinition(deploymentTargetFlagVar);
  1585. if(!archs.empty() && !lang.empty() && (lang[0] =='C' || lang[0] == 'F'))
  1586. {
  1587. for(std::vector<std::string>::iterator i = archs.begin();
  1588. i != archs.end(); ++i)
  1589. {
  1590. flags += " -arch ";
  1591. flags += *i;
  1592. }
  1593. }
  1594. if(sysrootFlag && *sysrootFlag && sysroot && *sysroot)
  1595. {
  1596. flags += " ";
  1597. flags += sysrootFlag;
  1598. flags += " ";
  1599. flags += this->Convert(sysroot, NONE, SHELL);
  1600. }
  1601. if (deploymentTargetFlag && *deploymentTargetFlag &&
  1602. deploymentTarget && *deploymentTarget)
  1603. {
  1604. flags += " ";
  1605. flags += deploymentTargetFlag;
  1606. flags += deploymentTarget;
  1607. }
  1608. }
  1609. }
  1610. //----------------------------------------------------------------------------
  1611. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1612. const std::string& lang,
  1613. const std::string& config)
  1614. {
  1615. // Add language-specific flags.
  1616. std::string flagsVar = "CMAKE_";
  1617. flagsVar += lang;
  1618. flagsVar += "_FLAGS";
  1619. this->AddConfigVariableFlags(flags, flagsVar, config);
  1620. }
  1621. //----------------------------------------------------------------------------
  1622. bool cmLocalGenerator::GetRealDependency(const std::string& inName,
  1623. const std::string& config,
  1624. std::string& dep)
  1625. {
  1626. // Older CMake code may specify the dependency using the target
  1627. // output file rather than the target name. Such code would have
  1628. // been written before there was support for target properties that
  1629. // modify the name so stripping down to just the file name should
  1630. // produce the target name in this case.
  1631. std::string name = cmSystemTools::GetFilenameName(inName);
  1632. // If the input name is the empty string, there is no real
  1633. // dependency. Short-circuit the other checks:
  1634. if(name == "")
  1635. {
  1636. return false;
  1637. }
  1638. if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
  1639. {
  1640. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1641. }
  1642. // Look for a CMake target with the given name.
  1643. if(cmGeneratorTarget* target =
  1644. this->Makefile->FindGeneratorTargetToUse(name))
  1645. {
  1646. // make sure it is not just a coincidence that the target name
  1647. // found is part of the inName
  1648. if(cmSystemTools::FileIsFullPath(inName.c_str()))
  1649. {
  1650. std::string tLocation;
  1651. if(target->GetType() >= cmTarget::EXECUTABLE &&
  1652. target->GetType() <= cmTarget::MODULE_LIBRARY)
  1653. {
  1654. tLocation = target->GetLocation(config);
  1655. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1656. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  1657. }
  1658. std::string depLocation = cmSystemTools::GetFilenamePath(
  1659. std::string(inName));
  1660. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  1661. if(depLocation != tLocation)
  1662. {
  1663. // it is a full path to a depend that has the same name
  1664. // as a target but is in a different location so do not use
  1665. // the target as the depend
  1666. dep = inName;
  1667. return true;
  1668. }
  1669. }
  1670. switch (target->GetType())
  1671. {
  1672. case cmTarget::EXECUTABLE:
  1673. case cmTarget::STATIC_LIBRARY:
  1674. case cmTarget::SHARED_LIBRARY:
  1675. case cmTarget::MODULE_LIBRARY:
  1676. case cmTarget::UNKNOWN_LIBRARY:
  1677. dep = target->GetLocation(config);
  1678. return true;
  1679. case cmTarget::OBJECT_LIBRARY:
  1680. // An object library has no single file on which to depend.
  1681. // This was listed to get the target-level dependency.
  1682. return false;
  1683. case cmTarget::INTERFACE_LIBRARY:
  1684. // An interface library has no file on which to depend.
  1685. // This was listed to get the target-level dependency.
  1686. return false;
  1687. case cmTarget::UTILITY:
  1688. case cmTarget::GLOBAL_TARGET:
  1689. // A utility target has no file on which to depend. This was listed
  1690. // only to get the target-level dependency.
  1691. return false;
  1692. }
  1693. }
  1694. // The name was not that of a CMake target. It must name a file.
  1695. if(cmSystemTools::FileIsFullPath(inName.c_str()))
  1696. {
  1697. // This is a full path. Return it as given.
  1698. dep = inName;
  1699. return true;
  1700. }
  1701. // Check for a source file in this directory that matches the
  1702. // dependency.
  1703. if(cmSourceFile* sf = this->Makefile->GetSource(inName))
  1704. {
  1705. dep = sf->GetFullPath();
  1706. return true;
  1707. }
  1708. // Treat the name as relative to the source directory in which it
  1709. // was given.
  1710. dep = this->StateSnapshot.GetDirectory().GetCurrentSource();
  1711. dep += "/";
  1712. dep += inName;
  1713. return true;
  1714. }
  1715. //----------------------------------------------------------------------------
  1716. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1717. const std::string& lang,
  1718. bool shared)
  1719. {
  1720. std::string flagsVar;
  1721. // Add flags for dealing with shared libraries for this language.
  1722. if(shared)
  1723. {
  1724. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1725. flagsVar += lang;
  1726. flagsVar += "_FLAGS";
  1727. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1728. }
  1729. }
  1730. //----------------------------------------------------------------------------
  1731. void cmLocalGenerator::
  1732. AddCompilerRequirementFlag(std::string &flags, cmTarget const* target,
  1733. const std::string& lang)
  1734. {
  1735. if (lang.empty())
  1736. {
  1737. return;
  1738. }
  1739. const char* defaultStd
  1740. = this->Makefile->GetDefinition("CMAKE_" + lang + "_STANDARD_DEFAULT");
  1741. if (!defaultStd || !*defaultStd)
  1742. {
  1743. // This compiler has no notion of language standard levels.
  1744. return;
  1745. }
  1746. std::string stdProp = lang + "_STANDARD";
  1747. const char *standardProp = target->GetProperty(stdProp);
  1748. if (!standardProp)
  1749. {
  1750. return;
  1751. }
  1752. std::string extProp = lang + "_EXTENSIONS";
  1753. std::string type = "EXTENSION";
  1754. bool ext = true;
  1755. if (const char* extPropValue = target->GetProperty(extProp))
  1756. {
  1757. if (cmSystemTools::IsOff(extPropValue))
  1758. {
  1759. ext = false;
  1760. type = "STANDARD";
  1761. }
  1762. }
  1763. if (target->GetPropertyAsBool(lang + "_STANDARD_REQUIRED"))
  1764. {
  1765. std::string option_flag =
  1766. "CMAKE_" + lang + standardProp
  1767. + "_" + type + "_COMPILE_OPTION";
  1768. const char *opt = target->GetMakefile()->GetDefinition(option_flag);
  1769. if (!opt)
  1770. {
  1771. std::ostringstream e;
  1772. e << "Target \"" << target->GetName() << "\" requires the language "
  1773. "dialect \"" << lang << standardProp << "\" "
  1774. << (ext ? "(with compiler extensions)" : "") << ", but CMake "
  1775. "does not know the compile flags to use to enable it.";
  1776. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  1777. }
  1778. else
  1779. {
  1780. this->AppendFlagEscape(flags, opt);
  1781. }
  1782. return;
  1783. }
  1784. static std::map<std::string, std::vector<std::string> > langStdMap;
  1785. if (langStdMap.empty())
  1786. {
  1787. // Maintain sorted order, most recent first.
  1788. langStdMap["CXX"].push_back("14");
  1789. langStdMap["CXX"].push_back("11");
  1790. langStdMap["CXX"].push_back("98");
  1791. langStdMap["C"].push_back("11");
  1792. langStdMap["C"].push_back("99");
  1793. langStdMap["C"].push_back("90");
  1794. }
  1795. std::string standard(standardProp);
  1796. std::vector<std::string>& stds = langStdMap[lang];
  1797. std::vector<std::string>::const_iterator stdIt =
  1798. std::find(stds.begin(), stds.end(), standard);
  1799. if (stdIt == stds.end())
  1800. {
  1801. std::string e =
  1802. lang + "_STANDARD is set to invalid value '" + standard + "'";
  1803. this->GetGlobalGenerator()->GetCMakeInstance()
  1804. ->IssueMessage(cmake::FATAL_ERROR, e, target->GetBacktrace());
  1805. return;
  1806. }
  1807. std::vector<std::string>::const_iterator defaultStdIt =
  1808. std::find(stds.begin(), stds.end(), defaultStd);
  1809. if (defaultStdIt == stds.end())
  1810. {
  1811. std::string e =
  1812. "CMAKE_" + lang + "_STANDARD_DEFAULT is set to invalid value '" +
  1813. std::string(defaultStd) + "'";
  1814. this->IssueMessage(cmake::INTERNAL_ERROR, e);
  1815. return;
  1816. }
  1817. // Greater or equal because the standards are stored in
  1818. // backward chronological order.
  1819. if (stdIt >= defaultStdIt)
  1820. {
  1821. std::string option_flag =
  1822. "CMAKE_" + lang + *stdIt
  1823. + "_" + type + "_COMPILE_OPTION";
  1824. const char *opt =
  1825. target->GetMakefile()->GetRequiredDefinition(option_flag);
  1826. this->AppendFlagEscape(flags, opt);
  1827. return;
  1828. }
  1829. for ( ; stdIt < defaultStdIt; ++stdIt)
  1830. {
  1831. std::string option_flag =
  1832. "CMAKE_" + lang + *stdIt
  1833. + "_" + type + "_COMPILE_OPTION";
  1834. if (const char *opt = target->GetMakefile()->GetDefinition(option_flag))
  1835. {
  1836. this->AppendFlagEscape(flags, opt);
  1837. return;
  1838. }
  1839. }
  1840. }
  1841. static void AddVisibilityCompileOption(std::string &flags,
  1842. cmTarget const* target,
  1843. cmLocalGenerator *lg,
  1844. const std::string& lang,
  1845. std::string* warnCMP0063)
  1846. {
  1847. std::string l(lang);
  1848. std::string compileOption = "CMAKE_" + l + "_COMPILE_OPTIONS_VISIBILITY";
  1849. const char *opt = lg->GetMakefile()->GetDefinition(compileOption);
  1850. if (!opt)
  1851. {
  1852. return;
  1853. }
  1854. std::string flagDefine = l + "_VISIBILITY_PRESET";
  1855. const char *prop = target->GetProperty(flagDefine);
  1856. if (!prop)
  1857. {
  1858. return;
  1859. }
  1860. if (warnCMP0063)
  1861. {
  1862. *warnCMP0063 += " " + flagDefine + "\n";
  1863. return;
  1864. }
  1865. if (strcmp(prop, "hidden") != 0
  1866. && strcmp(prop, "default") != 0
  1867. && strcmp(prop, "protected") != 0
  1868. && strcmp(prop, "internal") != 0 )
  1869. {
  1870. std::ostringstream e;
  1871. e << "Target " << target->GetName() << " uses unsupported value \""
  1872. << prop << "\" for " << flagDefine << ".";
  1873. cmSystemTools::Error(e.str().c_str());
  1874. return;
  1875. }
  1876. std::string option = std::string(opt) + prop;
  1877. lg->AppendFlags(flags, option);
  1878. }
  1879. static void AddInlineVisibilityCompileOption(std::string &flags,
  1880. cmTarget const* target,
  1881. cmLocalGenerator *lg,
  1882. std::string* warnCMP0063)
  1883. {
  1884. std::string compileOption
  1885. = "CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN";
  1886. const char *opt = lg->GetMakefile()->GetDefinition(compileOption);
  1887. if (!opt)
  1888. {
  1889. return;
  1890. }
  1891. bool prop = target->GetPropertyAsBool("VISIBILITY_INLINES_HIDDEN");
  1892. if (!prop)
  1893. {
  1894. return;
  1895. }
  1896. if (warnCMP0063)
  1897. {
  1898. *warnCMP0063 += " VISIBILITY_INLINES_HIDDEN\n";
  1899. return;
  1900. }
  1901. lg->AppendFlags(flags, opt);
  1902. }
  1903. //----------------------------------------------------------------------------
  1904. void cmLocalGenerator
  1905. ::AddVisibilityPresetFlags(std::string &flags, cmTarget const* target,
  1906. const std::string& lang)
  1907. {
  1908. if (lang.empty())
  1909. {
  1910. return;
  1911. }
  1912. std::string warnCMP0063;
  1913. std::string *pWarnCMP0063 = 0;
  1914. if (target->GetType() != cmTarget::SHARED_LIBRARY &&
  1915. target->GetType() != cmTarget::MODULE_LIBRARY &&
  1916. !target->IsExecutableWithExports())
  1917. {
  1918. switch (target->GetPolicyStatusCMP0063())
  1919. {
  1920. case cmPolicies::OLD:
  1921. return;
  1922. case cmPolicies::WARN:
  1923. pWarnCMP0063 = &warnCMP0063;
  1924. break;
  1925. default:
  1926. break;
  1927. }
  1928. }
  1929. AddVisibilityCompileOption(flags, target, this, lang, pWarnCMP0063);
  1930. if(lang == "CXX")
  1931. {
  1932. AddInlineVisibilityCompileOption(flags, target, this, pWarnCMP0063);
  1933. }
  1934. if (!warnCMP0063.empty() &&
  1935. this->WarnCMP0063.insert(target).second)
  1936. {
  1937. std::ostringstream w;
  1938. w <<
  1939. cmPolicies::GetPolicyWarning(cmPolicies::CMP0063) << "\n"
  1940. "Target \"" << target->GetName() << "\" of "
  1941. "type \"" << cmTarget::GetTargetTypeName(target->GetType()) << "\" "
  1942. "has the following visibility properties set for " << lang << ":\n" <<
  1943. warnCMP0063 <<
  1944. "For compatibility CMake is not honoring them for this target.";
  1945. target->GetMakefile()->GetCMakeInstance()
  1946. ->IssueMessage(cmake::AUTHOR_WARNING, w.str(), target->GetBacktrace());
  1947. }
  1948. }
  1949. //----------------------------------------------------------------------------
  1950. void cmLocalGenerator::AddCMP0018Flags(std::string &flags,
  1951. cmTarget const* target,
  1952. std::string const& lang,
  1953. const std::string& config)
  1954. {
  1955. int targetType = target->GetType();
  1956. bool shared = ((targetType == cmTarget::SHARED_LIBRARY) ||
  1957. (targetType == cmTarget::MODULE_LIBRARY));
  1958. if (this->GetShouldUseOldFlags(shared, lang))
  1959. {
  1960. this->AddSharedFlags(flags, lang, shared);
  1961. }
  1962. else
  1963. {
  1964. if (target->GetType() == cmTarget::OBJECT_LIBRARY)
  1965. {
  1966. if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE"))
  1967. {
  1968. this->AddPositionIndependentFlags(flags, lang, targetType);
  1969. }
  1970. return;
  1971. }
  1972. cmGeneratorTarget* gtgt =
  1973. this->GlobalGenerator->GetGeneratorTarget(target);
  1974. if (gtgt->GetLinkInterfaceDependentBoolProperty(
  1975. "POSITION_INDEPENDENT_CODE",
  1976. config))
  1977. {
  1978. this->AddPositionIndependentFlags(flags, lang, targetType);
  1979. }
  1980. if (shared)
  1981. {
  1982. this->AppendFeatureOptions(flags, lang, "DLL");
  1983. }
  1984. }
  1985. }
  1986. //----------------------------------------------------------------------------
  1987. bool cmLocalGenerator::GetShouldUseOldFlags(bool shared,
  1988. const std::string &lang) const
  1989. {
  1990. std::string originalFlags =
  1991. this->GlobalGenerator->GetSharedLibFlagsForLanguage(lang);
  1992. if (shared)
  1993. {
  1994. std::string flagsVar = "CMAKE_SHARED_LIBRARY_";
  1995. flagsVar += lang;
  1996. flagsVar += "_FLAGS";
  1997. const char* flags =
  1998. this->Makefile->GetSafeDefinition(flagsVar);
  1999. if (flags && flags != originalFlags)
  2000. {
  2001. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0018))
  2002. {
  2003. case cmPolicies::WARN:
  2004. {
  2005. std::ostringstream e;
  2006. e << "Variable " << flagsVar << " has been modified. CMake "
  2007. "will ignore the POSITION_INDEPENDENT_CODE target property for "
  2008. "shared libraries and will use the " << flagsVar << " variable "
  2009. "instead. This may cause errors if the original content of "
  2010. << flagsVar << " was removed.\n"
  2011. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0018);
  2012. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  2013. // fall through to OLD behaviour
  2014. }
  2015. case cmPolicies::OLD:
  2016. return true;
  2017. case cmPolicies::REQUIRED_IF_USED:
  2018. case cmPolicies::REQUIRED_ALWAYS:
  2019. case cmPolicies::NEW:
  2020. return false;
  2021. }
  2022. }
  2023. }
  2024. return false;
  2025. }
  2026. //----------------------------------------------------------------------------
  2027. void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
  2028. std::string const& lang,
  2029. int targetType)
  2030. {
  2031. const char* picFlags = 0;
  2032. if(targetType == cmTarget::EXECUTABLE)
  2033. {
  2034. std::string flagsVar = "CMAKE_";
  2035. flagsVar += lang;
  2036. flagsVar += "_COMPILE_OPTIONS_PIE";
  2037. picFlags = this->Makefile->GetSafeDefinition(flagsVar);
  2038. }
  2039. if (!picFlags)
  2040. {
  2041. std::string flagsVar = "CMAKE_";
  2042. flagsVar += lang;
  2043. flagsVar += "_COMPILE_OPTIONS_PIC";
  2044. picFlags = this->Makefile->GetSafeDefinition(flagsVar);
  2045. }
  2046. if (picFlags)
  2047. {
  2048. std::vector<std::string> options;
  2049. cmSystemTools::ExpandListArgument(picFlags, options);
  2050. for(std::vector<std::string>::const_iterator oi = options.begin();
  2051. oi != options.end(); ++oi)
  2052. {
  2053. this->AppendFlagEscape(flags, *oi);
  2054. }
  2055. }
  2056. }
  2057. //----------------------------------------------------------------------------
  2058. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  2059. const std::string& var,
  2060. const std::string& config)
  2061. {
  2062. // Add the flags from the variable itself.
  2063. std::string flagsVar = var;
  2064. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  2065. // Add the flags from the build-type specific variable.
  2066. if(!config.empty())
  2067. {
  2068. flagsVar += "_";
  2069. flagsVar += cmSystemTools::UpperCase(config);
  2070. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  2071. }
  2072. }
  2073. //----------------------------------------------------------------------------
  2074. void cmLocalGenerator::AppendFlags(std::string& flags,
  2075. const std::string& newFlags)
  2076. {
  2077. if(!newFlags.empty())
  2078. {
  2079. if(!flags.empty())
  2080. {
  2081. flags += " ";
  2082. }
  2083. flags += newFlags;
  2084. }
  2085. }
  2086. //----------------------------------------------------------------------------
  2087. void cmLocalGenerator::AppendFlags(std::string& flags,
  2088. const char* newFlags)
  2089. {
  2090. if(newFlags && *newFlags)
  2091. {
  2092. this->AppendFlags(flags, std::string(newFlags));
  2093. }
  2094. }
  2095. //----------------------------------------------------------------------------
  2096. void cmLocalGenerator::AppendFlagEscape(std::string& flags,
  2097. const std::string& rawFlag)
  2098. {
  2099. this->AppendFlags(flags, this->EscapeForShell(rawFlag));
  2100. }
  2101. //----------------------------------------------------------------------------
  2102. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  2103. const char* defines_list)
  2104. {
  2105. // Short-circuit if there are no definitions.
  2106. if(!defines_list)
  2107. {
  2108. return;
  2109. }
  2110. // Expand the list of definitions.
  2111. std::vector<std::string> defines_vec;
  2112. cmSystemTools::ExpandListArgument(defines_list, defines_vec);
  2113. this->AppendDefines(defines, defines_vec);
  2114. }
  2115. //----------------------------------------------------------------------------
  2116. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  2117. const std::vector<std::string> &defines_vec)
  2118. {
  2119. for(std::vector<std::string>::const_iterator di = defines_vec.begin();
  2120. di != defines_vec.end(); ++di)
  2121. {
  2122. // Skip unsupported definitions.
  2123. if(!this->CheckDefinition(*di))
  2124. {
  2125. continue;
  2126. }
  2127. defines.insert(*di);
  2128. }
  2129. }
  2130. //----------------------------------------------------------------------------
  2131. void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines,
  2132. std::string &definesString,
  2133. const std::string& lang)
  2134. {
  2135. // Lookup the define flag for the current language.
  2136. std::string dflag = "-D";
  2137. if(!lang.empty())
  2138. {
  2139. std::string defineFlagVar = "CMAKE_";
  2140. defineFlagVar += lang;
  2141. defineFlagVar += "_DEFINE_FLAG";
  2142. const char* df = this->Makefile->GetDefinition(defineFlagVar);
  2143. if(df && *df)
  2144. {
  2145. dflag = df;
  2146. }
  2147. }
  2148. std::set<std::string>::const_iterator defineIt = defines.begin();
  2149. const std::set<std::string>::const_iterator defineEnd = defines.end();
  2150. const char* itemSeparator = definesString.empty() ? "" : " ";
  2151. for( ; defineIt != defineEnd; ++defineIt)
  2152. {
  2153. // Append the definition with proper escaping.
  2154. std::string def = dflag;
  2155. if(this->GetState()->UseWatcomWMake())
  2156. {
  2157. // The Watcom compiler does its own command line parsing instead
  2158. // of using the windows shell rules. Definitions are one of
  2159. // -DNAME
  2160. // -DNAME=<cpp-token>
  2161. // -DNAME="c-string with spaces and other characters(?@#$)"
  2162. //
  2163. // Watcom will properly parse each of these cases from the
  2164. // command line without any escapes. However we still have to
  2165. // get the '$' and '#' characters through WMake as '$$' and
  2166. // '$#'.
  2167. for(const char* c = defineIt->c_str(); *c; ++c)
  2168. {
  2169. if(*c == '$' || *c == '#')
  2170. {
  2171. def += '$';
  2172. }
  2173. def += *c;
  2174. }
  2175. }
  2176. else
  2177. {
  2178. // Make the definition appear properly on the command line. Use
  2179. // -DNAME="value" instead of -D"NAME=value" to help VS6 parser.
  2180. std::string::size_type eq = defineIt->find("=");
  2181. def += defineIt->substr(0, eq);
  2182. if(eq != defineIt->npos)
  2183. {
  2184. def += "=";
  2185. def += this->EscapeForShell(defineIt->c_str() + eq + 1, true);
  2186. }
  2187. }
  2188. definesString += itemSeparator;
  2189. itemSeparator = " ";
  2190. definesString += def;
  2191. }
  2192. }
  2193. //----------------------------------------------------------------------------
  2194. void cmLocalGenerator::AppendFeatureOptions(
  2195. std::string& flags, const std::string& lang, const char* feature)
  2196. {
  2197. std::string optVar = "CMAKE_";
  2198. optVar += lang;
  2199. optVar += "_COMPILE_OPTIONS_";
  2200. optVar += feature;
  2201. if(const char* optionList = this->Makefile->GetDefinition(optVar))
  2202. {
  2203. std::vector<std::string> options;
  2204. cmSystemTools::ExpandListArgument(optionList, options);
  2205. for(std::vector<std::string>::const_iterator oi = options.begin();
  2206. oi != options.end(); ++oi)
  2207. {
  2208. this->AppendFlagEscape(flags, *oi);
  2209. }
  2210. }
  2211. }
  2212. //----------------------------------------------------------------------------
  2213. const char* cmLocalGenerator::GetFeature(const std::string& feature,
  2214. const std::string& config)
  2215. {
  2216. std::string featureName = feature;
  2217. // TODO: Define accumulation policy for features (prepend, append, replace).
  2218. // Currently we always replace.
  2219. if(!config.empty())
  2220. {
  2221. featureName += "_";
  2222. featureName += cmSystemTools::UpperCase(config);
  2223. }
  2224. cmState::Snapshot snp = this->StateSnapshot;
  2225. while(snp.IsValid())
  2226. {
  2227. if(const char* value = snp.GetDirectory().GetProperty(featureName))
  2228. {
  2229. return value;
  2230. }
  2231. snp = snp.GetBuildsystemDirectoryParent();
  2232. }
  2233. return 0;
  2234. }
  2235. //----------------------------------------------------------------------------
  2236. std::string
  2237. cmLocalGenerator::ConstructComment(cmCustomCommandGenerator const& ccg,
  2238. const char* default_comment)
  2239. {
  2240. // Check for a comment provided with the command.
  2241. if(ccg.GetComment())
  2242. {
  2243. return ccg.GetComment();
  2244. }
  2245. // Construct a reasonable default comment if possible.
  2246. if(!ccg.GetOutputs().empty())
  2247. {
  2248. std::string comment;
  2249. comment = "Generating ";
  2250. const char* sep = "";
  2251. for(std::vector<std::string>::const_iterator o = ccg.GetOutputs().begin();
  2252. o != ccg.GetOutputs().end(); ++o)
  2253. {
  2254. comment += sep;
  2255. comment += this->Convert(*o, cmLocalGenerator::START_OUTPUT);
  2256. sep = ", ";
  2257. }
  2258. return comment;
  2259. }
  2260. // Otherwise use the provided default.
  2261. return default_comment;
  2262. }
  2263. //----------------------------------------------------------------------------
  2264. class cmInstallTargetGeneratorLocal: public cmInstallTargetGenerator
  2265. {
  2266. public:
  2267. cmInstallTargetGeneratorLocal(cmLocalGenerator* lg, std::string const& t,
  2268. const char* dest, bool implib):
  2269. cmInstallTargetGenerator(
  2270. t, dest, implib, "", std::vector<std::string>(), "Unspecified",
  2271. cmInstallGenerator::SelectMessageLevel(lg->GetMakefile()),
  2272. false)
  2273. {
  2274. this->Compute(lg);
  2275. }
  2276. };
  2277. //----------------------------------------------------------------------------
  2278. void
  2279. cmLocalGenerator
  2280. ::GenerateTargetInstallRules(
  2281. std::ostream& os, const std::string& config,
  2282. std::vector<std::string> const& configurationTypes)
  2283. {
  2284. // Convert the old-style install specification from each target to
  2285. // an install generator and run it.
  2286. cmTargets& tgts = this->Makefile->GetTargets();
  2287. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  2288. {
  2289. if (l->second.GetType() == cmTarget::INTERFACE_LIBRARY)
  2290. {
  2291. continue;
  2292. }
  2293. // Include the user-specified pre-install script for this target.
  2294. if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
  2295. {
  2296. cmInstallScriptGenerator g(preinstall, false, 0);
  2297. g.Generate(os, config, configurationTypes);
  2298. }
  2299. // Install this target if a destination is given.
  2300. if(l->second.GetInstallPath() != "")
  2301. {
  2302. // Compute the full install destination. Note that converting
  2303. // to unix slashes also removes any trailing slash.
  2304. // We also skip over the leading slash given by the user.
  2305. std::string destination = l->second.GetInstallPath().substr(1);
  2306. cmSystemTools::ConvertToUnixSlashes(destination);
  2307. if(destination.empty())
  2308. {
  2309. destination = ".";
  2310. }
  2311. // Generate the proper install generator for this target type.
  2312. switch(l->second.GetType())
  2313. {
  2314. case cmTarget::EXECUTABLE:
  2315. case cmTarget::STATIC_LIBRARY:
  2316. case cmTarget::MODULE_LIBRARY:
  2317. {
  2318. // Use a target install generator.
  2319. cmInstallTargetGeneratorLocal
  2320. g(this, l->first, destination.c_str(), false);
  2321. g.Generate(os, config, configurationTypes);
  2322. }
  2323. break;
  2324. case cmTarget::SHARED_LIBRARY:
  2325. {
  2326. #if defined(_WIN32) || defined(__CYGWIN__)
  2327. // Special code to handle DLL. Install the import library
  2328. // to the normal destination and the DLL to the runtime
  2329. // destination.
  2330. cmInstallTargetGeneratorLocal
  2331. g1(this, l->first, destination.c_str(), true);
  2332. g1.Generate(os, config, configurationTypes);
  2333. // We also skip over the leading slash given by the user.
  2334. destination = l->second.GetRuntimeInstallPath().substr(1);
  2335. cmSystemTools::ConvertToUnixSlashes(destination);
  2336. cmInstallTargetGeneratorLocal
  2337. g2(this, l->first, destination.c_str(), false);
  2338. g2.Generate(os, config, configurationTypes);
  2339. #else
  2340. // Use a target install generator.
  2341. cmInstallTargetGeneratorLocal
  2342. g(this, l->first, destination.c_str(), false);
  2343. g.Generate(os, config, configurationTypes);
  2344. #endif
  2345. }
  2346. break;
  2347. default:
  2348. break;
  2349. }
  2350. }
  2351. // Include the user-specified post-install script for this target.
  2352. if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
  2353. {
  2354. cmInstallScriptGenerator g(postinstall, false, 0);
  2355. g.Generate(os, config, configurationTypes);
  2356. }
  2357. }
  2358. }
  2359. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2360. static std::string cmLocalGeneratorMD5(const char* input)
  2361. {
  2362. char md5out[32];
  2363. cmsysMD5* md5 = cmsysMD5_New();
  2364. cmsysMD5_Initialize(md5);
  2365. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
  2366. cmsysMD5_FinalizeHex(md5, md5out);
  2367. cmsysMD5_Delete(md5);
  2368. return std::string(md5out, 32);
  2369. }
  2370. static bool
  2371. cmLocalGeneratorShortenObjectName(std::string& objName,
  2372. std::string::size_type max_len)
  2373. {
  2374. // Replace the beginning of the path portion of the object name with
  2375. // its own md5 sum.
  2376. std::string::size_type pos = objName.find('/', objName.size()-max_len+32);
  2377. if(pos != objName.npos)
  2378. {
  2379. std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
  2380. md5name += objName.substr(pos);
  2381. objName = md5name;
  2382. // The object name is now short enough.
  2383. return true;
  2384. }
  2385. else
  2386. {
  2387. // The object name could not be shortened enough.
  2388. return false;
  2389. }
  2390. }
  2391. static
  2392. bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2393. std::string::size_type dir_len,
  2394. std::string::size_type max_total_len)
  2395. {
  2396. // Enforce the maximum file name length if possible.
  2397. std::string::size_type max_obj_len = max_total_len;
  2398. if(dir_len < max_total_len)
  2399. {
  2400. max_obj_len = max_total_len - dir_len;
  2401. if(objName.size() > max_obj_len)
  2402. {
  2403. // The current object file name is too long. Try to shorten it.
  2404. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2405. }
  2406. else
  2407. {
  2408. // The object file name is short enough.
  2409. return true;
  2410. }
  2411. }
  2412. else
  2413. {
  2414. // The build directory in which the object will be stored is
  2415. // already too deep.
  2416. return false;
  2417. }
  2418. }
  2419. #endif
  2420. //----------------------------------------------------------------------------
  2421. std::string&
  2422. cmLocalGenerator
  2423. ::CreateSafeUniqueObjectFileName(const std::string& sin,
  2424. std::string const& dir_max)
  2425. {
  2426. // Look for an existing mapped name for this object file.
  2427. std::map<std::string,std::string>::iterator it =
  2428. this->UniqueObjectNamesMap.find(sin);
  2429. // If no entry exists create one.
  2430. if(it == this->UniqueObjectNamesMap.end())
  2431. {
  2432. // Start with the original name.
  2433. std::string ssin = sin;
  2434. // Avoid full paths by removing leading slashes.
  2435. ssin.erase(0, ssin.find_first_not_of("/"));
  2436. // Avoid full paths by removing colons.
  2437. cmSystemTools::ReplaceString(ssin, ":", "_");
  2438. // Avoid relative paths that go up the tree.
  2439. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2440. // Avoid spaces.
  2441. cmSystemTools::ReplaceString(ssin, " ", "_");
  2442. // Mangle the name if necessary.
  2443. if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
  2444. {
  2445. bool done;
  2446. int cc = 0;
  2447. char rpstr[100];
  2448. sprintf(rpstr, "_p_");
  2449. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2450. std::string sssin = sin;
  2451. do
  2452. {
  2453. done = true;
  2454. for ( it = this->UniqueObjectNamesMap.begin();
  2455. it != this->UniqueObjectNamesMap.end();
  2456. ++ it )
  2457. {
  2458. if ( it->second == ssin )
  2459. {
  2460. done = false;
  2461. }
  2462. }
  2463. if ( done )
  2464. {
  2465. break;
  2466. }
  2467. sssin = ssin;
  2468. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2469. sprintf(rpstr, "_p%d_", cc++);
  2470. }
  2471. while ( !done );
  2472. }
  2473. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2474. if(!cmLocalGeneratorCheckObjectName(ssin, dir_max.size(),
  2475. this->ObjectPathMax))
  2476. {
  2477. // Warn if this is the first time the path has been seen.
  2478. if(this->ObjectMaxPathViolations.insert(dir_max).second)
  2479. {
  2480. std::ostringstream m;
  2481. m << "The object file directory\n"
  2482. << " " << dir_max << "\n"
  2483. << "has " << dir_max.size() << " characters. "
  2484. << "The maximum full path to an object file is "
  2485. << this->ObjectPathMax << " characters "
  2486. << "(see CMAKE_OBJECT_PATH_MAX). "
  2487. << "Object file\n"
  2488. << " " << ssin << "\n"
  2489. << "cannot be safely placed under this directory. "
  2490. << "The build may not work correctly.";
  2491. this->IssueMessage(cmake::WARNING, m.str());
  2492. }
  2493. }
  2494. #else
  2495. (void)dir_max;
  2496. #endif
  2497. // Insert the newly mapped object file name.
  2498. std::map<std::string, std::string>::value_type e(sin, ssin);
  2499. it = this->UniqueObjectNamesMap.insert(e).first;
  2500. }
  2501. // Return the map entry.
  2502. return it->second;
  2503. }
  2504. //----------------------------------------------------------------------------
  2505. void cmLocalGenerator::ComputeObjectFilenames(
  2506. std::map<cmSourceFile const*, std::string>&,
  2507. cmGeneratorTarget const*)
  2508. {
  2509. }
  2510. bool cmLocalGenerator::IsWindowsShell() const
  2511. {
  2512. return this->GetState()->UseWindowsShell();
  2513. }
  2514. bool cmLocalGenerator::IsWatcomWMake() const
  2515. {
  2516. return this->GetState()->UseWatcomWMake();
  2517. }
  2518. bool cmLocalGenerator::IsMinGWMake() const
  2519. {
  2520. return this->GetState()->UseMinGWMake();
  2521. }
  2522. bool cmLocalGenerator::IsNMake() const
  2523. {
  2524. return this->GetState()->UseNMake();
  2525. }
  2526. //----------------------------------------------------------------------------
  2527. std::string
  2528. cmLocalGenerator
  2529. ::GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  2530. std::string const& dir_max,
  2531. bool* hasSourceExtension)
  2532. {
  2533. // Construct the object file name using the full path to the source
  2534. // file which is its only unique identification.
  2535. const char* fullPath = source.GetFullPath().c_str();
  2536. // Try referencing the source relative to the source tree.
  2537. std::string relFromSource = this->Convert(fullPath, START);
  2538. assert(!relFromSource.empty());
  2539. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
  2540. bool subSource = relSource && relFromSource[0] != '.';
  2541. // Try referencing the source relative to the binary tree.
  2542. std::string relFromBinary = this->Convert(fullPath, START_OUTPUT);
  2543. assert(!relFromBinary.empty());
  2544. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
  2545. bool subBinary = relBinary && relFromBinary[0] != '.';
  2546. // Select a nice-looking reference to the source file to construct
  2547. // the object file name.
  2548. std::string objectName;
  2549. if((relSource && !relBinary) || (subSource && !subBinary))
  2550. {
  2551. objectName = relFromSource;
  2552. }
  2553. else if((relBinary && !relSource) || (subBinary && !subSource))
  2554. {
  2555. objectName = relFromBinary;
  2556. }
  2557. else if(relFromBinary.length() < relFromSource.length())
  2558. {
  2559. objectName = relFromBinary;
  2560. }
  2561. else
  2562. {
  2563. objectName = relFromSource;
  2564. }
  2565. // if it is still a full path check for the try compile case
  2566. // try compile never have in source sources, and should not
  2567. // have conflicting source file names in the same target
  2568. if(cmSystemTools::FileIsFullPath(objectName.c_str()))
  2569. {
  2570. if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
  2571. {
  2572. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2573. }
  2574. }
  2575. // Replace the original source file extension with the object file
  2576. // extension.
  2577. bool keptSourceExtension = true;
  2578. if(!source.GetPropertyAsBool("KEEP_EXTENSION"))
  2579. {
  2580. // Decide whether this language wants to replace the source
  2581. // extension with the object extension. For CMake 2.4
  2582. // compatibility do this by default.
  2583. bool replaceExt = this->NeedBackwardsCompatibility_2_4();
  2584. if(!replaceExt)
  2585. {
  2586. std::string lang = source.GetLanguage();
  2587. if(!lang.empty())
  2588. {
  2589. std::string repVar = "CMAKE_";
  2590. repVar += lang;
  2591. repVar += "_OUTPUT_EXTENSION_REPLACE";
  2592. replaceExt = this->Makefile->IsOn(repVar);
  2593. }
  2594. }
  2595. // Remove the source extension if it is to be replaced.
  2596. if(replaceExt)
  2597. {
  2598. keptSourceExtension = false;
  2599. std::string::size_type dot_pos = objectName.rfind(".");
  2600. if(dot_pos != std::string::npos)
  2601. {
  2602. objectName = objectName.substr(0, dot_pos);
  2603. }
  2604. }
  2605. // Store the new extension.
  2606. objectName +=
  2607. this->GlobalGenerator->GetLanguageOutputExtension(source);
  2608. }
  2609. if(hasSourceExtension)
  2610. {
  2611. *hasSourceExtension = keptSourceExtension;
  2612. }
  2613. // Convert to a safe name.
  2614. return this->CreateSafeUniqueObjectFileName(objectName, dir_max);
  2615. }
  2616. //----------------------------------------------------------------------------
  2617. std::string
  2618. cmLocalGenerator
  2619. ::GetSourceFileLanguage(const cmSourceFile& source)
  2620. {
  2621. return source.GetLanguage();
  2622. }
  2623. //----------------------------------------------------------------------------
  2624. std::string
  2625. cmLocalGenerator::GetTargetDirectory(cmTarget const&) const
  2626. {
  2627. cmSystemTools::Error("GetTargetDirectory"
  2628. " called on cmLocalGenerator");
  2629. return "";
  2630. }
  2631. //----------------------------------------------------------------------------
  2632. cmIML_INT_uint64_t cmLocalGenerator::GetBackwardsCompatibility()
  2633. {
  2634. // The computed version may change until the project is fully
  2635. // configured.
  2636. if(!this->BackwardsCompatibilityFinal)
  2637. {
  2638. unsigned int major = 0;
  2639. unsigned int minor = 0;
  2640. unsigned int patch = 0;
  2641. if(const char* value
  2642. = this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
  2643. {
  2644. switch(sscanf(value, "%u.%u.%u", &major, &minor, &patch))
  2645. {
  2646. case 2: patch = 0; break;
  2647. case 1: minor = 0; patch = 0; break;
  2648. default: break;
  2649. }
  2650. }
  2651. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  2652. this->BackwardsCompatibilityFinal = this->Makefile->IsConfigured();
  2653. }
  2654. return this->BackwardsCompatibility;
  2655. }
  2656. //----------------------------------------------------------------------------
  2657. bool cmLocalGenerator::NeedBackwardsCompatibility_2_4()
  2658. {
  2659. // Check the policy to decide whether to pay attention to this
  2660. // variable.
  2661. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0001))
  2662. {
  2663. case cmPolicies::WARN:
  2664. // WARN is just OLD without warning because user code does not
  2665. // always affect whether this check is done.
  2666. case cmPolicies::OLD:
  2667. // Old behavior is to check the variable.
  2668. break;
  2669. case cmPolicies::NEW:
  2670. // New behavior is to ignore the variable.
  2671. return false;
  2672. case cmPolicies::REQUIRED_IF_USED:
  2673. case cmPolicies::REQUIRED_ALWAYS:
  2674. // This will never be the case because the only way to require
  2675. // the setting is to require the user to specify version policy
  2676. // 2.6 or higher. Once we add that requirement then this whole
  2677. // method can be removed anyway.
  2678. return false;
  2679. }
  2680. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  2681. // equal to or lower than the given version.
  2682. cmIML_INT_uint64_t actual_compat = this->GetBackwardsCompatibility();
  2683. return (actual_compat &&
  2684. actual_compat <= CMake_VERSION_ENCODE(2, 4, 255));
  2685. }
  2686. //----------------------------------------------------------------------------
  2687. bool cmLocalGenerator::CheckDefinition(std::string const& define) const
  2688. {
  2689. // Many compilers do not support -DNAME(arg)=sdf so we disable it.
  2690. std::string::size_type pos = define.find_first_of("(=");
  2691. if (pos != std::string::npos)
  2692. {
  2693. if (define[pos] == '(')
  2694. {
  2695. std::ostringstream e;
  2696. e << "WARNING: Function-style preprocessor definitions may not be "
  2697. << "passed on the compiler command line because many compilers "
  2698. << "do not support it.\n"
  2699. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2700. << "Consider defining the macro in a (configured) header file.\n";
  2701. cmSystemTools::Message(e.str().c_str());
  2702. return false;
  2703. }
  2704. }
  2705. // Many compilers do not support # in the value so we disable it.
  2706. if(define.find_first_of("#") != define.npos)
  2707. {
  2708. std::ostringstream e;
  2709. e << "WARNING: Preprocessor definitions containing '#' may not be "
  2710. << "passed on the compiler command line because many compilers "
  2711. << "do not support it.\n"
  2712. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2713. << "Consider defining the macro in a (configured) header file.\n";
  2714. cmSystemTools::Message(e.str().c_str());
  2715. return false;
  2716. }
  2717. // Assume it is supported.
  2718. return true;
  2719. }
  2720. //----------------------------------------------------------------------------
  2721. static void cmLGInfoProp(cmMakefile* mf, cmTarget* target,
  2722. const std::string& prop)
  2723. {
  2724. if(const char* val = target->GetProperty(prop))
  2725. {
  2726. mf->AddDefinition(prop, val);
  2727. }
  2728. }
  2729. //----------------------------------------------------------------------------
  2730. void cmLocalGenerator::GenerateAppleInfoPList(cmTarget* target,
  2731. const std::string& targetName,
  2732. const char* fname)
  2733. {
  2734. // Find the Info.plist template.
  2735. const char* in = target->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
  2736. std::string inFile = (in && *in)? in : "MacOSXBundleInfo.plist.in";
  2737. if(!cmSystemTools::FileIsFullPath(inFile.c_str()))
  2738. {
  2739. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  2740. if(!inMod.empty())
  2741. {
  2742. inFile = inMod;
  2743. }
  2744. }
  2745. if(!cmSystemTools::FileExists(inFile.c_str(), true))
  2746. {
  2747. std::ostringstream e;
  2748. e << "Target " << target->GetName() << " Info.plist template \""
  2749. << inFile << "\" could not be found.";
  2750. cmSystemTools::Error(e.str().c_str());
  2751. return;
  2752. }
  2753. // Convert target properties to variables in an isolated makefile
  2754. // scope to configure the file. If properties are set they will
  2755. // override user make variables. If not the configuration will fall
  2756. // back to the directory-level values set by the user.
  2757. cmMakefile* mf = this->Makefile;
  2758. mf->PushScope();
  2759. mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName.c_str());
  2760. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
  2761. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
  2762. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
  2763. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
  2764. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_NAME");
  2765. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
  2766. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
  2767. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
  2768. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  2769. mf->PopScope();
  2770. }
  2771. //----------------------------------------------------------------------------
  2772. void cmLocalGenerator::GenerateFrameworkInfoPList(cmTarget* target,
  2773. const std::string& targetName,
  2774. const char* fname)
  2775. {
  2776. // Find the Info.plist template.
  2777. const char* in = target->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
  2778. std::string inFile = (in && *in)? in : "MacOSXFrameworkInfo.plist.in";
  2779. if(!cmSystemTools::FileIsFullPath(inFile.c_str()))
  2780. {
  2781. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  2782. if(!inMod.empty())
  2783. {
  2784. inFile = inMod;
  2785. }
  2786. }
  2787. if(!cmSystemTools::FileExists(inFile.c_str(), true))
  2788. {
  2789. std::ostringstream e;
  2790. e << "Target " << target->GetName() << " Info.plist template \""
  2791. << inFile << "\" could not be found.";
  2792. cmSystemTools::Error(e.str().c_str());
  2793. return;
  2794. }
  2795. // Convert target properties to variables in an isolated makefile
  2796. // scope to configure the file. If properties are set they will
  2797. // override user make variables. If not the configuration will fall
  2798. // back to the directory-level values set by the user.
  2799. cmMakefile* mf = this->Makefile;
  2800. mf->PushScope();
  2801. mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName.c_str());
  2802. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
  2803. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
  2804. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
  2805. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
  2806. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  2807. mf->PopScope();
  2808. }