cmLocalGenerator.cxx 110 KB

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