cmLocalGenerator.cxx 91 KB

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