cmLocalGenerator.cxx 43 KB

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