cmLocalGenerator.cxx 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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. // NOTE, much of this was copied to
  364. // cmGlobalXCodeGenerator::ConvertToRelativeOutputPath
  365. // fixes here should be made there as well.
  366. // copy to a string class
  367. std::string pathIn = p;
  368. // check to see if the path is already relative, it is
  369. // considered relative if one of the following is true
  370. // - has no / in it at all
  371. // - does not start with / or drive leter :
  372. // - starts with a ".."
  373. if(pathIn.find('/') == pathIn.npos ||
  374. (pathIn[0] != '/' && pathIn[1] != ':') ||
  375. pathIn.find("..") == 0)
  376. {
  377. return cmSystemTools::ConvertToOutputPath(p);
  378. }
  379. // do not use relative paths for network build trees
  380. // the network paths do not work
  381. const char* outputDirectory = m_Makefile->GetHomeOutputDirectory();
  382. if ( outputDirectory && *outputDirectory && *(outputDirectory+1) &&
  383. outputDirectory[0] == '/' && outputDirectory[1] == '/' )
  384. {
  385. return cmSystemTools::ConvertToOutputPath(p);
  386. }
  387. // if the path is double quoted remove the double quotes
  388. if(pathIn.size() && pathIn[0] == '\"')
  389. {
  390. pathIn = pathIn.substr(1, pathIn.size()-2);
  391. }
  392. // The first time this is called
  393. // initialize m_CurrentOutputDirectory to contain
  394. // the full path to the current output directory
  395. // This has to be done here and not in the constructor
  396. // because the output directory is not yet set in the constructor.
  397. if(m_CurrentOutputDirectory.size() == 0)
  398. {
  399. m_CurrentOutputDirectory = cmSystemTools::CollapseFullPath(m_Makefile->GetCurrentOutputDirectory());
  400. }
  401. // Given that we are in m_CurrentOutputDirectory how to we
  402. // get to pathIn with a relative path, store in ret
  403. std::string ret = cmSystemTools::RelativePath(m_CurrentOutputDirectory.c_str(), pathIn.c_str());
  404. // If the path is 0 sized make it a .
  405. // this happens when pathIn is the same as m_CurrentOutputDirectory
  406. if(ret.size() == 0)
  407. {
  408. ret = ".";
  409. }
  410. // if there was a trailing / there still is one, and
  411. // if there was not one, there still is not one
  412. if(ret[ret.size()-1] == '/' &&
  413. pathIn[pathIn.size()-1] != '/')
  414. {
  415. ret.erase(ret.size()-1, 1);
  416. }
  417. if(ret[ret.size()-1] != '/' &&
  418. pathIn[pathIn.size()-1] == '/')
  419. {
  420. ret += "/";
  421. }
  422. // Now convert the relative path to an output path
  423. ret = cmSystemTools::ConvertToOutputPath(ret.c_str());
  424. // finally return the path
  425. // at this point it should be relative and in the correct format
  426. // for the native build system. (i.e. \ for windows and / for unix,
  427. // and correct escaping/quoting of spaces in the path
  428. return ret;
  429. }
  430. void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
  431. const char* lang,
  432. cmSourceFile& source,
  433. cmTarget& )
  434. {
  435. std::string objectFile = this->ConvertToRelativeOutputPath(ofname);
  436. std::string sourceFile = this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  437. std::string varString = "CMAKE_";
  438. varString += lang;
  439. varString += "_COMPILE_OBJECT";
  440. std::vector<std::string> rules;
  441. rules.push_back(m_Makefile->GetRequiredDefinition(varString.c_str()));
  442. varString = "CMAKE_";
  443. varString += lang;
  444. varString += "_FLAGS";
  445. std::string flags;
  446. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  447. flags += " ";
  448. flags += this->GetIncludeFlags(lang);
  449. std::vector<std::string> commands;
  450. cmSystemTools::ExpandList(rules, commands);
  451. for(std::vector<std::string>::iterator i = commands.begin();
  452. i != commands.end(); ++i)
  453. {
  454. this->ExpandRuleVariables(*i,
  455. lang,
  456. 0, // no objects
  457. 0, // no target
  458. 0, // no link libs
  459. sourceFile.c_str(),
  460. objectFile.c_str(),
  461. flags.c_str());
  462. }
  463. std::vector<std::string> sourceAndDeps;
  464. sourceAndDeps.push_back(sourceFile);
  465. if(commands.size() > 1)
  466. {
  467. cmSystemTools::Error("Currently custom rules can only have one command sorry ");
  468. }
  469. // Check for extra object-file dependencies.
  470. std::vector<std::string> depends;
  471. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  472. if(additionalDeps)
  473. {
  474. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  475. for(std::vector<std::string>::iterator i = depends.begin();
  476. i != depends.end(); ++i)
  477. {
  478. sourceAndDeps.push_back(this->ConvertToRelativeOutputPath(i->c_str()));
  479. }
  480. }
  481. std::string command;
  482. std::string args;
  483. cmSystemTools::SplitProgramFromArgs(commands[0].c_str(), command, args);
  484. std::vector<std::string> argsv;
  485. argsv.push_back(args);
  486. m_Makefile->AddCustomCommandToOutput(ofname,
  487. command.c_str(),
  488. argsv,
  489. source.GetFullPath().c_str(),
  490. sourceAndDeps,
  491. "build from source");
  492. }
  493. void cmLocalGenerator::AddBuildTargetRule(const char* llang, cmTarget& target)
  494. {
  495. cmStdString objs;
  496. std::vector<std::string> objVector;
  497. // Add all the sources outputs to the depends of the target
  498. std::vector<cmSourceFile*>& classes = target.GetSourceFiles();
  499. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  500. i != classes.end(); ++i)
  501. {
  502. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  503. !(*i)->GetCustomCommand())
  504. {
  505. std::string outExt =
  506. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  507. (*i)->GetSourceExtension().c_str());
  508. if(outExt.size() && !(*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  509. {
  510. std::string ofname = m_Makefile->GetCurrentOutputDirectory();
  511. ofname += "/";
  512. ofname += (*i)->GetSourceName() + outExt;
  513. objVector.push_back(ofname);
  514. this->AddCustomCommandToCreateObject(ofname.c_str(), llang, *(*i), target);
  515. objs += this->ConvertToRelativeOutputPath(ofname.c_str());
  516. objs += " ";
  517. }
  518. }
  519. }
  520. std::string createRule = "CMAKE_";
  521. createRule += llang;
  522. createRule += target.GetCreateRuleVariable();
  523. std::string targetName = this->GetFullTargetName(target.GetName(), target);
  524. // Executable :
  525. // Shared Library:
  526. // Static Library:
  527. // Shared Module:
  528. std::string linkLibs; // should be set
  529. std::string flags; // should be set
  530. std::string linkFlags; // should be set
  531. this->GetTargetFlags(linkLibs, flags, linkFlags, target);
  532. // Change the type to utility
  533. // target.SetType(cmTarget::UTILITY, target.GetName());
  534. std::string rule = m_Makefile->GetRequiredDefinition(createRule.c_str());
  535. this->ExpandRuleVariables(rule,
  536. llang, // language
  537. objs.c_str(), // objects
  538. targetName.c_str(), // target
  539. linkLibs.c_str(), // link libs
  540. 0, // source
  541. 0, // object
  542. flags.c_str(), // flags
  543. 0, // objects quoted
  544. 0, // target base name
  545. 0, // target so name,
  546. linkFlags.c_str() // link flags
  547. );
  548. std::string command;
  549. std::string args;
  550. cmSystemTools::SplitProgramFromArgs(rule.c_str(), command, args);
  551. // Just like ADD_CUSTOM_TARGET(foo ALL DEPENDS a.o b.o)
  552. // Add a custom command for generating each .o file
  553. cmCustomCommand cc(command.c_str(), args.c_str(), objVector, targetName.c_str());
  554. target.GetPostBuildCommands().push_back(cc);
  555. }
  556. void cmLocalGenerator::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
  557. {
  558. cmTargets &tgts = m_Makefile->GetTargets();
  559. for(cmTargets::iterator l = tgts.begin();
  560. l != tgts.end(); l++)
  561. {
  562. cmTarget& target = l->second;
  563. switch(target.GetType())
  564. {
  565. case cmTarget::STATIC_LIBRARY:
  566. case cmTarget::SHARED_LIBRARY:
  567. case cmTarget::MODULE_LIBRARY:
  568. case cmTarget::EXECUTABLE:
  569. {
  570. const char* llang = target.GetLinkerLanguage(this->GetGlobalGenerator());
  571. if(!llang)
  572. {
  573. cmSystemTools::Error("CMake can not determine linker language for target:",
  574. target.GetName());
  575. return;
  576. }
  577. // if the language is not in the set lang then create custom
  578. // commands to build the target
  579. if(lang.count(llang) == 0)
  580. {
  581. this->AddBuildTargetRule(llang, target);
  582. }
  583. }
  584. break;
  585. case cmTarget::UTILITY:
  586. case cmTarget::INSTALL_FILES:
  587. case cmTarget::INSTALL_PROGRAMS:
  588. break;
  589. }
  590. }
  591. }
  592. struct RuleVariables
  593. {
  594. const char* variable;
  595. };
  596. // List of variables that are replaced when
  597. // rules are expanced. These variables are
  598. // replaced in the form <var> with GetSafeDefinition(var).
  599. // ${LANG} is replaced in the variable first with all enabled
  600. // languages.
  601. static const char* ruleReplaceVars[] =
  602. {
  603. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  604. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  605. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  606. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  607. "CMAKE_${LANG}_LINK_FLAGS",
  608. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  609. "CMAKE_${LANG}_ARCHIVE",
  610. "CMAKE_${LANG}_COMPILER",
  611. "CMAKE_AR",
  612. "CMAKE_CURRENT_SOURCE_DIR",
  613. "CMAKE_CURRENT_BINARY_DIR",
  614. "CMAKE_RANLIB",
  615. 0
  616. };
  617. void
  618. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  619. const char* lang,
  620. const char* objects,
  621. const char* target,
  622. const char* linkLibs,
  623. const char* source,
  624. const char* object,
  625. const char* flags,
  626. const char* objectsquoted,
  627. const char* targetBase,
  628. const char* targetSOName,
  629. const char* linkFlags)
  630. {
  631. std::vector<std::string> enabledLanguages;
  632. m_GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  633. if(linkFlags)
  634. {
  635. cmSystemTools::ReplaceString(s, "<LINK_FLAGS>", linkFlags);
  636. }
  637. if(flags)
  638. {
  639. cmSystemTools::ReplaceString(s, "<FLAGS>", flags);
  640. }
  641. if(source)
  642. {
  643. cmSystemTools::ReplaceString(s, "<SOURCE>", source);
  644. }
  645. if(object)
  646. {
  647. cmSystemTools::ReplaceString(s, "<OBJECT>", object);
  648. }
  649. if(objects)
  650. {
  651. cmSystemTools::ReplaceString(s, "<OBJECTS>", objects);
  652. }
  653. if(objectsquoted)
  654. {
  655. cmSystemTools::ReplaceString(s, "<OBJECTS_QUOTED>", objectsquoted);
  656. }
  657. if(target)
  658. {
  659. std::string targetQuoted = target;
  660. if(targetQuoted.size() && targetQuoted[0] != '\"')
  661. {
  662. targetQuoted = '\"';
  663. targetQuoted += target;
  664. targetQuoted += '\"';
  665. }
  666. cmSystemTools::ReplaceString(s, "<TARGET_QUOTED>", targetQuoted.c_str());
  667. cmSystemTools::ReplaceString(s, "<TARGET>", target);
  668. }
  669. if(targetBase)
  670. {
  671. // special case for quoted paths with spaces
  672. // if you see <TARGET_BASE>.lib then put the .lib inside
  673. // the quotes, same for .dll
  674. if((strlen(targetBase) > 1) && targetBase[0] == '\"')
  675. {
  676. std::string base = targetBase;
  677. base[base.size()-1] = '.';
  678. std::string baseLib = base + "lib\"";
  679. std::string baseDll = base + "dll\"";
  680. cmSystemTools::ReplaceString(s, "<TARGET_BASE>.lib", baseLib.c_str());
  681. cmSystemTools::ReplaceString(s, "<TARGET_BASE>.dll", baseDll.c_str());
  682. }
  683. cmSystemTools::ReplaceString(s, "<TARGET_BASE>", targetBase);
  684. }
  685. if(targetSOName)
  686. {
  687. bool replaced = false;
  688. if(lang)
  689. {
  690. std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
  691. name += lang;
  692. name += "_FLAG";
  693. if(m_Makefile->GetDefinition(name.c_str()))
  694. {
  695. replaced = true;
  696. cmSystemTools::ReplaceString(s, "<TARGET_SONAME>", targetSOName);
  697. }
  698. }
  699. if(!replaced)
  700. {
  701. cmSystemTools::ReplaceString(s, "<TARGET_SONAME>", "");
  702. }
  703. }
  704. if(linkLibs)
  705. {
  706. cmSystemTools::ReplaceString(s, "<LINK_LIBRARIES>", linkLibs);
  707. }
  708. // loop over language specific replace variables
  709. int pos = 0;
  710. while(ruleReplaceVars[pos])
  711. {
  712. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  713. i != enabledLanguages.end(); ++i)
  714. {
  715. lang = i->c_str();
  716. std::string replace = "<";
  717. replace += ruleReplaceVars[pos];
  718. replace += ">";
  719. std::string replaceWith = ruleReplaceVars[pos];
  720. std::string actualReplace = replace;
  721. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  722. std::string actualReplaceWith = replaceWith;
  723. cmSystemTools::ReplaceString(actualReplaceWith, "${LANG}", lang);
  724. replace = m_Makefile->GetSafeDefinition(actualReplaceWith.c_str());
  725. // if the variable is not a FLAG then treat it like a path
  726. if(actualReplaceWith.find("_FLAG") == actualReplaceWith.npos)
  727. {
  728. replace = this->ConvertToOutputForExisting(replace.c_str());
  729. }
  730. if(actualReplace.size())
  731. {
  732. cmSystemTools::ReplaceString(s, actualReplace.c_str(), replace.c_str());
  733. }
  734. }
  735. pos++;
  736. }
  737. }
  738. std::string
  739. cmLocalGenerator::ConvertToOutputForExisting(const char* p)
  740. {
  741. std::string ret = this->ConvertToRelativeOutputPath(p);
  742. // if there are spaces in the path, then get the short path version
  743. // if there is one
  744. if(ret.find(' ') != std::string::npos)
  745. {
  746. if(cmSystemTools::FileExists(p))
  747. {
  748. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  749. {
  750. ret = this->ConvertToRelativeOutputPath(p);
  751. }
  752. }
  753. }
  754. return ret;
  755. }
  756. const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
  757. {
  758. if(!lang)
  759. {
  760. return "";
  761. }
  762. if(m_LanguageToIncludeFlags.count(lang))
  763. {
  764. return m_LanguageToIncludeFlags[lang].c_str();
  765. }
  766. cmOStringStream includeFlags;
  767. std::vector<std::string> includes;
  768. this->GetIncludeDirectories(includes);
  769. std::vector<std::string>::iterator i;
  770. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  771. flagVar += lang;
  772. const char* includeFlag = m_Makefile->GetDefinition(flagVar.c_str());
  773. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  774. flagVar += lang;
  775. const char* sep = m_Makefile->GetDefinition(flagVar.c_str());
  776. bool repeatFlag = true; // should the include flag be repeated like ie. -IA -IB
  777. if(!sep)
  778. {
  779. sep = " ";
  780. }
  781. else
  782. {
  783. // if there is a separator then the flag is not repeated but is only given once
  784. // i.e. -classpath a:b:c
  785. repeatFlag = false;
  786. }
  787. bool flagUsed = false;
  788. for(i = includes.begin(); i != includes.end(); ++i)
  789. {
  790. std::string include = *i;
  791. if(!flagUsed || repeatFlag)
  792. {
  793. includeFlags << includeFlag;
  794. flagUsed = true;
  795. }
  796. includeFlags << this->ConvertToOutputForExisting(i->c_str()) << sep;
  797. }
  798. std::string flags = includeFlags.str();
  799. // remove trailing separators
  800. if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
  801. {
  802. flags[flags.size()-1] = ' ';
  803. }
  804. flags += m_Makefile->GetDefineFlags();
  805. m_LanguageToIncludeFlags[lang] = flags;
  806. return m_LanguageToIncludeFlags[lang].c_str();
  807. }
  808. //----------------------------------------------------------------------------
  809. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs)
  810. {
  811. // Output Include paths
  812. std::set<cmStdString> implicitIncludes;
  813. // CMake versions below 2.0 would add the source tree to the -I path
  814. // automatically. Preserve compatibility.
  815. bool includeSourceDir = false;
  816. const char* versionValue =
  817. m_Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  818. if(versionValue)
  819. {
  820. int major = 0;
  821. int minor = 0;
  822. if(sscanf(versionValue, "%d.%d", &major, &minor) == 2 && major < 2)
  823. {
  824. includeSourceDir = true;
  825. }
  826. }
  827. const char* vtkSourceDir =
  828. m_Makefile->GetDefinition("VTK_SOURCE_DIR");
  829. if(vtkSourceDir)
  830. {
  831. // Special hack for VTK 4.0 - 4.4.
  832. const char* vtk_major = m_Makefile->GetDefinition("VTK_MAJOR_VERSION");
  833. const char* vtk_minor = m_Makefile->GetDefinition("VTK_MINOR_VERSION");
  834. vtk_major = vtk_major? vtk_major : "4";
  835. vtk_minor = vtk_minor? vtk_minor : "4";
  836. int major = 0;
  837. int minor = 0;
  838. if(sscanf(vtk_major, "%d", &major) && sscanf(vtk_minor, "%d", &minor) &&
  839. major == 4 && minor <= 4)
  840. {
  841. includeSourceDir = true;
  842. }
  843. }
  844. if(includeSourceDir)
  845. {
  846. dirs.push_back(m_Makefile->GetStartDirectory());
  847. }
  848. // Do not explicitly add the standard include path "/usr/include".
  849. // This can cause problems with certain standard library
  850. // implementations because the wrong headers may be found first.
  851. implicitIncludes.insert("/usr/include");
  852. if(m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  853. {
  854. std::string arg = m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES");
  855. std::vector<std::string> implicitIncludeVec;
  856. cmSystemTools::ExpandListArgument(arg, implicitIncludeVec);
  857. for(unsigned int k =0; k < implicitIncludeVec.size(); k++)
  858. {
  859. implicitIncludes.insert(implicitIncludeVec[k]);
  860. }
  861. }
  862. // Construct the ordered list.
  863. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  864. for(std::vector<std::string>::iterator i = includes.begin();
  865. i != includes.end(); ++i)
  866. {
  867. if(implicitIncludes.find(*i) == implicitIncludes.end())
  868. {
  869. dirs.push_back(*i);
  870. }
  871. }
  872. }
  873. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  874. std::string& flags,
  875. std::string& linkFlags,
  876. cmTarget& target)
  877. {
  878. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  879. buildType = cmSystemTools::UpperCase(buildType);
  880. const char* libraryLinkVariable = "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  881. switch(target.GetType())
  882. {
  883. case cmTarget::STATIC_LIBRARY:
  884. {
  885. const char* targetLinkFlags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  886. if(targetLinkFlags)
  887. {
  888. linkFlags += targetLinkFlags;
  889. linkFlags += " ";
  890. }
  891. }
  892. break;
  893. case cmTarget::MODULE_LIBRARY:
  894. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  895. case cmTarget::SHARED_LIBRARY:
  896. {
  897. linkFlags = m_Makefile->GetSafeDefinition(libraryLinkVariable);
  898. linkFlags += " ";
  899. if(buildType.size())
  900. {
  901. std::string build = libraryLinkVariable;
  902. build += "_";
  903. build += buildType;
  904. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  905. linkFlags += " ";
  906. }
  907. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  908. {
  909. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  910. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  911. i != sources.end(); ++i)
  912. {
  913. if((*i)->GetSourceExtension() == "def")
  914. {
  915. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  916. linkFlags += this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str());
  917. linkFlags += " ";
  918. }
  919. }
  920. }
  921. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  922. if(targetLinkFlags)
  923. {
  924. linkFlags += targetLinkFlags;
  925. linkFlags += " ";
  926. }
  927. cmOStringStream linklibsStr;
  928. this->OutputLinkLibraries(linklibsStr, target.GetName(), target);
  929. linkLibs = linklibsStr.str();
  930. }
  931. break;
  932. case cmTarget::EXECUTABLE:
  933. {
  934. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  935. linkFlags += " ";
  936. if(buildType.size())
  937. {
  938. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  939. build += buildType;
  940. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  941. linkFlags += " ";
  942. }
  943. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  944. if(!linkLanguage)
  945. {
  946. cmSystemTools::Error("CMake can not determine linker language for target:",
  947. target.GetName());
  948. return;
  949. }
  950. std::string langVar = "CMAKE_";
  951. langVar += linkLanguage;
  952. std::string flagsVar = langVar + "_FLAGS";
  953. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  954. sharedFlagsVar += linkLanguage;
  955. sharedFlagsVar += "_FLAGS";
  956. flags += m_Makefile->GetSafeDefinition(flagsVar.c_str());
  957. flags += " ";
  958. flags += m_Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  959. flags += " ";
  960. cmOStringStream linklibs;
  961. this->OutputLinkLibraries(linklibs, 0, target);
  962. linkLibs = linklibs.str();
  963. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  964. {
  965. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") + linkLanguage
  966. + std::string("_FLAGS");
  967. linkFlags += m_Makefile->GetSafeDefinition(sFlagVar.c_str());
  968. linkFlags += " ";
  969. }
  970. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  971. {
  972. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  973. linkFlags += " ";
  974. }
  975. else
  976. {
  977. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  978. linkFlags += " ";
  979. }
  980. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  981. if(targetLinkFlags)
  982. {
  983. linkFlags += targetLinkFlags;
  984. linkFlags += " ";
  985. }
  986. }
  987. break;
  988. case cmTarget::UTILITY:
  989. case cmTarget::INSTALL_FILES:
  990. case cmTarget::INSTALL_PROGRAMS:
  991. break;
  992. }
  993. }
  994. /**
  995. * Output the linking rules on a command line. For executables,
  996. * targetLibrary should be a NULL pointer. For libraries, it should point
  997. * to the name of the library. This will not link a library against itself.
  998. */
  999. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  1000. const char* targetLibrary,
  1001. const cmTarget &tgt)
  1002. {
  1003. // Try to emit each search path once
  1004. std::set<cmStdString> emitted;
  1005. // Embed runtime search paths if possible and if required.
  1006. bool outputRuntime = true;
  1007. std::string runtimeFlag;
  1008. std::string runtimeSep;
  1009. std::vector<std::string> runtimeDirs;
  1010. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1011. buildType = cmSystemTools::UpperCase(buildType);
  1012. const char* linkLanguage = tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1013. if(!linkLanguage)
  1014. {
  1015. cmSystemTools::Error("CMake can not determine linker language for target:",
  1016. tgt.GetName());
  1017. return;
  1018. }
  1019. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1020. runTimeFlagVar += linkLanguage;
  1021. runTimeFlagVar += "_FLAG";
  1022. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1023. runtimeFlag = m_Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1024. runtimeSep = m_Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1025. // concatenate all paths or no?
  1026. bool runtimeConcatenate = ( runtimeSep!="" );
  1027. if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
  1028. {
  1029. outputRuntime = false;
  1030. }
  1031. // Some search paths should never be emitted
  1032. emitted.insert("");
  1033. emitted.insert("/usr/lib");
  1034. std::string libPathFlag = m_Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1035. std::string libLinkFlag = m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1036. // collect all the flags needed for linking libraries
  1037. std::string linkLibs;
  1038. // Flags to link an executable to shared libraries.
  1039. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1040. linkFlagsVar += linkLanguage;
  1041. linkFlagsVar += "_FLAGS";
  1042. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1043. {
  1044. linkLibs = m_Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1045. linkLibs += " ";
  1046. }
  1047. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  1048. for(std::vector<std::string>::const_iterator libDir = libdirs.begin();
  1049. libDir != libdirs.end(); ++libDir)
  1050. {
  1051. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1052. if(emitted.insert(libpath).second)
  1053. {
  1054. std::string fullLibPath;
  1055. if(!m_WindowsShell && m_UseRelativePaths)
  1056. {
  1057. fullLibPath = "\"`cd ";
  1058. }
  1059. fullLibPath += libpath;
  1060. if(!m_WindowsShell && m_UseRelativePaths)
  1061. {
  1062. fullLibPath += ";pwd`\"";
  1063. }
  1064. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  1065. if((pos == std::string::npos || pos > 0)
  1066. && libDir->find("${") == std::string::npos)
  1067. {
  1068. linkLibs += libPathFlag;
  1069. if(outputRuntime)
  1070. {
  1071. runtimeDirs.push_back( fullLibPath );
  1072. }
  1073. }
  1074. linkLibs += fullLibPath;
  1075. linkLibs += " ";
  1076. }
  1077. }
  1078. std::string linkSuffix = m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1079. std::string regexp = ".*\\";
  1080. regexp += linkSuffix;
  1081. regexp += "$";
  1082. cmsys::RegularExpression hasSuffix(regexp.c_str());
  1083. std::string librariesLinked;
  1084. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  1085. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  1086. lib != libs.end(); ++lib)
  1087. {
  1088. // Don't link the library against itself!
  1089. if(targetLibrary && (lib->first == targetLibrary)) continue;
  1090. // use the correct lib for the current configuration
  1091. if (lib->second == cmTarget::DEBUG && buildType != "DEBUG")
  1092. {
  1093. continue;
  1094. }
  1095. if (lib->second == cmTarget::OPTIMIZED && buildType == "DEBUG")
  1096. {
  1097. continue;
  1098. }
  1099. // skip zero size library entries, this may happen
  1100. // if a variable expands to nothing.
  1101. if (lib->first.size() == 0) continue;
  1102. // if it is a full path break it into -L and -l
  1103. cmsys::RegularExpression reg("^([ \t]*\\-[lWRB])|([ \t]*\\-framework)|(\\${)|([ \t]*\\-pthread)|([ \t]*`)");
  1104. if(lib->first.find('/') != std::string::npos
  1105. && !reg.find(lib->first))
  1106. {
  1107. std::string dir, file;
  1108. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  1109. dir, file);
  1110. std::string libpath = this->ConvertToOutputForExisting(dir.c_str());
  1111. if(emitted.insert(libpath).second)
  1112. {
  1113. linkLibs += libPathFlag;
  1114. linkLibs += libpath;
  1115. linkLibs += " ";
  1116. if(outputRuntime)
  1117. {
  1118. runtimeDirs.push_back( libpath );
  1119. }
  1120. }
  1121. cmsys::RegularExpression libname("^lib([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  1122. cmsys::RegularExpression libname_noprefix("([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  1123. if(libname.find(file))
  1124. {
  1125. // Library had "lib" prefix.
  1126. librariesLinked += libLinkFlag;
  1127. file = libname.match(1);
  1128. // if ignore libprefix is on,
  1129. // then add the lib prefix back into the name
  1130. if(m_IgnoreLibPrefix)
  1131. {
  1132. file = "lib" + file;
  1133. }
  1134. librariesLinked += file;
  1135. if(linkSuffix.size() && !hasSuffix.find(file))
  1136. {
  1137. librariesLinked += linkSuffix;
  1138. }
  1139. librariesLinked += " ";
  1140. }
  1141. else if(libname_noprefix.find(file))
  1142. {
  1143. // Library had no "lib" prefix.
  1144. librariesLinked += libLinkFlag;
  1145. file = libname_noprefix.match(1);
  1146. librariesLinked += file;
  1147. if(linkSuffix.size() && !hasSuffix.find(file))
  1148. {
  1149. librariesLinked += linkSuffix;
  1150. }
  1151. librariesLinked += " ";
  1152. }
  1153. else
  1154. {
  1155. // Error parsing the library name. Just use the full path.
  1156. // The linker will give an error if it is invalid.
  1157. librariesLinked += lib->first;
  1158. librariesLinked += " ";
  1159. }
  1160. }
  1161. // not a full path, so add -l name
  1162. else
  1163. {
  1164. if(!reg.find(lib->first))
  1165. {
  1166. librariesLinked += libLinkFlag;
  1167. }
  1168. librariesLinked += lib->first;
  1169. if(linkSuffix.size() && !hasSuffix.find(lib->first))
  1170. {
  1171. librariesLinked += linkSuffix;
  1172. }
  1173. librariesLinked += " ";
  1174. }
  1175. }
  1176. linkLibs += librariesLinked;
  1177. fout << linkLibs;
  1178. if(outputRuntime && runtimeDirs.size()>0)
  1179. {
  1180. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1181. // a "-R a -R b -R c" type link line
  1182. fout << runtimeFlag;
  1183. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1184. fout << *itr;
  1185. ++itr;
  1186. for( ; itr != runtimeDirs.end(); ++itr )
  1187. {
  1188. if(runtimeConcatenate)
  1189. {
  1190. fout << runtimeSep << *itr;
  1191. }
  1192. else
  1193. {
  1194. fout << " " << runtimeFlag << *itr;
  1195. }
  1196. }
  1197. fout << " ";
  1198. }
  1199. if(m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  1200. {
  1201. fout << m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES") << " ";
  1202. }
  1203. }
  1204. //----------------------------------------------------------------------------
  1205. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1206. const char* lang)
  1207. {
  1208. // Add language-specific flags.
  1209. std::string flagsVar = "CMAKE_";
  1210. flagsVar += lang;
  1211. flagsVar += "_FLAGS";
  1212. this->AddConfigVariableFlags(flags, flagsVar.c_str());
  1213. }
  1214. //----------------------------------------------------------------------------
  1215. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1216. const char* lang,
  1217. bool shared)
  1218. {
  1219. std::string flagsVar;
  1220. // Add flags for dealing with shared libraries for this language.
  1221. if(shared)
  1222. {
  1223. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1224. flagsVar += lang;
  1225. flagsVar += "_FLAGS";
  1226. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1227. }
  1228. // Add flags specific to shared builds.
  1229. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1230. {
  1231. flagsVar = "CMAKE_SHARED_BUILD_";
  1232. flagsVar += lang;
  1233. flagsVar += "_FLAGS";
  1234. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1235. }
  1236. }
  1237. //----------------------------------------------------------------------------
  1238. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1239. const char* var)
  1240. {
  1241. // Add the flags from the variable itself.
  1242. std::string flagsVar = var;
  1243. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1244. // Add the flags from the build-type specific variable.
  1245. const char* buildType = m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1246. if(buildType && *buildType)
  1247. {
  1248. flagsVar += "_";
  1249. flagsVar += cmSystemTools::UpperCase(buildType);
  1250. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1251. }
  1252. }
  1253. //----------------------------------------------------------------------------
  1254. void cmLocalGenerator::AppendFlags(std::string& flags,
  1255. const char* newFlags)
  1256. {
  1257. if(newFlags && *newFlags)
  1258. {
  1259. if(flags.size())
  1260. {
  1261. flags += " ";
  1262. }
  1263. flags += newFlags;
  1264. }
  1265. }