cmLocalGenerator.cxx 112 KB

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