cmLocalGenerator.cxx 43 KB

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