cmLocalGenerator.cxx 106 KB

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