cmLocalGenerator.cxx 93 KB

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