cmLocalGenerator.cxx 74 KB

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