cmLocalGenerator.cxx 70 KB

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