cmLocalGenerator.cxx 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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>::const_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::const_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. {
  340. std::string properties;
  341. #if defined(_WIN32) && !defined(__CYGWIN__)
  342. const char* exe_version = 0;
  343. #else
  344. const char* exe_version = l->second.GetProperty("VERSION");
  345. #endif
  346. if(exe_version)
  347. {
  348. properties += " VERSION ";
  349. properties += exe_version;
  350. }
  351. if(l->second.GetPropertyAsBool("MACOSX_BUNDLE"))
  352. {
  353. fname = exeOutPath;
  354. fname += l->second.GetFullName(m_Makefile);
  355. std::string plist = fname;
  356. plist += ".app/Contents/Info.plist";
  357. fname += ".app/Contents/MacOS/";
  358. fname += l->second.GetName();
  359. files = fname.c_str();
  360. std::string bdest = dest;
  361. bdest += "/";
  362. bdest += l->second.GetName();
  363. std::string pdest = bdest;
  364. pdest += ".app/Contents";
  365. bdest += ".app/Contents/MacOS";
  366. // first install the actual executable
  367. this->AddInstallRule(fout, bdest.c_str(), type, files,
  368. false, properties.c_str());
  369. files = plist.c_str();
  370. // now install the Info.plist file
  371. this->AddInstallRule(fout, pdest.c_str(),
  372. cmTarget::INSTALL_FILES, files);
  373. }
  374. else
  375. {
  376. fname = exeOutPath;
  377. fname += l->second.GetFullName(m_Makefile);
  378. files = fname.c_str();
  379. this->AddInstallRule(fout, dest, type, files, false,
  380. properties.c_str());
  381. }
  382. }
  383. break;
  384. case cmTarget::INSTALL_FILES:
  385. {
  386. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  387. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  388. sourcePath += "/";
  389. binaryPath += "/";
  390. const std::vector<std::string> &sf = l->second.GetSourceLists();
  391. std::vector<std::string>::const_iterator i;
  392. for (i = sf.begin(); i != sf.end(); ++i)
  393. {
  394. std::string f = *i;
  395. if(f.substr(0, sourcePath.length()) == sourcePath)
  396. {
  397. f = f.substr(sourcePath.length());
  398. }
  399. else if(f.substr(0, binaryPath.length()) == binaryPath)
  400. {
  401. f = f.substr(binaryPath.length());
  402. }
  403. files = i->c_str();
  404. this->AddInstallRule(fout, dest, type, files);
  405. }
  406. }
  407. break;
  408. case cmTarget::INSTALL_PROGRAMS:
  409. {
  410. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  411. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  412. sourcePath += "/";
  413. binaryPath += "/";
  414. const std::vector<std::string> &sf = l->second.GetSourceLists();
  415. std::vector<std::string>::const_iterator i;
  416. for (i = sf.begin(); i != sf.end(); ++i)
  417. {
  418. std::string f = *i;
  419. if(f.substr(0, sourcePath.length()) == sourcePath)
  420. {
  421. f = f.substr(sourcePath.length());
  422. }
  423. else if(f.substr(0, binaryPath.length()) == binaryPath)
  424. {
  425. f = f.substr(binaryPath.length());
  426. }
  427. files = i->c_str();
  428. this->AddInstallRule(fout, dest, type, files);
  429. }
  430. }
  431. break;
  432. case cmTarget::UTILITY:
  433. default:
  434. break;
  435. }
  436. }
  437. if ( postinstall )
  438. {
  439. fout << "INCLUDE(\"" << postinstall << "\")" << std::endl;
  440. }
  441. }
  442. if ( this->Children.size())
  443. {
  444. std::vector<cmLocalGenerator*>::const_iterator i = this->Children.begin();
  445. for(; i != this->Children.end(); ++i)
  446. {
  447. std::string odir = (*i)->GetMakefile()->GetStartOutputDirectory();
  448. cmSystemTools::ConvertToUnixSlashes(odir);
  449. fout << "INCLUDE(\"" << odir.c_str()
  450. << "/cmake_install.cmake\")" << std::endl;
  451. }
  452. fout << std::endl;;
  453. }
  454. if ( toplevel_install )
  455. {
  456. fout << "FILE(WRITE \"" << homedir.c_str() << "/install_manifest.txt\" "
  457. << "\"\")" << std::endl;
  458. fout << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  459. << " FILE(APPEND \"" << homedir.c_str() << "/install_manifest.txt\" "
  460. << "\"${file}\\n\")" << std::endl
  461. << "ENDFOREACH(file)" << std::endl;
  462. }
  463. }
  464. void cmLocalGenerator::AddInstallRule(std::ostream& fout, const char* dest,
  465. int type, const char* files, bool optional /* = false */, const char* properties /* = 0 */)
  466. {
  467. std::string sfiles = files;
  468. std::string destination = dest;
  469. std::string stype;
  470. switch ( type )
  471. {
  472. case cmTarget::INSTALL_PROGRAMS: stype = "PROGRAM"; break;
  473. case cmTarget::EXECUTABLE: stype = "EXECUTABLE"; break;
  474. case cmTarget::STATIC_LIBRARY: stype = "STATIC_LIBRARY"; break;
  475. case cmTarget::SHARED_LIBRARY: stype = "SHARED_LIBRARY"; break;
  476. case cmTarget::MODULE_LIBRARY: stype = "MODULE"; break;
  477. case cmTarget::INSTALL_FILES:
  478. default: stype = "FILE"; break;
  479. }
  480. std::string fname = cmSystemTools::GetFilenameName(sfiles.c_str());
  481. fout
  482. << "MESSAGE(STATUS \"Installing " << destination.c_str()
  483. << "/" << fname.c_str() << "\")\n"
  484. << "FILE(INSTALL DESTINATION \"" << destination.c_str()
  485. << "\" TYPE " << stype.c_str() << (optional?" OPTIONAL":"") ;
  486. if ( properties && *properties )
  487. {
  488. fout << " PROPERTIES" << properties;
  489. }
  490. fout
  491. << " FILES \"" << sfiles.c_str() << "\")\n";
  492. }
  493. void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
  494. const char* lang,
  495. cmSourceFile& source,
  496. cmTarget& )
  497. {
  498. std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
  499. std::string sourceFile =
  500. this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
  501. std::string varString = "CMAKE_";
  502. varString += lang;
  503. varString += "_COMPILE_OBJECT";
  504. std::vector<std::string> rules;
  505. rules.push_back(m_Makefile->GetRequiredDefinition(varString.c_str()));
  506. varString = "CMAKE_";
  507. varString += lang;
  508. varString += "_FLAGS";
  509. std::string flags;
  510. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  511. flags += " ";
  512. flags += this->GetIncludeFlags(lang);
  513. std::vector<std::string> commands;
  514. cmSystemTools::ExpandList(rules, commands);
  515. for(std::vector<std::string>::iterator i = commands.begin();
  516. i != commands.end(); ++i)
  517. {
  518. this->ExpandRuleVariables(*i,
  519. lang,
  520. 0, // no objects
  521. 0, // no target
  522. 0, // no link libs
  523. sourceFile.c_str(),
  524. objectFile.c_str(),
  525. flags.c_str());
  526. }
  527. std::vector<std::string> sourceAndDeps;
  528. sourceAndDeps.push_back(sourceFile);
  529. if(commands.size() > 1)
  530. {
  531. cmSystemTools::Error("Currently custom rules can only have one command sorry ");
  532. }
  533. // Check for extra object-file dependencies.
  534. std::vector<std::string> depends;
  535. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  536. if(additionalDeps)
  537. {
  538. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  539. for(std::vector<std::string>::iterator i = depends.begin();
  540. i != depends.end(); ++i)
  541. {
  542. sourceAndDeps.push_back(this->Convert(i->c_str(),START_OUTPUT,SHELL));
  543. }
  544. }
  545. #if 0
  546. std::string command;
  547. std::string args;
  548. cmSystemTools::SplitProgramFromArgs(commands[0].c_str(), command, args);
  549. std::vector<std::string> argsv;
  550. argsv.push_back(args);
  551. m_Makefile->AddCustomCommandToOutput(ofname,
  552. command.c_str(),
  553. argsv,
  554. source.GetFullPath().c_str(),
  555. sourceAndDeps,
  556. "build from source");
  557. #endif
  558. }
  559. void cmLocalGenerator::AddBuildTargetRule(const char* llang, cmTarget& target)
  560. {
  561. cmStdString objs;
  562. std::vector<std::string> objVector;
  563. // Add all the sources outputs to the depends of the target
  564. std::vector<cmSourceFile*>& classes = target.GetSourceFiles();
  565. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  566. i != classes.end(); ++i)
  567. {
  568. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  569. !(*i)->GetCustomCommand())
  570. {
  571. std::string outExt =
  572. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  573. (*i)->GetSourceExtension().c_str());
  574. if(outExt.size() && !(*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  575. {
  576. std::string ofname = m_Makefile->GetCurrentOutputDirectory();
  577. ofname += "/";
  578. ofname += (*i)->GetSourceName() + outExt;
  579. objVector.push_back(ofname);
  580. this->AddCustomCommandToCreateObject(ofname.c_str(), llang, *(*i), target);
  581. objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
  582. objs += " ";
  583. }
  584. }
  585. }
  586. std::string createRule = "CMAKE_";
  587. createRule += llang;
  588. createRule += target.GetCreateRuleVariable();
  589. std::string targetName = target.GetFullName(m_Makefile);
  590. // Executable :
  591. // Shared Library:
  592. // Static Library:
  593. // Shared Module:
  594. std::string linkLibs; // should be set
  595. std::string flags; // should be set
  596. std::string linkFlags; // should be set
  597. this->GetTargetFlags(linkLibs, flags, linkFlags, target);
  598. std::string rule = m_Makefile->GetRequiredDefinition(createRule.c_str());
  599. this->ExpandRuleVariables(rule,
  600. llang, // language
  601. objs.c_str(), // objects
  602. targetName.c_str(), // target
  603. linkLibs.c_str(), // link libs
  604. 0, // source
  605. 0, // object
  606. flags.c_str(), // flags
  607. 0, // objects quoted
  608. 0, // target base name
  609. 0, // target so name,
  610. linkFlags.c_str() // link flags
  611. );
  612. #if 0
  613. std::string command;
  614. std::string args;
  615. cmSystemTools::SplitProgramFromArgs(rule.c_str(), command, args);
  616. // Just like ADD_CUSTOM_TARGET(foo ALL DEPENDS a.o b.o)
  617. // Add a custom command for generating each .o file
  618. cmCustomCommand cc(command.c_str(), args.c_str(), objVector,
  619. targetName.c_str(), 0);
  620. target.GetPostBuildCommands().push_back(cc);
  621. #endif
  622. }
  623. void cmLocalGenerator::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
  624. {
  625. cmTargets &tgts = m_Makefile->GetTargets();
  626. for(cmTargets::iterator l = tgts.begin();
  627. l != tgts.end(); l++)
  628. {
  629. cmTarget& target = l->second;
  630. switch(target.GetType())
  631. {
  632. case cmTarget::STATIC_LIBRARY:
  633. case cmTarget::SHARED_LIBRARY:
  634. case cmTarget::MODULE_LIBRARY:
  635. case cmTarget::EXECUTABLE:
  636. {
  637. const char* llang = target.GetLinkerLanguage(this->GetGlobalGenerator());
  638. if(!llang)
  639. {
  640. cmSystemTools::Error("CMake can not determine linker language for target:",
  641. target.GetName());
  642. return;
  643. }
  644. // if the language is not in the set lang then create custom
  645. // commands to build the target
  646. if(lang.count(llang) == 0)
  647. {
  648. this->AddBuildTargetRule(llang, target);
  649. }
  650. }
  651. break;
  652. case cmTarget::UTILITY:
  653. case cmTarget::INSTALL_FILES:
  654. case cmTarget::INSTALL_PROGRAMS:
  655. break;
  656. }
  657. }
  658. }
  659. // List of variables that are replaced when
  660. // rules are expanced. These variables are
  661. // replaced in the form <var> with GetSafeDefinition(var).
  662. // ${LANG} is replaced in the variable first with all enabled
  663. // languages.
  664. static const char* ruleReplaceVars[] =
  665. {
  666. "CMAKE_${LANG}_COMPILER",
  667. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  668. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  669. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  670. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  671. "CMAKE_${LANG}_LINK_FLAGS",
  672. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  673. "CMAKE_${LANG}_ARCHIVE",
  674. "CMAKE_AR",
  675. "CMAKE_CURRENT_SOURCE_DIR",
  676. "CMAKE_CURRENT_BINARY_DIR",
  677. "CMAKE_RANLIB",
  678. 0
  679. };
  680. std::string
  681. cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
  682. const char* lang,
  683. const char* objects,
  684. const char* target,
  685. const char* linkLibs,
  686. const char* source,
  687. const char* object,
  688. const char* flags,
  689. const char* objectsquoted,
  690. const char* targetBase,
  691. const char* targetSOName,
  692. const char* linkFlags)
  693. {
  694. if(linkFlags)
  695. {
  696. if(variable == "LINK_FLAGS")
  697. {
  698. return linkFlags;
  699. }
  700. }
  701. if(flags)
  702. {
  703. if(variable == "FLAGS")
  704. {
  705. return flags;
  706. }
  707. }
  708. if(source)
  709. {
  710. if(variable == "SOURCE")
  711. {
  712. return source;
  713. }
  714. }
  715. if(object)
  716. {
  717. if(variable == "OBJECT")
  718. {
  719. return object;
  720. }
  721. }
  722. if(objects)
  723. {
  724. if(variable == "OBJECTS")
  725. {
  726. return objects;
  727. }
  728. }
  729. if(objectsquoted)
  730. {
  731. if(variable == "OBJECTS_QUOTED")
  732. {
  733. return objectsquoted;
  734. }
  735. }
  736. if(target)
  737. {
  738. if(variable == "TARGET_QUOTED")
  739. {
  740. std::string targetQuoted = target;
  741. if(targetQuoted.size() && targetQuoted[0] != '\"')
  742. {
  743. targetQuoted = '\"';
  744. targetQuoted += target;
  745. targetQuoted += '\"';
  746. return targetQuoted;
  747. }
  748. }
  749. if(variable == "TARGET")
  750. {
  751. return target;
  752. }
  753. }
  754. if(targetBase)
  755. {
  756. if(variable == "TARGET_BASE.lib" || variable == "TARGET_BASE.dll")
  757. {
  758. // special case for quoted paths with spaces
  759. // if you see <TARGET_BASE>.lib then put the .lib inside
  760. // the quotes, same for .dll
  761. if((strlen(targetBase) > 1) && targetBase[0] == '\"')
  762. {
  763. std::string base = targetBase;
  764. base[base.size()-1] = '.';
  765. std::string baseLib = base + "lib\"";
  766. std::string baseDll = base + "dll\"";
  767. if(variable == "TARGET_BASE.lib" )
  768. {
  769. return baseLib;
  770. }
  771. if(variable == "TARGET_BASE.dll" )
  772. {
  773. return baseDll;
  774. }
  775. }
  776. }
  777. if(variable == "TARGET_BASE")
  778. {
  779. return targetBase;
  780. }
  781. }
  782. if(targetSOName)
  783. {
  784. if(variable == "TARGET_SONAME")
  785. {
  786. if(lang)
  787. {
  788. std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
  789. name += lang;
  790. name += "_FLAG";
  791. if(m_Makefile->GetDefinition(name.c_str()))
  792. {
  793. return targetSOName;
  794. }
  795. }
  796. return "";
  797. }
  798. }
  799. if(linkLibs)
  800. {
  801. if(variable == "LINK_LIBRARIES")
  802. {
  803. return linkLibs;
  804. }
  805. }
  806. std::vector<std::string> enabledLanguages;
  807. m_GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  808. // loop over language specific replace variables
  809. int pos = 0;
  810. while(ruleReplaceVars[pos])
  811. {
  812. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  813. i != enabledLanguages.end(); ++i)
  814. {
  815. lang = i->c_str();
  816. std::string actualReplace = ruleReplaceVars[pos];
  817. // If this is the compiler then look for the extra variable
  818. // _COMPILER_ARG1 which must be the first argument to the compiler
  819. const char* compilerArg1 = 0;
  820. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  821. {
  822. std::string arg1 = actualReplace + "_ARG1";
  823. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  824. compilerArg1 = m_Makefile->GetDefinition(arg1.c_str());
  825. }
  826. if(actualReplace.find("${LANG}") != actualReplace.npos)
  827. {
  828. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  829. }
  830. if(actualReplace == variable)
  831. {
  832. std::string replace = m_Makefile->GetSafeDefinition(variable.c_str());
  833. // if the variable is not a FLAG then treat it like a path
  834. if(variable.find("_FLAG") == variable.npos)
  835. {
  836. std::string ret = this->ConvertToOutputForExisting(replace.c_str());
  837. // if there is a required first argument to the compiler add it to the compiler string
  838. if(compilerArg1)
  839. {
  840. ret += " ";
  841. ret += compilerArg1;
  842. }
  843. return ret;
  844. }
  845. return replace;
  846. }
  847. }
  848. pos++;
  849. }
  850. return variable;
  851. }
  852. void
  853. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  854. const char* lang,
  855. const char* objects,
  856. const char* target,
  857. const char* linkLibs,
  858. const char* source,
  859. const char* object,
  860. const char* flags,
  861. const char* objectsquoted,
  862. const char* targetBase,
  863. const char* targetSOName,
  864. const char* linkFlags)
  865. {
  866. std::vector<std::string> enabledLanguages;
  867. m_GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  868. std::string::size_type start = s.find('<');
  869. // no variables to expand
  870. if(start == s.npos)
  871. {
  872. return;
  873. }
  874. std::string::size_type pos = 0;
  875. std::string expandedInput;
  876. while(start != s.npos && start < s.size()-2)
  877. {
  878. std::string::size_type end = s.find('>', start);
  879. // if we find a < with no > we are done
  880. if(end == s.npos)
  881. {
  882. return;
  883. }
  884. char c = s[start+1];
  885. // if the next char after the < is not A-Za-z then
  886. // skip it and try to find the next < in the string
  887. if(!isalpha(c))
  888. {
  889. start = s.find('<', start+1);
  890. }
  891. else
  892. {
  893. // extract the var
  894. std::string var = s.substr(start+1, end - start-1);
  895. std::string replace = this->ExpandRuleVariable(var, lang, objects,
  896. target, linkLibs,
  897. source, object, flags,
  898. objectsquoted,
  899. targetBase, targetSOName,
  900. linkFlags);
  901. expandedInput += s.substr(pos, start-pos);
  902. expandedInput += replace;
  903. // move to next one
  904. start = s.find('<', start+var.size()+2);
  905. pos = end+1;
  906. }
  907. }
  908. // add the rest of the input
  909. expandedInput += s.substr(pos, s.size()-pos);
  910. s = expandedInput;
  911. }
  912. std::string
  913. cmLocalGenerator::ConvertToOutputForExisting(const char* p)
  914. {
  915. std::string ret = this->Convert(p, START_OUTPUT, SHELL, true);
  916. // if there are spaces in the path, then get the short path version
  917. // if there is one
  918. if(ret.find(' ') != std::string::npos)
  919. {
  920. if(cmSystemTools::FileExists(p))
  921. {
  922. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  923. {
  924. ret = this->Convert(p,START_OUTPUT,MAKEFILE,true);
  925. }
  926. }
  927. }
  928. return ret;
  929. }
  930. const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
  931. {
  932. if(!lang)
  933. {
  934. return "";
  935. }
  936. if(m_LanguageToIncludeFlags.count(lang))
  937. {
  938. return m_LanguageToIncludeFlags[lang].c_str();
  939. }
  940. cmOStringStream includeFlags;
  941. std::vector<std::string> includes;
  942. this->GetIncludeDirectories(includes);
  943. std::vector<std::string>::iterator i;
  944. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  945. flagVar += lang;
  946. const char* includeFlag = m_Makefile->GetDefinition(flagVar.c_str());
  947. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  948. flagVar += lang;
  949. const char* sep = m_Makefile->GetDefinition(flagVar.c_str());
  950. bool repeatFlag = true; // should the include flag be repeated like ie. -IA -IB
  951. if(!sep)
  952. {
  953. sep = " ";
  954. }
  955. else
  956. {
  957. // if there is a separator then the flag is not repeated but is only given once
  958. // i.e. -classpath a:b:c
  959. repeatFlag = false;
  960. }
  961. bool flagUsed = false;
  962. for(i = includes.begin(); i != includes.end(); ++i)
  963. {
  964. std::string include = *i;
  965. if(!flagUsed || repeatFlag)
  966. {
  967. includeFlags << includeFlag;
  968. flagUsed = true;
  969. }
  970. includeFlags << this->ConvertToOutputForExisting(i->c_str()) << sep;
  971. }
  972. std::string flags = includeFlags.str();
  973. // remove trailing separators
  974. if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
  975. {
  976. flags[flags.size()-1] = ' ';
  977. }
  978. flags += m_Makefile->GetDefineFlags();
  979. m_LanguageToIncludeFlags[lang] = flags;
  980. return m_LanguageToIncludeFlags[lang].c_str();
  981. }
  982. //----------------------------------------------------------------------------
  983. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs)
  984. {
  985. // Output Include paths
  986. std::set<cmStdString> implicitIncludes;
  987. // CMake versions below 2.0 would add the source tree to the -I path
  988. // automatically. Preserve compatibility.
  989. bool includeSourceDir = false;
  990. const char* versionValue =
  991. m_Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  992. if(versionValue)
  993. {
  994. int major = 0;
  995. int minor = 0;
  996. if(sscanf(versionValue, "%d.%d", &major, &minor) == 2 && major < 2)
  997. {
  998. includeSourceDir = true;
  999. }
  1000. }
  1001. const char* vtkSourceDir =
  1002. m_Makefile->GetDefinition("VTK_SOURCE_DIR");
  1003. if(vtkSourceDir)
  1004. {
  1005. // Special hack for VTK 4.0 - 4.4.
  1006. const char* vtk_major = m_Makefile->GetDefinition("VTK_MAJOR_VERSION");
  1007. const char* vtk_minor = m_Makefile->GetDefinition("VTK_MINOR_VERSION");
  1008. vtk_major = vtk_major? vtk_major : "4";
  1009. vtk_minor = vtk_minor? vtk_minor : "4";
  1010. int major = 0;
  1011. int minor = 0;
  1012. if(sscanf(vtk_major, "%d", &major) && sscanf(vtk_minor, "%d", &minor) &&
  1013. major == 4 && minor <= 4)
  1014. {
  1015. includeSourceDir = true;
  1016. }
  1017. }
  1018. if(includeSourceDir)
  1019. {
  1020. dirs.push_back(m_Makefile->GetStartDirectory());
  1021. }
  1022. // Do not explicitly add the standard include path "/usr/include".
  1023. // This can cause problems with certain standard library
  1024. // implementations because the wrong headers may be found first.
  1025. implicitIncludes.insert("/usr/include");
  1026. if(m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  1027. {
  1028. std::string arg = m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES");
  1029. std::vector<std::string> implicitIncludeVec;
  1030. cmSystemTools::ExpandListArgument(arg, implicitIncludeVec);
  1031. for(unsigned int k =0; k < implicitIncludeVec.size(); k++)
  1032. {
  1033. implicitIncludes.insert(implicitIncludeVec[k]);
  1034. }
  1035. }
  1036. // Construct the ordered list.
  1037. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1038. for(std::vector<std::string>::iterator i = includes.begin();
  1039. i != includes.end(); ++i)
  1040. {
  1041. if(implicitIncludes.find(*i) == implicitIncludes.end())
  1042. {
  1043. dirs.push_back(*i);
  1044. }
  1045. }
  1046. }
  1047. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1048. std::string& flags,
  1049. std::string& linkFlags,
  1050. cmTarget& target)
  1051. {
  1052. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1053. buildType = cmSystemTools::UpperCase(buildType);
  1054. const char* libraryLinkVariable = "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1055. switch(target.GetType())
  1056. {
  1057. case cmTarget::STATIC_LIBRARY:
  1058. {
  1059. const char* targetLinkFlags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  1060. if(targetLinkFlags)
  1061. {
  1062. linkFlags += targetLinkFlags;
  1063. linkFlags += " ";
  1064. }
  1065. }
  1066. break;
  1067. case cmTarget::MODULE_LIBRARY:
  1068. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1069. case cmTarget::SHARED_LIBRARY:
  1070. {
  1071. linkFlags = m_Makefile->GetSafeDefinition(libraryLinkVariable);
  1072. linkFlags += " ";
  1073. if(buildType.size())
  1074. {
  1075. std::string build = libraryLinkVariable;
  1076. build += "_";
  1077. build += buildType;
  1078. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  1079. linkFlags += " ";
  1080. }
  1081. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1082. {
  1083. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1084. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1085. i != sources.end(); ++i)
  1086. {
  1087. if((*i)->GetSourceExtension() == "def")
  1088. {
  1089. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1090. linkFlags += this->Convert((*i)->GetFullPath().c_str(),START_OUTPUT,MAKEFILE);
  1091. linkFlags += " ";
  1092. }
  1093. }
  1094. }
  1095. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1096. if(targetLinkFlags)
  1097. {
  1098. linkFlags += targetLinkFlags;
  1099. linkFlags += " ";
  1100. }
  1101. cmOStringStream linklibsStr;
  1102. this->OutputLinkLibraries(linklibsStr, target.GetName(), target);
  1103. linkLibs = linklibsStr.str();
  1104. }
  1105. break;
  1106. case cmTarget::EXECUTABLE:
  1107. {
  1108. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1109. linkFlags += " ";
  1110. if(buildType.size())
  1111. {
  1112. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1113. build += buildType;
  1114. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  1115. linkFlags += " ";
  1116. }
  1117. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  1118. if(!linkLanguage)
  1119. {
  1120. cmSystemTools::Error("CMake can not determine linker language for target:",
  1121. target.GetName());
  1122. return;
  1123. }
  1124. std::string langVar = "CMAKE_";
  1125. langVar += linkLanguage;
  1126. std::string flagsVar = langVar + "_FLAGS";
  1127. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  1128. sharedFlagsVar += linkLanguage;
  1129. sharedFlagsVar += "_FLAGS";
  1130. flags += m_Makefile->GetSafeDefinition(flagsVar.c_str());
  1131. flags += " ";
  1132. flags += m_Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  1133. flags += " ";
  1134. cmOStringStream linklibs;
  1135. this->OutputLinkLibraries(linklibs, 0, target);
  1136. linkLibs = linklibs.str();
  1137. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1138. {
  1139. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") + linkLanguage
  1140. + std::string("_FLAGS");
  1141. linkFlags += m_Makefile->GetSafeDefinition(sFlagVar.c_str());
  1142. linkFlags += " ";
  1143. }
  1144. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1145. {
  1146. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1147. linkFlags += " ";
  1148. }
  1149. else
  1150. {
  1151. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1152. linkFlags += " ";
  1153. }
  1154. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1155. if(targetLinkFlags)
  1156. {
  1157. linkFlags += targetLinkFlags;
  1158. linkFlags += " ";
  1159. }
  1160. }
  1161. break;
  1162. case cmTarget::UTILITY:
  1163. case cmTarget::INSTALL_FILES:
  1164. case cmTarget::INSTALL_PROGRAMS:
  1165. break;
  1166. }
  1167. }
  1168. /**
  1169. * Output the linking rules on a command line. For executables,
  1170. * targetLibrary should be a NULL pointer. For libraries, it should point
  1171. * to the name of the library. This will not link a library against itself.
  1172. */
  1173. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  1174. const char* targetLibrary,
  1175. cmTarget &tgt)
  1176. {
  1177. // Try to emit each search path once
  1178. std::set<cmStdString> emitted;
  1179. // Embed runtime search paths if possible and if required.
  1180. bool outputRuntime = true;
  1181. std::string runtimeFlag;
  1182. std::string runtimeSep;
  1183. std::vector<std::string> runtimeDirs;
  1184. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1185. buildType = cmSystemTools::UpperCase(buildType);
  1186. cmTarget::LinkLibraryType cmakeBuildType = cmTarget::GENERAL;
  1187. if(buildType == "DEBUG")
  1188. {
  1189. cmakeBuildType = cmTarget::DEBUG;
  1190. }
  1191. else if(buildType.size())
  1192. {
  1193. cmakeBuildType = cmTarget::OPTIMIZED;
  1194. }
  1195. const char* linkLanguage = tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1196. if(!linkLanguage)
  1197. {
  1198. cmSystemTools::
  1199. Error("CMake can not determine linker language for target:",
  1200. tgt.GetName());
  1201. return;
  1202. }
  1203. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1204. runTimeFlagVar += linkLanguage;
  1205. runTimeFlagVar += "_FLAG";
  1206. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1207. runtimeFlag = m_Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1208. runtimeSep = m_Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1209. // concatenate all paths or no?
  1210. bool runtimeConcatenate = ( runtimeSep!="" );
  1211. if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
  1212. {
  1213. outputRuntime = false;
  1214. }
  1215. // Some search paths should never be emitted
  1216. emitted.insert("");
  1217. emitted.insert("/usr/lib");
  1218. std::string libPathFlag = m_Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1219. std::string libLinkFlag = m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1220. // collect all the flags needed for linking libraries
  1221. std::string linkLibs;
  1222. // Flags to link an executable to shared libraries.
  1223. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1224. linkFlagsVar += linkLanguage;
  1225. linkFlagsVar += "_FLAGS";
  1226. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1227. {
  1228. linkLibs = m_Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1229. linkLibs += " ";
  1230. }
  1231. cmOrderLinkDirectories orderLibs;
  1232. std::string ext =
  1233. m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1234. if(ext.size())
  1235. {
  1236. orderLibs.AddLinkExtension(ext.c_str());
  1237. }
  1238. ext =
  1239. m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1240. if(ext.size())
  1241. {
  1242. orderLibs.SetLinkPrefix(ext.c_str());
  1243. }
  1244. ext =
  1245. m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1246. if(ext.size())
  1247. {
  1248. orderLibs.AddLinkExtension(ext.c_str());
  1249. }
  1250. ext =
  1251. m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1252. if(ext.size())
  1253. {
  1254. orderLibs.AddLinkExtension(ext.c_str());
  1255. }
  1256. // compute the correct order for -L paths
  1257. orderLibs.SetLinkInformation(tgt, cmakeBuildType, targetLibrary);
  1258. orderLibs.DetermineLibraryPathOrder();
  1259. std::vector<cmStdString> libdirs;
  1260. std::vector<cmStdString> linkItems;
  1261. orderLibs.GetLinkerInformation(libdirs, linkItems);
  1262. for(std::vector<cmStdString>::const_iterator libDir = libdirs.begin();
  1263. libDir != libdirs.end(); ++libDir)
  1264. {
  1265. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1266. if(emitted.insert(libpath).second)
  1267. {
  1268. std::string fullLibPath;
  1269. if(!m_WindowsShell && m_UseRelativePaths)
  1270. {
  1271. fullLibPath = "\"`cd ";
  1272. }
  1273. fullLibPath += libpath;
  1274. if(!m_WindowsShell && m_UseRelativePaths)
  1275. {
  1276. fullLibPath += ";pwd`\"";
  1277. }
  1278. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  1279. if((pos == std::string::npos || pos > 0)
  1280. && libDir->find("${") == std::string::npos)
  1281. {
  1282. linkLibs += libPathFlag;
  1283. if(outputRuntime)
  1284. {
  1285. runtimeDirs.push_back( fullLibPath );
  1286. }
  1287. }
  1288. linkLibs += fullLibPath;
  1289. linkLibs += " ";
  1290. }
  1291. }
  1292. std::string linkSuffix =
  1293. m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1294. std::string regexp = ".*\\";
  1295. regexp += linkSuffix;
  1296. regexp += "$";
  1297. cmsys::RegularExpression hasSuffix(regexp.c_str());
  1298. std::string librariesLinked;
  1299. for(std::vector<cmStdString>::iterator lib = linkItems.begin();
  1300. lib != linkItems.end(); ++lib)
  1301. {
  1302. cmStdString& linkItem = *lib;
  1303. // check to see if the link item has a -l already
  1304. cmsys::RegularExpression reg("^([ \t]*\\-[lLWRB])|([ \t]*\\-framework)|(\\${)|([ \t]*\\-pthread)|([ \t]*`)");
  1305. if(!reg.find(linkItem))
  1306. {
  1307. librariesLinked += libLinkFlag;
  1308. }
  1309. librariesLinked += linkItem;
  1310. if(linkSuffix.size() && !hasSuffix.find(linkItem))
  1311. {
  1312. librariesLinked += linkSuffix;
  1313. }
  1314. librariesLinked += " ";
  1315. }
  1316. linkLibs += librariesLinked;
  1317. fout << linkLibs;
  1318. if(outputRuntime && runtimeDirs.size()>0)
  1319. {
  1320. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1321. // a "-R a -R b -R c" type link line
  1322. fout << runtimeFlag;
  1323. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1324. fout << *itr;
  1325. ++itr;
  1326. for( ; itr != runtimeDirs.end(); ++itr )
  1327. {
  1328. if(runtimeConcatenate)
  1329. {
  1330. fout << runtimeSep << *itr;
  1331. }
  1332. else
  1333. {
  1334. fout << " " << runtimeFlag << *itr;
  1335. }
  1336. }
  1337. fout << " ";
  1338. }
  1339. if(m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  1340. {
  1341. fout << m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES") << " ";
  1342. }
  1343. }
  1344. //----------------------------------------------------------------------------
  1345. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1346. const char* lang)
  1347. {
  1348. // Add language-specific flags.
  1349. std::string flagsVar = "CMAKE_";
  1350. flagsVar += lang;
  1351. flagsVar += "_FLAGS";
  1352. this->AddConfigVariableFlags(flags, flagsVar.c_str());
  1353. }
  1354. //----------------------------------------------------------------------------
  1355. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1356. const char* lang,
  1357. bool shared)
  1358. {
  1359. std::string flagsVar;
  1360. // Add flags for dealing with shared libraries for this language.
  1361. if(shared)
  1362. {
  1363. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1364. flagsVar += lang;
  1365. flagsVar += "_FLAGS";
  1366. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1367. }
  1368. // Add flags specific to shared builds.
  1369. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1370. {
  1371. flagsVar = "CMAKE_SHARED_BUILD_";
  1372. flagsVar += lang;
  1373. flagsVar += "_FLAGS";
  1374. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1375. }
  1376. }
  1377. //----------------------------------------------------------------------------
  1378. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1379. const char* var)
  1380. {
  1381. // Add the flags from the variable itself.
  1382. std::string flagsVar = var;
  1383. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1384. // Add the flags from the build-type specific variable.
  1385. const char* buildType = m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1386. if(buildType && *buildType)
  1387. {
  1388. flagsVar += "_";
  1389. flagsVar += cmSystemTools::UpperCase(buildType);
  1390. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1391. }
  1392. }
  1393. //----------------------------------------------------------------------------
  1394. void cmLocalGenerator::AppendFlags(std::string& flags,
  1395. const char* newFlags)
  1396. {
  1397. if(newFlags && *newFlags)
  1398. {
  1399. if(flags.size())
  1400. {
  1401. flags += " ";
  1402. }
  1403. flags += newFlags;
  1404. }
  1405. }
  1406. //----------------------------------------------------------------------------
  1407. std::string
  1408. cmLocalGenerator::ConstructScript(const cmCustomCommandLines& commandLines,
  1409. const char* newline)
  1410. {
  1411. // Store the script in a string.
  1412. std::string script;
  1413. // Write each command on a single line.
  1414. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  1415. cl != commandLines.end(); ++cl)
  1416. {
  1417. // Start with the command name.
  1418. const cmCustomCommandLine& commandLine = *cl;
  1419. script += this->Convert(commandLine[0].c_str(),START_OUTPUT,SHELL);
  1420. // Add the arguments.
  1421. for(unsigned int j=1;j < commandLine.size(); ++j)
  1422. {
  1423. script += " ";
  1424. script += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  1425. }
  1426. // End the line.
  1427. script += newline;
  1428. }
  1429. return script;
  1430. }
  1431. //----------------------------------------------------------------------------
  1432. std::string
  1433. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  1434. {
  1435. return this->Convert(remote, START_OUTPUT, SHELL, true);
  1436. }
  1437. //----------------------------------------------------------------------------
  1438. std::string cmLocalGenerator::Convert(const char* source,
  1439. RelativeRoot relative,
  1440. OutputFormat output,
  1441. bool optional)
  1442. {
  1443. // Convert the path to a relative path.
  1444. std::string result = source;
  1445. if (!optional || m_UseRelativePaths)
  1446. {
  1447. switch (relative)
  1448. {
  1449. case HOME:
  1450. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1451. result = m_GlobalGenerator->
  1452. ConvertToRelativePath(m_HomeDirectoryComponents, result.c_str());
  1453. break;
  1454. case START:
  1455. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1456. result = m_GlobalGenerator->
  1457. ConvertToRelativePath(m_StartDirectoryComponents, result.c_str());
  1458. break;
  1459. case HOME_OUTPUT:
  1460. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1461. result = m_GlobalGenerator->
  1462. ConvertToRelativePath(m_HomeOutputDirectoryComponents, result.c_str());
  1463. break;
  1464. case START_OUTPUT:
  1465. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1466. result = m_GlobalGenerator->
  1467. ConvertToRelativePath(m_StartOutputDirectoryComponents, result.c_str());
  1468. break;
  1469. case FULL:
  1470. result = cmSystemTools::CollapseFullPath(result.c_str());
  1471. break;
  1472. case NONE:
  1473. break;
  1474. }
  1475. }
  1476. // Now convert it to an output path.
  1477. if (output == MAKEFILE || output == SHELL)
  1478. {
  1479. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1480. }
  1481. return result;
  1482. }