cmLocalGenerator.cxx 43 KB

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