cmLocalGenerator.cxx 100 KB

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