cmLocalGenerator.cxx 109 KB

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