cmLocalGenerator.cxx 47 KB

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