cmLocalGenerator.cxx 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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 fout(file.c_str());
  81. fout.SetCopyIfDifferent(true);
  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(!llang)
  569. {
  570. cmSystemTools::Error("CMake can not determine linker language for target:",
  571. target.GetName());
  572. return;
  573. }
  574. // if the language is not in the set lang then create custom
  575. // commands to build the target
  576. if(lang.count(llang) == 0)
  577. {
  578. this->AddBuildTargetRule(llang, target);
  579. }
  580. }
  581. break;
  582. case cmTarget::UTILITY:
  583. case cmTarget::INSTALL_FILES:
  584. case cmTarget::INSTALL_PROGRAMS:
  585. break;
  586. }
  587. }
  588. }
  589. struct RuleVariables
  590. {
  591. const char* variable;
  592. };
  593. // List of variables that are replaced when
  594. // rules are expanced. These variables are
  595. // replaced in the form <var> with GetSafeDefinition(var).
  596. // ${LANG} is replaced in the variable first with all enabled
  597. // languages.
  598. static const char* ruleReplaceVars[] =
  599. {
  600. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  601. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  602. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  603. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  604. "CMAKE_${LANG}_LINK_FLAGS",
  605. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  606. "CMAKE_${LANG}_ARCHIVE",
  607. "CMAKE_${LANG}_COMPILER",
  608. "CMAKE_AR",
  609. "CMAKE_CURRENT_SOURCE_DIR",
  610. "CMAKE_CURRENT_BINARY_DIR",
  611. "CMAKE_RANLIB",
  612. 0
  613. };
  614. void
  615. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  616. const char* lang,
  617. const char* objects,
  618. const char* target,
  619. const char* linkLibs,
  620. const char* source,
  621. const char* object,
  622. const char* flags,
  623. const char* objectsquoted,
  624. const char* targetBase,
  625. const char* targetSOName,
  626. const char* linkFlags)
  627. {
  628. std::vector<std::string> enabledLanguages;
  629. m_GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  630. if(linkFlags)
  631. {
  632. cmSystemTools::ReplaceString(s, "<LINK_FLAGS>", linkFlags);
  633. }
  634. if(flags)
  635. {
  636. cmSystemTools::ReplaceString(s, "<FLAGS>", flags);
  637. }
  638. if(source)
  639. {
  640. cmSystemTools::ReplaceString(s, "<SOURCE>", source);
  641. }
  642. if(object)
  643. {
  644. cmSystemTools::ReplaceString(s, "<OBJECT>", object);
  645. }
  646. if(objects)
  647. {
  648. cmSystemTools::ReplaceString(s, "<OBJECTS>", objects);
  649. }
  650. if(objectsquoted)
  651. {
  652. cmSystemTools::ReplaceString(s, "<OBJECTS_QUOTED>", objectsquoted);
  653. }
  654. if(target)
  655. {
  656. std::string targetQuoted = target;
  657. if(targetQuoted.size() && targetQuoted[0] != '\"')
  658. {
  659. targetQuoted = '\"';
  660. targetQuoted += target;
  661. targetQuoted += '\"';
  662. }
  663. cmSystemTools::ReplaceString(s, "<TARGET_QUOTED>", targetQuoted.c_str());
  664. cmSystemTools::ReplaceString(s, "<TARGET>", target);
  665. }
  666. if(targetBase)
  667. {
  668. // special case for quoted paths with spaces
  669. // if you see <TARGET_BASE>.lib then put the .lib inside
  670. // the quotes, same for .dll
  671. if((strlen(targetBase) > 1) && targetBase[0] == '\"')
  672. {
  673. std::string base = targetBase;
  674. base[base.size()-1] = '.';
  675. std::string baseLib = base + "lib\"";
  676. std::string baseDll = base + "dll\"";
  677. cmSystemTools::ReplaceString(s, "<TARGET_BASE>.lib", baseLib.c_str());
  678. cmSystemTools::ReplaceString(s, "<TARGET_BASE>.dll", baseDll.c_str());
  679. }
  680. cmSystemTools::ReplaceString(s, "<TARGET_BASE>", targetBase);
  681. }
  682. if(targetSOName)
  683. {
  684. bool replaced = false;
  685. if(lang)
  686. {
  687. std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
  688. name += lang;
  689. name += "_FLAG";
  690. if(m_Makefile->GetDefinition(name.c_str()))
  691. {
  692. replaced = true;
  693. cmSystemTools::ReplaceString(s, "<TARGET_SONAME>", targetSOName);
  694. }
  695. }
  696. if(!replaced)
  697. {
  698. cmSystemTools::ReplaceString(s, "<TARGET_SONAME>", "");
  699. }
  700. }
  701. if(linkLibs)
  702. {
  703. cmSystemTools::ReplaceString(s, "<LINK_LIBRARIES>", linkLibs);
  704. }
  705. // loop over language specific replace variables
  706. int pos = 0;
  707. while(ruleReplaceVars[pos])
  708. {
  709. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  710. i != enabledLanguages.end(); ++i)
  711. {
  712. lang = i->c_str();
  713. std::string replace = "<";
  714. replace += ruleReplaceVars[pos];
  715. replace += ">";
  716. std::string replaceWith = ruleReplaceVars[pos];
  717. std::string actualReplace = replace;
  718. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  719. std::string actualReplaceWith = replaceWith;
  720. cmSystemTools::ReplaceString(actualReplaceWith, "${LANG}", lang);
  721. replace = m_Makefile->GetSafeDefinition(actualReplaceWith.c_str());
  722. // if the variable is not a FLAG then treat it like a path
  723. if(actualReplaceWith.find("_FLAG") == actualReplaceWith.npos)
  724. {
  725. replace = this->ConvertToOutputForExisting(replace.c_str());
  726. }
  727. if(actualReplace.size())
  728. {
  729. cmSystemTools::ReplaceString(s, actualReplace.c_str(), replace.c_str());
  730. }
  731. }
  732. pos++;
  733. }
  734. }
  735. std::string
  736. cmLocalGenerator::ConvertToOutputForExisting(const char* p)
  737. {
  738. std::string ret = this->ConvertToRelativeOutputPath(p);
  739. // if there are spaces in the path, then get the short path version
  740. // if there is one
  741. if(ret.find(' ') != std::string::npos)
  742. {
  743. if(cmSystemTools::FileExists(p))
  744. {
  745. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  746. {
  747. ret = this->ConvertToRelativeOutputPath(p);
  748. }
  749. }
  750. }
  751. return ret;
  752. }
  753. const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
  754. {
  755. if(!lang)
  756. {
  757. return "";
  758. }
  759. if(m_LanguageToIncludeFlags.count(lang))
  760. {
  761. return m_LanguageToIncludeFlags[lang].c_str();
  762. }
  763. cmOStringStream includeFlags;
  764. std::vector<std::string> includes;
  765. this->GetIncludeDirectories(includes);
  766. std::vector<std::string>::iterator i;
  767. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  768. flagVar += lang;
  769. const char* includeFlag = m_Makefile->GetDefinition(flagVar.c_str());
  770. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  771. flagVar += lang;
  772. const char* sep = m_Makefile->GetDefinition(flagVar.c_str());
  773. bool repeatFlag = true; // should the include flag be repeated like ie. -IA -IB
  774. if(!sep)
  775. {
  776. sep = " ";
  777. }
  778. else
  779. {
  780. // if there is a separator then the flag is not repeated but is only given once
  781. // i.e. -classpath a:b:c
  782. repeatFlag = false;
  783. }
  784. bool flagUsed = false;
  785. for(i = includes.begin(); i != includes.end(); ++i)
  786. {
  787. std::string include = *i;
  788. if(!flagUsed || repeatFlag)
  789. {
  790. includeFlags << includeFlag;
  791. flagUsed = true;
  792. }
  793. includeFlags << this->ConvertToOutputForExisting(i->c_str()) << sep;
  794. }
  795. std::string flags = includeFlags.str();
  796. // remove trailing separators
  797. if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
  798. {
  799. flags[flags.size()-1] = ' ';
  800. }
  801. flags += m_Makefile->GetDefineFlags();
  802. m_LanguageToIncludeFlags[lang] = flags;
  803. return m_LanguageToIncludeFlags[lang].c_str();
  804. }
  805. //----------------------------------------------------------------------------
  806. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs)
  807. {
  808. // Output Include paths
  809. std::set<cmStdString> implicitIncludes;
  810. // CMake versions below 2.0 would add the source tree to the -I path
  811. // automatically. Preserve compatibility.
  812. bool includeSourceDir = false;
  813. const char* versionValue =
  814. m_Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  815. if(versionValue)
  816. {
  817. int major = 0;
  818. int minor = 0;
  819. if(sscanf(versionValue, "%d.%d", &major, &minor) == 2 && major < 2)
  820. {
  821. includeSourceDir = true;
  822. }
  823. }
  824. const char* vtkSourceDir =
  825. m_Makefile->GetDefinition("VTK_SOURCE_DIR");
  826. if(vtkSourceDir)
  827. {
  828. // Special hack for VTK 4.0 - 4.4.
  829. const char* vtk_major = m_Makefile->GetDefinition("VTK_MAJOR_VERSION");
  830. const char* vtk_minor = m_Makefile->GetDefinition("VTK_MINOR_VERSION");
  831. vtk_major = vtk_major? vtk_major : "4";
  832. vtk_minor = vtk_minor? vtk_minor : "4";
  833. int major = 0;
  834. int minor = 0;
  835. if(sscanf(vtk_major, "%d", &major) && sscanf(vtk_minor, "%d", &minor) &&
  836. major == 4 && minor <= 4)
  837. {
  838. includeSourceDir = true;
  839. }
  840. }
  841. if(includeSourceDir)
  842. {
  843. dirs.push_back(m_Makefile->GetStartDirectory());
  844. }
  845. // Do not explicitly add the standard include path "/usr/include".
  846. // This can cause problems with certain standard library
  847. // implementations because the wrong headers may be found first.
  848. implicitIncludes.insert("/usr/include");
  849. if(m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  850. {
  851. std::string arg = m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES");
  852. std::vector<std::string> implicitIncludeVec;
  853. cmSystemTools::ExpandListArgument(arg, implicitIncludeVec);
  854. for(unsigned int k =0; k < implicitIncludeVec.size(); k++)
  855. {
  856. implicitIncludes.insert(implicitIncludeVec[k]);
  857. }
  858. }
  859. // Construct the ordered list.
  860. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  861. for(std::vector<std::string>::iterator i = includes.begin();
  862. i != includes.end(); ++i)
  863. {
  864. if(implicitIncludes.find(*i) == implicitIncludes.end())
  865. {
  866. dirs.push_back(*i);
  867. }
  868. }
  869. }
  870. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  871. std::string& flags,
  872. std::string& linkFlags,
  873. cmTarget& target)
  874. {
  875. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  876. buildType = cmSystemTools::UpperCase(buildType);
  877. const char* libraryLinkVariable = "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  878. switch(target.GetType())
  879. {
  880. case cmTarget::STATIC_LIBRARY:
  881. {
  882. const char* targetLinkFlags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  883. if(targetLinkFlags)
  884. {
  885. linkFlags += targetLinkFlags;
  886. linkFlags += " ";
  887. }
  888. }
  889. break;
  890. case cmTarget::MODULE_LIBRARY:
  891. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  892. case cmTarget::SHARED_LIBRARY:
  893. {
  894. linkFlags = m_Makefile->GetSafeDefinition(libraryLinkVariable);
  895. linkFlags += " ";
  896. if(buildType.size())
  897. {
  898. std::string build = libraryLinkVariable;
  899. build += "_";
  900. build += buildType;
  901. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  902. linkFlags += " ";
  903. }
  904. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  905. {
  906. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  907. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  908. i != sources.end(); ++i)
  909. {
  910. if((*i)->GetSourceExtension() == "def")
  911. {
  912. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  913. linkFlags += this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str());
  914. linkFlags += " ";
  915. }
  916. }
  917. }
  918. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  919. if(targetLinkFlags)
  920. {
  921. linkFlags += targetLinkFlags;
  922. linkFlags += " ";
  923. }
  924. cmOStringStream linklibsStr;
  925. this->OutputLinkLibraries(linklibsStr, target.GetName(), target);
  926. linkLibs = linklibsStr.str();
  927. }
  928. break;
  929. case cmTarget::EXECUTABLE:
  930. {
  931. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  932. linkFlags += " ";
  933. if(buildType.size())
  934. {
  935. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  936. build += buildType;
  937. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  938. linkFlags += " ";
  939. }
  940. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  941. if(!linkLanguage)
  942. {
  943. cmSystemTools::Error("CMake can not determine linker language for target:",
  944. target.GetName());
  945. return;
  946. }
  947. std::string langVar = "CMAKE_";
  948. langVar += linkLanguage;
  949. std::string flagsVar = langVar + "_FLAGS";
  950. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  951. sharedFlagsVar += linkLanguage;
  952. sharedFlagsVar += "_FLAGS";
  953. flags += m_Makefile->GetSafeDefinition(flagsVar.c_str());
  954. flags += " ";
  955. flags += m_Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  956. flags += " ";
  957. cmOStringStream linklibs;
  958. this->OutputLinkLibraries(linklibs, 0, target);
  959. linkLibs = linklibs.str();
  960. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  961. {
  962. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") + linkLanguage
  963. + std::string("_FLAGS");
  964. linkFlags += m_Makefile->GetSafeDefinition(sFlagVar.c_str());
  965. linkFlags += " ";
  966. }
  967. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  968. {
  969. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  970. linkFlags += " ";
  971. }
  972. else
  973. {
  974. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  975. linkFlags += " ";
  976. }
  977. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  978. if(targetLinkFlags)
  979. {
  980. linkFlags += targetLinkFlags;
  981. linkFlags += " ";
  982. }
  983. }
  984. break;
  985. case cmTarget::UTILITY:
  986. case cmTarget::INSTALL_FILES:
  987. case cmTarget::INSTALL_PROGRAMS:
  988. break;
  989. }
  990. }
  991. /**
  992. * Output the linking rules on a command line. For executables,
  993. * targetLibrary should be a NULL pointer. For libraries, it should point
  994. * to the name of the library. This will not link a library against itself.
  995. */
  996. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  997. const char* targetLibrary,
  998. const cmTarget &tgt)
  999. {
  1000. // Try to emit each search path once
  1001. std::set<cmStdString> emitted;
  1002. // Embed runtime search paths if possible and if required.
  1003. bool outputRuntime = true;
  1004. std::string runtimeFlag;
  1005. std::string runtimeSep;
  1006. std::vector<std::string> runtimeDirs;
  1007. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1008. buildType = cmSystemTools::UpperCase(buildType);
  1009. const char* linkLanguage = tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1010. if(!linkLanguage)
  1011. {
  1012. cmSystemTools::Error("CMake can not determine linker language for target:",
  1013. tgt.GetName());
  1014. return;
  1015. }
  1016. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1017. runTimeFlagVar += linkLanguage;
  1018. runTimeFlagVar += "_FLAG";
  1019. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1020. runtimeFlag = m_Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1021. runtimeSep = m_Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1022. // concatenate all paths or no?
  1023. bool runtimeConcatenate = ( runtimeSep!="" );
  1024. if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
  1025. {
  1026. outputRuntime = false;
  1027. }
  1028. // Some search paths should never be emitted
  1029. emitted.insert("");
  1030. emitted.insert("/usr/lib");
  1031. std::string libPathFlag = m_Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1032. std::string libLinkFlag = m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1033. // collect all the flags needed for linking libraries
  1034. std::string linkLibs;
  1035. // Flags to link an executable to shared libraries.
  1036. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1037. linkFlagsVar += linkLanguage;
  1038. linkFlagsVar += "_FLAGS";
  1039. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1040. {
  1041. linkLibs = m_Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1042. linkLibs += " ";
  1043. }
  1044. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  1045. for(std::vector<std::string>::const_iterator libDir = libdirs.begin();
  1046. libDir != libdirs.end(); ++libDir)
  1047. {
  1048. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1049. if(emitted.insert(libpath).second)
  1050. {
  1051. std::string fullLibPath;
  1052. if(!m_WindowsShell && m_UseRelativePaths)
  1053. {
  1054. fullLibPath = "\"`cd ";
  1055. }
  1056. fullLibPath += libpath;
  1057. if(!m_WindowsShell && m_UseRelativePaths)
  1058. {
  1059. fullLibPath += ";pwd`\"";
  1060. }
  1061. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  1062. if((pos == std::string::npos || pos > 0)
  1063. && libDir->find("${") == std::string::npos)
  1064. {
  1065. linkLibs += libPathFlag;
  1066. if(outputRuntime)
  1067. {
  1068. runtimeDirs.push_back( fullLibPath );
  1069. }
  1070. }
  1071. linkLibs += fullLibPath;
  1072. linkLibs += " ";
  1073. }
  1074. }
  1075. std::string linkSuffix = m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1076. std::string regexp = ".*\\";
  1077. regexp += linkSuffix;
  1078. regexp += "$";
  1079. cmsys::RegularExpression hasSuffix(regexp.c_str());
  1080. std::string librariesLinked;
  1081. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  1082. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  1083. lib != libs.end(); ++lib)
  1084. {
  1085. // Don't link the library against itself!
  1086. if(targetLibrary && (lib->first == targetLibrary)) continue;
  1087. // use the correct lib for the current configuration
  1088. if (lib->second == cmTarget::DEBUG && buildType != "DEBUG")
  1089. {
  1090. continue;
  1091. }
  1092. if (lib->second == cmTarget::OPTIMIZED && buildType == "DEBUG")
  1093. {
  1094. continue;
  1095. }
  1096. // skip zero size library entries, this may happen
  1097. // if a variable expands to nothing.
  1098. if (lib->first.size() == 0) continue;
  1099. // if it is a full path break it into -L and -l
  1100. cmsys::RegularExpression reg("^([ \t]*\\-[lWRB])|([ \t]*\\-framework)|(\\${)|([ \t]*\\-pthread)|([ \t]*`)");
  1101. if(lib->first.find('/') != std::string::npos
  1102. && !reg.find(lib->first))
  1103. {
  1104. std::string dir, file;
  1105. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  1106. dir, file);
  1107. std::string libpath = this->ConvertToOutputForExisting(dir.c_str());
  1108. if(emitted.insert(libpath).second)
  1109. {
  1110. linkLibs += libPathFlag;
  1111. linkLibs += libpath;
  1112. linkLibs += " ";
  1113. if(outputRuntime)
  1114. {
  1115. runtimeDirs.push_back( libpath );
  1116. }
  1117. }
  1118. cmsys::RegularExpression libname("^lib([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  1119. cmsys::RegularExpression libname_noprefix("([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  1120. if(libname.find(file))
  1121. {
  1122. // Library had "lib" prefix.
  1123. librariesLinked += libLinkFlag;
  1124. file = libname.match(1);
  1125. // if ignore libprefix is on,
  1126. // then add the lib prefix back into the name
  1127. if(m_IgnoreLibPrefix)
  1128. {
  1129. file = "lib" + file;
  1130. }
  1131. librariesLinked += file;
  1132. if(linkSuffix.size() && !hasSuffix.find(file))
  1133. {
  1134. librariesLinked += linkSuffix;
  1135. }
  1136. librariesLinked += " ";
  1137. }
  1138. else if(libname_noprefix.find(file))
  1139. {
  1140. // Library had no "lib" prefix.
  1141. librariesLinked += libLinkFlag;
  1142. file = libname_noprefix.match(1);
  1143. librariesLinked += file;
  1144. if(linkSuffix.size() && !hasSuffix.find(file))
  1145. {
  1146. librariesLinked += linkSuffix;
  1147. }
  1148. librariesLinked += " ";
  1149. }
  1150. else
  1151. {
  1152. // Error parsing the library name. Just use the full path.
  1153. // The linker will give an error if it is invalid.
  1154. librariesLinked += lib->first;
  1155. librariesLinked += " ";
  1156. }
  1157. }
  1158. // not a full path, so add -l name
  1159. else
  1160. {
  1161. if(!reg.find(lib->first))
  1162. {
  1163. librariesLinked += libLinkFlag;
  1164. }
  1165. librariesLinked += lib->first;
  1166. if(linkSuffix.size() && !hasSuffix.find(lib->first))
  1167. {
  1168. librariesLinked += linkSuffix;
  1169. }
  1170. librariesLinked += " ";
  1171. }
  1172. }
  1173. linkLibs += librariesLinked;
  1174. fout << linkLibs;
  1175. if(outputRuntime && runtimeDirs.size()>0)
  1176. {
  1177. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1178. // a "-R a -R b -R c" type link line
  1179. fout << runtimeFlag;
  1180. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1181. fout << *itr;
  1182. ++itr;
  1183. for( ; itr != runtimeDirs.end(); ++itr )
  1184. {
  1185. if(runtimeConcatenate)
  1186. {
  1187. fout << runtimeSep << *itr;
  1188. }
  1189. else
  1190. {
  1191. fout << " " << runtimeFlag << *itr;
  1192. }
  1193. }
  1194. fout << " ";
  1195. }
  1196. if(m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  1197. {
  1198. fout << m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES") << " ";
  1199. }
  1200. }
  1201. //----------------------------------------------------------------------------
  1202. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1203. const char* lang)
  1204. {
  1205. // Add language-specific flags.
  1206. std::string flagsVar = "CMAKE_";
  1207. flagsVar += lang;
  1208. flagsVar += "_FLAGS";
  1209. this->AddConfigVariableFlags(flags, flagsVar.c_str());
  1210. }
  1211. //----------------------------------------------------------------------------
  1212. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1213. const char* lang,
  1214. bool shared)
  1215. {
  1216. std::string flagsVar;
  1217. // Add flags for dealing with shared libraries for this language.
  1218. if(shared)
  1219. {
  1220. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1221. flagsVar += lang;
  1222. flagsVar += "_FLAGS";
  1223. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1224. }
  1225. // Add flags specific to shared builds.
  1226. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1227. {
  1228. flagsVar = "CMAKE_SHARED_BUILD_";
  1229. flagsVar += lang;
  1230. flagsVar += "_FLAGS";
  1231. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1232. }
  1233. }
  1234. //----------------------------------------------------------------------------
  1235. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1236. const char* var)
  1237. {
  1238. // Add the flags from the variable itself.
  1239. std::string flagsVar = var;
  1240. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1241. // Add the flags from the build-type specific variable.
  1242. const char* buildType = m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1243. if(buildType && *buildType)
  1244. {
  1245. flagsVar += "_";
  1246. flagsVar += cmSystemTools::UpperCase(buildType);
  1247. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1248. }
  1249. }
  1250. //----------------------------------------------------------------------------
  1251. void cmLocalGenerator::AppendFlags(std::string& flags,
  1252. const char* newFlags)
  1253. {
  1254. if(newFlags && *newFlags)
  1255. {
  1256. if(flags.size())
  1257. {
  1258. flags += " ";
  1259. }
  1260. flags += newFlags;
  1261. }
  1262. }