cmLocalGenerator.cxx 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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 "cmGlobalGenerator.h"
  15. #include "cmake.h"
  16. #include "cmMakefile.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmSourceFile.h"
  19. #include "cmOrderLinkDirectories.h"
  20. #include "cmTest.h"
  21. #include <ctype.h> // for isalpha
  22. cmLocalGenerator::cmLocalGenerator()
  23. {
  24. m_Makefile = new cmMakefile;
  25. m_Makefile->SetLocalGenerator(this);
  26. m_ExcludeFromAll = false;
  27. m_Parent = 0;
  28. m_WindowsShell = false;
  29. m_IgnoreLibPrefix = false;
  30. m_UseRelativePaths = false;
  31. this->Configured = false;
  32. }
  33. cmLocalGenerator::~cmLocalGenerator()
  34. {
  35. delete m_Makefile;
  36. }
  37. void cmLocalGenerator::Configure()
  38. {
  39. // find & read the list file
  40. std::string currentStart = m_Makefile->GetStartDirectory();
  41. currentStart += "/CMakeLists.txt";
  42. m_Makefile->ReadListFile(currentStart.c_str());
  43. // at the end of the ReadListFile handle any old style subdirs
  44. // first get all the subdirectories
  45. std::vector<cmLocalGenerator *> subdirs = this->GetChildren();
  46. // for each subdir recurse
  47. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  48. for (; sdi != subdirs.end(); ++sdi)
  49. {
  50. if (!(*sdi)->Configured)
  51. {
  52. m_Makefile->ConfigureSubDirectory(*sdi);
  53. }
  54. }
  55. this->SetupPathConversions();
  56. // Check whether relative paths should be used for optionally
  57. // relative paths.
  58. m_UseRelativePaths = m_Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
  59. this->Configured = true;
  60. }
  61. void cmLocalGenerator::SetupPathConversions()
  62. {
  63. // Setup the current output directory components for use by
  64. // Convert
  65. std::string outdir;
  66. outdir =
  67. cmSystemTools::CollapseFullPath(m_Makefile->GetHomeDirectory());
  68. cmSystemTools::SplitPath(outdir.c_str(), m_HomeDirectoryComponents);
  69. outdir =
  70. cmSystemTools::CollapseFullPath(m_Makefile->GetStartDirectory());
  71. cmSystemTools::SplitPath(outdir.c_str(), m_StartDirectoryComponents);
  72. outdir =
  73. cmSystemTools::CollapseFullPath(m_Makefile->GetHomeOutputDirectory());
  74. cmSystemTools::SplitPath(outdir.c_str(), m_HomeOutputDirectoryComponents);
  75. outdir =
  76. cmSystemTools::CollapseFullPath(m_Makefile->GetStartOutputDirectory());
  77. cmSystemTools::SplitPath(outdir.c_str(), m_StartOutputDirectoryComponents);
  78. }
  79. void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
  80. {
  81. m_GlobalGenerator = gg;
  82. // setup the home directories
  83. m_Makefile->SetHomeDirectory(
  84. gg->GetCMakeInstance()->GetHomeDirectory());
  85. m_Makefile->SetHomeOutputDirectory(
  86. gg->GetCMakeInstance()->GetHomeOutputDirectory());
  87. }
  88. void cmLocalGenerator::ConfigureFinalPass()
  89. {
  90. m_Makefile->ConfigureFinalPass();
  91. }
  92. void cmLocalGenerator::GenerateTestFiles()
  93. {
  94. if ( !m_Makefile->IsOn("CMAKE_TESTING_ENABLED") )
  95. {
  96. return;
  97. }
  98. std::string file = m_Makefile->GetStartOutputDirectory();
  99. file += "/";
  100. if ( m_Makefile->IsSet("CTEST_NEW_FORMAT") )
  101. {
  102. file += "CTestTestfile.cmake";
  103. }
  104. else
  105. {
  106. file += "DartTestfile.txt";
  107. }
  108. cmGeneratedFileStream fout(file.c_str());
  109. fout.SetCopyIfDifferent(true);
  110. fout << "# CMake generated Testfile for " << std::endl
  111. << "# Source directory: " << m_Makefile->GetStartDirectory() << std::endl
  112. << "# Build directory: " << m_Makefile->GetStartOutputDirectory() << std::endl
  113. << "# " << std::endl
  114. << "# This file replicates the SUBDIRS() and ADD_TEST() commands from the source" << std::endl
  115. << "# tree CMakeLists.txt file, skipping any SUBDIRS() or ADD_TEST() commands" << std::endl
  116. << "# that are excluded by CMake control structures, i.e. IF() commands." << std::endl
  117. << "#" << std::endl
  118. << "# The next line is critical for Dart to work" << std::endl
  119. << "# Duh :-)" << std::endl << std::endl;
  120. const char* testIncludeFile = m_Makefile->GetProperty("TEST_INCLUDE_FILE");
  121. if ( testIncludeFile )
  122. {
  123. fout << "INCLUDE(\"" << testIncludeFile << "\")" << std::endl;
  124. }
  125. const std::vector<cmTest*> *tests = m_Makefile->GetTests();
  126. std::vector<cmTest*>::const_iterator it;
  127. for ( it = tests->begin(); it != tests->end(); ++ it )
  128. {
  129. cmTest* test = *it;
  130. fout << "ADD_TEST(";
  131. fout << test->GetName() << " \"" << test->GetCommand() << "\"";
  132. std::vector<cmStdString>::iterator argit;
  133. for (argit = test->GetArguments().begin();
  134. argit != test->GetArguments().end(); ++argit)
  135. {
  136. // Just double-quote all arguments so they are re-parsed
  137. // correctly by the test system.
  138. fout << " \"";
  139. for(std::string::iterator c = argit->begin(); c != argit->end(); ++c)
  140. {
  141. // Escape quotes within arguments. We should escape
  142. // backslashes too but we cannot because it makes the result
  143. // inconsistent with previous behavior of this command.
  144. if((*c == '"'))
  145. {
  146. fout << '\\';
  147. }
  148. fout << *c;
  149. }
  150. fout << "\"";
  151. }
  152. fout << ")" << std::endl;
  153. }
  154. if ( this->Children.size())
  155. {
  156. fout << "SUBDIRS(";
  157. size_t i;
  158. std::string outDir = m_Makefile->GetStartOutputDirectory();
  159. outDir += "/";
  160. for(i = 0; i < this->Children.size(); ++i)
  161. {
  162. std::string binP = this->Children[i]->GetMakefile()->GetStartOutputDirectory();
  163. cmSystemTools::ReplaceString(binP, outDir.c_str(), "");
  164. if ( i > 0 )
  165. {
  166. fout << " ";
  167. }
  168. fout << binP.c_str();
  169. }
  170. fout << ")" << std::endl << std::endl;;
  171. }
  172. }
  173. void cmLocalGenerator::GenerateInstallRules()
  174. {
  175. cmTargets &tgts = m_Makefile->GetTargets();
  176. const char* prefix
  177. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  178. #if defined(_WIN32) && !defined(__CYGWIN__)
  179. std::string prefix_win32;
  180. if(!prefix)
  181. {
  182. if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32))
  183. {
  184. prefix_win32 = "C:";
  185. }
  186. const char* project_name = m_Makefile->GetDefinition("PROJECT_NAME");
  187. if(project_name && project_name[0])
  188. {
  189. prefix_win32 += "/Program Files/";
  190. prefix_win32 += project_name;
  191. }
  192. else
  193. {
  194. prefix_win32 += "/InstalledCMakeProject";
  195. }
  196. prefix = prefix_win32.c_str();
  197. }
  198. #else
  199. if (!prefix)
  200. {
  201. prefix = "/usr/local";
  202. }
  203. #endif
  204. std::string file = m_Makefile->GetStartOutputDirectory();
  205. std::string homedir = m_Makefile->GetHomeOutputDirectory();
  206. std::string currdir = m_Makefile->GetCurrentOutputDirectory();
  207. cmSystemTools::ConvertToUnixSlashes(file);
  208. cmSystemTools::ConvertToUnixSlashes(homedir);
  209. cmSystemTools::ConvertToUnixSlashes(currdir);
  210. int toplevel_install = 0;
  211. if ( currdir == homedir )
  212. {
  213. toplevel_install = 1;
  214. }
  215. file += "/cmake_install.cmake";
  216. cmGeneratedFileStream fout(file.c_str());
  217. fout.SetCopyIfDifferent(true);
  218. fout << "# Install script for directory: " << m_Makefile->GetCurrentDirectory()
  219. << std::endl << std::endl;
  220. const char* cmakeDebugPosfix = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  221. if ( cmakeDebugPosfix )
  222. {
  223. fout << "SET(CMAKE_DEBUG_POSTFIX \"" << cmakeDebugPosfix << "\")"
  224. << std::endl << std::endl;
  225. }
  226. std::string libOutPath = "";
  227. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  228. {
  229. libOutPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  230. if(libOutPath.size())
  231. {
  232. if(libOutPath[libOutPath.size() -1] != '/')
  233. {
  234. libOutPath += "/";
  235. }
  236. }
  237. }
  238. std::string exeOutPath = "";
  239. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  240. {
  241. exeOutPath =
  242. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  243. if(exeOutPath.size())
  244. {
  245. if(exeOutPath[exeOutPath.size() -1] != '/')
  246. {
  247. exeOutPath += "/";
  248. }
  249. }
  250. }
  251. if ( libOutPath.size() == 0 )
  252. {
  253. // LIBRARY_OUTPUT_PATH not defined
  254. libOutPath = currdir + "/";
  255. }
  256. if ( exeOutPath.size() == 0 )
  257. {
  258. // EXECUTABLE_OUTPUT_PATH not defined
  259. exeOutPath = currdir + "/";
  260. }
  261. std::string destination;
  262. for(cmTargets::iterator l = tgts.begin();
  263. l != tgts.end(); l++)
  264. {
  265. const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT");
  266. const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT");
  267. if ( preinstall )
  268. {
  269. fout << "INCLUDE(\"" << preinstall << "\")" << std::endl;
  270. }
  271. if (l->second.GetInstallPath() != "")
  272. {
  273. destination = prefix + l->second.GetInstallPath();
  274. cmSystemTools::ConvertToUnixSlashes(destination);
  275. const char* dest = destination.c_str();
  276. int type = l->second.GetType();
  277. std::string fname;
  278. const char* files;
  279. // now install the target
  280. switch (type)
  281. {
  282. case cmTarget::STATIC_LIBRARY:
  283. case cmTarget::MODULE_LIBRARY:
  284. fname = libOutPath;
  285. fname += l->second.GetFullName(m_Makefile);
  286. files = fname.c_str();
  287. this->AddInstallRule(fout, dest, type, files);
  288. break;
  289. case cmTarget::SHARED_LIBRARY:
  290. {
  291. // Special code to handle DLL
  292. fname = libOutPath;
  293. fname += l->second.GetFullName(m_Makefile);
  294. std::string ext = cmSystemTools::GetFilenameExtension(fname);
  295. ext = cmSystemTools::LowerCase(ext);
  296. if ( ext == ".dll" )
  297. {
  298. std::string libname = libOutPath;
  299. libname += cmSystemTools::GetFilenameWithoutExtension(fname);
  300. libname += ".lib";
  301. files = libname.c_str();
  302. this->AddInstallRule(fout, dest, cmTarget::STATIC_LIBRARY, files, true);
  303. std::string dlldest = prefix + l->second.GetRuntimeInstallPath();
  304. files = fname.c_str();
  305. this->AddInstallRule(fout, dlldest.c_str(), type, files);
  306. }
  307. else
  308. {
  309. files = fname.c_str();
  310. std::string properties;
  311. const char* lib_version = l->second.GetProperty("VERSION");
  312. const char* lib_soversion = l->second.GetProperty("SOVERSION");
  313. if(!m_Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SONAME_C_FLAG"))
  314. {
  315. // Versioning is supported only for shared libraries and modules,
  316. // and then only when the platform supports an soname flag.
  317. lib_version = 0;
  318. lib_soversion = 0;
  319. }
  320. if ( lib_version )
  321. {
  322. properties += " VERSION ";
  323. properties += lib_version;
  324. }
  325. if ( lib_soversion )
  326. {
  327. properties += " SOVERSION ";
  328. properties += lib_soversion;
  329. }
  330. this->AddInstallRule(fout, dest, type, files, false, properties.c_str());
  331. }
  332. }
  333. break;
  334. case cmTarget::EXECUTABLE:
  335. if(l->second.GetPropertyAsBool("MACOSX_BUNDLE"))
  336. {
  337. fname = exeOutPath;
  338. fname += l->second.GetFullName(m_Makefile);
  339. std::string plist = fname;
  340. plist += ".app/Contents/Info.plist";
  341. fname += ".app/Contents/MacOS/";
  342. fname += l->second.GetName();
  343. files = fname.c_str();
  344. std::string bdest = dest;
  345. bdest += "/";
  346. bdest += l->second.GetName();
  347. std::string pdest = bdest;
  348. pdest += ".app/Contents";
  349. bdest += ".app/Contents/MacOS";
  350. // first install the actual executable
  351. this->AddInstallRule(fout, bdest.c_str(), type, files);
  352. files = plist.c_str();
  353. // now install the Info.plist file
  354. this->AddInstallRule(fout, pdest.c_str(),
  355. cmTarget::INSTALL_FILES, files);
  356. }
  357. else
  358. {
  359. fname = exeOutPath;
  360. fname += l->second.GetFullName(m_Makefile);
  361. files = fname.c_str();
  362. this->AddInstallRule(fout, dest, type, files);
  363. }
  364. break;
  365. case cmTarget::INSTALL_FILES:
  366. {
  367. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  368. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  369. sourcePath += "/";
  370. binaryPath += "/";
  371. const std::vector<std::string> &sf = l->second.GetSourceLists();
  372. std::vector<std::string>::const_iterator i;
  373. for (i = sf.begin(); i != sf.end(); ++i)
  374. {
  375. std::string f = *i;
  376. if(f.substr(0, sourcePath.length()) == sourcePath)
  377. {
  378. f = f.substr(sourcePath.length());
  379. }
  380. else if(f.substr(0, binaryPath.length()) == binaryPath)
  381. {
  382. f = f.substr(binaryPath.length());
  383. }
  384. files = i->c_str();
  385. this->AddInstallRule(fout, dest, type, files);
  386. }
  387. }
  388. break;
  389. case cmTarget::INSTALL_PROGRAMS:
  390. {
  391. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  392. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  393. sourcePath += "/";
  394. binaryPath += "/";
  395. const std::vector<std::string> &sf = l->second.GetSourceLists();
  396. std::vector<std::string>::const_iterator i;
  397. for (i = sf.begin(); i != sf.end(); ++i)
  398. {
  399. std::string f = *i;
  400. if(f.substr(0, sourcePath.length()) == sourcePath)
  401. {
  402. f = f.substr(sourcePath.length());
  403. }
  404. else if(f.substr(0, binaryPath.length()) == binaryPath)
  405. {
  406. f = f.substr(binaryPath.length());
  407. }
  408. files = i->c_str();
  409. this->AddInstallRule(fout, dest, type, files);
  410. }
  411. }
  412. break;
  413. case cmTarget::UTILITY:
  414. default:
  415. break;
  416. }
  417. }
  418. if ( postinstall )
  419. {
  420. fout << "INCLUDE(\"" << postinstall << "\")" << std::endl;
  421. }
  422. }
  423. if ( this->Children.size())
  424. {
  425. std::vector<cmLocalGenerator*>::const_iterator i = this->Children.begin();
  426. for(; i != this->Children.end(); ++i)
  427. {
  428. std::string odir = (*i)->GetMakefile()->GetStartOutputDirectory();
  429. cmSystemTools::ConvertToUnixSlashes(odir);
  430. fout << "INCLUDE(\"" << odir.c_str()
  431. << "/cmake_install.cmake\")" << std::endl;
  432. }
  433. fout << std::endl;;
  434. }
  435. if ( toplevel_install )
  436. {
  437. fout << "FILE(WRITE \"" << homedir.c_str() << "/install_manifest.txt\" "
  438. << "\"\")" << std::endl;
  439. fout << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  440. << " FILE(APPEND \"" << homedir.c_str() << "/install_manifest.txt\" "
  441. << "\"${file}\\n\")" << std::endl
  442. << "ENDFOREACH(file)" << std::endl;
  443. }
  444. }
  445. void cmLocalGenerator::AddInstallRule(std::ostream& fout, const char* dest,
  446. int type, const char* files, bool optional /* = false */, const char* properties /* = 0 */)
  447. {
  448. std::string sfiles = files;
  449. std::string destination = dest;
  450. std::string stype;
  451. switch ( type )
  452. {
  453. case cmTarget::INSTALL_PROGRAMS: stype = "PROGRAM"; break;
  454. case cmTarget::EXECUTABLE: stype = "EXECUTABLE"; break;
  455. case cmTarget::STATIC_LIBRARY: stype = "STATIC_LIBRARY"; break;
  456. case cmTarget::SHARED_LIBRARY: stype = "SHARED_LIBRARY"; break;
  457. case cmTarget::MODULE_LIBRARY: stype = "MODULE"; break;
  458. case cmTarget::INSTALL_FILES:
  459. default: stype = "FILE"; break;
  460. }
  461. std::string fname = cmSystemTools::GetFilenameName(sfiles.c_str());
  462. fout
  463. << "MESSAGE(STATUS \"Installing " << destination.c_str()
  464. << "/" << fname.c_str() << "\")\n"
  465. << "FILE(INSTALL DESTINATION \"" << destination.c_str()
  466. << "\" TYPE " << stype.c_str() << (optional?" OPTIONAL":"") ;
  467. if ( properties && *properties )
  468. {
  469. fout << " PROPERTIES" << properties;
  470. }
  471. fout
  472. << " FILES \"" << sfiles.c_str() << "\")\n";
  473. }
  474. void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
  475. const char* lang,
  476. cmSourceFile& source,
  477. cmTarget& )
  478. {
  479. std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
  480. std::string sourceFile =
  481. this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
  482. std::string varString = "CMAKE_";
  483. varString += lang;
  484. varString += "_COMPILE_OBJECT";
  485. std::vector<std::string> rules;
  486. rules.push_back(m_Makefile->GetRequiredDefinition(varString.c_str()));
  487. varString = "CMAKE_";
  488. varString += lang;
  489. varString += "_FLAGS";
  490. std::string flags;
  491. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  492. flags += " ";
  493. flags += this->GetIncludeFlags(lang);
  494. std::vector<std::string> commands;
  495. cmSystemTools::ExpandList(rules, commands);
  496. for(std::vector<std::string>::iterator i = commands.begin();
  497. i != commands.end(); ++i)
  498. {
  499. this->ExpandRuleVariables(*i,
  500. lang,
  501. 0, // no objects
  502. 0, // no target
  503. 0, // no link libs
  504. sourceFile.c_str(),
  505. objectFile.c_str(),
  506. flags.c_str());
  507. }
  508. std::vector<std::string> sourceAndDeps;
  509. sourceAndDeps.push_back(sourceFile);
  510. if(commands.size() > 1)
  511. {
  512. cmSystemTools::Error("Currently custom rules can only have one command sorry ");
  513. }
  514. // Check for extra object-file dependencies.
  515. std::vector<std::string> depends;
  516. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  517. if(additionalDeps)
  518. {
  519. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  520. for(std::vector<std::string>::iterator i = depends.begin();
  521. i != depends.end(); ++i)
  522. {
  523. sourceAndDeps.push_back(this->Convert(i->c_str(),START_OUTPUT,SHELL));
  524. }
  525. }
  526. #if 0
  527. std::string command;
  528. std::string args;
  529. cmSystemTools::SplitProgramFromArgs(commands[0].c_str(), command, args);
  530. std::vector<std::string> argsv;
  531. argsv.push_back(args);
  532. m_Makefile->AddCustomCommandToOutput(ofname,
  533. command.c_str(),
  534. argsv,
  535. source.GetFullPath().c_str(),
  536. sourceAndDeps,
  537. "build from source");
  538. #endif
  539. }
  540. void cmLocalGenerator::AddBuildTargetRule(const char* llang, cmTarget& target)
  541. {
  542. cmStdString objs;
  543. std::vector<std::string> objVector;
  544. // Add all the sources outputs to the depends of the target
  545. std::vector<cmSourceFile*>& classes = target.GetSourceFiles();
  546. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  547. i != classes.end(); ++i)
  548. {
  549. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  550. !(*i)->GetCustomCommand())
  551. {
  552. std::string outExt =
  553. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  554. (*i)->GetSourceExtension().c_str());
  555. if(outExt.size() && !(*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  556. {
  557. std::string ofname = m_Makefile->GetCurrentOutputDirectory();
  558. ofname += "/";
  559. ofname += (*i)->GetSourceName() + outExt;
  560. objVector.push_back(ofname);
  561. this->AddCustomCommandToCreateObject(ofname.c_str(), llang, *(*i), target);
  562. objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
  563. objs += " ";
  564. }
  565. }
  566. }
  567. std::string createRule = "CMAKE_";
  568. createRule += llang;
  569. createRule += target.GetCreateRuleVariable();
  570. std::string targetName = target.GetFullName(m_Makefile);
  571. // Executable :
  572. // Shared Library:
  573. // Static Library:
  574. // Shared Module:
  575. std::string linkLibs; // should be set
  576. std::string flags; // should be set
  577. std::string linkFlags; // should be set
  578. this->GetTargetFlags(linkLibs, flags, linkFlags, target);
  579. std::string rule = m_Makefile->GetRequiredDefinition(createRule.c_str());
  580. this->ExpandRuleVariables(rule,
  581. llang, // language
  582. objs.c_str(), // objects
  583. targetName.c_str(), // target
  584. linkLibs.c_str(), // link libs
  585. 0, // source
  586. 0, // object
  587. flags.c_str(), // flags
  588. 0, // objects quoted
  589. 0, // target base name
  590. 0, // target so name,
  591. linkFlags.c_str() // link flags
  592. );
  593. #if 0
  594. std::string command;
  595. std::string args;
  596. cmSystemTools::SplitProgramFromArgs(rule.c_str(), command, args);
  597. // Just like ADD_CUSTOM_TARGET(foo ALL DEPENDS a.o b.o)
  598. // Add a custom command for generating each .o file
  599. cmCustomCommand cc(command.c_str(), args.c_str(), objVector,
  600. targetName.c_str(), 0);
  601. target.GetPostBuildCommands().push_back(cc);
  602. #endif
  603. }
  604. void cmLocalGenerator::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
  605. {
  606. cmTargets &tgts = m_Makefile->GetTargets();
  607. for(cmTargets::iterator l = tgts.begin();
  608. l != tgts.end(); l++)
  609. {
  610. cmTarget& target = l->second;
  611. switch(target.GetType())
  612. {
  613. case cmTarget::STATIC_LIBRARY:
  614. case cmTarget::SHARED_LIBRARY:
  615. case cmTarget::MODULE_LIBRARY:
  616. case cmTarget::EXECUTABLE:
  617. {
  618. const char* llang = target.GetLinkerLanguage(this->GetGlobalGenerator());
  619. if(!llang)
  620. {
  621. cmSystemTools::Error("CMake can not determine linker language for target:",
  622. target.GetName());
  623. return;
  624. }
  625. // if the language is not in the set lang then create custom
  626. // commands to build the target
  627. if(lang.count(llang) == 0)
  628. {
  629. this->AddBuildTargetRule(llang, target);
  630. }
  631. }
  632. break;
  633. case cmTarget::UTILITY:
  634. case cmTarget::INSTALL_FILES:
  635. case cmTarget::INSTALL_PROGRAMS:
  636. break;
  637. }
  638. }
  639. }
  640. // List of variables that are replaced when
  641. // rules are expanced. These variables are
  642. // replaced in the form <var> with GetSafeDefinition(var).
  643. // ${LANG} is replaced in the variable first with all enabled
  644. // languages.
  645. static const char* ruleReplaceVars[] =
  646. {
  647. "CMAKE_${LANG}_COMPILER",
  648. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  649. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  650. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  651. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  652. "CMAKE_${LANG}_LINK_FLAGS",
  653. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  654. "CMAKE_${LANG}_ARCHIVE",
  655. "CMAKE_AR",
  656. "CMAKE_CURRENT_SOURCE_DIR",
  657. "CMAKE_CURRENT_BINARY_DIR",
  658. "CMAKE_RANLIB",
  659. 0
  660. };
  661. std::string
  662. cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
  663. const char* lang,
  664. const char* objects,
  665. const char* target,
  666. const char* linkLibs,
  667. const char* source,
  668. const char* object,
  669. const char* flags,
  670. const char* objectsquoted,
  671. const char* targetBase,
  672. const char* targetSOName,
  673. const char* linkFlags)
  674. {
  675. if(linkFlags)
  676. {
  677. if(variable == "LINK_FLAGS")
  678. {
  679. return linkFlags;
  680. }
  681. }
  682. if(flags)
  683. {
  684. if(variable == "FLAGS")
  685. {
  686. return flags;
  687. }
  688. }
  689. if(source)
  690. {
  691. if(variable == "SOURCE")
  692. {
  693. return source;
  694. }
  695. }
  696. if(object)
  697. {
  698. if(variable == "OBJECT")
  699. {
  700. return object;
  701. }
  702. }
  703. if(objects)
  704. {
  705. if(variable == "OBJECTS")
  706. {
  707. return objects;
  708. }
  709. }
  710. if(objectsquoted)
  711. {
  712. if(variable == "OBJECTS_QUOTED")
  713. {
  714. return objectsquoted;
  715. }
  716. }
  717. if(target)
  718. {
  719. if(variable == "TARGET_QUOTED")
  720. {
  721. std::string targetQuoted = target;
  722. if(targetQuoted.size() && targetQuoted[0] != '\"')
  723. {
  724. targetQuoted = '\"';
  725. targetQuoted += target;
  726. targetQuoted += '\"';
  727. return targetQuoted;
  728. }
  729. }
  730. if(variable == "TARGET")
  731. {
  732. return target;
  733. }
  734. }
  735. if(targetBase)
  736. {
  737. if(variable == "TARGET_BASE.lib" || variable == "TARGET_BASE.dll")
  738. {
  739. // special case for quoted paths with spaces
  740. // if you see <TARGET_BASE>.lib then put the .lib inside
  741. // the quotes, same for .dll
  742. if((strlen(targetBase) > 1) && targetBase[0] == '\"')
  743. {
  744. std::string base = targetBase;
  745. base[base.size()-1] = '.';
  746. std::string baseLib = base + "lib\"";
  747. std::string baseDll = base + "dll\"";
  748. if(variable == "TARGET_BASE.lib" )
  749. {
  750. return baseLib;
  751. }
  752. if(variable == "TARGET_BASE.dll" )
  753. {
  754. return baseDll;
  755. }
  756. }
  757. }
  758. if(variable == "TARGET_BASE")
  759. {
  760. return targetBase;
  761. }
  762. }
  763. if(targetSOName)
  764. {
  765. if(variable == "TARGET_SONAME")
  766. {
  767. if(lang)
  768. {
  769. std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
  770. name += lang;
  771. name += "_FLAG";
  772. if(m_Makefile->GetDefinition(name.c_str()))
  773. {
  774. return targetSOName;
  775. }
  776. }
  777. return "";
  778. }
  779. }
  780. if(linkLibs)
  781. {
  782. if(variable == "LINK_LIBRARIES")
  783. {
  784. return linkLibs;
  785. }
  786. }
  787. std::vector<std::string> enabledLanguages;
  788. m_GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  789. // loop over language specific replace variables
  790. int pos = 0;
  791. while(ruleReplaceVars[pos])
  792. {
  793. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  794. i != enabledLanguages.end(); ++i)
  795. {
  796. lang = i->c_str();
  797. std::string actualReplace = ruleReplaceVars[pos];
  798. // If this is the compiler then look for the extra variable
  799. // _COMPILER_ARG1 which must be the first argument to the compiler
  800. const char* compilerArg1 = 0;
  801. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  802. {
  803. std::string arg1 = actualReplace + "_ARG1";
  804. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  805. compilerArg1 = m_Makefile->GetDefinition(arg1.c_str());
  806. }
  807. if(actualReplace.find("${LANG}") != actualReplace.npos)
  808. {
  809. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  810. }
  811. if(actualReplace == variable)
  812. {
  813. std::string replace = m_Makefile->GetSafeDefinition(variable.c_str());
  814. // if the variable is not a FLAG then treat it like a path
  815. if(variable.find("_FLAG") == variable.npos)
  816. {
  817. std::string ret = this->ConvertToOutputForExisting(replace.c_str());
  818. // if there is a required first argument to the compiler add it to the compiler string
  819. if(compilerArg1)
  820. {
  821. ret += " ";
  822. ret += compilerArg1;
  823. }
  824. return ret;
  825. }
  826. return replace;
  827. }
  828. }
  829. pos++;
  830. }
  831. return variable;
  832. }
  833. void
  834. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  835. const char* lang,
  836. const char* objects,
  837. const char* target,
  838. const char* linkLibs,
  839. const char* source,
  840. const char* object,
  841. const char* flags,
  842. const char* objectsquoted,
  843. const char* targetBase,
  844. const char* targetSOName,
  845. const char* linkFlags)
  846. {
  847. std::vector<std::string> enabledLanguages;
  848. m_GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  849. std::string::size_type start = s.find('<');
  850. // no variables to expand
  851. if(start == s.npos)
  852. {
  853. return;
  854. }
  855. std::string::size_type pos = 0;
  856. std::string expandedInput;
  857. while(start != s.npos && start < s.size()-2)
  858. {
  859. std::string::size_type end = s.find('>', start);
  860. // if we find a < with no > we are done
  861. if(end == s.npos)
  862. {
  863. return;
  864. }
  865. char c = s[start+1];
  866. // if the next char after the < is not A-Za-z then
  867. // skip it and try to find the next < in the string
  868. if(!isalpha(c))
  869. {
  870. start = s.find('<', start+1);
  871. }
  872. else
  873. {
  874. // extract the var
  875. std::string var = s.substr(start+1, end - start-1);
  876. std::string replace = this->ExpandRuleVariable(var, lang, objects,
  877. target, linkLibs,
  878. source, object, flags,
  879. objectsquoted,
  880. targetBase, targetSOName,
  881. linkFlags);
  882. expandedInput += s.substr(pos, start-pos);
  883. expandedInput += replace;
  884. // move to next one
  885. start = s.find('<', start+var.size()+2);
  886. pos = end+1;
  887. }
  888. }
  889. // add the rest of the input
  890. expandedInput += s.substr(pos, s.size()-pos);
  891. s = expandedInput;
  892. }
  893. std::string
  894. cmLocalGenerator::ConvertToOutputForExisting(const char* p)
  895. {
  896. std::string ret = this->Convert(p, START_OUTPUT, SHELL, true);
  897. // if there are spaces in the path, then get the short path version
  898. // if there is one
  899. if(ret.find(' ') != std::string::npos)
  900. {
  901. if(cmSystemTools::FileExists(p))
  902. {
  903. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  904. {
  905. ret = this->Convert(p,START_OUTPUT,MAKEFILE,true);
  906. }
  907. }
  908. }
  909. return ret;
  910. }
  911. const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
  912. {
  913. if(!lang)
  914. {
  915. return "";
  916. }
  917. if(m_LanguageToIncludeFlags.count(lang))
  918. {
  919. return m_LanguageToIncludeFlags[lang].c_str();
  920. }
  921. cmOStringStream includeFlags;
  922. std::vector<std::string> includes;
  923. this->GetIncludeDirectories(includes);
  924. std::vector<std::string>::iterator i;
  925. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  926. flagVar += lang;
  927. const char* includeFlag = m_Makefile->GetDefinition(flagVar.c_str());
  928. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  929. flagVar += lang;
  930. const char* sep = m_Makefile->GetDefinition(flagVar.c_str());
  931. bool repeatFlag = true; // should the include flag be repeated like ie. -IA -IB
  932. if(!sep)
  933. {
  934. sep = " ";
  935. }
  936. else
  937. {
  938. // if there is a separator then the flag is not repeated but is only given once
  939. // i.e. -classpath a:b:c
  940. repeatFlag = false;
  941. }
  942. bool flagUsed = false;
  943. for(i = includes.begin(); i != includes.end(); ++i)
  944. {
  945. std::string include = *i;
  946. if(!flagUsed || repeatFlag)
  947. {
  948. includeFlags << includeFlag;
  949. flagUsed = true;
  950. }
  951. includeFlags << this->ConvertToOutputForExisting(i->c_str()) << sep;
  952. }
  953. std::string flags = includeFlags.str();
  954. // remove trailing separators
  955. if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
  956. {
  957. flags[flags.size()-1] = ' ';
  958. }
  959. flags += m_Makefile->GetDefineFlags();
  960. m_LanguageToIncludeFlags[lang] = flags;
  961. return m_LanguageToIncludeFlags[lang].c_str();
  962. }
  963. //----------------------------------------------------------------------------
  964. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs)
  965. {
  966. // Output Include paths
  967. std::set<cmStdString> implicitIncludes;
  968. // CMake versions below 2.0 would add the source tree to the -I path
  969. // automatically. Preserve compatibility.
  970. bool includeSourceDir = false;
  971. const char* versionValue =
  972. m_Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  973. if(versionValue)
  974. {
  975. int major = 0;
  976. int minor = 0;
  977. if(sscanf(versionValue, "%d.%d", &major, &minor) == 2 && major < 2)
  978. {
  979. includeSourceDir = true;
  980. }
  981. }
  982. const char* vtkSourceDir =
  983. m_Makefile->GetDefinition("VTK_SOURCE_DIR");
  984. if(vtkSourceDir)
  985. {
  986. // Special hack for VTK 4.0 - 4.4.
  987. const char* vtk_major = m_Makefile->GetDefinition("VTK_MAJOR_VERSION");
  988. const char* vtk_minor = m_Makefile->GetDefinition("VTK_MINOR_VERSION");
  989. vtk_major = vtk_major? vtk_major : "4";
  990. vtk_minor = vtk_minor? vtk_minor : "4";
  991. int major = 0;
  992. int minor = 0;
  993. if(sscanf(vtk_major, "%d", &major) && sscanf(vtk_minor, "%d", &minor) &&
  994. major == 4 && minor <= 4)
  995. {
  996. includeSourceDir = true;
  997. }
  998. }
  999. if(includeSourceDir)
  1000. {
  1001. dirs.push_back(m_Makefile->GetStartDirectory());
  1002. }
  1003. // Do not explicitly add the standard include path "/usr/include".
  1004. // This can cause problems with certain standard library
  1005. // implementations because the wrong headers may be found first.
  1006. implicitIncludes.insert("/usr/include");
  1007. if(m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  1008. {
  1009. std::string arg = m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES");
  1010. std::vector<std::string> implicitIncludeVec;
  1011. cmSystemTools::ExpandListArgument(arg, implicitIncludeVec);
  1012. for(unsigned int k =0; k < implicitIncludeVec.size(); k++)
  1013. {
  1014. implicitIncludes.insert(implicitIncludeVec[k]);
  1015. }
  1016. }
  1017. // Construct the ordered list.
  1018. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1019. for(std::vector<std::string>::iterator i = includes.begin();
  1020. i != includes.end(); ++i)
  1021. {
  1022. if(implicitIncludes.find(*i) == implicitIncludes.end())
  1023. {
  1024. dirs.push_back(*i);
  1025. }
  1026. }
  1027. }
  1028. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1029. std::string& flags,
  1030. std::string& linkFlags,
  1031. cmTarget& target)
  1032. {
  1033. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1034. buildType = cmSystemTools::UpperCase(buildType);
  1035. const char* libraryLinkVariable = "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1036. switch(target.GetType())
  1037. {
  1038. case cmTarget::STATIC_LIBRARY:
  1039. {
  1040. const char* targetLinkFlags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  1041. if(targetLinkFlags)
  1042. {
  1043. linkFlags += targetLinkFlags;
  1044. linkFlags += " ";
  1045. }
  1046. }
  1047. break;
  1048. case cmTarget::MODULE_LIBRARY:
  1049. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1050. case cmTarget::SHARED_LIBRARY:
  1051. {
  1052. linkFlags = m_Makefile->GetSafeDefinition(libraryLinkVariable);
  1053. linkFlags += " ";
  1054. if(buildType.size())
  1055. {
  1056. std::string build = libraryLinkVariable;
  1057. build += "_";
  1058. build += buildType;
  1059. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  1060. linkFlags += " ";
  1061. }
  1062. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1063. {
  1064. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1065. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1066. i != sources.end(); ++i)
  1067. {
  1068. if((*i)->GetSourceExtension() == "def")
  1069. {
  1070. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1071. linkFlags += this->Convert((*i)->GetFullPath().c_str(),START_OUTPUT,MAKEFILE);
  1072. linkFlags += " ";
  1073. }
  1074. }
  1075. }
  1076. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1077. if(targetLinkFlags)
  1078. {
  1079. linkFlags += targetLinkFlags;
  1080. linkFlags += " ";
  1081. }
  1082. cmOStringStream linklibsStr;
  1083. this->OutputLinkLibraries(linklibsStr, target.GetName(), target);
  1084. linkLibs = linklibsStr.str();
  1085. }
  1086. break;
  1087. case cmTarget::EXECUTABLE:
  1088. {
  1089. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1090. linkFlags += " ";
  1091. if(buildType.size())
  1092. {
  1093. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1094. build += buildType;
  1095. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  1096. linkFlags += " ";
  1097. }
  1098. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  1099. if(!linkLanguage)
  1100. {
  1101. cmSystemTools::Error("CMake can not determine linker language for target:",
  1102. target.GetName());
  1103. return;
  1104. }
  1105. std::string langVar = "CMAKE_";
  1106. langVar += linkLanguage;
  1107. std::string flagsVar = langVar + "_FLAGS";
  1108. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  1109. sharedFlagsVar += linkLanguage;
  1110. sharedFlagsVar += "_FLAGS";
  1111. flags += m_Makefile->GetSafeDefinition(flagsVar.c_str());
  1112. flags += " ";
  1113. flags += m_Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  1114. flags += " ";
  1115. cmOStringStream linklibs;
  1116. this->OutputLinkLibraries(linklibs, 0, target);
  1117. linkLibs = linklibs.str();
  1118. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1119. {
  1120. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") + linkLanguage
  1121. + std::string("_FLAGS");
  1122. linkFlags += m_Makefile->GetSafeDefinition(sFlagVar.c_str());
  1123. linkFlags += " ";
  1124. }
  1125. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1126. {
  1127. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1128. linkFlags += " ";
  1129. }
  1130. else
  1131. {
  1132. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1133. linkFlags += " ";
  1134. }
  1135. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1136. if(targetLinkFlags)
  1137. {
  1138. linkFlags += targetLinkFlags;
  1139. linkFlags += " ";
  1140. }
  1141. }
  1142. break;
  1143. case cmTarget::UTILITY:
  1144. case cmTarget::INSTALL_FILES:
  1145. case cmTarget::INSTALL_PROGRAMS:
  1146. break;
  1147. }
  1148. }
  1149. /**
  1150. * Output the linking rules on a command line. For executables,
  1151. * targetLibrary should be a NULL pointer. For libraries, it should point
  1152. * to the name of the library. This will not link a library against itself.
  1153. */
  1154. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  1155. const char* targetLibrary,
  1156. cmTarget &tgt)
  1157. {
  1158. // Try to emit each search path once
  1159. std::set<cmStdString> emitted;
  1160. // Embed runtime search paths if possible and if required.
  1161. bool outputRuntime = true;
  1162. std::string runtimeFlag;
  1163. std::string runtimeSep;
  1164. std::vector<std::string> runtimeDirs;
  1165. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1166. buildType = cmSystemTools::UpperCase(buildType);
  1167. cmTarget::LinkLibraryType cmakeBuildType = cmTarget::GENERAL;
  1168. if(buildType == "DEBUG")
  1169. {
  1170. cmakeBuildType = cmTarget::DEBUG;
  1171. }
  1172. else if(buildType.size())
  1173. {
  1174. cmakeBuildType = cmTarget::OPTIMIZED;
  1175. }
  1176. const char* linkLanguage = tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1177. if(!linkLanguage)
  1178. {
  1179. cmSystemTools::
  1180. Error("CMake can not determine linker language for target:",
  1181. tgt.GetName());
  1182. return;
  1183. }
  1184. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1185. runTimeFlagVar += linkLanguage;
  1186. runTimeFlagVar += "_FLAG";
  1187. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1188. runtimeFlag = m_Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1189. runtimeSep = m_Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1190. // concatenate all paths or no?
  1191. bool runtimeConcatenate = ( runtimeSep!="" );
  1192. if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
  1193. {
  1194. outputRuntime = false;
  1195. }
  1196. // Some search paths should never be emitted
  1197. emitted.insert("");
  1198. emitted.insert("/usr/lib");
  1199. std::string libPathFlag = m_Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1200. std::string libLinkFlag = m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1201. // collect all the flags needed for linking libraries
  1202. std::string linkLibs;
  1203. // Flags to link an executable to shared libraries.
  1204. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1205. linkFlagsVar += linkLanguage;
  1206. linkFlagsVar += "_FLAGS";
  1207. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1208. {
  1209. linkLibs = m_Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1210. linkLibs += " ";
  1211. }
  1212. cmOrderLinkDirectories orderLibs;
  1213. std::string ext =
  1214. m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1215. if(ext.size())
  1216. {
  1217. orderLibs.AddLinkExtension(ext.c_str());
  1218. }
  1219. ext =
  1220. m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1221. if(ext.size())
  1222. {
  1223. orderLibs.AddLinkExtension(ext.c_str());
  1224. }
  1225. ext =
  1226. m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1227. if(ext.size())
  1228. {
  1229. orderLibs.AddLinkExtension(ext.c_str());
  1230. }
  1231. // compute the correct order for -L paths
  1232. orderLibs.SetLinkInformation(tgt, cmakeBuildType, targetLibrary);
  1233. orderLibs.DetermineLibraryPathOrder();
  1234. std::vector<cmStdString> libdirs;
  1235. std::vector<cmStdString> linkItems;
  1236. orderLibs.GetLinkerInformation(libdirs, linkItems);
  1237. for(std::vector<cmStdString>::const_iterator libDir = libdirs.begin();
  1238. libDir != libdirs.end(); ++libDir)
  1239. {
  1240. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1241. if(emitted.insert(libpath).second)
  1242. {
  1243. std::string fullLibPath;
  1244. if(!m_WindowsShell && m_UseRelativePaths)
  1245. {
  1246. fullLibPath = "\"`cd ";
  1247. }
  1248. fullLibPath += libpath;
  1249. if(!m_WindowsShell && m_UseRelativePaths)
  1250. {
  1251. fullLibPath += ";pwd`\"";
  1252. }
  1253. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  1254. if((pos == std::string::npos || pos > 0)
  1255. && libDir->find("${") == std::string::npos)
  1256. {
  1257. linkLibs += libPathFlag;
  1258. if(outputRuntime)
  1259. {
  1260. runtimeDirs.push_back( fullLibPath );
  1261. }
  1262. }
  1263. linkLibs += fullLibPath;
  1264. linkLibs += " ";
  1265. }
  1266. }
  1267. std::string linkSuffix =
  1268. m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1269. std::string regexp = ".*\\";
  1270. regexp += linkSuffix;
  1271. regexp += "$";
  1272. cmsys::RegularExpression hasSuffix(regexp.c_str());
  1273. std::string librariesLinked;
  1274. for(std::vector<cmStdString>::iterator lib = linkItems.begin();
  1275. lib != linkItems.end(); ++lib)
  1276. {
  1277. cmStdString& linkItem = *lib;
  1278. // check to see if the link item has a -l already
  1279. cmsys::RegularExpression reg("^([ \t]*\\-[lLWRB])|([ \t]*\\-framework)|(\\${)|([ \t]*\\-pthread)|([ \t]*`)");
  1280. if(!reg.find(linkItem))
  1281. {
  1282. librariesLinked += libLinkFlag;
  1283. }
  1284. librariesLinked += linkItem;
  1285. if(linkSuffix.size() && !hasSuffix.find(linkItem))
  1286. {
  1287. librariesLinked += linkSuffix;
  1288. }
  1289. librariesLinked += " ";
  1290. }
  1291. linkLibs += librariesLinked;
  1292. fout << linkLibs;
  1293. if(outputRuntime && runtimeDirs.size()>0)
  1294. {
  1295. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1296. // a "-R a -R b -R c" type link line
  1297. fout << runtimeFlag;
  1298. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1299. fout << *itr;
  1300. ++itr;
  1301. for( ; itr != runtimeDirs.end(); ++itr )
  1302. {
  1303. if(runtimeConcatenate)
  1304. {
  1305. fout << runtimeSep << *itr;
  1306. }
  1307. else
  1308. {
  1309. fout << " " << runtimeFlag << *itr;
  1310. }
  1311. }
  1312. fout << " ";
  1313. }
  1314. if(m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  1315. {
  1316. fout << m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES") << " ";
  1317. }
  1318. }
  1319. //----------------------------------------------------------------------------
  1320. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1321. const char* lang)
  1322. {
  1323. // Add language-specific flags.
  1324. std::string flagsVar = "CMAKE_";
  1325. flagsVar += lang;
  1326. flagsVar += "_FLAGS";
  1327. this->AddConfigVariableFlags(flags, flagsVar.c_str());
  1328. }
  1329. //----------------------------------------------------------------------------
  1330. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1331. const char* lang,
  1332. bool shared)
  1333. {
  1334. std::string flagsVar;
  1335. // Add flags for dealing with shared libraries for this language.
  1336. if(shared)
  1337. {
  1338. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1339. flagsVar += lang;
  1340. flagsVar += "_FLAGS";
  1341. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1342. }
  1343. // Add flags specific to shared builds.
  1344. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1345. {
  1346. flagsVar = "CMAKE_SHARED_BUILD_";
  1347. flagsVar += lang;
  1348. flagsVar += "_FLAGS";
  1349. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1350. }
  1351. }
  1352. //----------------------------------------------------------------------------
  1353. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1354. const char* var)
  1355. {
  1356. // Add the flags from the variable itself.
  1357. std::string flagsVar = var;
  1358. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1359. // Add the flags from the build-type specific variable.
  1360. const char* buildType = m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1361. if(buildType && *buildType)
  1362. {
  1363. flagsVar += "_";
  1364. flagsVar += cmSystemTools::UpperCase(buildType);
  1365. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1366. }
  1367. }
  1368. //----------------------------------------------------------------------------
  1369. void cmLocalGenerator::AppendFlags(std::string& flags,
  1370. const char* newFlags)
  1371. {
  1372. if(newFlags && *newFlags)
  1373. {
  1374. if(flags.size())
  1375. {
  1376. flags += " ";
  1377. }
  1378. flags += newFlags;
  1379. }
  1380. }
  1381. //----------------------------------------------------------------------------
  1382. std::string
  1383. cmLocalGenerator::ConstructScript(const cmCustomCommandLines& commandLines,
  1384. const char* newline)
  1385. {
  1386. // Store the script in a string.
  1387. std::string script;
  1388. // Write each command on a single line.
  1389. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  1390. cl != commandLines.end(); ++cl)
  1391. {
  1392. // Start with the command name.
  1393. const cmCustomCommandLine& commandLine = *cl;
  1394. script += this->Convert(commandLine[0].c_str(),START_OUTPUT,SHELL);
  1395. // Add the arguments.
  1396. for(unsigned int j=1;j < commandLine.size(); ++j)
  1397. {
  1398. script += " ";
  1399. script += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  1400. }
  1401. // End the line.
  1402. script += newline;
  1403. }
  1404. return script;
  1405. }
  1406. //----------------------------------------------------------------------------
  1407. std::string
  1408. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  1409. {
  1410. return this->Convert(remote, START_OUTPUT, SHELL, true);
  1411. }
  1412. //----------------------------------------------------------------------------
  1413. std::string cmLocalGenerator::Convert(const char* source,
  1414. RelativeRoot relative,
  1415. OutputFormat output,
  1416. bool optional)
  1417. {
  1418. // Convert the path to a relative path.
  1419. std::string result = source;
  1420. if (!optional || m_UseRelativePaths)
  1421. {
  1422. switch (relative)
  1423. {
  1424. case HOME:
  1425. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1426. result = m_GlobalGenerator->
  1427. ConvertToRelativePath(m_HomeDirectoryComponents, result.c_str());
  1428. break;
  1429. case START:
  1430. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1431. result = m_GlobalGenerator->
  1432. ConvertToRelativePath(m_StartDirectoryComponents, result.c_str());
  1433. break;
  1434. case HOME_OUTPUT:
  1435. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1436. result = m_GlobalGenerator->
  1437. ConvertToRelativePath(m_HomeOutputDirectoryComponents, result.c_str());
  1438. break;
  1439. case START_OUTPUT:
  1440. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1441. result = m_GlobalGenerator->
  1442. ConvertToRelativePath(m_StartOutputDirectoryComponents, result.c_str());
  1443. break;
  1444. case FULL:
  1445. result = cmSystemTools::CollapseFullPath(result.c_str());
  1446. break;
  1447. case NONE:
  1448. break;
  1449. }
  1450. }
  1451. // Now convert it to an output path.
  1452. if (output == MAKEFILE || output == SHELL)
  1453. {
  1454. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1455. }
  1456. return result;
  1457. }