cmLocalGenerator.cxx 48 KB

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