cmLocalGenerator.cxx 44 KB

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