cmLocalGenerator.cxx 45 KB

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