cmLocalGenerator.cxx 93 KB

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