cmLocalGenerator.cxx 71 KB

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