cmLocalGenerator.cxx 42 KB

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