cmLocalGenerator.cxx 40 KB

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