cmLocalGenerator.cxx 93 KB

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