cmLocalUnixMakefileGenerator.cxx 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621
  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 "cmGlobalGenerator.h"
  14. #include "cmLocalUnixMakefileGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmMakeDepend.h"
  19. #include "cmCacheManager.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include <stdio.h>
  22. cmLocalUnixMakefileGenerator::cmLocalUnixMakefileGenerator()
  23. {
  24. m_WindowsShell = false;
  25. m_IncludeDirective = "include";
  26. m_MakefileVariableSize = 0;
  27. }
  28. cmLocalUnixMakefileGenerator::~cmLocalUnixMakefileGenerator()
  29. {
  30. }
  31. void cmLocalUnixMakefileGenerator::Generate(bool fromTheTop)
  32. {
  33. // suppoirt override in output directories
  34. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  35. {
  36. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  37. if(m_LibraryOutputPath.size())
  38. {
  39. if(m_LibraryOutputPath[m_LibraryOutputPath.size() -1] != '/')
  40. {
  41. m_LibraryOutputPath += "/";
  42. }
  43. if(!cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str()))
  44. {
  45. cmSystemTools::Error("Error failed create "
  46. "LIBRARY_OUTPUT_PATH directory:",
  47. m_LibraryOutputPath.c_str());
  48. }
  49. m_Makefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
  50. }
  51. }
  52. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  53. {
  54. m_ExecutableOutputPath =
  55. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  56. if(m_ExecutableOutputPath.size())
  57. {
  58. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size() -1] != '/')
  59. {
  60. m_ExecutableOutputPath += "/";
  61. }
  62. if(!cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str()))
  63. {
  64. cmSystemTools::Error("Error failed to create "
  65. "EXECUTABLE_OUTPUT_PATH directory:",
  66. m_ExecutableOutputPath.c_str());
  67. }
  68. m_Makefile->AddLinkDirectory(m_ExecutableOutputPath.c_str());
  69. }
  70. }
  71. if (!fromTheTop)
  72. {
  73. // Generate depends
  74. cmMakeDepend md;
  75. md.SetMakefile(m_Makefile);
  76. md.GenerateMakefileDependencies();
  77. this->ProcessDepends(md);
  78. }
  79. // output the makefile fragment
  80. std::string dest = m_Makefile->GetStartOutputDirectory();
  81. dest += "/Makefile";
  82. this->OutputMakefile(dest.c_str(), !fromTheTop);
  83. }
  84. void
  85. cmLocalUnixMakefileGenerator::AddDependenciesToSourceFile(cmDependInformation const *info,
  86. cmSourceFile *i,
  87. std::set<cmDependInformation const*> *visited)
  88. {
  89. // add info to the visited set
  90. visited->insert(info);
  91. // add this dependency and the recurse
  92. // now recurse with info's dependencies
  93. for(cmDependInformation::DependencySet::const_iterator d =
  94. info->m_DependencySet.begin();
  95. d != info->m_DependencySet.end(); ++d)
  96. {
  97. if (visited->find(*d) == visited->end())
  98. {
  99. if((*d)->m_FullPath != "")
  100. {
  101. i->GetDepends().push_back((*d)->m_FullPath);
  102. }
  103. this->AddDependenciesToSourceFile(*d,i,visited);
  104. }
  105. }
  106. }
  107. void cmLocalUnixMakefileGenerator::ProcessDepends(const cmMakeDepend &md)
  108. {
  109. // Now create cmDependInformation objects for files in the directory
  110. cmTargets &tgts = m_Makefile->GetTargets();
  111. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  112. {
  113. std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  114. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  115. i != classes.end(); ++i)
  116. {
  117. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  118. {
  119. // get the depends
  120. const cmDependInformation *info =
  121. md.GetDependInformationForSourceFile(*(*i));
  122. // Delete any hints from the source file's dependencies.
  123. (*i)->GetDepends().erase((*i)->GetDepends().begin(), (*i)->GetDepends().end());
  124. // Now add the real dependencies for the file.
  125. if (info)
  126. {
  127. // create visited set
  128. std::set<cmDependInformation const*> visited;
  129. this->AddDependenciesToSourceFile(info,*i, &visited);
  130. }
  131. }
  132. }
  133. }
  134. }
  135. // This is where CMakeTargets.make is generated
  136. void cmLocalUnixMakefileGenerator::OutputMakefile(const char* file,
  137. bool withDepends)
  138. {
  139. // Create sub directories fro aux source directories
  140. std::vector<std::string>& auxSourceDirs =
  141. m_Makefile->GetAuxSourceDirectories();
  142. if( auxSourceDirs.size() )
  143. {
  144. // For the case when this is running as a remote build
  145. // on unix, make the directory
  146. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  147. i != auxSourceDirs.end(); ++i)
  148. {
  149. if(i->c_str()[0] != '/')
  150. {
  151. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  152. if(dir.size() && dir[dir.size()-1] != '/')
  153. {
  154. dir += "/";
  155. }
  156. dir += *i;
  157. cmSystemTools::MakeDirectory(dir.c_str());
  158. }
  159. else
  160. {
  161. cmSystemTools::MakeDirectory(i->c_str());
  162. }
  163. }
  164. }
  165. // Create a stream that writes to a temporary file
  166. // then does a copy at the end. This is to allow users
  167. // to hit control-c during the make of the makefile
  168. cmGeneratedFileStream tempFile(file);
  169. tempFile.SetAlwaysCopy(true);
  170. std::ostream& fout = tempFile.GetStream();
  171. if(!fout)
  172. {
  173. cmSystemTools::Error("Error can not open for write: ", file);
  174. return;
  175. }
  176. fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
  177. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  178. << " Generator, CMake Version "
  179. << cmMakefile::GetMajorVersion() << "."
  180. << cmMakefile::GetMinorVersion() << "\n"
  181. << "# Generated from the following files:\n# "
  182. << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  183. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  184. // sort the array
  185. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  186. // remove duplicates
  187. std::vector<std::string>::iterator new_end =
  188. std::unique(lfiles.begin(), lfiles.end());
  189. lfiles.erase(new_end, lfiles.end());
  190. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  191. i != lfiles.end(); ++i)
  192. {
  193. fout << "# " << i->c_str() << "\n";
  194. }
  195. fout << "\n\n";
  196. if(!m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  197. {
  198. fout << "# Suppresses display of executed commands\n";
  199. fout << ".SILENT:\n";
  200. }
  201. fout << "# disable some common implicit rules to speed things up\n";
  202. fout << ".SUFFIXES:\n";
  203. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  204. // create a make variable with all of the sources for this Makefile
  205. // for depend purposes.
  206. fout << "CMAKE_MAKEFILE_SOURCES = ";
  207. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  208. i != lfiles.end(); ++i)
  209. {
  210. fout << " " << cmSystemTools::ConvertToOutputPath(i->c_str());
  211. }
  212. // Add the cache to the list
  213. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  214. cacheFile += "/CMakeCache.txt";
  215. fout << " " << cmSystemTools::ConvertToOutputPath(cacheFile.c_str());
  216. fout << "\n\n\n";
  217. this->OutputMakeVariables(fout);
  218. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  219. checkCache += "/cmake.check_cache";
  220. checkCache = cmSystemTools::ConvertToOutputPath(checkCache.c_str());
  221. // Set up the default target as the VERY first target, so that make with no arguments will run it
  222. this->
  223. OutputMakeRule(fout,
  224. "Default target executed when no arguments are given to make, first make sure cmake.depends exists, cmake.check_depends is up-to-date, check the sources, then build the all target",
  225. "default_target",
  226. checkCache.c_str(),
  227. "$(MAKE) $(MAKESILENT) cmake.depends",
  228. "$(MAKE) $(MAKESILENT) cmake.check_depends",
  229. "$(MAKE) $(MAKESILENT) -f cmake.check_depends",
  230. "$(MAKE) $(MAKESILENT) all");
  231. this->OutputTargetRules(fout);
  232. this->OutputDependLibs(fout);
  233. this->OutputTargets(fout);
  234. this->OutputSubDirectoryRules(fout);
  235. std::string dependName = m_Makefile->GetStartOutputDirectory();
  236. dependName += "/cmake.depends";
  237. if(withDepends)
  238. {
  239. std::ofstream dependout(dependName.c_str());
  240. if(!dependout)
  241. {
  242. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  243. return;
  244. }
  245. dependout << "# .o dependencies in this directory." << std::endl;
  246. std::string checkDepend = m_Makefile->GetStartOutputDirectory();
  247. checkDepend += "/cmake.check_depends";
  248. std::ofstream checkdependout(checkDepend.c_str());
  249. if(!checkdependout)
  250. {
  251. cmSystemTools::Error("Error can not open for write: ", checkDepend.c_str());
  252. return;
  253. }
  254. checkdependout << "# This file is used as a tag file, that all sources depend on. If a source changes, then the rule to rebuild this file will cause cmake.depends to be rebuilt." << std::endl;
  255. // if there were any depends output, then output the check depends
  256. // information inot checkdependout
  257. if(this->OutputObjectDepends(dependout))
  258. {
  259. this->OutputCheckDepends(checkdependout);
  260. }
  261. else
  262. {
  263. checkdependout << "all:\n\t@echo cmake.depends is up-to-date\n";
  264. }
  265. }
  266. this->OutputCustomRules(fout);
  267. this->OutputMakeRules(fout);
  268. this->OutputInstallRules(fout);
  269. // only add the depend include if the depend file exists
  270. if(cmSystemTools::FileExists(dependName.c_str()))
  271. {
  272. fout << m_IncludeDirective << " cmake.depends\n";
  273. }
  274. }
  275. #if defined(_WIN32) && ! defined(__CYGWIN__)
  276. std::string cmLocalUnixMakefileGenerator::GetOutputExtension(const char* s)
  277. {
  278. std::string sourceExtension = s;
  279. if(sourceExtension == "def")
  280. {
  281. return "";
  282. }
  283. if(sourceExtension == "ico" || sourceExtension == "rc2")
  284. {
  285. return "";
  286. }
  287. if(sourceExtension == "rc")
  288. {
  289. return ".res";
  290. }
  291. return ".obj";
  292. }
  293. #else
  294. std::string cmLocalUnixMakefileGenerator::GetOutputExtension(const char*)
  295. {
  296. return ".o";
  297. }
  298. #endif
  299. // Output the rules for any targets
  300. void cmLocalUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  301. {
  302. // for each target add to the list of targets
  303. fout << "TARGETS = ";
  304. const cmTargets &tgts = m_Makefile->GetTargets();
  305. // list libraries first
  306. for(cmTargets::const_iterator l = tgts.begin();
  307. l != tgts.end(); l++)
  308. {
  309. if (l->second.IsInAll())
  310. {
  311. std::string path = m_LibraryOutputPath;
  312. if(l->second.GetType() == cmTarget::STATIC_LIBRARY)
  313. {
  314. path +=
  315. this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX") +
  316. l->first
  317. + this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  318. fout << " \\\n"
  319. << cmSystemTools::ConvertToOutputPath(path.c_str());
  320. }
  321. else if(l->second.GetType() == cmTarget::SHARED_LIBRARY)
  322. {
  323. path +=
  324. this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX") +
  325. l->first
  326. + this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  327. fout << " \\\n"
  328. << cmSystemTools::ConvertToOutputPath(path.c_str());
  329. }
  330. else if(l->second.GetType() == cmTarget::MODULE_LIBRARY)
  331. {
  332. path +=
  333. this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX") +
  334. l->first
  335. + this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  336. fout << " \\\n"
  337. << cmSystemTools::ConvertToOutputPath(path.c_str());
  338. }
  339. }
  340. }
  341. // executables
  342. for(cmTargets::const_iterator l = tgts.begin();
  343. l != tgts.end(); l++)
  344. {
  345. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  346. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  347. l->second.IsInAll())
  348. {
  349. std::string path = m_ExecutableOutputPath + l->first +
  350. cmSystemTools::GetExecutableExtension();
  351. fout << " \\\n" << cmSystemTools::ConvertToOutputPath(path.c_str());
  352. }
  353. }
  354. // list utilities last
  355. for(cmTargets::const_iterator l = tgts.begin();
  356. l != tgts.end(); l++)
  357. {
  358. if (l->second.GetType() == cmTarget::UTILITY &&
  359. l->second.IsInAll())
  360. {
  361. fout << " \\\n" << l->first.c_str();
  362. }
  363. }
  364. fout << "\n\n";
  365. // get the classes from the source lists then add them to the groups
  366. for(cmTargets::const_iterator l = tgts.begin();
  367. l != tgts.end(); l++)
  368. {
  369. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  370. if (classes.begin() != classes.end())
  371. {
  372. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS") << " = ";
  373. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  374. i != classes.end(); i++)
  375. {
  376. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  377. {
  378. std::string outExt(
  379. this->GetOutputExtension((*i)->GetSourceExtension().c_str()));
  380. if(outExt.size())
  381. {
  382. fout << "\\\n"
  383. << cmSystemTools::ConvertToOutputPath((*i)->GetSourceName().c_str())
  384. << outExt.c_str() << " ";
  385. }
  386. }
  387. }
  388. fout << "\n\n";
  389. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS_QUOTED") << " = ";
  390. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  391. i != classes.end(); i++)
  392. {
  393. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  394. {
  395. std::string outExt(this->GetOutputExtension((*i)->GetSourceExtension().c_str()));
  396. if(outExt.size())
  397. {
  398. fout << "\\\n\"" << cmSystemTools::ConvertToOutputPath((*i)->GetSourceName().c_str())
  399. << outExt.c_str() << "\" ";
  400. }
  401. }
  402. }
  403. fout << "\n\n";
  404. }
  405. }
  406. fout << "CLEAN_OBJECT_FILES = ";
  407. for(cmTargets::const_iterator l = tgts.begin();
  408. l != tgts.end(); l++)
  409. {
  410. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  411. if (classes.begin() != classes.end())
  412. {
  413. fout << "$(" << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS")
  414. << ") ";
  415. }
  416. }
  417. fout << "\n\n";
  418. const char * qt_files = m_Makefile->GetDefinition("GENERATED_QT_FILES");
  419. if (qt_files != NULL &&
  420. strlen(m_Makefile->GetDefinition("GENERATED_QT_FILES"))>0)
  421. {
  422. fout << "GENERATED_QT_FILES = ";
  423. fout << qt_files;
  424. fout << "\n\n";
  425. }
  426. }
  427. /**
  428. * Output the linking rules on a command line. For executables,
  429. * targetLibrary should be a NULL pointer. For libraries, it should point
  430. * to the name of the library. This will not link a library against itself.
  431. */
  432. void cmLocalUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  433. const char* targetLibrary,
  434. const cmTarget &tgt)
  435. {
  436. // Try to emit each search path once
  437. std::set<std::string> emitted;
  438. // Embed runtime search paths if possible and if required.
  439. bool outputRuntime = true;
  440. std::string runtimeFlag;
  441. std::string runtimeSep;
  442. std::vector<std::string> runtimeDirs;
  443. bool cxx = tgt.HasCxx();
  444. if(!cxx )
  445. {
  446. // if linking a c executable use the C runtime flag as cc
  447. // may not be the same program that creates shared libaries
  448. // and may have different flags
  449. runtimeFlag = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_FLAG");
  450. runtimeSep = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_FLAG_SEP");
  451. }
  452. else
  453. {
  454. runtimeFlag = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG");
  455. runtimeSep = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP");
  456. }
  457. // concatenate all paths or no?
  458. bool runtimeConcatenate = ( runtimeSep!="" );
  459. if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
  460. {
  461. outputRuntime = false;
  462. }
  463. // Some search paths should never be emitted
  464. emitted.insert("");
  465. emitted.insert("/usr/lib");
  466. std::string libPathFlag = m_Makefile->GetDefinition("CMAKE_LIBRARY_PATH_FLAG");
  467. std::string libLinkFlag = this->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  468. // collect all the flags needed for linking libraries
  469. std::string linkLibs;
  470. // Flags to link an executable to shared libraries.
  471. if( tgt.GetType() == cmTarget::EXECUTABLE)
  472. {
  473. if(cxx)
  474. {
  475. linkLibs = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS");
  476. }
  477. else
  478. {
  479. linkLibs = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_LINK_FLAGS");
  480. }
  481. linkLibs += " ";
  482. }
  483. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  484. for(std::vector<std::string>::const_iterator libDir = libdirs.begin();
  485. libDir != libdirs.end(); ++libDir)
  486. {
  487. std::string libpath = cmSystemTools::ConvertToOutputPath(libDir->c_str());
  488. if(emitted.insert(libpath).second)
  489. {
  490. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  491. if((pos == std::string::npos || pos > 0)
  492. && libDir->find("${") == std::string::npos)
  493. {
  494. linkLibs += libPathFlag;
  495. if(outputRuntime)
  496. {
  497. runtimeDirs.push_back( libpath );
  498. }
  499. }
  500. linkLibs += libpath;
  501. linkLibs += " ";
  502. }
  503. }
  504. std::string linkSuffix = this->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  505. std::string regexp = ".*\\";
  506. regexp += linkSuffix;
  507. regexp += "$";
  508. cmRegularExpression hasSuffix(regexp.c_str());
  509. std::string librariesLinked;
  510. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  511. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  512. lib != libs.end(); ++lib)
  513. {
  514. // Don't link the library against itself!
  515. if(targetLibrary && (lib->first == targetLibrary)) continue;
  516. // don't look at debug libraries
  517. if (lib->second == cmTarget::DEBUG) continue;
  518. // skip zero size library entries, this may happen
  519. // if a variable expands to nothing.
  520. if (lib->first.size() == 0) continue;
  521. // if it is a full path break it into -L and -l
  522. cmRegularExpression reg("([ \t]*\\-l)|([ \t]*\\-framework)|(\\${)");
  523. if(lib->first.find('/') != std::string::npos
  524. && !reg.find(lib->first))
  525. {
  526. std::string dir, file;
  527. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  528. dir, file);
  529. std::string libpath = this->ConvertToOutputForExisting(dir.c_str());
  530. if(emitted.insert(libpath).second)
  531. {
  532. linkLibs += libPathFlag;
  533. linkLibs += libpath;
  534. linkLibs += " ";
  535. if(outputRuntime)
  536. {
  537. runtimeDirs.push_back( libpath );
  538. }
  539. }
  540. cmRegularExpression libname("lib([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  541. cmRegularExpression libname_noprefix("([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  542. if(libname.find(file))
  543. {
  544. // Library had "lib" prefix.
  545. librariesLinked += libLinkFlag;
  546. file = libname.match(1);
  547. librariesLinked += file;
  548. if(linkSuffix.size() && !hasSuffix.find(file))
  549. {
  550. librariesLinked += linkSuffix;
  551. }
  552. librariesLinked += " ";
  553. }
  554. else if(libname_noprefix.find(file))
  555. {
  556. // Library had no "lib" prefix.
  557. librariesLinked += libLinkFlag;
  558. file = libname_noprefix.match(1);
  559. librariesLinked += file;
  560. if(linkSuffix.size() && !hasSuffix.find(file))
  561. {
  562. librariesLinked += linkSuffix;
  563. }
  564. librariesLinked += " ";
  565. }
  566. else
  567. {
  568. // Error parsing the library name. Just use the full path.
  569. // The linker will give an error if it is invalid.
  570. librariesLinked += lib->first;
  571. librariesLinked += " ";
  572. }
  573. }
  574. // not a full path, so add -l name
  575. else
  576. {
  577. if(!reg.find(lib->first))
  578. {
  579. librariesLinked += libLinkFlag;
  580. }
  581. librariesLinked += lib->first;
  582. if(linkSuffix.size() && !hasSuffix.find(lib->first))
  583. {
  584. librariesLinked += linkSuffix;
  585. }
  586. librariesLinked += " ";
  587. }
  588. }
  589. linkLibs += librariesLinked;
  590. fout << linkLibs;
  591. if(outputRuntime && runtimeDirs.size()>0)
  592. {
  593. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  594. // a "-R a -R b -R c" type link line
  595. fout << runtimeFlag;
  596. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  597. fout << *itr;
  598. ++itr;
  599. for( ; itr != runtimeDirs.end(); ++itr )
  600. {
  601. if(runtimeConcatenate)
  602. {
  603. fout << runtimeSep << *itr;
  604. }
  605. else
  606. {
  607. fout << " " << runtimeFlag << *itr;
  608. }
  609. }
  610. fout << " ";
  611. }
  612. if(m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  613. {
  614. fout << m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES") << " ";
  615. }
  616. }
  617. std::string cmLocalUnixMakefileGenerator::CreateTargetRules(const cmTarget &target,
  618. const char* targetName)
  619. {
  620. std::string customRuleCode = "";
  621. bool initNext = false;
  622. for (std::vector<cmCustomCommand>::const_iterator cr =
  623. target.GetCustomCommands().begin();
  624. cr != target.GetCustomCommands().end(); ++cr)
  625. {
  626. cmCustomCommand cc(*cr);
  627. cc.ExpandVariables(*m_Makefile);
  628. if (cc.GetSourceName() == targetName)
  629. {
  630. if(initNext)
  631. {
  632. customRuleCode += "\n\t";
  633. }
  634. else
  635. {
  636. initNext = true;
  637. }
  638. std::string command = cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str());
  639. customRuleCode += command + " " + cc.GetArguments();
  640. }
  641. }
  642. return customRuleCode;
  643. }
  644. struct RuleVariables
  645. {
  646. const char* replace;
  647. const char* lookup;
  648. };
  649. static RuleVariables ruleReplaceVars[] =
  650. {
  651. {"<CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS>", "CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS"},
  652. {"<CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS>", "CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS"},
  653. {"<CMAKE_SHARED_MODULE_C_FLAGS>", "CMAKE_SHARED_MODULE_C_FLAGS"},
  654. {"<CMAKE_SHARED_MODULE_CXX_FLAGS>", "CMAKE_SHARED_MODULE_CXX_FLAGS"},
  655. {"<CMAKE_SHARED_LIBRARY_C_FLAGS>", "CMAKE_SHARED_LIBRARY_C_FLAGS"},
  656. {"<CMAKE_SHARED_LIBRARY_CXX_FLAGS>", "CMAKE_SHARED_LIBRARY_CXX_FLAGS"},
  657. {"<CMAKE_CXX_LINK_FLAGS>", "CMAKE_CXX_LINK_FLAGS"},
  658. {"<CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS>", "CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS"},
  659. {"<CMAKE_SHARED_MODULE_CREATE_C_FLAGS>", "CMAKE_SHARED_MODULE_CREATE_C_FLAGS"},
  660. {"<CMAKE_C_LINK_FLAGS>", "CMAKE_C_LINK_FLAGS"},
  661. {"<CMAKE_AR>", "CMAKE_AR"},
  662. {"<CMAKE_RANLIB>", "CMAKE_RANLIB"},
  663. {0, 0}
  664. };
  665. void
  666. cmLocalUnixMakefileGenerator::ExpandRuleVariables(std::string& s,
  667. const char* objects,
  668. const char* target,
  669. const char* linkLibs,
  670. const char* source,
  671. const char* object,
  672. const char* flags,
  673. const char* objectsquoted,
  674. const char* targetBase,
  675. const char* linkFlags)
  676. {
  677. std::string cxxcompiler = this->ConvertToOutputForExisting(
  678. this->GetSafeDefinition("CMAKE_CXX_COMPILER"));
  679. std::string ccompiler = this->ConvertToOutputForExisting(
  680. this->GetSafeDefinition("CMAKE_C_COMPILER"));
  681. cmSystemTools::ReplaceString(s, "<CMAKE_CXX_COMPILER>", cxxcompiler.c_str());
  682. cmSystemTools::ReplaceString(s, "<CMAKE_C_COMPILER>", ccompiler.c_str());
  683. if(linkFlags)
  684. {
  685. cmSystemTools::ReplaceString(s, "<LINK_FLAGS>", linkFlags);
  686. }
  687. if(flags)
  688. {
  689. cmSystemTools::ReplaceString(s, "<FLAGS>", flags);
  690. }
  691. if(source)
  692. {
  693. cmSystemTools::ReplaceString(s, "<SOURCE>", source);
  694. }
  695. if(object)
  696. {
  697. cmSystemTools::ReplaceString(s, "<OBJECT>", object);
  698. }
  699. if(objects)
  700. {
  701. cmSystemTools::ReplaceString(s, "<OBJECTS>", objects);
  702. }
  703. if(objectsquoted)
  704. {
  705. cmSystemTools::ReplaceString(s, "<OBJECTS_QUOTED>", objectsquoted);
  706. }
  707. if(target)
  708. {
  709. cmSystemTools::ReplaceString(s, "<TARGET>", target);
  710. }
  711. if(targetBase)
  712. {
  713. cmSystemTools::ReplaceString(s, "<TARGET_BASE>", targetBase);
  714. }
  715. if(linkLibs)
  716. {
  717. cmSystemTools::ReplaceString(s, "<LINK_LIBRARIES>", linkLibs);
  718. }
  719. RuleVariables* rv = ruleReplaceVars;
  720. while(rv->replace)
  721. {
  722. cmSystemTools::ReplaceString(s, rv->replace,
  723. this->GetSafeDefinition(rv->lookup));
  724. rv++;
  725. }
  726. }
  727. void cmLocalUnixMakefileGenerator::OutputLibraryRule(std::ostream& fout,
  728. const char* name,
  729. const cmTarget &t,
  730. const char* prefix,
  731. const char* suffix,
  732. const char* createVariable,
  733. const char* comment,
  734. const char* linkFlags
  735. )
  736. {
  737. // create the library name
  738. std::string targetNameBase = prefix;
  739. targetNameBase += name;
  740. std::string targetName = prefix;
  741. targetName += name;
  742. targetName += suffix;
  743. // create the target full path name
  744. std::string targetFullPath = m_LibraryOutputPath + targetName;
  745. std::string targetBaseFullPath = m_LibraryOutputPath + targetNameBase;
  746. targetBaseFullPath =
  747. cmSystemTools::ConvertToOutputPath(targetBaseFullPath.c_str());
  748. targetFullPath = cmSystemTools::ConvertToOutputPath(targetFullPath.c_str());
  749. // get the objects that are used to link this library
  750. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  751. std::string objsQuoted = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED") + ") ";
  752. // create a variable with the objects that this library depends on
  753. std::string depend = objs + " $("
  754. + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  755. // collect up the build rules
  756. std::vector<std::string> rules;
  757. std::string command = "$(RM) " + targetFullPath;
  758. rules.push_back(command);
  759. rules.push_back(m_Makefile->GetDefinition(createVariable));
  760. // expand multi-command semi-colon separated lists
  761. // of commands into separate commands
  762. std::vector<std::string> commands;
  763. cmSystemTools::ExpandList(rules, commands);
  764. // collect custom commands for this target and add them to the list
  765. std::string customCommands = this->CreateTargetRules(t, name);
  766. if(customCommands.size() > 0)
  767. {
  768. commands.push_back(customCommands);
  769. }
  770. // collect up the link libraries
  771. cmOStringStream linklibs;
  772. this->OutputLinkLibraries(linklibs, name, t);
  773. for(std::vector<std::string>::iterator i = commands.begin();
  774. i != commands.end(); ++i)
  775. {
  776. this->ExpandRuleVariables(*i,
  777. objs.c_str(),
  778. targetFullPath.c_str(),
  779. linklibs.str().c_str(),
  780. 0, 0, 0, objsQuoted.c_str(),
  781. targetBaseFullPath.c_str(),
  782. linkFlags);
  783. }
  784. this->OutputMakeRule(fout, comment,
  785. targetFullPath.c_str(),
  786. depend.c_str(),
  787. commands);
  788. }
  789. void cmLocalUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  790. const char* name,
  791. const cmTarget &t)
  792. {
  793. const char* createRule;
  794. if(t.HasCxx())
  795. {
  796. createRule = "CMAKE_CXX_CREATE_SHARED_LIBRARY";
  797. }
  798. else
  799. {
  800. createRule = "CMAKE_C_CREATE_SHARED_LIBRARY";
  801. }
  802. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  803. buildType = cmSystemTools::UpperCase(buildType);
  804. std::string linkFlags = this->GetSafeDefinition("CMAKE_SHARED_LINKER_FLAGS");
  805. linkFlags += " ";
  806. if(buildType.size())
  807. {
  808. std::string build = "CMAKE_SHARED_LINKER_FLAGS_";
  809. build += buildType;
  810. linkFlags += this->GetSafeDefinition(build.c_str());
  811. linkFlags += " ";
  812. }
  813. #ifdef _WIN32
  814. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  815. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  816. i != sources.end(); ++i)
  817. {
  818. if((*i)->GetSourceExtension() == "def")
  819. {
  820. linkFlags += this->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  821. linkFlags += (*i)->GetFullPath();
  822. linkFlags += " ";
  823. }
  824. }
  825. #endif
  826. this->OutputLibraryRule(fout, name, t,
  827. this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX"),
  828. this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
  829. createRule,
  830. "shared library",
  831. linkFlags.c_str());
  832. }
  833. void cmLocalUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  834. const char* name,
  835. const cmTarget &t)
  836. {
  837. const char* createRule;
  838. if(t.HasCxx())
  839. {
  840. createRule = "CMAKE_CXX_CREATE_SHARED_MODULE";
  841. }
  842. else
  843. {
  844. createRule = "CMAKE_C_CREATE_SHARED_MODULE";
  845. }
  846. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  847. buildType = cmSystemTools::UpperCase(buildType);
  848. std::string linkFlags = this->GetSafeDefinition("CMAKE_MODULE_LINKER_FLAGS");
  849. linkFlags += " ";
  850. if(buildType.size())
  851. {
  852. std::string build = "CMAKE_MODULE_LINKER_FLAGS_";
  853. build += buildType;
  854. linkFlags += this->GetSafeDefinition(build.c_str());
  855. linkFlags += " ";
  856. }
  857. this->OutputLibraryRule(fout, name, t,
  858. this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX"),
  859. this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX"),
  860. createRule,
  861. "shared module",
  862. linkFlags.c_str());
  863. }
  864. void cmLocalUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  865. const char* name,
  866. const cmTarget &t)
  867. {
  868. const char* createRule;
  869. if(t.HasCxx())
  870. {
  871. createRule = "CMAKE_CXX_CREATE_STATIC_LIBRARY";
  872. }
  873. else
  874. {
  875. createRule = "CMAKE_C_CREATE_STATIC_LIBRARY";
  876. }
  877. this->OutputLibraryRule(fout, name, t,
  878. this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX"),
  879. this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
  880. createRule,
  881. "static library", 0);
  882. }
  883. void cmLocalUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  884. const char* name,
  885. const cmTarget &t)
  886. {
  887. std::string linkFlags;
  888. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  889. buildType = cmSystemTools::UpperCase(buildType);
  890. std::string flags;
  891. std::string target = m_ExecutableOutputPath + name
  892. + cmSystemTools::GetExecutableExtension();
  893. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  894. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  895. std::string depend = "$(";
  896. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  897. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  898. std::vector<std::string> rules;
  899. linkFlags += this->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  900. linkFlags += " ";
  901. if(buildType.size())
  902. {
  903. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  904. build += buildType;
  905. linkFlags += this->GetSafeDefinition(build.c_str());
  906. linkFlags += " ";
  907. }
  908. if(t.HasCxx())
  909. {
  910. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_LINK_EXECUTABLE"));
  911. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  912. flags += " ";
  913. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  914. flags += " ";
  915. }
  916. else
  917. {
  918. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_LINK_FLAGS");
  919. flags += " ";
  920. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_LINK_EXECUTABLE"));
  921. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  922. flags += " ";
  923. }
  924. cmOStringStream linklibs;
  925. this->OutputLinkLibraries(linklibs, 0, t);
  926. std::string comment = "executable";
  927. std::vector<std::string> commands;
  928. cmSystemTools::ExpandList(rules, commands);
  929. std::string customCommands = this->CreateTargetRules(t, name);
  930. if(customCommands.size() > 0)
  931. {
  932. commands.push_back(customCommands.c_str());
  933. }
  934. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  935. {
  936. linkFlags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  937. linkFlags += " ";
  938. }
  939. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  940. {
  941. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  942. linkFlags += " ";
  943. }
  944. else
  945. {
  946. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  947. linkFlags += " ";
  948. }
  949. for(std::vector<std::string>::iterator i = commands.begin();
  950. i != commands.end(); ++i)
  951. {
  952. this->ExpandRuleVariables(*i,
  953. objs.c_str(),
  954. target.c_str(),
  955. linklibs.str().c_str(),
  956. 0,
  957. 0,
  958. flags.c_str(),
  959. 0,
  960. 0,
  961. linkFlags.c_str());
  962. }
  963. this->OutputMakeRule(fout,
  964. comment.c_str(),
  965. target.c_str(),
  966. depend.c_str(),
  967. commands);
  968. }
  969. void cmLocalUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  970. const char* name,
  971. const cmTarget &t)
  972. {
  973. std::string customCommands = this->CreateTargetRules(t, name);
  974. const char* cc = 0;
  975. if(customCommands.size() > 0)
  976. {
  977. cc = customCommands.c_str();
  978. }
  979. std::string comment = "Utility";
  980. std::string depends;
  981. std::string replaceVars;
  982. const std::vector<cmCustomCommand> &ccs = t.GetCustomCommands();
  983. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  984. i != ccs.end(); ++i)
  985. {
  986. const std::vector<std::string> & dep = i->GetDepends();
  987. for(std::vector<std::string>::const_iterator d = dep.begin();
  988. d != dep.end(); ++d)
  989. {
  990. depends += " \\\n";
  991. replaceVars = *d;
  992. m_Makefile->ExpandVariablesInString(replaceVars);
  993. depends += cmSystemTools::ConvertToOutputPath(replaceVars.c_str());
  994. }
  995. }
  996. this->OutputMakeRule(fout, comment.c_str(), name,
  997. depends.c_str(), cc);
  998. }
  999. void cmLocalUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  1000. {
  1001. // for each target
  1002. const cmTargets &tgts = m_Makefile->GetTargets();
  1003. for(cmTargets::const_iterator l = tgts.begin();
  1004. l != tgts.end(); l++)
  1005. {
  1006. switch(l->second.GetType())
  1007. {
  1008. case cmTarget::STATIC_LIBRARY:
  1009. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  1010. break;
  1011. case cmTarget::SHARED_LIBRARY:
  1012. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  1013. break;
  1014. case cmTarget::MODULE_LIBRARY:
  1015. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  1016. break;
  1017. case cmTarget::EXECUTABLE:
  1018. case cmTarget::WIN32_EXECUTABLE:
  1019. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  1020. break;
  1021. case cmTarget::UTILITY:
  1022. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  1023. break;
  1024. // This is handled by the OutputCustomRules method
  1025. case cmTarget::INSTALL_FILES:
  1026. // This is handled by the OutputInstallRules method
  1027. case cmTarget::INSTALL_PROGRAMS:
  1028. // This is handled by the OutputInstallRules method
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. // For each target that is an executable or shared library, generate
  1034. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  1035. void cmLocalUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  1036. {
  1037. // Build a set of libraries that will be linked into any target in
  1038. // this directory.
  1039. std::set<std::string> used;
  1040. // for each target
  1041. const cmTargets &tgts = m_Makefile->GetTargets();
  1042. for(cmTargets::const_iterator l = tgts.begin();
  1043. l != tgts.end(); l++)
  1044. {
  1045. // Each dependency should only be emitted once per target.
  1046. std::set<std::string> emitted;
  1047. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  1048. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  1049. || (l->second.GetType() == cmTarget::STATIC_LIBRARY)
  1050. || (l->second.GetType() == cmTarget::EXECUTABLE)
  1051. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  1052. {
  1053. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  1054. // A library should not depend on itself!
  1055. emitted.insert(l->first);
  1056. // for static libraries do not depend on other libraries
  1057. if(l->second.GetType() != cmTarget::STATIC_LIBRARY)
  1058. {
  1059. // Now, look at all link libraries specific to this target.
  1060. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  1061. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1062. lib != tlibs.end(); ++lib)
  1063. {
  1064. // Record that this library was used.
  1065. used.insert(lib->first);
  1066. // Don't emit the same library twice for this target.
  1067. if(emitted.insert(lib->first).second)
  1068. {
  1069. // Output this dependency.
  1070. this->OutputLibDepend(fout, lib->first.c_str());
  1071. }
  1072. }
  1073. }
  1074. // for all targets depend on utilities
  1075. // Now, look at all utilities specific to this target.
  1076. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  1077. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1078. util != tutils.end(); ++util)
  1079. {
  1080. // Record that this utility was used.
  1081. used.insert(*util);
  1082. // Don't emit the same utility twice for this target.
  1083. if(emitted.insert(*util).second)
  1084. {
  1085. // Output this dependency.
  1086. this->OutputExeDepend(fout, util->c_str());
  1087. }
  1088. }
  1089. fout << "\n";
  1090. }
  1091. }
  1092. fout << "\n";
  1093. // Loop over the libraries used and make sure there is a rule to
  1094. // build them in this makefile. If the library is in another
  1095. // directory, add a rule to jump to that directory and make sure it
  1096. // exists.
  1097. for(std::set<std::string>::const_iterator lib = used.begin();
  1098. lib != used.end(); ++lib)
  1099. {
  1100. // loop over the list of directories that the libraries might
  1101. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  1102. // be stored in the cache
  1103. std::string libPath = *lib + "_CMAKE_PATH";
  1104. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1105. // if cache and not the current directory add a rule, to
  1106. // jump into the directory and build for the first time
  1107. if(cacheValue &&
  1108. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  1109. {
  1110. // add the correct extension
  1111. std::string ltname = *lib+"_LIBRARY_TYPE";
  1112. const char* libType
  1113. = m_Makefile->GetDefinition(ltname.c_str());
  1114. // if it was a library..
  1115. if (libType)
  1116. {
  1117. std::string library;
  1118. std::string libpath = cacheValue;
  1119. if(libType && std::string(libType) == "SHARED")
  1120. {
  1121. library = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1122. library += *lib;
  1123. library += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1124. }
  1125. else if(libType && std::string(libType) == "MODULE")
  1126. {
  1127. library = this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1128. library += *lib;
  1129. library += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1130. }
  1131. else if(libType && std::string(libType) == "STATIC")
  1132. {
  1133. library = this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1134. library += *lib;
  1135. library += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1136. }
  1137. else
  1138. {
  1139. cmSystemTools::Error("Unknown library type!");
  1140. return;
  1141. }
  1142. if(m_LibraryOutputPath.size())
  1143. {
  1144. libpath = m_LibraryOutputPath;
  1145. }
  1146. else
  1147. {
  1148. libpath += "/";
  1149. }
  1150. libpath += library;
  1151. // put out a rule to build the library if it does not exist
  1152. this->OutputBuildTargetInDir(fout,
  1153. cacheValue,
  1154. library.c_str(),
  1155. libpath.c_str(),
  1156. m_Makefile->
  1157. GetDefinition("LIBRARY_OUTPUT_PATH")
  1158. );
  1159. }
  1160. // something other than a library...
  1161. else
  1162. {
  1163. std::string exepath = cacheValue;
  1164. if(m_ExecutableOutputPath.size())
  1165. {
  1166. exepath = m_ExecutableOutputPath;
  1167. }
  1168. else
  1169. {
  1170. exepath += "/";
  1171. }
  1172. std::string fullName = lib->c_str();
  1173. fullName += cmSystemTools::GetExecutableExtension();
  1174. exepath += fullName;
  1175. this->OutputBuildTargetInDir(fout,
  1176. cacheValue,
  1177. fullName.c_str(),
  1178. exepath.c_str(),
  1179. m_Makefile->
  1180. GetDefinition("EXECUTABLE_OUTPUT_PATH")
  1181. );
  1182. }
  1183. }
  1184. }
  1185. }
  1186. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDirWindows(std::ostream& fout,
  1187. const char* path,
  1188. const char* library,
  1189. const char* fullpath,
  1190. const char* libOutPath)
  1191. {
  1192. const char* makeTarget = library;
  1193. std::string currentDir =
  1194. cmSystemTools::ConvertToOutputPath(m_Makefile->GetCurrentOutputDirectory());
  1195. std::string wpath = cmSystemTools::ConvertToOutputPath(path);
  1196. std::string wfullpath = cmSystemTools::ConvertToOutputPath(fullpath);
  1197. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  1198. {
  1199. makeTarget = wfullpath.c_str();
  1200. }
  1201. fout << wfullpath
  1202. << ":\n\tcd " << wpath << "\n"
  1203. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  1204. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  1205. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  1206. << "\t$(MAKE) $(MAKESILENT) " << makeTarget
  1207. << "\n\tcd " << currentDir << "\n";
  1208. }
  1209. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  1210. const char* path,
  1211. const char* library,
  1212. const char* fullpath,
  1213. const char* outputPath)
  1214. {
  1215. if(m_WindowsShell)
  1216. {
  1217. this->OutputBuildTargetInDirWindows(fout, path, library, fullpath, outputPath);
  1218. return;
  1219. }
  1220. const char* makeTarget = library;
  1221. if(outputPath && strcmp( outputPath, "" ) != 0)
  1222. {
  1223. makeTarget = fullpath;
  1224. }
  1225. fout << cmSystemTools::ConvertToOutputPath(fullpath)
  1226. << ":\n\tcd " << cmSystemTools::ConvertToOutputPath(path)
  1227. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  1228. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  1229. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  1230. << "; $(MAKE) $(MAKESILENT) "
  1231. << cmSystemTools::ConvertToOutputPath(makeTarget) << "\n\n";
  1232. }
  1233. bool cmLocalUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  1234. {
  1235. if (strcmp(path1, path2) == 0)
  1236. {
  1237. return true;
  1238. }
  1239. #if defined(_WIN32) || defined(__APPLE__)
  1240. return
  1241. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  1242. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2));
  1243. #else
  1244. return false;
  1245. #endif
  1246. }
  1247. void cmLocalUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  1248. const char* name)
  1249. {
  1250. std::string libPath = name;
  1251. libPath += "_CMAKE_PATH";
  1252. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1253. if(cacheValue )
  1254. {
  1255. // if there is a cache value, then this is a library that cmake
  1256. // knows how to build, so we can depend on it
  1257. std::string libpath;
  1258. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1259. {
  1260. // if the library is not in the current directory, then get the full
  1261. // path to it
  1262. if(m_LibraryOutputPath.size())
  1263. {
  1264. libpath = m_LibraryOutputPath;
  1265. }
  1266. else
  1267. {
  1268. libpath = cacheValue;
  1269. libpath += "/";
  1270. }
  1271. }
  1272. else
  1273. {
  1274. // library is in current Makefile so use lib as a prefix
  1275. libpath = m_LibraryOutputPath;
  1276. }
  1277. // add the correct extension
  1278. std::string ltname = name;
  1279. ltname += "_LIBRARY_TYPE";
  1280. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1281. if(libType && std::string(libType) == "SHARED")
  1282. {
  1283. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1284. libpath += name;
  1285. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1286. }
  1287. else if (libType && std::string(libType) == "MODULE")
  1288. {
  1289. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1290. libpath += name;
  1291. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1292. }
  1293. else if (libType && std::string(libType) == "STATIC")
  1294. {
  1295. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1296. libpath += name;
  1297. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1298. }
  1299. fout << cmSystemTools::ConvertToOutputPath(libpath.c_str()) << " ";
  1300. }
  1301. }
  1302. void cmLocalUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1303. const char* name)
  1304. {
  1305. std::string exePath = name;
  1306. exePath += "_CMAKE_PATH";
  1307. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1308. if(cacheValue )
  1309. {
  1310. // if there is a cache value, then this is a executable/utility that cmake
  1311. // knows how to build, so we can depend on it
  1312. std::string exepath;
  1313. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1314. {
  1315. // if the exe/utility is not in the current directory, then get the full
  1316. // path to it
  1317. if(m_ExecutableOutputPath.size())
  1318. {
  1319. exepath = m_ExecutableOutputPath;
  1320. }
  1321. else
  1322. {
  1323. exepath = cacheValue;
  1324. exepath += "/";
  1325. }
  1326. }
  1327. else
  1328. {
  1329. // library is in current Makefile
  1330. exepath = m_ExecutableOutputPath;
  1331. }
  1332. // add the library name
  1333. exepath += name;
  1334. // add the correct extension
  1335. exepath += cmSystemTools::GetExecutableExtension();
  1336. fout << cmSystemTools::ConvertToOutputPath(exepath.c_str()) << " ";
  1337. }
  1338. }
  1339. // fix up names of directories so they can be used
  1340. // as targets in makefiles.
  1341. inline std::string FixDirectoryName(const char* dir)
  1342. {
  1343. std::string s = dir;
  1344. // replace ../ with 3 under bars
  1345. size_t pos = s.find("../");
  1346. if(pos != std::string::npos)
  1347. {
  1348. s.replace(pos, 3, "___");
  1349. }
  1350. // replace / directory separators with a single under bar
  1351. pos = s.find("/");
  1352. while(pos != std::string::npos)
  1353. {
  1354. s.replace(pos, 1, "_");
  1355. pos = s.find("/");
  1356. }
  1357. return s;
  1358. }
  1359. void cmLocalUnixMakefileGenerator::BuildInSubDirectoryWindows(std::ostream& fout,
  1360. const char* directory,
  1361. const char* target1,
  1362. const char* target2,
  1363. bool silent)
  1364. {
  1365. if(target1)
  1366. {
  1367. std::string dir = cmSystemTools::ConvertToOutputPath(directory);
  1368. if(dir[0] == '\"')
  1369. {
  1370. dir = dir.substr(1, dir.size()-2);
  1371. }
  1372. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  1373. << " "
  1374. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  1375. if (!silent)
  1376. {
  1377. fout << "\techo " << directory << ": building " << target1 << "\n";
  1378. }
  1379. fout << "\tcd " << dir << "\n"
  1380. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  1381. }
  1382. if(target2)
  1383. {
  1384. if (!silent)
  1385. {
  1386. fout << "\techo " << directory << ": building " << target2 << "\n";
  1387. }
  1388. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  1389. }
  1390. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  1391. fout << "\tcd " << cmSystemTools::ConvertToOutputPath(currentDir.c_str()) << "\n\n";
  1392. }
  1393. void cmLocalUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1394. const char* dir,
  1395. const char* target1,
  1396. const char* target2,
  1397. bool silent)
  1398. {
  1399. if(m_WindowsShell)
  1400. {
  1401. this->BuildInSubDirectoryWindows(fout, dir, target1, target2, silent);
  1402. return;
  1403. }
  1404. std::string directory = cmSystemTools::ConvertToOutputPath(dir);
  1405. if(target1)
  1406. {
  1407. fout << "\t@if test ! -d " << directory
  1408. << "; then $(MAKE) rebuild_cache; fi\n";
  1409. if (!silent)
  1410. {
  1411. fout << "\techo " << directory << ": building " << target1 << "\n";
  1412. }
  1413. fout << "\t@cd " << directory
  1414. << "; $(MAKE) " << target1 << "\n";
  1415. }
  1416. if(target2)
  1417. {
  1418. if (!silent)
  1419. {
  1420. fout << "\techo " << directory << ": building " << target2 << "\n";
  1421. }
  1422. fout << "\t@cd " << directory
  1423. << "; $(MAKE) " << target2 << "\n";
  1424. }
  1425. fout << "\n";
  1426. }
  1427. void
  1428. cmLocalUnixMakefileGenerator::
  1429. OutputSubDirectoryVars(std::ostream& fout,
  1430. const char* var,
  1431. const char* target,
  1432. const char* target1,
  1433. const char* target2,
  1434. const char* depend,
  1435. const std::vector<std::string>& SubDirectories,
  1436. bool silent)
  1437. {
  1438. if(!depend)
  1439. {
  1440. depend = "";
  1441. }
  1442. if( SubDirectories.size() == 0)
  1443. {
  1444. return;
  1445. }
  1446. fout << "# Variable for making " << target << " in subdirectories.\n";
  1447. fout << var << " = \\\n";
  1448. unsigned int ii;
  1449. for(ii =0; ii < SubDirectories.size(); ii++)
  1450. {
  1451. std::string subdir = FixDirectoryName(SubDirectories[ii].c_str());
  1452. fout << target << "_" << subdir.c_str();
  1453. if(ii == SubDirectories.size()-1)
  1454. {
  1455. fout << " \n\n";
  1456. }
  1457. else
  1458. {
  1459. fout << " \\\n";
  1460. }
  1461. }
  1462. fout << "# Targets for making " << target << " in subdirectories.\n";
  1463. std::string last = "";
  1464. for(unsigned int cc =0; cc < SubDirectories.size(); cc++)
  1465. {
  1466. std::string subdir = FixDirectoryName(SubDirectories[cc].c_str());
  1467. fout << target << "_" << subdir.c_str() << ": " << depend;
  1468. // Make each subdirectory depend on previous one. This forces
  1469. // parallel builds (make -j 2) to build in same order as a single
  1470. // threaded build to avoid dependency problems.
  1471. if(cc > 0)
  1472. {
  1473. fout << " " << target << "_" << last.c_str();
  1474. }
  1475. fout << "\n";
  1476. last = subdir;
  1477. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1478. dir += "/";
  1479. dir += SubDirectories[cc];
  1480. this->BuildInSubDirectory(fout, dir.c_str(),
  1481. target1, target2, silent);
  1482. }
  1483. fout << "\n\n";
  1484. }
  1485. // output rules for decending into sub directories
  1486. void cmLocalUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1487. {
  1488. // Output Sub directory build rules
  1489. const std::vector<std::string>& SubDirectories
  1490. = m_Makefile->GetSubDirectories();
  1491. if( SubDirectories.size() == 0)
  1492. {
  1493. return;
  1494. }
  1495. this->OutputSubDirectoryVars(fout,
  1496. "SUBDIR_BUILD",
  1497. "default_target",
  1498. "default_target",
  1499. 0, "$(TARGETS)",
  1500. SubDirectories,
  1501. false);
  1502. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1503. "clean",
  1504. 0, 0,
  1505. SubDirectories);
  1506. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1507. "depend",
  1508. 0, 0,
  1509. SubDirectories);
  1510. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1511. "install",
  1512. 0, 0,
  1513. SubDirectories);
  1514. }
  1515. // Output the depend information for all the classes
  1516. // in the makefile. These would have been generated
  1517. // by the class cmMakeDepend GenerateMakefile
  1518. bool cmLocalUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1519. {
  1520. bool ret = false;
  1521. // Iterate over every target.
  1522. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1523. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1524. target != targets.end(); ++target)
  1525. {
  1526. // Iterate over every source for this target.
  1527. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1528. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1529. source != sources.end(); ++source)
  1530. {
  1531. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1532. {
  1533. if(!(*source)->GetDepends().empty())
  1534. {
  1535. // Iterate through all the dependencies for this source.
  1536. for(std::vector<std::string>::const_iterator dep =
  1537. (*source)->GetDepends().begin();
  1538. dep != (*source)->GetDepends().end(); ++dep)
  1539. {
  1540. fout << (*source)->GetSourceName()
  1541. << this->GetOutputExtension(
  1542. (*source)->GetSourceExtension().c_str()) << " : "
  1543. << cmSystemTools::ConvertToOutputPath(dep->c_str()) << "\n";
  1544. ret = true;
  1545. }
  1546. fout << "\n\n";
  1547. }
  1548. }
  1549. }
  1550. }
  1551. return ret;
  1552. }
  1553. // Output the depend information for all the classes
  1554. // in the makefile. These would have been generated
  1555. // by the class cmMakeDepend GenerateMakefile
  1556. void cmLocalUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1557. {
  1558. std::set<std::string> emittedLowerPath;
  1559. std::set<std::string> emitted;
  1560. // Iterate over every target.
  1561. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1562. fout << "# Suppresses display of executed commands\n";
  1563. fout << ".SILENT:\n";
  1564. fout << "# disable some common implicit rules to speed things up\n";
  1565. fout << ".SUFFIXES:\n";
  1566. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1567. this->OutputMakeVariables(fout);
  1568. fout << "default:\n";
  1569. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  1570. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1571. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1572. target != targets.end(); ++target)
  1573. {
  1574. // Iterate over every source for this target.
  1575. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1576. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1577. source != sources.end(); ++source)
  1578. {
  1579. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1580. {
  1581. if(!(*source)->GetDepends().empty())
  1582. {
  1583. for(std::vector<std::string>::const_iterator dep =
  1584. (*source)->GetDepends().begin();
  1585. dep != (*source)->GetDepends().end(); ++dep)
  1586. {
  1587. std::string dependfile =
  1588. cmSystemTools::ConvertToOutputPath(cmSystemTools::CollapseFullPath(dep->c_str()).c_str());
  1589. // use the lower path function to create uniqe names
  1590. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  1591. if(emittedLowerPath.insert(lowerpath).second)
  1592. {
  1593. emitted.insert(dependfile);
  1594. fout << "all:: " << dependfile << "\n";
  1595. }
  1596. }
  1597. }
  1598. }
  1599. }
  1600. }
  1601. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1602. std::set<std::string>::iterator i;
  1603. for(i = emitted.begin(); i != emitted.end(); ++i)
  1604. {
  1605. fout << "cmake.depends:: " << *i <<
  1606. "\n\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1607. }
  1608. fout << "\n\n";
  1609. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1610. for(std::set<std::string>::iterator it = emitted.begin();
  1611. it != emitted.end(); ++it)
  1612. {
  1613. fout << *it << ":\n"
  1614. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1615. }
  1616. }
  1617. // Output each custom rule in the following format:
  1618. // output: source depends...
  1619. // (tab) command...
  1620. void cmLocalUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1621. {
  1622. // We may be modifying the source groups temporarily, so make a copy.
  1623. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  1624. const cmTargets &tgts = m_Makefile->GetTargets();
  1625. for(cmTargets::const_iterator tgt = tgts.begin();
  1626. tgt != tgts.end(); ++tgt)
  1627. {
  1628. // add any custom rules to the source groups
  1629. for (std::vector<cmCustomCommand>::const_iterator cr =
  1630. tgt->second.GetCustomCommands().begin();
  1631. cr != tgt->second.GetCustomCommands().end(); ++cr)
  1632. {
  1633. // if the source for the custom command is the same name
  1634. // as the target, then to not create a rule in the makefile for
  1635. // the custom command, as the command will be fired when the other target
  1636. // is built.
  1637. if ( cr->GetSourceName().compare(tgt->first) !=0)
  1638. {
  1639. cmSourceGroup& sourceGroup =
  1640. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  1641. sourceGroups);
  1642. cmCustomCommand cc(*cr);
  1643. cc.ExpandVariables(*m_Makefile);
  1644. sourceGroup.AddCustomCommand(cc);
  1645. }
  1646. }
  1647. }
  1648. // Loop through every source group.
  1649. for(std::vector<cmSourceGroup>::const_iterator sg =
  1650. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  1651. {
  1652. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  1653. if(buildRules.empty())
  1654. { continue; }
  1655. std::string name = sg->GetName();
  1656. if(name != "")
  1657. {
  1658. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  1659. }
  1660. // Loop through each source in the source group.
  1661. for(cmSourceGroup::BuildRules::const_iterator cc =
  1662. buildRules.begin(); cc != buildRules.end(); ++ cc)
  1663. {
  1664. std::string source = cc->first;
  1665. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  1666. // Loop through every command generating code from the current source.
  1667. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1668. c != commands.end(); ++c)
  1669. {
  1670. // escape spaces and convert to native slashes path for
  1671. // the command
  1672. const char* comment = c->second.m_Comment.c_str();
  1673. std::string command = c->second.m_Command;
  1674. cmSystemTools::ReplaceString(command, "/./", "/");
  1675. command = cmSystemTools::ConvertToOutputPath(command.c_str());
  1676. command += " ";
  1677. // now add the arguments
  1678. command += c->second.m_Arguments;
  1679. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  1680. // if the command has no outputs, then it is a utility command
  1681. // with no outputs
  1682. if(commandFiles.m_Outputs.size() == 0)
  1683. {
  1684. std::string depends;
  1685. // collect out all the dependencies for this rule.
  1686. for(std::set<std::string>::const_iterator d =
  1687. commandFiles.m_Depends.begin();
  1688. d != commandFiles.m_Depends.end(); ++d)
  1689. {
  1690. std::string dep = *d;
  1691. cmSystemTools::ReplaceString(dep, "/./", "/");
  1692. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  1693. dep = cmSystemTools::ConvertToOutputPath(dep.c_str());
  1694. depends += " ";
  1695. depends += dep;
  1696. }
  1697. // output rule
  1698. this->OutputMakeRule(fout,
  1699. (*comment?comment:"Custom command"),
  1700. source.c_str(),
  1701. depends.c_str(),
  1702. command.c_str());
  1703. }
  1704. // Write a rule for every output generated by this command.
  1705. for(std::set<std::string>::const_iterator output =
  1706. commandFiles.m_Outputs.begin();
  1707. output != commandFiles.m_Outputs.end(); ++output)
  1708. {
  1709. std::string src = cmSystemTools::ConvertToOutputPath(source.c_str());
  1710. std::string depends;
  1711. depends += src;
  1712. // Collect out all the dependencies for this rule.
  1713. for(std::set<std::string>::const_iterator d =
  1714. commandFiles.m_Depends.begin();
  1715. d != commandFiles.m_Depends.end(); ++d)
  1716. {
  1717. std::string dep = *d;
  1718. cmSystemTools::ReplaceString(dep, "/./", "/");
  1719. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  1720. dep = cmSystemTools::ConvertToOutputPath(dep.c_str());
  1721. depends += " ";
  1722. depends += dep;
  1723. }
  1724. // output rule
  1725. this->OutputMakeRule(fout,
  1726. (*comment?comment:"Custom command"),
  1727. output->c_str(),
  1728. depends.c_str(),
  1729. command.c_str());
  1730. }
  1731. }
  1732. }
  1733. if(name != "")
  1734. {
  1735. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  1736. }
  1737. }
  1738. }
  1739. std::string
  1740. cmLocalUnixMakefileGenerator::ConvertToOutputForExisting(const char* p)
  1741. {
  1742. std::string ret = cmSystemTools::ConvertToOutputPath(p);
  1743. // if there are spaces in the path, then get the short path version
  1744. // if there is one
  1745. if(ret.find(' ') != std::string::npos)
  1746. {
  1747. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  1748. {
  1749. ret = p;
  1750. }
  1751. }
  1752. return ret;
  1753. }
  1754. void cmLocalUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1755. {
  1756. const char* variables =
  1757. "# the standard shell for make\n"
  1758. "SHELL = /bin/sh\n"
  1759. "\n";
  1760. if(!m_WindowsShell)
  1761. {
  1762. fout << variables;
  1763. }
  1764. else
  1765. {
  1766. fout <<
  1767. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  1768. "NULL=\n"
  1769. "!ELSE \n"
  1770. "NULL=nul\n"
  1771. "!ENDIF \n";
  1772. }
  1773. if(m_MakeSilentFlag.size())
  1774. {
  1775. fout << "MAKESILENT = " << m_MakeSilentFlag << "\n";
  1776. }
  1777. std::string cmakecommand = this->ConvertToOutputForExisting(
  1778. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  1779. fout << "CMAKE_COMMAND = "
  1780. << cmakecommand
  1781. << "\n";
  1782. fout << "RM = " << cmakecommand.c_str() << " -E remove -f\n";
  1783. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1784. {
  1785. fout << "CMAKE_EDIT_COMMAND = "
  1786. << this->ConvertToOutputForExisting(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1787. << "\n";
  1788. }
  1789. fout << "CMAKE_CURRENT_SOURCE = " <<
  1790. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory())
  1791. << "\n";
  1792. fout << "CMAKE_CURRENT_BINARY = " <<
  1793. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory())
  1794. << "\n";
  1795. fout << "CMAKE_SOURCE_DIR = " <<
  1796. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory())
  1797. << "\n";
  1798. fout << "CMAKE_BINARY_DIR = " <<
  1799. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory())
  1800. << "\n";
  1801. // Output Include paths
  1802. fout << "INCLUDE_FLAGS = ";
  1803. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1804. std::vector<std::string>::iterator i;
  1805. fout << "-I" <<
  1806. this->ConvertToOutputForExisting(m_Makefile->GetStartDirectory()) << " ";
  1807. for(i = includes.begin(); i != includes.end(); ++i)
  1808. {
  1809. std::string include = *i;
  1810. // Don't output a -I for the standard include path "/usr/include".
  1811. // This can cause problems with certain standard library
  1812. // implementations because the wrong headers may be found first.
  1813. if(include != "/usr/include")
  1814. {
  1815. fout << "-I" << this->ConvertToOutputForExisting(i->c_str()) << " ";
  1816. }
  1817. }
  1818. fout << m_Makefile->GetDefineFlags();
  1819. fout << "\n\n";
  1820. }
  1821. void cmLocalUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1822. {
  1823. const char* root
  1824. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1825. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  1826. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  1827. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  1828. const cmTargets &tgts = m_Makefile->GetTargets();
  1829. fout << "install: $(SUBDIR_INSTALL)\n";
  1830. fout << "\t@echo \"Installing ...\"\n";
  1831. const char* prefix
  1832. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  1833. if (!prefix)
  1834. {
  1835. prefix = "/usr/local";
  1836. }
  1837. for(cmTargets::const_iterator l = tgts.begin();
  1838. l != tgts.end(); l++)
  1839. {
  1840. if (l->second.GetInstallPath() != "")
  1841. {
  1842. // first make the directories for each target
  1843. fout << "\t@if [ ! -d $(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  1844. " ] ; then \\\n";
  1845. fout << "\t echo \"Making directory $(DESTDIR)" << prefix
  1846. << l->second.GetInstallPath() << " \"; \\\n";
  1847. fout << "\t mkdir -p $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1848. << "; \\\n";
  1849. fout << "\t chmod 755 $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1850. << "; \\\n";
  1851. fout << "\t else true; \\\n";
  1852. fout << "\t fi\n";
  1853. // now install the target
  1854. switch (l->second.GetType())
  1855. {
  1856. case cmTarget::STATIC_LIBRARY:
  1857. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1858. << l->first;
  1859. fout << ".a";
  1860. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1861. break;
  1862. case cmTarget::SHARED_LIBRARY:
  1863. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath
  1864. << this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX")
  1865. << l->first;
  1866. fout << this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1867. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1868. break;
  1869. case cmTarget::MODULE_LIBRARY:
  1870. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath
  1871. << this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX")
  1872. << l->first;
  1873. fout << this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1874. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1875. break;
  1876. case cmTarget::WIN32_EXECUTABLE:
  1877. case cmTarget::EXECUTABLE:
  1878. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  1879. << l->first
  1880. << cmSystemTools::GetExecutableExtension()
  1881. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1882. break;
  1883. case cmTarget::INSTALL_FILES:
  1884. {
  1885. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  1886. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  1887. sourcePath += "/";
  1888. binaryPath += "/";
  1889. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1890. std::vector<std::string>::const_iterator i;
  1891. for (i = sf.begin(); i != sf.end(); ++i)
  1892. {
  1893. std::string f = *i;
  1894. if(f.substr(0, sourcePath.length()) == sourcePath)
  1895. {
  1896. f = f.substr(sourcePath.length());
  1897. }
  1898. else if(f.substr(0, binaryPath.length()) == binaryPath)
  1899. {
  1900. f = f.substr(binaryPath.length());
  1901. }
  1902. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  1903. // avoid using install-sh to install install-sh
  1904. // does not work on windows....
  1905. if(*i == "install-sh")
  1906. {
  1907. fout << "\t @cp ";
  1908. }
  1909. else
  1910. {
  1911. fout << "\t @$(INSTALL_DATA) ";
  1912. }
  1913. fout << *i
  1914. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1915. }
  1916. }
  1917. break;
  1918. case cmTarget::INSTALL_PROGRAMS:
  1919. {
  1920. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  1921. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  1922. sourcePath += "/";
  1923. binaryPath += "/";
  1924. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1925. std::vector<std::string>::const_iterator i;
  1926. for (i = sf.begin(); i != sf.end(); ++i)
  1927. {
  1928. std::string f = *i;
  1929. if(f.substr(0, sourcePath.length()) == sourcePath)
  1930. {
  1931. f = f.substr(sourcePath.length());
  1932. }
  1933. else if(f.substr(0, binaryPath.length()) == binaryPath)
  1934. {
  1935. f = f.substr(binaryPath.length());
  1936. }
  1937. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  1938. // avoid using install-sh to install install-sh
  1939. // does not work on windows....
  1940. if(*i == "install-sh")
  1941. {
  1942. fout << "\t @cp ";
  1943. }
  1944. else
  1945. {
  1946. fout << "\t @$(INSTALL_PROGRAM) ";
  1947. }
  1948. fout << *i
  1949. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1950. }
  1951. }
  1952. break;
  1953. case cmTarget::UTILITY:
  1954. default:
  1955. break;
  1956. }
  1957. }
  1958. }
  1959. }
  1960. void cmLocalUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1961. {
  1962. this->OutputMakeRule(fout,
  1963. "default build rule",
  1964. "all",
  1965. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  1966. 0);
  1967. this->OutputMakeRule(fout,
  1968. "clean generated files",
  1969. "clean",
  1970. "$(SUBDIR_CLEAN)",
  1971. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  1972. " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  1973. // collect up all the sources
  1974. std::string allsources;
  1975. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1976. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1977. target != targets.end(); ++target)
  1978. {
  1979. // Iterate over every source for this target.
  1980. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1981. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1982. source != sources.end(); ++source)
  1983. {
  1984. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1985. {
  1986. allsources += " \\\n";
  1987. allsources += cmSystemTools::ConvertToOutputPath((*source)->GetFullPath().c_str());
  1988. }
  1989. }
  1990. }
  1991. this->OutputMakeRule(fout,
  1992. "dependencies.",
  1993. "cmake.depends",
  1994. "$(CMAKE_MAKEFILE_SOURCES)",
  1995. "$(CMAKE_COMMAND) "
  1996. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1997. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1998. );
  1999. this->OutputMakeRule(fout,
  2000. "dependencies",
  2001. "cmake.check_depends",
  2002. allsources.c_str(),
  2003. "$(CMAKE_COMMAND) "
  2004. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2005. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  2006. );
  2007. this->OutputMakeRule(fout,
  2008. "dependencies",
  2009. "depend",
  2010. "$(SUBDIR_DEPEND)",
  2011. "$(CMAKE_COMMAND) "
  2012. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2013. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2014. this->OutputMakeRule(fout,
  2015. "dependencies",
  2016. "dependlocal",
  2017. 0,
  2018. "$(CMAKE_COMMAND) "
  2019. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2020. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2021. this->OutputMakeRule(fout,
  2022. "CMakeCache.txt",
  2023. "rebuild_cache",
  2024. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  2025. "$(CMAKE_COMMAND) "
  2026. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2027. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  2028. checkCache += "/cmake.check_cache";
  2029. checkCache = cmSystemTools::ConvertToOutputPath(checkCache.c_str());
  2030. std::string CMakeCache = m_Makefile->GetHomeOutputDirectory();
  2031. CMakeCache += "/CMakeCache.txt";
  2032. CMakeCache = cmSystemTools::ConvertToOutputPath(CMakeCache.c_str());
  2033. this->OutputMakeRule(fout,
  2034. "CMakeCache.txt because out-of-date:",
  2035. checkCache.c_str(),
  2036. CMakeCache.c_str(),
  2037. "$(CMAKE_COMMAND) "
  2038. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2039. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  2040. // called edit_cache
  2041. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2042. {
  2043. this->OutputMakeRule(fout,
  2044. "edit CMakeCache.txt",
  2045. "edit_cache",
  2046. 0,
  2047. "$(CMAKE_EDIT_COMMAND) "
  2048. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2049. }
  2050. this->OutputMakeRule(fout,
  2051. "CMakeCache.txt",
  2052. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  2053. 0,
  2054. "$(CMAKE_COMMAND) "
  2055. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2056. this->OutputMakeRule(fout,
  2057. "Rule to keep make from removing Makefiles "
  2058. "if control-C is hit during a run of cmake.",
  2059. ".PRECIOUS",
  2060. "Makefile cmake.depends",
  2061. 0);
  2062. this->OutputSourceObjectBuildRules(fout);
  2063. // find ctest
  2064. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2065. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2066. ctest += "/";
  2067. ctest += "ctest";
  2068. ctest += cmSystemTools::GetExecutableExtension();
  2069. if(!cmSystemTools::FileExists(ctest.c_str()))
  2070. {
  2071. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2072. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2073. ctest += "/Debug/";
  2074. ctest += "ctest";
  2075. ctest += cmSystemTools::GetExecutableExtension();
  2076. }
  2077. if(!cmSystemTools::FileExists(ctest.c_str()))
  2078. {
  2079. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2080. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2081. ctest += "/Release/";
  2082. ctest += "ctest";
  2083. ctest += cmSystemTools::GetExecutableExtension();
  2084. }
  2085. if (cmSystemTools::FileExists(ctest.c_str()))
  2086. {
  2087. this->OutputMakeRule(fout,
  2088. "tests",
  2089. "test",
  2090. "",
  2091. this->ConvertToOutputForExisting(ctest.c_str()).c_str());
  2092. }
  2093. }
  2094. void
  2095. cmLocalUnixMakefileGenerator::
  2096. OutputBuildObjectFromSource(std::ostream& fout,
  2097. const char* shortName,
  2098. const cmSourceFile& source,
  2099. const char* extraCompileFlags,
  2100. bool shared)
  2101. {
  2102. // Header files shouldn't have build rules.
  2103. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  2104. {
  2105. return;
  2106. }
  2107. std::string comment = "object file";
  2108. std::string objectFile = std::string(shortName) +
  2109. this->GetOutputExtension(source.GetSourceExtension().c_str());
  2110. objectFile = cmSystemTools::ConvertToOutputPath(objectFile.c_str());
  2111. cmSystemTools::FileFormat format =
  2112. cmSystemTools::GetFileFormat(source.GetSourceExtension().c_str());
  2113. std::vector<std::string> rules;
  2114. std::string flags;
  2115. if(extraCompileFlags)
  2116. {
  2117. flags += extraCompileFlags;
  2118. }
  2119. std::string sourceFile =
  2120. cmSystemTools::ConvertToOutputPath(source.GetFullPath().c_str());
  2121. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2122. buildType = cmSystemTools::UpperCase(buildType);
  2123. switch(format)
  2124. {
  2125. case cmSystemTools::C_FILE_FORMAT:
  2126. {
  2127. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_COMPILE_OBJECT"));
  2128. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  2129. flags += " ";
  2130. if(buildType.size())
  2131. {
  2132. std::string build = "CMAKE_C_FLAGS_";
  2133. build += buildType;
  2134. flags += this->GetSafeDefinition(build.c_str());
  2135. flags += " ";
  2136. }
  2137. if(shared)
  2138. {
  2139. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  2140. flags += " ";
  2141. }
  2142. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2143. {
  2144. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_C_FLAGS");
  2145. flags += " ";
  2146. }
  2147. break;
  2148. }
  2149. case cmSystemTools::CXX_FILE_FORMAT:
  2150. {
  2151. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_COMPILE_OBJECT"));
  2152. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  2153. flags += " ";
  2154. if(buildType.size())
  2155. {
  2156. std::string build = "CMAKE_CXX_FLAGS_";
  2157. build += buildType;
  2158. flags += this->GetSafeDefinition(build.c_str());
  2159. flags += " ";
  2160. }
  2161. if(shared)
  2162. {
  2163. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  2164. flags += " ";
  2165. }
  2166. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2167. {
  2168. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  2169. flags += " ";
  2170. }
  2171. break;
  2172. }
  2173. case cmSystemTools::HEADER_FILE_FORMAT:
  2174. return;
  2175. case cmSystemTools::DEFINITION_FILE_FORMAT:
  2176. return;
  2177. case cmSystemTools::RESOURCE_FILE_FORMAT:
  2178. {
  2179. flags = " $(INCLUDE_FLAGS) ";
  2180. // use rc rule here if it is defined
  2181. const char* rule = m_Makefile->GetDefinition("CMAKE_COMPILE_RESOURCE");
  2182. if(rule)
  2183. {
  2184. rules.push_back(rule);
  2185. }
  2186. }
  2187. break;
  2188. case cmSystemTools::NO_FILE_FORMAT:
  2189. case cmSystemTools::JAVA_FILE_FORMAT:
  2190. case cmSystemTools::STATIC_LIBRARY_FILE_FORMAT:
  2191. case cmSystemTools::SHARED_LIBRARY_FILE_FORMAT:
  2192. case cmSystemTools::MODULE_FILE_FORMAT:
  2193. case cmSystemTools::OBJECT_FILE_FORMAT:
  2194. case cmSystemTools::UNKNOWN_FILE_FORMAT:
  2195. cmSystemTools::Error("Unexpected file type ",
  2196. sourceFile.c_str());
  2197. break;
  2198. }
  2199. flags += "$(INCLUDE_FLAGS) ";
  2200. // expand multi-command semi-colon separated lists
  2201. // of commands into separate commands
  2202. std::vector<std::string> commands;
  2203. cmSystemTools::ExpandList(rules, commands);
  2204. for(std::vector<std::string>::iterator i = commands.begin();
  2205. i != commands.end(); ++i)
  2206. {
  2207. this->ExpandRuleVariables(*i,
  2208. 0, // no objects
  2209. 0, // no target
  2210. 0, // no link libs
  2211. sourceFile.c_str(),
  2212. objectFile.c_str(),
  2213. flags.c_str());
  2214. }
  2215. this->OutputMakeRule(fout,
  2216. comment.c_str(),
  2217. objectFile.c_str(),
  2218. sourceFile.c_str(),
  2219. commands);
  2220. }
  2221. void cmLocalUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  2222. {
  2223. fout << "# Rules to build " << this->GetOutputExtension("")
  2224. << " files from their sources:\n";
  2225. std::set<std::string> rules;
  2226. // Iterate over every target.
  2227. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2228. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2229. target != targets.end(); ++target)
  2230. {
  2231. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2232. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  2233. std::string exportsDef = "";
  2234. if(shared)
  2235. {
  2236. exportsDef = "-D"+target->first+"_EXPORTS ";
  2237. }
  2238. // Iterate over every source for this target.
  2239. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  2240. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2241. source != sources.end(); ++source)
  2242. {
  2243. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  2244. {
  2245. std::string shortName;
  2246. std::string sourceName;
  2247. // If the full path to the source file includes this
  2248. // directory, we want to use the relative path for the
  2249. // filename of the object file. Otherwise, we will use just
  2250. // the filename portion.
  2251. if((cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
  2252. || (cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->
  2253. GetCurrentOutputDirectory()) == 0))
  2254. {
  2255. sourceName = (*source)->GetSourceName()+"."+(*source)->GetSourceExtension();
  2256. shortName = (*source)->GetSourceName();
  2257. // The path may be relative. See if a directory needs to be
  2258. // created for the output file. This is a ugly, and perhaps
  2259. // should be moved elsewhere.
  2260. std::string relPath =
  2261. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  2262. if(relPath != "")
  2263. {
  2264. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  2265. outPath += "/"+relPath;
  2266. cmSystemTools::MakeDirectory(outPath.c_str());
  2267. }
  2268. }
  2269. else
  2270. {
  2271. sourceName = (*source)->GetFullPath();
  2272. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  2273. }
  2274. std::string shortNameWithExt = shortName +
  2275. (*source)->GetSourceExtension();
  2276. // Only output a rule for each .o once.
  2277. std::string compileFlags = exportsDef;
  2278. compileFlags += " ";
  2279. if(rules.find(shortNameWithExt) == rules.end())
  2280. {
  2281. if((*source)->GetProperty("COMPILE_FLAGS"))
  2282. {
  2283. compileFlags += (*source)->GetProperty("COMPILE_FLAGS");
  2284. compileFlags += " ";
  2285. }
  2286. this->OutputBuildObjectFromSource(fout,
  2287. shortName.c_str(),
  2288. *(*source),
  2289. compileFlags.c_str(),
  2290. shared);
  2291. rules.insert(shortNameWithExt);
  2292. }
  2293. }
  2294. }
  2295. }
  2296. }
  2297. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2298. const char* comment,
  2299. const char* target,
  2300. const char* depends,
  2301. const char* command,
  2302. const char* command2,
  2303. const char* command3,
  2304. const char* command4)
  2305. {
  2306. std::vector<std::string> commands;
  2307. if(command)
  2308. {
  2309. commands.push_back(command);
  2310. }
  2311. if(command2)
  2312. {
  2313. commands.push_back(command2);
  2314. }
  2315. if(command3)
  2316. {
  2317. commands.push_back(command3);
  2318. }
  2319. if(command4)
  2320. {
  2321. commands.push_back(command4);
  2322. }
  2323. this->OutputMakeRule(fout, comment, target, depends, commands);
  2324. }
  2325. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2326. const char* comment,
  2327. const char* target,
  2328. const char* depends,
  2329. const std::vector<std::string>& commands)
  2330. {
  2331. if(!target)
  2332. {
  2333. cmSystemTools::Error("no target for OutputMakeRule");
  2334. return;
  2335. }
  2336. std::string replace;
  2337. if(comment)
  2338. {
  2339. replace = comment;
  2340. m_Makefile->ExpandVariablesInString(replace);
  2341. fout << "#---------------------------------------------------------\n";
  2342. fout << "# " << replace;
  2343. fout << "\n#\n";
  2344. }
  2345. fout << "\n";
  2346. replace = target;
  2347. m_Makefile->ExpandVariablesInString(replace);
  2348. fout << cmSystemTools::ConvertToOutputPath(replace.c_str()) << ": ";
  2349. if(depends)
  2350. {
  2351. replace = depends;
  2352. m_Makefile->ExpandVariablesInString(replace);
  2353. fout << replace.c_str();
  2354. }
  2355. fout << "\n";
  2356. int count = 0;
  2357. for (std::vector<std::string>::const_iterator i = commands.begin();
  2358. i != commands.end(); ++i)
  2359. {
  2360. replace = *i;
  2361. m_Makefile->ExpandVariablesInString(replace);
  2362. if(count == 0 && replace.find_first_not_of(" \t\n\r") != std::string::npos &&
  2363. replace[0] != '-' && replace.find("echo") != 0
  2364. && replace.find("$(MAKE)") != 0)
  2365. {
  2366. std::string echostring = "Building ";
  2367. echostring += comment;
  2368. echostring += " ";
  2369. echostring += target;
  2370. echostring += "...";
  2371. // for unix we want to quote the output of echo
  2372. // for nmake and borland, the echo should not be quoted
  2373. if(strcmp(m_GlobalGenerator->GetName(), "Unix Makefiles") == 0)
  2374. {
  2375. cmSystemTools::ReplaceString(echostring, "\\\n", " ");
  2376. cmSystemTools::ReplaceString(echostring, " \t", " ");
  2377. cmSystemTools::ReplaceString(echostring, "\n\t", "\"\n\techo \"");
  2378. fout << "\techo \"" << echostring.c_str() << "\"\n";
  2379. }
  2380. else
  2381. {
  2382. cmSystemTools::ReplaceString(echostring, "\n\t", "\n\techo ");
  2383. fout << "\techo " << echostring.c_str() << "\n";
  2384. }
  2385. }
  2386. fout << "\t" << replace.c_str() << "\n";
  2387. count++;
  2388. }
  2389. fout << "\n";
  2390. }
  2391. const char* cmLocalUnixMakefileGenerator::GetSafeDefinition(const char* def)
  2392. {
  2393. const char* ret = m_Makefile->GetDefinition(def);
  2394. if(!ret)
  2395. {
  2396. return "";
  2397. }
  2398. return ret;
  2399. }
  2400. std::string cmLocalUnixMakefileGenerator::LowerCasePath(const char* path)
  2401. {
  2402. #ifdef _WIN32
  2403. return cmSystemTools::LowerCase(path);
  2404. #else
  2405. return std::string(path);
  2406. #endif
  2407. }
  2408. std::string
  2409. cmLocalUnixMakefileGenerator::CreateMakeVariable(const char* s, const char* s2)
  2410. {
  2411. if(!m_MakefileVariableSize)
  2412. {
  2413. return std::string(s) + std::string(s2);
  2414. }
  2415. std::string unmodified = s;
  2416. unmodified += s2;
  2417. // see if th
  2418. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  2419. if(i != m_MakeVariableMap.end())
  2420. {
  2421. return i->second;
  2422. }
  2423. std::string ret = unmodified;
  2424. // if the string is greater the 32 chars it is an invalid vairable name
  2425. // for borland make
  2426. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  2427. {
  2428. int keep = m_MakefileVariableSize - 8;
  2429. int size = keep + 3;
  2430. std::string str1 = s;
  2431. std::string str2 = s2;
  2432. // we must shorten the combined string by 4 charactors
  2433. // keep no more than 24 charactors from the second string
  2434. if(static_cast<int>(str2.size()) > keep)
  2435. {
  2436. str2 = str2.substr(0, keep);
  2437. }
  2438. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  2439. {
  2440. str1 = str1.substr(0, size - str2.size());
  2441. }
  2442. char buffer[5];
  2443. int ni = 0;
  2444. sprintf(buffer, "%04d", ni);
  2445. ret = str1 + str2 + buffer;
  2446. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2447. {
  2448. ++ni;
  2449. sprintf(buffer, "%04d", ni);
  2450. ret = str1 + str2 + buffer;
  2451. }
  2452. if(ni == 1000)
  2453. {
  2454. cmSystemTools::Error("Borland makefile varible length too long");
  2455. return unmodified;
  2456. }
  2457. // once an unused variable is found
  2458. m_ShortMakeVariableMap[ret] = "1";
  2459. }
  2460. // always make an entry into the unmodified to varible map
  2461. m_MakeVariableMap[unmodified] = ret;
  2462. return ret;
  2463. }