1
0

cmLocalGenerator.cxx 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  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 << "# Include the install script for each subdirectory.\n";
  364. for(std::vector<cmLocalGenerator*>::const_iterator
  365. ci = this->Children.begin(); ci != this->Children.end(); ++ci)
  366. {
  367. if(!(*ci)->GetExcludeAll())
  368. {
  369. std::string odir = (*ci)->GetMakefile()->GetStartOutputDirectory();
  370. cmSystemTools::ConvertToUnixSlashes(odir);
  371. fout << "INCLUDE(\"" << odir.c_str()
  372. << "/cmake_install.cmake\")" << std::endl;
  373. }
  374. }
  375. fout << "\n";
  376. }
  377. // Record the install manifest.
  378. if ( toplevel_install )
  379. {
  380. fout <<
  381. "IF(CMAKE_INSTALL_COMPONENT)\n"
  382. " SET(CMAKE_INSTALL_MANIFEST \"install_manifest_"
  383. "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
  384. "ELSE(CMAKE_INSTALL_COMPONENT)\n"
  385. " SET(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
  386. "ENDIF(CMAKE_INSTALL_COMPONENT)\n";
  387. fout
  388. << "FILE(WRITE \""
  389. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  390. << "\"\")" << std::endl;
  391. fout
  392. << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  393. << " FILE(APPEND \""
  394. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  395. << "\"${file}\\n\")" << std::endl
  396. << "ENDFOREACH(file)" << std::endl;
  397. }
  398. }
  399. //----------------------------------------------------------------------------
  400. void cmLocalGenerator::GenerateTargetManifest(cmTargetManifest& manifest)
  401. {
  402. // Collect the set of configuration types.
  403. std::vector<std::string> configNames;
  404. if(const char* configurationTypes =
  405. this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  406. {
  407. cmSystemTools::ExpandListArgument(configurationTypes, configNames);
  408. }
  409. else if(const char* buildType =
  410. this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
  411. {
  412. if(*buildType)
  413. {
  414. configNames.push_back(buildType);
  415. }
  416. }
  417. // Add our targets to the manifest for each configuration.
  418. cmTargets& targets = this->Makefile->GetTargets();
  419. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  420. {
  421. cmTarget& target = t->second;
  422. cmTarget::TargetType type = target.GetType();
  423. if(type == cmTarget::STATIC_LIBRARY ||
  424. type == cmTarget::SHARED_LIBRARY ||
  425. type == cmTarget::MODULE_LIBRARY ||
  426. type == cmTarget::EXECUTABLE)
  427. {
  428. if(configNames.empty())
  429. {
  430. manifest[""].insert(target.GetFullPath(0, false));
  431. if(type == cmTarget::SHARED_LIBRARY &&
  432. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
  433. {
  434. manifest[""].insert(target.GetFullPath(0, true));
  435. }
  436. }
  437. else
  438. {
  439. for(std::vector<std::string>::iterator ci = configNames.begin();
  440. ci != configNames.end(); ++ci)
  441. {
  442. const char* config = ci->c_str();
  443. manifest[config].insert(target.GetFullPath(config, false));
  444. if(type == cmTarget::SHARED_LIBRARY &&
  445. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
  446. {
  447. manifest[config].insert(target.GetFullPath(config, true));
  448. }
  449. }
  450. }
  451. }
  452. }
  453. }
  454. void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
  455. const char* lang,
  456. cmSourceFile& source,
  457. cmTarget& )
  458. {
  459. std::string objectDir = cmSystemTools::GetFilenamePath(std::string(ofname));
  460. objectDir = this->Convert(objectDir.c_str(),START_OUTPUT,SHELL);
  461. std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
  462. std::string sourceFile =
  463. this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
  464. std::string varString = "CMAKE_";
  465. varString += lang;
  466. varString += "_COMPILE_OBJECT";
  467. std::vector<std::string> rules;
  468. rules.push_back(this->Makefile->GetRequiredDefinition(varString.c_str()));
  469. varString = "CMAKE_";
  470. varString += lang;
  471. varString += "_FLAGS";
  472. std::string flags;
  473. flags += this->Makefile->GetSafeDefinition(varString.c_str());
  474. flags += " ";
  475. flags += this->GetIncludeFlags(lang);
  476. // Construct the command lines.
  477. cmCustomCommandLines commandLines;
  478. std::vector<std::string> commands;
  479. cmSystemTools::ExpandList(rules, commands);
  480. cmLocalGenerator::RuleVariables vars;
  481. vars.Language = lang;
  482. vars.Source = sourceFile.c_str();
  483. vars.Object = objectFile.c_str();
  484. vars.ObjectDir = objectDir.c_str();
  485. vars.Flags = flags.c_str();
  486. for(std::vector<std::string>::iterator i = commands.begin();
  487. i != commands.end(); ++i)
  488. {
  489. // Expand the full command line string.
  490. this->ExpandRuleVariables(*i, vars);
  491. // Parse the string to get the custom command line.
  492. cmCustomCommandLine commandLine;
  493. std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
  494. for(std::vector<cmStdString>::iterator a = cmd.begin();
  495. a != cmd.end(); ++a)
  496. {
  497. commandLine.push_back(*a);
  498. }
  499. // Store this command line.
  500. commandLines.push_back(commandLine);
  501. }
  502. // Check for extra object-file dependencies.
  503. std::vector<std::string> depends;
  504. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  505. if(additionalDeps)
  506. {
  507. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  508. }
  509. // Generate a meaningful comment for the command.
  510. std::string comment = "Building ";
  511. comment += lang;
  512. comment += " object ";
  513. comment += this->Convert(ofname, START_OUTPUT);
  514. // Add the custom command to build the object file.
  515. this->Makefile->AddCustomCommandToOutput(
  516. ofname,
  517. depends,
  518. source.GetFullPath().c_str(),
  519. commandLines,
  520. comment.c_str(),
  521. this->Makefile->GetStartOutputDirectory()
  522. );
  523. }
  524. void cmLocalGenerator::AddBuildTargetRule(const char* llang, cmTarget& target)
  525. {
  526. cmStdString objs;
  527. std::vector<std::string> objVector;
  528. // Add all the sources outputs to the depends of the target
  529. std::vector<cmSourceFile*>& classes = target.GetSourceFiles();
  530. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  531. i != classes.end(); ++i)
  532. {
  533. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  534. !(*i)->GetCustomCommand())
  535. {
  536. std::string outExt =
  537. this->GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  538. (*i)->GetSourceExtension().c_str());
  539. if(outExt.size() && !(*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  540. {
  541. std::string ofname = this->Makefile->GetCurrentOutputDirectory();
  542. ofname += "/";
  543. ofname += (*i)->GetSourceName() + outExt;
  544. objVector.push_back(ofname);
  545. this->AddCustomCommandToCreateObject(ofname.c_str(),
  546. llang, *(*i), target);
  547. objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
  548. objs += " ";
  549. }
  550. }
  551. }
  552. std::string createRule = "CMAKE_";
  553. createRule += llang;
  554. createRule += target.GetCreateRuleVariable();
  555. std::string targetName = target.GetFullName();
  556. // Executable :
  557. // Shared Library:
  558. // Static Library:
  559. // Shared Module:
  560. std::string linkLibs; // should be set
  561. std::string flags; // should be set
  562. std::string linkFlags; // should be set
  563. this->GetTargetFlags(linkLibs, flags, linkFlags, target);
  564. cmLocalGenerator::RuleVariables vars;
  565. vars.Language = llang;
  566. vars.Objects = objs.c_str();
  567. vars.ObjectDir = ".";
  568. vars.Target = targetName.c_str();
  569. vars.LinkLibraries = linkLibs.c_str();
  570. vars.Flags = flags.c_str();
  571. vars.LinkFlags = linkFlags.c_str();
  572. cmCustomCommandLines commandLines;
  573. std::vector<std::string> rules;
  574. rules.push_back(this->Makefile->GetRequiredDefinition(createRule.c_str()));
  575. std::vector<std::string> commands;
  576. cmSystemTools::ExpandList(rules, commands);
  577. for(std::vector<std::string>::iterator i = commands.begin();
  578. i != commands.end(); ++i)
  579. {
  580. // Expand the full command line string.
  581. this->ExpandRuleVariables(*i, vars);
  582. // Parse the string to get the custom command line.
  583. cmCustomCommandLine commandLine;
  584. std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
  585. for(std::vector<cmStdString>::iterator a = cmd.begin();
  586. a != cmd.end(); ++a)
  587. {
  588. commandLine.push_back(*a);
  589. }
  590. // Store this command line.
  591. commandLines.push_back(commandLine);
  592. }
  593. std::string targetFullPath = target.GetFullPath();
  594. // Generate a meaningful comment for the command.
  595. std::string comment = "Linking ";
  596. comment += llang;
  597. comment += " target ";
  598. comment += this->Convert(targetFullPath.c_str(), START_OUTPUT);
  599. this->Makefile->AddCustomCommandToOutput(
  600. targetFullPath.c_str(),
  601. objVector,
  602. 0,
  603. commandLines,
  604. comment.c_str(),
  605. this->Makefile->GetStartOutputDirectory()
  606. );
  607. target.GetSourceFiles().push_back
  608. (this->Makefile->GetSource(targetFullPath.c_str()));
  609. }
  610. void cmLocalGenerator
  611. ::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
  612. {
  613. cmTargets &tgts = this->Makefile->GetTargets();
  614. for(cmTargets::iterator l = tgts.begin();
  615. l != tgts.end(); l++)
  616. {
  617. cmTarget& target = l->second;
  618. switch(target.GetType())
  619. {
  620. case cmTarget::STATIC_LIBRARY:
  621. case cmTarget::SHARED_LIBRARY:
  622. case cmTarget::MODULE_LIBRARY:
  623. case cmTarget::EXECUTABLE:
  624. {
  625. const char* llang =
  626. target.GetLinkerLanguage(this->GetGlobalGenerator());
  627. if(!llang)
  628. {
  629. cmSystemTools::Error
  630. ("CMake can not determine linker language for target:",
  631. target.GetName());
  632. return;
  633. }
  634. // if the language is not in the set lang then create custom
  635. // commands to build the target
  636. if(lang.count(llang) == 0)
  637. {
  638. this->AddBuildTargetRule(llang, target);
  639. }
  640. }
  641. break;
  642. case cmTarget::UTILITY:
  643. case cmTarget::GLOBAL_TARGET:
  644. case cmTarget::INSTALL_FILES:
  645. case cmTarget::INSTALL_PROGRAMS:
  646. case cmTarget::INSTALL_DIRECTORY:
  647. break;
  648. }
  649. }
  650. }
  651. // List of variables that are replaced when
  652. // rules are expanced. These variables are
  653. // replaced in the form <var> with GetSafeDefinition(var).
  654. // ${LANG} is replaced in the variable first with all enabled
  655. // languages.
  656. static const char* ruleReplaceVars[] =
  657. {
  658. "CMAKE_${LANG}_COMPILER",
  659. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  660. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  661. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  662. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  663. "CMAKE_${LANG}_LINK_FLAGS",
  664. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  665. "CMAKE_${LANG}_ARCHIVE",
  666. "CMAKE_AR",
  667. "CMAKE_CURRENT_SOURCE_DIR",
  668. "CMAKE_CURRENT_BINARY_DIR",
  669. "CMAKE_RANLIB",
  670. 0
  671. };
  672. std::string
  673. cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
  674. const RuleVariables& replaceValues)
  675. {
  676. if(replaceValues.LinkFlags)
  677. {
  678. if(variable == "LINK_FLAGS")
  679. {
  680. return replaceValues.LinkFlags;
  681. }
  682. }
  683. if(replaceValues.Flags)
  684. {
  685. if(variable == "FLAGS")
  686. {
  687. return replaceValues.Flags;
  688. }
  689. }
  690. if(replaceValues.Source)
  691. {
  692. if(variable == "SOURCE")
  693. {
  694. return replaceValues.Source;
  695. }
  696. }
  697. if(replaceValues.Object)
  698. {
  699. if(variable == "OBJECT")
  700. {
  701. return replaceValues.Object;
  702. }
  703. }
  704. if(replaceValues.ObjectDir)
  705. {
  706. if(variable == "OBJECT_DIR")
  707. {
  708. return replaceValues.ObjectDir;
  709. }
  710. }
  711. if(replaceValues.Objects)
  712. {
  713. if(variable == "OBJECTS")
  714. {
  715. return replaceValues.Objects;
  716. }
  717. }
  718. if(replaceValues.ObjectsQuoted)
  719. {
  720. if(variable == "OBJECTS_QUOTED")
  721. {
  722. return replaceValues.ObjectsQuoted;
  723. }
  724. }
  725. if(replaceValues.TargetPDB )
  726. {
  727. if(variable == "TARGET_PDB")
  728. {
  729. return replaceValues.TargetPDB;
  730. }
  731. }
  732. if(replaceValues.Target)
  733. {
  734. if(variable == "TARGET_QUOTED")
  735. {
  736. std::string targetQuoted = replaceValues.Target;
  737. if(targetQuoted.size() && targetQuoted[0] != '\"')
  738. {
  739. targetQuoted = '\"';
  740. targetQuoted += replaceValues.Target;
  741. targetQuoted += '\"';
  742. return targetQuoted;
  743. }
  744. }
  745. if(variable == "LANGUAGE_COMPILE_FLAGS")
  746. {
  747. return replaceValues.LanguageCompileFlags;
  748. }
  749. if(variable == "TARGET")
  750. {
  751. return replaceValues.Target;
  752. }
  753. if(variable == "TARGET_IMPLIB")
  754. {
  755. return this->TargetImplib;
  756. }
  757. if(variable == "TARGET_BASE")
  758. {
  759. // Strip the last extension off the target name.
  760. std::string targetBase = replaceValues.Target;
  761. std::string::size_type pos = targetBase.rfind(".");
  762. if(pos != targetBase.npos)
  763. {
  764. return targetBase.substr(0, pos);
  765. }
  766. else
  767. {
  768. return targetBase;
  769. }
  770. }
  771. }
  772. if(replaceValues.TargetSOName)
  773. {
  774. if(variable == "TARGET_SONAME")
  775. {
  776. if(replaceValues.Language)
  777. {
  778. std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
  779. name += replaceValues.Language;
  780. name += "_FLAG";
  781. if(this->Makefile->GetDefinition(name.c_str()))
  782. {
  783. return replaceValues.TargetSOName;
  784. }
  785. }
  786. return "";
  787. }
  788. }
  789. if(replaceValues.TargetInstallNameDir)
  790. {
  791. if(variable == "TARGET_INSTALLNAME_DIR")
  792. {
  793. return replaceValues.TargetInstallNameDir;
  794. }
  795. }
  796. if(replaceValues.LinkLibraries)
  797. {
  798. if(variable == "LINK_LIBRARIES")
  799. {
  800. return replaceValues.LinkLibraries;
  801. }
  802. }
  803. std::vector<std::string> enabledLanguages;
  804. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  805. // loop over language specific replace variables
  806. int pos = 0;
  807. while(ruleReplaceVars[pos])
  808. {
  809. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  810. i != enabledLanguages.end(); ++i)
  811. {
  812. const char* lang = i->c_str();
  813. std::string actualReplace = ruleReplaceVars[pos];
  814. // If this is the compiler then look for the extra variable
  815. // _COMPILER_ARG1 which must be the first argument to the compiler
  816. const char* compilerArg1 = 0;
  817. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  818. {
  819. std::string arg1 = actualReplace + "_ARG1";
  820. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  821. compilerArg1 = this->Makefile->GetDefinition(arg1.c_str());
  822. }
  823. if(actualReplace.find("${LANG}") != actualReplace.npos)
  824. {
  825. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  826. }
  827. if(actualReplace == variable)
  828. {
  829. std::string replace =
  830. this->Makefile->GetSafeDefinition(variable.c_str());
  831. // if the variable is not a FLAG then treat it like a path
  832. if(variable.find("_FLAG") == variable.npos)
  833. {
  834. std::string ret = this->ConvertToOutputForExisting(replace.c_str());
  835. // if there is a required first argument to the compiler add it
  836. // to the compiler string
  837. if(compilerArg1)
  838. {
  839. ret += " ";
  840. ret += compilerArg1;
  841. }
  842. return ret;
  843. }
  844. return replace;
  845. }
  846. }
  847. pos++;
  848. }
  849. return variable;
  850. }
  851. void
  852. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  853. const RuleVariables& replaceValues)
  854. {
  855. std::vector<std::string> enabledLanguages;
  856. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  857. std::string::size_type start = s.find('<');
  858. // no variables to expand
  859. if(start == s.npos)
  860. {
  861. return;
  862. }
  863. std::string::size_type pos = 0;
  864. std::string expandedInput;
  865. while(start != s.npos && start < s.size()-2)
  866. {
  867. std::string::size_type end = s.find('>', start);
  868. // if we find a < with no > we are done
  869. if(end == s.npos)
  870. {
  871. return;
  872. }
  873. char c = s[start+1];
  874. // if the next char after the < is not A-Za-z then
  875. // skip it and try to find the next < in the string
  876. if(!isalpha(c))
  877. {
  878. start = s.find('<', start+1);
  879. }
  880. else
  881. {
  882. // extract the var
  883. std::string var = s.substr(start+1, end - start-1);
  884. std::string replace = this->ExpandRuleVariable(var,
  885. replaceValues);
  886. expandedInput += s.substr(pos, start-pos);
  887. expandedInput += replace;
  888. // move to next one
  889. start = s.find('<', start+var.size()+2);
  890. pos = end+1;
  891. }
  892. }
  893. // add the rest of the input
  894. expandedInput += s.substr(pos, s.size()-pos);
  895. s = expandedInput;
  896. }
  897. std::string
  898. cmLocalGenerator::ConvertToOutputForExisting(const char* p)
  899. {
  900. std::string ret = this->Convert(p, START_OUTPUT, SHELL, true);
  901. // if there are spaces in the path, then get the short path version
  902. // if there is one
  903. if(ret.find(' ') != std::string::npos)
  904. {
  905. if(cmSystemTools::FileExists(p))
  906. {
  907. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  908. {
  909. ret = this->Convert(p,START_OUTPUT,MAKEFILE,true);
  910. }
  911. }
  912. }
  913. return ret;
  914. }
  915. const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
  916. {
  917. if(!lang)
  918. {
  919. return "";
  920. }
  921. if(this->LanguageToIncludeFlags.count(lang))
  922. {
  923. return this->LanguageToIncludeFlags[lang].c_str();
  924. }
  925. cmOStringStream includeFlags;
  926. std::vector<std::string> includes;
  927. this->GetIncludeDirectories(includes);
  928. std::vector<std::string>::iterator i;
  929. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  930. flagVar += lang;
  931. const char* includeFlag = this->Makefile->GetDefinition(flagVar.c_str());
  932. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  933. flagVar += lang;
  934. const char* sep = this->Makefile->GetDefinition(flagVar.c_str());
  935. bool quotePaths = false;
  936. if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
  937. {
  938. quotePaths = true;
  939. }
  940. bool repeatFlag = true;
  941. // should the include flag be repeated like ie. -IA -IB
  942. if(!sep)
  943. {
  944. sep = " ";
  945. }
  946. else
  947. {
  948. // if there is a separator then the flag is not repeated but is only
  949. // given once i.e. -classpath a:b:c
  950. repeatFlag = false;
  951. }
  952. bool flagUsed = false;
  953. std::set<cmStdString> emitted;
  954. for(i = includes.begin(); i != includes.end(); ++i)
  955. {
  956. #ifdef __APPLE__
  957. if(cmSystemTools::IsPathToFramework(i->c_str()))
  958. {
  959. std::string frameworkDir = *i;
  960. frameworkDir += "/../";
  961. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  962. if(emitted.insert(frameworkDir).second)
  963. {
  964. includeFlags
  965. << "-F"
  966. << this->ConvertToOutputForExisting(frameworkDir.c_str()) << " ";
  967. }
  968. continue;
  969. }
  970. #endif
  971. std::string include = *i;
  972. if(!flagUsed || repeatFlag)
  973. {
  974. includeFlags << includeFlag;
  975. flagUsed = true;
  976. }
  977. std::string includePath = this->ConvertToOutputForExisting(i->c_str());
  978. if(quotePaths && includePath.size() && includePath[0] != '\"')
  979. {
  980. includeFlags << "\"";
  981. }
  982. includeFlags << includePath;
  983. if(quotePaths && includePath.size() && includePath[0] != '\"')
  984. {
  985. includeFlags << "\"";
  986. }
  987. includeFlags << sep;
  988. }
  989. std::string flags = includeFlags.str();
  990. // remove trailing separators
  991. if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
  992. {
  993. flags[flags.size()-1] = ' ';
  994. }
  995. flags += this->Makefile->GetDefineFlags();
  996. this->LanguageToIncludeFlags[lang] = flags;
  997. // Use this temorary variable for the return value to work-around a
  998. // bogus GCC 2.95 warning.
  999. const char* ret = this->LanguageToIncludeFlags[lang].c_str();
  1000. return ret;
  1001. }
  1002. //----------------------------------------------------------------------------
  1003. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs)
  1004. {
  1005. // Need to decide whether to automatically include the source and
  1006. // binary directories at the beginning of the include path.
  1007. bool includeSourceDir = false;
  1008. bool includeBinaryDir = false;
  1009. // When automatic include directories are requested for a build then
  1010. // include the source and binary directories at the beginning of the
  1011. // include path to approximate include file behavior for an
  1012. // in-source build. This does not account for the case of a source
  1013. // file in a subdirectory of the current source directory but we
  1014. // cannot fix this because not all native build tools support
  1015. // per-source-file include paths.
  1016. if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
  1017. {
  1018. includeSourceDir = true;
  1019. includeBinaryDir = true;
  1020. }
  1021. // CMake versions below 2.0 would add the source tree to the -I path
  1022. // automatically. Preserve compatibility.
  1023. const char* versionValue =
  1024. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  1025. int major = 0;
  1026. int minor = 0;
  1027. if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
  1028. {
  1029. versionValue = 0;
  1030. }
  1031. if(versionValue && major < 2)
  1032. {
  1033. includeSourceDir = true;
  1034. }
  1035. // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
  1036. // not set the backwards compatibility level automatically.
  1037. const char* vtkSourceDir =
  1038. this->Makefile->GetDefinition("VTK_SOURCE_DIR");
  1039. if(vtkSourceDir)
  1040. {
  1041. const char* vtk_major =
  1042. this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
  1043. const char* vtk_minor =
  1044. this->Makefile->GetDefinition("VTK_MINOR_VERSION");
  1045. vtk_major = vtk_major? vtk_major : "4";
  1046. vtk_minor = vtk_minor? vtk_minor : "4";
  1047. int vmajor = 0;
  1048. int vminor = 0;
  1049. if(sscanf(vtk_major, "%d", &vmajor) &&
  1050. sscanf(vtk_minor, "%d", &vminor) && vmajor == 4 && vminor <= 4)
  1051. {
  1052. includeSourceDir = true;
  1053. }
  1054. }
  1055. // Do not repeat an include path.
  1056. std::set<cmStdString> emitted;
  1057. // Store the automatic include paths.
  1058. if(includeBinaryDir)
  1059. {
  1060. dirs.push_back(this->Makefile->GetStartOutputDirectory());
  1061. emitted.insert(this->Makefile->GetStartOutputDirectory());
  1062. }
  1063. if(includeSourceDir)
  1064. {
  1065. if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
  1066. {
  1067. dirs.push_back(this->Makefile->GetStartDirectory());
  1068. emitted.insert(this->Makefile->GetStartDirectory());
  1069. }
  1070. }
  1071. // Do not explicitly add the standard include path "/usr/include".
  1072. // This can cause problems with certain standard library
  1073. // implementations because the wrong headers may be found first.
  1074. emitted.insert("/usr/include");
  1075. if(const char* implicitIncludes = this->Makefile->GetDefinition
  1076. ("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  1077. {
  1078. std::vector<std::string> implicitIncludeVec;
  1079. cmSystemTools::ExpandListArgument(implicitIncludes, implicitIncludeVec);
  1080. for(unsigned int k = 0; k < implicitIncludeVec.size(); ++k)
  1081. {
  1082. emitted.insert(implicitIncludeVec[k]);
  1083. }
  1084. }
  1085. // Get the project-specified include directories.
  1086. std::vector<std::string>& includes =
  1087. this->Makefile->GetIncludeDirectories();
  1088. // Support putting all the in-project include directories first if
  1089. // it is requested by the project.
  1090. if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
  1091. {
  1092. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1093. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1094. for(std::vector<std::string>::iterator i = includes.begin();
  1095. i != includes.end(); ++i)
  1096. {
  1097. // Emit this directory only if it is a subdirectory of the
  1098. // top-level source or binary tree.
  1099. if(cmSystemTools::ComparePath(i->c_str(), topSourceDir) ||
  1100. cmSystemTools::ComparePath(i->c_str(), topBinaryDir) ||
  1101. cmSystemTools::IsSubDirectory(i->c_str(), topSourceDir) ||
  1102. cmSystemTools::IsSubDirectory(i->c_str(), topBinaryDir))
  1103. {
  1104. if(emitted.insert(*i).second)
  1105. {
  1106. dirs.push_back(*i);
  1107. }
  1108. }
  1109. }
  1110. }
  1111. // Construct the final ordered include directory list.
  1112. for(std::vector<std::string>::iterator i = includes.begin();
  1113. i != includes.end(); ++i)
  1114. {
  1115. if(emitted.insert(*i).second)
  1116. {
  1117. dirs.push_back(*i);
  1118. }
  1119. }
  1120. }
  1121. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1122. std::string& flags,
  1123. std::string& linkFlags,
  1124. cmTarget& target)
  1125. {
  1126. std::string buildType =
  1127. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1128. buildType = cmSystemTools::UpperCase(buildType);
  1129. const char* libraryLinkVariable =
  1130. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1131. switch(target.GetType())
  1132. {
  1133. case cmTarget::STATIC_LIBRARY:
  1134. {
  1135. const char* targetLinkFlags =
  1136. target.GetProperty("STATIC_LIBRARY_FLAGS");
  1137. if(targetLinkFlags)
  1138. {
  1139. linkFlags += targetLinkFlags;
  1140. linkFlags += " ";
  1141. }
  1142. }
  1143. break;
  1144. case cmTarget::MODULE_LIBRARY:
  1145. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1146. case cmTarget::SHARED_LIBRARY:
  1147. {
  1148. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  1149. linkFlags += " ";
  1150. if(buildType.size())
  1151. {
  1152. std::string build = libraryLinkVariable;
  1153. build += "_";
  1154. build += buildType;
  1155. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1156. linkFlags += " ";
  1157. }
  1158. if(this->Makefile->IsOn("WIN32") &&
  1159. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  1160. {
  1161. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1162. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1163. i != sources.end(); ++i)
  1164. {
  1165. if((*i)->GetSourceExtension() == "def")
  1166. {
  1167. linkFlags +=
  1168. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1169. linkFlags += this->Convert((*i)->GetFullPath().c_str(),
  1170. START_OUTPUT,MAKEFILE);
  1171. linkFlags += " ";
  1172. }
  1173. }
  1174. }
  1175. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1176. if(targetLinkFlags)
  1177. {
  1178. linkFlags += targetLinkFlags;
  1179. linkFlags += " ";
  1180. std::string configLinkFlags = targetLinkFlags;
  1181. configLinkFlags += buildType;
  1182. targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
  1183. if(targetLinkFlags)
  1184. {
  1185. linkFlags += targetLinkFlags;
  1186. linkFlags += " ";
  1187. }
  1188. }
  1189. cmOStringStream linklibsStr;
  1190. this->OutputLinkLibraries(linklibsStr, target, false);
  1191. linkLibs = linklibsStr.str();
  1192. }
  1193. break;
  1194. case cmTarget::EXECUTABLE:
  1195. {
  1196. linkFlags +=
  1197. this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1198. linkFlags += " ";
  1199. if(buildType.size())
  1200. {
  1201. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1202. build += buildType;
  1203. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1204. linkFlags += " ";
  1205. }
  1206. const char* linkLanguage =
  1207. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1208. if(!linkLanguage)
  1209. {
  1210. cmSystemTools::Error
  1211. ("CMake can not determine linker language for target:",
  1212. target.GetName());
  1213. return;
  1214. }
  1215. std::string langVar = "CMAKE_";
  1216. langVar += linkLanguage;
  1217. std::string flagsVar = langVar + "_FLAGS";
  1218. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  1219. sharedFlagsVar += linkLanguage;
  1220. sharedFlagsVar += "_FLAGS";
  1221. flags += this->Makefile->GetSafeDefinition(flagsVar.c_str());
  1222. flags += " ";
  1223. flags += this->Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  1224. flags += " ";
  1225. cmOStringStream linklibs;
  1226. this->OutputLinkLibraries(linklibs, target, false);
  1227. linkLibs = linklibs.str();
  1228. if(cmSystemTools::IsOn
  1229. (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1230. {
  1231. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
  1232. + linkLanguage + std::string("_FLAGS");
  1233. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
  1234. linkFlags += " ";
  1235. }
  1236. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1237. {
  1238. linkFlags +=
  1239. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1240. linkFlags += " ";
  1241. }
  1242. else
  1243. {
  1244. linkFlags +=
  1245. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1246. linkFlags += " ";
  1247. }
  1248. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1249. if(targetLinkFlags)
  1250. {
  1251. linkFlags += targetLinkFlags;
  1252. linkFlags += " ";
  1253. std::string configLinkFlags = targetLinkFlags;
  1254. configLinkFlags += buildType;
  1255. targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
  1256. if(targetLinkFlags)
  1257. {
  1258. linkFlags += targetLinkFlags;
  1259. linkFlags += " ";
  1260. }
  1261. }
  1262. }
  1263. break;
  1264. case cmTarget::UTILITY:
  1265. case cmTarget::GLOBAL_TARGET:
  1266. case cmTarget::INSTALL_FILES:
  1267. case cmTarget::INSTALL_PROGRAMS:
  1268. case cmTarget::INSTALL_DIRECTORY:
  1269. break;
  1270. }
  1271. }
  1272. /**
  1273. * Output the linking rules on a command line. For executables,
  1274. * targetLibrary should be a NULL pointer. For libraries, it should point
  1275. * to the name of the library. This will not link a library against itself.
  1276. */
  1277. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  1278. cmTarget& tgt,
  1279. bool relink)
  1280. {
  1281. // Try to emit each search path once
  1282. std::set<cmStdString> emitted;
  1283. // Embed runtime search paths if possible and if required.
  1284. bool outputRuntime = true;
  1285. std::string runtimeFlag;
  1286. std::string runtimeSep;
  1287. const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1288. const char* linkLanguage =
  1289. tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1290. if(!linkLanguage)
  1291. {
  1292. cmSystemTools::
  1293. Error("CMake can not determine linker language for target:",
  1294. tgt.GetName());
  1295. return;
  1296. }
  1297. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1298. runTimeFlagVar += linkLanguage;
  1299. runTimeFlagVar += "_FLAG";
  1300. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1301. runtimeFlag = this->Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1302. runtimeSep = this->Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1303. // concatenate all paths or no?
  1304. bool runtimeConcatenate = ( runtimeSep!="" );
  1305. if(runtimeFlag == "" || this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
  1306. {
  1307. outputRuntime = false;
  1308. }
  1309. // Some search paths should never be emitted
  1310. emitted.insert("");
  1311. emitted.insert("/usr/lib");
  1312. std::string libPathFlag =
  1313. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1314. std::string libLinkFlag =
  1315. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1316. // collect all the flags needed for linking libraries
  1317. std::string linkLibs;
  1318. // Flags to link an executable to shared libraries.
  1319. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1320. linkFlagsVar += linkLanguage;
  1321. linkFlagsVar += "_FLAGS";
  1322. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1323. {
  1324. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1325. linkLibs += " ";
  1326. }
  1327. // Compute the link library and directory information.
  1328. std::vector<cmStdString> libNames;
  1329. std::vector<cmStdString> libDirs;
  1330. this->ComputeLinkInformation(tgt, config, libNames, libDirs);
  1331. // Select whether to generate an rpath for the install tree or the
  1332. // build tree.
  1333. bool linking_for_install =
  1334. relink || tgt.GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  1335. bool use_install_rpath =
  1336. outputRuntime && tgt.HaveInstallTreeRPATH() && linking_for_install;
  1337. bool use_build_rpath =
  1338. outputRuntime && tgt.HaveBuildTreeRPATH() && !linking_for_install;
  1339. bool use_link_rpath =
  1340. outputRuntime && linking_for_install &&
  1341. tgt.GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
  1342. // Construct the RPATH.
  1343. std::vector<std::string> runtimeDirs;
  1344. if(use_install_rpath)
  1345. {
  1346. const char* install_rpath = tgt.GetProperty("INSTALL_RPATH");
  1347. cmSystemTools::ExpandListArgument(install_rpath, runtimeDirs);
  1348. for(unsigned int i=0; i < runtimeDirs.size(); ++i)
  1349. {
  1350. runtimeDirs[i] =
  1351. this->Convert(runtimeDirs[i].c_str(), FULL, SHELL, false);
  1352. }
  1353. }
  1354. // Append the library search path flags.
  1355. for(std::vector<cmStdString>::const_iterator libDir = libDirs.begin();
  1356. libDir != libDirs.end(); ++libDir)
  1357. {
  1358. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1359. if(emitted.insert(libpath).second)
  1360. {
  1361. std::string fullLibPath;
  1362. if(!this->WindowsShell && this->UseRelativePaths)
  1363. {
  1364. fullLibPath = "\"`cd ";
  1365. }
  1366. fullLibPath += libpath;
  1367. if(!this->WindowsShell && this->UseRelativePaths)
  1368. {
  1369. fullLibPath += ";pwd`\"";
  1370. }
  1371. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  1372. if((pos == std::string::npos || pos > 0)
  1373. && libDir->find("${") == std::string::npos)
  1374. {
  1375. linkLibs += libPathFlag;
  1376. linkLibs += fullLibPath;
  1377. linkLibs += " ";
  1378. // Put this directory in the rpath if using build-tree rpath
  1379. // support or if using the link path as an rpath.
  1380. if(use_build_rpath)
  1381. {
  1382. runtimeDirs.push_back(fullLibPath);
  1383. }
  1384. else if(use_link_rpath)
  1385. {
  1386. // Do not add any path inside the source or build tree.
  1387. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1388. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1389. if(!cmSystemTools::ComparePath(libDir->c_str(), topSourceDir) &&
  1390. !cmSystemTools::ComparePath(libDir->c_str(), topBinaryDir) &&
  1391. !cmSystemTools::IsSubDirectory(libDir->c_str(), topSourceDir) &&
  1392. !cmSystemTools::IsSubDirectory(libDir->c_str(), topBinaryDir))
  1393. {
  1394. runtimeDirs.push_back(fullLibPath);
  1395. }
  1396. }
  1397. }
  1398. }
  1399. }
  1400. // Append the link libraries.
  1401. for(std::vector<cmStdString>::iterator lib = libNames.begin();
  1402. lib != libNames.end(); ++lib)
  1403. {
  1404. linkLibs += *lib;
  1405. linkLibs += " ";
  1406. }
  1407. fout << linkLibs;
  1408. if(!runtimeDirs.empty())
  1409. {
  1410. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1411. // a "-R a -R b -R c" type link line
  1412. fout << runtimeFlag;
  1413. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1414. fout << *itr;
  1415. ++itr;
  1416. for( ; itr != runtimeDirs.end(); ++itr )
  1417. {
  1418. if(runtimeConcatenate)
  1419. {
  1420. fout << runtimeSep << *itr;
  1421. }
  1422. else
  1423. {
  1424. fout << " " << runtimeFlag << *itr;
  1425. }
  1426. }
  1427. fout << " ";
  1428. }
  1429. // Add standard libraries for this language.
  1430. std::string standardLibsVar = "CMAKE_";
  1431. standardLibsVar += linkLanguage;
  1432. standardLibsVar += "_STANDARD_LIBRARIES";
  1433. if(const char* stdLibs =
  1434. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1435. {
  1436. fout << stdLibs << " ";
  1437. }
  1438. }
  1439. //----------------------------------------------------------------------------
  1440. void cmLocalGenerator
  1441. ::ComputeLinkInformation(cmTarget& target,
  1442. const char* config,
  1443. std::vector<cmStdString>& outLibs,
  1444. std::vector<cmStdString>& outDirs,
  1445. std::vector<cmStdString>* fullPathLibs)
  1446. {
  1447. // Compute which library configuration to link.
  1448. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  1449. if(config && cmSystemTools::UpperCase(config) == "DEBUG")
  1450. {
  1451. linkType = cmTarget::DEBUG;
  1452. }
  1453. // Get the list of libraries against which this target wants to link.
  1454. std::vector<std::string> linkLibraries;
  1455. const cmTarget::LinkLibraryVectorType& inLibs = target.GetLinkLibraries();
  1456. for(cmTarget::LinkLibraryVectorType::const_iterator j = inLibs.begin();
  1457. j != inLibs.end(); ++j)
  1458. {
  1459. // For backwards compatibility variables may have been expanded
  1460. // inside library names. Clean up the resulting name.
  1461. std::string lib = j->first;
  1462. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  1463. if(pos != lib.npos)
  1464. {
  1465. lib = lib.substr(pos, lib.npos);
  1466. }
  1467. pos = lib.find_last_not_of(" \t\r\n");
  1468. if(pos != lib.npos)
  1469. {
  1470. lib = lib.substr(0, pos+1);
  1471. }
  1472. if(lib.empty())
  1473. {
  1474. continue;
  1475. }
  1476. // Link to a library if it is not the same target and is meant for
  1477. // this configuration type.
  1478. if((target.GetType() == cmTarget::EXECUTABLE ||
  1479. lib != target.GetName()) &&
  1480. (j->second == cmTarget::GENERAL || j->second == linkType))
  1481. {
  1482. // Compute the proper name to use to link this library.
  1483. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, lib.c_str());
  1484. if(tgt)
  1485. {
  1486. // This is a CMake target. Ask the target for its real name.
  1487. // Pass the full path to the target file but purposely leave
  1488. // off the per-configuration subdirectory. The link directory
  1489. // ordering knows how to deal with this.
  1490. std::string linkItem = tgt->GetDirectory(0);
  1491. linkItem += "/";
  1492. linkItem += tgt->GetFullName(config);
  1493. linkLibraries.push_back(linkItem);
  1494. // For full path, use the true location.
  1495. if(fullPathLibs)
  1496. {
  1497. fullPathLibs->push_back(tgt->GetFullPath(config));
  1498. }
  1499. }
  1500. else
  1501. {
  1502. // This is not a CMake target. Use the name given.
  1503. linkLibraries.push_back(lib);
  1504. // Add to the list of full paths if this library is one.
  1505. if(fullPathLibs &&
  1506. cmSystemTools::FileIsFullPath(lib.c_str()) &&
  1507. !cmSystemTools::FileIsDirectory(lib.c_str()))
  1508. {
  1509. fullPathLibs->push_back(lib);
  1510. }
  1511. }
  1512. }
  1513. }
  1514. // Get the list of directories the target wants to search for libraries.
  1515. const std::vector<std::string>&
  1516. linkDirectories = target.GetLinkDirectories();
  1517. // Compute the link directory order needed to link the libraries.
  1518. cmOrderLinkDirectories orderLibs;
  1519. orderLibs.SetLinkPrefix(
  1520. this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
  1521. orderLibs.AddLinkExtension(
  1522. this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"));
  1523. orderLibs.AddLinkExtension(
  1524. this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"));
  1525. orderLibs.AddLinkExtension(
  1526. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"));
  1527. orderLibs.AddLinkExtension(
  1528. this->Makefile->GetDefinition("CMAKE_LINK_LIBRARY_SUFFIX"));
  1529. if(const char* linkSuffixes =
  1530. this->Makefile->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS"))
  1531. {
  1532. std::vector<std::string> linkSuffixVec;
  1533. cmSystemTools::ExpandListArgument(linkSuffixes, linkSuffixVec);
  1534. for(std::vector<std::string>::iterator i = linkSuffixVec.begin();
  1535. i != linkSuffixVec.end(); ++i)
  1536. {
  1537. orderLibs.AddLinkExtension(i->c_str());
  1538. }
  1539. }
  1540. std::string configSubdir;
  1541. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  1542. gg->AppendDirectoryForConfig("", config, "", configSubdir);
  1543. orderLibs.SetLinkInformation(target.GetName(),
  1544. linkLibraries,
  1545. linkDirectories,
  1546. gg->GetTargetManifest(),
  1547. configSubdir.c_str());
  1548. orderLibs.DetermineLibraryPathOrder();
  1549. std::vector<cmStdString> orderedLibs;
  1550. orderLibs.GetLinkerInformation(outDirs, orderedLibs);
  1551. // Make sure libraries are linked with the proper syntax.
  1552. std::string libLinkFlag =
  1553. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1554. std::string libLinkSuffix =
  1555. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1556. for(std::vector<cmStdString>::iterator l = orderedLibs.begin();
  1557. l != orderedLibs.end(); ++l)
  1558. {
  1559. std::string lib = *l;
  1560. if(lib[0] == '-' || lib[0] == '$' || lib[0] == '`')
  1561. {
  1562. // The library is linked with special syntax by the user.
  1563. outLibs.push_back(lib);
  1564. }
  1565. else
  1566. {
  1567. // Generate the proper link syntax.
  1568. lib = libLinkFlag;
  1569. lib += *l;
  1570. lib += libLinkSuffix;
  1571. outLibs.push_back(lib);
  1572. }
  1573. }
  1574. }
  1575. //----------------------------------------------------------------------------
  1576. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1577. const char* lang,
  1578. const char* config)
  1579. {
  1580. // Add language-specific flags.
  1581. std::string flagsVar = "CMAKE_";
  1582. flagsVar += lang;
  1583. flagsVar += "_FLAGS";
  1584. if(this->EmitUniversalBinaryFlags)
  1585. {
  1586. const char* osxArch =
  1587. this->Makefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  1588. const char* sysroot =
  1589. this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1590. if(osxArch && sysroot && lang && lang[0] =='C')
  1591. {
  1592. std::vector<std::string> archs;
  1593. cmSystemTools::ExpandListArgument(std::string(osxArch),
  1594. archs);
  1595. if(archs.size() > 1)
  1596. {
  1597. for( std::vector<std::string>::iterator i = archs.begin();
  1598. i != archs.end(); ++i)
  1599. {
  1600. flags += " -arch ";
  1601. flags += *i;
  1602. }
  1603. flags += " -isysroot ";
  1604. flags += sysroot;
  1605. }
  1606. }
  1607. }
  1608. this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
  1609. }
  1610. //----------------------------------------------------------------------------
  1611. std::string cmLocalGenerator::GetRealDependency(const char* inName,
  1612. const char* config)
  1613. {
  1614. // Older CMake code may specify the dependency using the target
  1615. // output file rather than the target name. Such code would have
  1616. // been written before there was support for target properties that
  1617. // modify the name so stripping down to just the file name should
  1618. // produce the target name in this case.
  1619. std::string name = cmSystemTools::GetFilenameName(inName);
  1620. if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
  1621. {
  1622. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1623. }
  1624. // Look for a CMake target with the given name.
  1625. if(cmTarget* target = this->GlobalGenerator->FindTarget(0, name.c_str()))
  1626. {
  1627. switch (target->GetType())
  1628. {
  1629. case cmTarget::EXECUTABLE:
  1630. case cmTarget::STATIC_LIBRARY:
  1631. case cmTarget::SHARED_LIBRARY:
  1632. case cmTarget::MODULE_LIBRARY:
  1633. {
  1634. // Get the location of the target's output file and depend on it.
  1635. if(const char* location = target->GetLocation(config))
  1636. {
  1637. return location;
  1638. }
  1639. }
  1640. break;
  1641. case cmTarget::UTILITY:
  1642. case cmTarget::GLOBAL_TARGET:
  1643. // Depending on a utility target may not work but just trust
  1644. // the user to have given a valid name.
  1645. return inName;
  1646. case cmTarget::INSTALL_FILES:
  1647. case cmTarget::INSTALL_PROGRAMS:
  1648. case cmTarget::INSTALL_DIRECTORY:
  1649. break;
  1650. }
  1651. }
  1652. // The name was not that of a CMake target. It must name a file.
  1653. if(cmSystemTools::FileIsFullPath(inName))
  1654. {
  1655. // This is a full path. Return it as given.
  1656. return inName;
  1657. }
  1658. // Treat the name as relative to the source directory in which it
  1659. // was given.
  1660. name = this->Makefile->GetCurrentDirectory();
  1661. name += "/";
  1662. name += inName;
  1663. return name;
  1664. }
  1665. //----------------------------------------------------------------------------
  1666. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1667. const char* lang,
  1668. bool shared)
  1669. {
  1670. std::string flagsVar;
  1671. // Add flags for dealing with shared libraries for this language.
  1672. if(shared)
  1673. {
  1674. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1675. flagsVar += lang;
  1676. flagsVar += "_FLAGS";
  1677. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1678. }
  1679. // Add flags specific to shared builds.
  1680. if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1681. {
  1682. flagsVar = "CMAKE_SHARED_BUILD_";
  1683. flagsVar += lang;
  1684. flagsVar += "_FLAGS";
  1685. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1686. }
  1687. }
  1688. //----------------------------------------------------------------------------
  1689. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1690. const char* var,
  1691. const char* config)
  1692. {
  1693. // Add the flags from the variable itself.
  1694. std::string flagsVar = var;
  1695. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1696. // Add the flags from the build-type specific variable.
  1697. if(config && *config)
  1698. {
  1699. flagsVar += "_";
  1700. flagsVar += cmSystemTools::UpperCase(config);
  1701. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1702. }
  1703. }
  1704. //----------------------------------------------------------------------------
  1705. void cmLocalGenerator::AppendFlags(std::string& flags,
  1706. const char* newFlags)
  1707. {
  1708. if(newFlags && *newFlags)
  1709. {
  1710. if(flags.size())
  1711. {
  1712. flags += " ";
  1713. }
  1714. flags += newFlags;
  1715. }
  1716. }
  1717. //----------------------------------------------------------------------------
  1718. std::string
  1719. cmLocalGenerator::ConstructScript(const cmCustomCommandLines& commandLines,
  1720. const char* workingDirectory,
  1721. const char* newline)
  1722. {
  1723. // Store the script in a string.
  1724. std::string script;
  1725. if(workingDirectory)
  1726. {
  1727. script += "cd ";
  1728. script += this->Convert(workingDirectory, START_OUTPUT, SHELL);
  1729. script += newline;
  1730. }
  1731. // for visual studio IDE add extra stuff to the PATH
  1732. // if CMAKE_MSVCIDE_RUN_PATH is set.
  1733. if(this->Makefile->GetDefinition("MSVC_IDE"))
  1734. {
  1735. const char* extraPath =
  1736. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  1737. if(extraPath)
  1738. {
  1739. script += "set PATH=";
  1740. script += extraPath;
  1741. script += ";%PATH%";
  1742. script += newline;
  1743. }
  1744. }
  1745. // Write each command on a single line.
  1746. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  1747. cl != commandLines.end(); ++cl)
  1748. {
  1749. // Start with the command name.
  1750. const cmCustomCommandLine& commandLine = *cl;
  1751. script += this->Convert(commandLine[0].c_str(),START_OUTPUT,SHELL);
  1752. // Add the arguments.
  1753. for(unsigned int j=1;j < commandLine.size(); ++j)
  1754. {
  1755. script += " ";
  1756. script += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  1757. }
  1758. // End the line.
  1759. script += newline;
  1760. }
  1761. return script;
  1762. }
  1763. //----------------------------------------------------------------------------
  1764. std::string
  1765. cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
  1766. const char* default_comment)
  1767. {
  1768. // Check for a comment provided with the command.
  1769. if(cc.GetComment() && *cc.GetComment())
  1770. {
  1771. return cc.GetComment();
  1772. }
  1773. // Construct a reasonable default comment if possible.
  1774. if(!cc.GetOutputs().empty())
  1775. {
  1776. std::string comment;
  1777. comment = "Generating ";
  1778. const char* sep = "";
  1779. for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
  1780. o != cc.GetOutputs().end(); ++o)
  1781. {
  1782. comment += sep;
  1783. comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
  1784. sep = ", ";
  1785. }
  1786. return comment;
  1787. }
  1788. // Otherwise use the provided default.
  1789. return default_comment;
  1790. }
  1791. //----------------------------------------------------------------------------
  1792. std::string
  1793. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  1794. {
  1795. return this->Convert(remote, START_OUTPUT, SHELL, true);
  1796. }
  1797. //----------------------------------------------------------------------------
  1798. std::string cmLocalGenerator::Convert(const char* source,
  1799. RelativeRoot relative,
  1800. OutputFormat output,
  1801. bool optional)
  1802. {
  1803. // Convert the path to a relative path.
  1804. std::string result = source;
  1805. if (!optional || this->UseRelativePaths)
  1806. {
  1807. switch (relative)
  1808. {
  1809. case HOME:
  1810. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1811. result = this->GlobalGenerator->
  1812. ConvertToRelativePath(this->HomeDirectoryComponents,
  1813. result.c_str());
  1814. break;
  1815. case START:
  1816. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1817. result = this->GlobalGenerator->
  1818. ConvertToRelativePath(this->StartDirectoryComponents,
  1819. result.c_str());
  1820. break;
  1821. case HOME_OUTPUT:
  1822. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1823. result = this->GlobalGenerator->
  1824. ConvertToRelativePath(this->HomeOutputDirectoryComponents,
  1825. result.c_str());
  1826. break;
  1827. case START_OUTPUT:
  1828. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1829. result = this->GlobalGenerator->
  1830. ConvertToRelativePath(this->StartOutputDirectoryComponents,
  1831. result.c_str());
  1832. break;
  1833. case FULL:
  1834. result = cmSystemTools::CollapseFullPath(result.c_str());
  1835. break;
  1836. case NONE:
  1837. break;
  1838. }
  1839. }
  1840. // Now convert it to an output path.
  1841. if (output == MAKEFILE)
  1842. {
  1843. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1844. }
  1845. if( output == SHELL)
  1846. {
  1847. // for shell commands if force unix is on, but this->WindowsShell
  1848. // is true, then turn off force unix paths for the output path
  1849. // so that the path is windows style and will work with windows
  1850. // cmd.exe.
  1851. bool forceOn = cmSystemTools::GetForceUnixPaths();
  1852. if(forceOn && this->WindowsShell)
  1853. {
  1854. cmSystemTools::SetForceUnixPaths(false);
  1855. }
  1856. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1857. if(forceOn && this->WindowsShell)
  1858. {
  1859. cmSystemTools::SetForceUnixPaths(true);
  1860. }
  1861. }
  1862. return result;
  1863. }
  1864. //----------------------------------------------------------------------------
  1865. void
  1866. cmLocalGenerator
  1867. ::GenerateTargetInstallRules(
  1868. std::ostream& os, const char* config,
  1869. std::vector<std::string> const& configurationTypes)
  1870. {
  1871. // Convert the old-style install specification from each target to
  1872. // an install generator and run it.
  1873. cmTargets& tgts = this->Makefile->GetTargets();
  1874. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  1875. {
  1876. // Include the user-specified pre-install script for this target.
  1877. if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
  1878. {
  1879. cmInstallScriptGenerator g(preinstall);
  1880. g.Generate(os, config, configurationTypes);
  1881. }
  1882. // Install this target if a destination is given.
  1883. if(l->second.GetInstallPath() != "")
  1884. {
  1885. // Compute the full install destination. Note that converting
  1886. // to unix slashes also removes any trailing slash.
  1887. std::string destination = "${CMAKE_INSTALL_PREFIX}";
  1888. destination += l->second.GetInstallPath();
  1889. cmSystemTools::ConvertToUnixSlashes(destination);
  1890. // Generate the proper install generator for this target type.
  1891. switch(l->second.GetType())
  1892. {
  1893. case cmTarget::EXECUTABLE:
  1894. case cmTarget::STATIC_LIBRARY:
  1895. case cmTarget::MODULE_LIBRARY:
  1896. {
  1897. // Use a target install generator.
  1898. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  1899. g.Generate(os, config, configurationTypes);
  1900. }
  1901. break;
  1902. case cmTarget::SHARED_LIBRARY:
  1903. {
  1904. #if defined(_WIN32) || defined(__CYGWIN__)
  1905. // Special code to handle DLL. Install the import library
  1906. // to the normal destination and the DLL to the runtime
  1907. // destination.
  1908. cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
  1909. g1.Generate(os, config, configurationTypes);
  1910. destination = "${CMAKE_INSTALL_PREFIX}";
  1911. destination += l->second.GetRuntimeInstallPath();
  1912. cmSystemTools::ConvertToUnixSlashes(destination);
  1913. cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
  1914. g2.Generate(os, config, configurationTypes);
  1915. #else
  1916. // Use a target install generator.
  1917. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  1918. g.Generate(os, config, configurationTypes);
  1919. #endif
  1920. }
  1921. break;
  1922. case cmTarget::INSTALL_FILES:
  1923. {
  1924. // Use a file install generator.
  1925. const char* no_permissions = "";
  1926. const char* no_rename = "";
  1927. const char* no_component = "";
  1928. std::vector<std::string> no_configurations;
  1929. cmInstallFilesGenerator g(l->second.GetSourceLists(),
  1930. destination.c_str(), false,
  1931. no_permissions, no_configurations,
  1932. no_component, no_rename);
  1933. g.Generate(os, config, configurationTypes);
  1934. }
  1935. break;
  1936. case cmTarget::INSTALL_PROGRAMS:
  1937. {
  1938. // Use a file install generator.
  1939. const char* no_permissions = "";
  1940. const char* no_rename = "";
  1941. const char* no_component = "";
  1942. std::vector<std::string> no_configurations;
  1943. cmInstallFilesGenerator g(l->second.GetSourceLists(),
  1944. destination.c_str(), true,
  1945. no_permissions, no_configurations,
  1946. no_component, no_rename);
  1947. g.Generate(os, config, configurationTypes);
  1948. }
  1949. break;
  1950. case cmTarget::UTILITY:
  1951. default:
  1952. break;
  1953. }
  1954. }
  1955. // Include the user-specified post-install script for this target.
  1956. if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
  1957. {
  1958. cmInstallScriptGenerator g(postinstall);
  1959. g.Generate(os, config, configurationTypes);
  1960. }
  1961. }
  1962. }