cmLocalGenerator.cxx 87 KB

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