cmLocalGenerator.cxx 87 KB

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