cmLocalGenerator.cxx 64 KB

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