cmLocalUnixMakefileGenerator3.cxx 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077
  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 "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmDepends.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGlobalUnixMakefileGenerator3.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmake.h"
  20. // Include dependency scanners for supported languages. Only the
  21. // C/C++ scanner is needed for bootstrapping CMake.
  22. #include "cmDependsC.h"
  23. #ifdef CMAKE_BUILD_WITH_CMAKE
  24. # include "cmDependsFortran.h"
  25. # include "cmDependsJava.h"
  26. #endif
  27. #include <memory> // auto_ptr
  28. #include <queue>
  29. // TODO: Add "help" target.
  30. // TODO: Identify remaining relative path violations.
  31. // TODO: Need test for separate executable/library output path.
  32. //----------------------------------------------------------------------------
  33. cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3()
  34. {
  35. m_WindowsShell = false;
  36. m_IncludeDirective = "include";
  37. m_MakefileVariableSize = 0;
  38. m_IgnoreLibPrefix = false;
  39. m_PassMakeflags = false;
  40. m_EchoNeedsQuote = true;
  41. }
  42. //----------------------------------------------------------------------------
  43. cmLocalUnixMakefileGenerator3::~cmLocalUnixMakefileGenerator3()
  44. {
  45. }
  46. //----------------------------------------------------------------------------
  47. void cmLocalUnixMakefileGenerator3::Configure()
  48. {
  49. this->ComputeHomeRelativeOutputPath();
  50. this->cmLocalGenerator::Configure();
  51. }
  52. //----------------------------------------------------------------------------
  53. void cmLocalUnixMakefileGenerator3::Generate()
  54. {
  55. // Setup our configuration variables for this directory.
  56. this->ConfigureOutputPaths();
  57. // Generate the rule files for each target.
  58. cmTargets& targets = m_Makefile->GetTargets();
  59. std::string empty;
  60. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  61. {
  62. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  63. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  64. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  65. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  66. {
  67. t->second.TraceVSDependencies(empty, m_Makefile);
  68. this->WriteTargetRuleFiles(t->second);
  69. }
  70. else if(t->second.GetType() == cmTarget::UTILITY)
  71. {
  72. t->second.TraceVSDependencies(empty, m_Makefile);
  73. this->WriteUtilityRuleFiles(t->second);
  74. }
  75. }
  76. // write the local Makefile
  77. this->WriteLocalMakefile();
  78. // Write the cmake file with information for this directory.
  79. this->WriteDirectoryInformationFile();
  80. // all custom commands used?
  81. const std::vector<cmSourceFile*> &srcs = m_Makefile->GetSourceFiles();
  82. std::vector<cmSourceFile*>::const_iterator sit = srcs.begin();
  83. for (;sit != srcs.end(); ++sit)
  84. {
  85. if ((*sit)->GetCustomCommand() && !(*sit)->GetCustomCommand()->IsUsed())
  86. {
  87. cmOStringStream err;
  88. err << "Warning: Custom command for source file "
  89. << (*sit)->GetSourceName().c_str() << "."
  90. << (*sit)->GetSourceExtension().c_str()
  91. << " was not used.";
  92. cmSystemTools::Message(err.str().c_str(), "Warning");
  93. }
  94. }
  95. }
  96. //----------------------------------------------------------------------------
  97. void cmLocalUnixMakefileGenerator3::ConfigureOutputPaths()
  98. {
  99. // Format the library and executable output paths.
  100. if(const char* libOut = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  101. {
  102. m_LibraryOutputPath = libOut;
  103. this->FormatOutputPath(m_LibraryOutputPath, "LIBRARY");
  104. }
  105. if(const char* exeOut = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  106. {
  107. m_ExecutableOutputPath = exeOut;
  108. this->FormatOutputPath(m_ExecutableOutputPath, "EXECUTABLE");
  109. }
  110. }
  111. //----------------------------------------------------------------------------
  112. void cmLocalUnixMakefileGenerator3::FormatOutputPath(std::string& path,
  113. const char* name)
  114. {
  115. if(!path.empty())
  116. {
  117. // Convert the output path to a full path in case it is
  118. // specified as a relative path. Treat a relative path as
  119. // relative to the current output directory for this makefile.
  120. path =
  121. cmSystemTools::CollapseFullPath(path.c_str(),
  122. m_Makefile->GetStartOutputDirectory());
  123. // Add a trailing slash for easy appending later.
  124. if(path.empty() || path[path.size()-1] != '/')
  125. {
  126. path += "/";
  127. }
  128. // Make sure the output path exists on disk.
  129. if(!cmSystemTools::MakeDirectory(path.c_str()))
  130. {
  131. cmSystemTools::Error("Error failed to create ",
  132. name, "_OUTPUT_PATH directory:", path.c_str());
  133. }
  134. // Add this as a link directory automatically.
  135. m_Makefile->AddLinkDirectory(path.c_str());
  136. }
  137. }
  138. void cmLocalUnixMakefileGenerator3
  139. ::WriteCustomCommands(cmTarget &target,std::ostream& ruleFileStream,
  140. std::vector<std::string>& cleanFiles)
  141. {
  142. std::string tgtDir = m_Makefile->GetStartOutputDirectory();
  143. tgtDir += "/";
  144. tgtDir += this->GetTargetDirectory(target);
  145. // add custom commands to the clean rules?
  146. const char* clean_no_custom = m_Makefile->GetProperty("CLEAN_NO_CUSTOM");
  147. bool clean = cmSystemTools::IsOff(clean_no_custom);
  148. // Generate the rule files for each custom command.
  149. // get the classes from the source lists then add them to the groups
  150. const std::vector<cmSourceFile*> &classes = target.GetSourceFiles();
  151. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  152. i != classes.end(); i++)
  153. {
  154. if(cmCustomCommand* cc = (*i)->GetCustomCommand())
  155. {
  156. cc->Used();
  157. this->GenerateCustomRuleFile(*cc,tgtDir.c_str(),ruleFileStream);
  158. if (clean)
  159. {
  160. cleanFiles.push_back
  161. (this->Convert(cc->GetOutput(),HOME_OUTPUT,SHELL));
  162. }
  163. }
  164. }
  165. }
  166. //----------------------------------------------------------------------------
  167. void cmLocalUnixMakefileGenerator3::WriteDirectoryInformationFile()
  168. {
  169. std::string infoFileName = m_Makefile->GetStartOutputDirectory();
  170. infoFileName += "/CMakeDirectoryInformation.cmake";
  171. // Open the output file.
  172. cmGeneratedFileStream infoFileStream(infoFileName.c_str());
  173. if(!infoFileStream)
  174. {
  175. return;
  176. }
  177. // Write the do not edit header.
  178. this->WriteDisclaimer(infoFileStream);
  179. // Tell the dependency scanner to use unix paths if necessary.
  180. if(cmSystemTools::GetForceUnixPaths())
  181. {
  182. infoFileStream
  183. << "# Force unix paths in dependencies.\n"
  184. << "SET(CMAKE_FORCE_UNIX_PATHS 1)\n"
  185. << "\n";
  186. }
  187. // Store the include search path for this directory.
  188. infoFileStream
  189. << "# The C and CXX include file search paths:\n";
  190. infoFileStream
  191. << "SET(CMAKE_C_INCLUDE_PATH\n";
  192. std::vector<std::string> includeDirs;
  193. this->GetIncludeDirectories(includeDirs);
  194. for(std::vector<std::string>::iterator i = includeDirs.begin();
  195. i != includeDirs.end(); ++i)
  196. {
  197. infoFileStream
  198. << " \"" << this->Convert(i->c_str(),HOME_OUTPUT).c_str() << "\"\n";
  199. }
  200. infoFileStream
  201. << " )\n";
  202. infoFileStream
  203. << "SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
  204. // Store the include regular expressions for this directory.
  205. infoFileStream
  206. << "\n"
  207. << "# The C and CXX include file regular expressions for this directory.\n";
  208. infoFileStream
  209. << "SET(CMAKE_C_INCLUDE_REGEX_SCAN ";
  210. this->WriteCMakeArgument(infoFileStream,
  211. m_Makefile->GetIncludeRegularExpression());
  212. infoFileStream
  213. << ")\n";
  214. infoFileStream
  215. << "SET(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
  216. this->WriteCMakeArgument(infoFileStream,
  217. m_Makefile->GetComplainRegularExpression());
  218. infoFileStream
  219. << ")\n";
  220. infoFileStream
  221. << "SET(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
  222. infoFileStream
  223. << "SET(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
  224. }
  225. //----------------------------------------------------------------------------
  226. std::string
  227. cmLocalUnixMakefileGenerator3
  228. ::ConvertToFullPath(const std::string& localPath)
  229. {
  230. std::string dir = m_Makefile->GetStartOutputDirectory();
  231. dir += "/";
  232. dir += localPath;
  233. return dir;
  234. }
  235. //----------------------------------------------------------------------------
  236. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  237. {
  238. os
  239. << "# CMAKE generated file: DO NOT EDIT!\n"
  240. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  241. << " Generator, CMake Version "
  242. << cmMakefile::GetMajorVersion() << "."
  243. << cmMakefile::GetMinorVersion() << "\n\n";
  244. }
  245. void cmLocalUnixMakefileGenerator3::ComputeHomeRelativeOutputPath()
  246. {
  247. // Include the rule file for each object.
  248. m_HomeRelativeOutputPath =
  249. cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(),
  250. m_Makefile->GetStartOutputDirectory());
  251. if (m_HomeRelativeOutputPath.size())
  252. {
  253. m_HomeRelativeOutputPath += "/";
  254. }
  255. }
  256. const std::string &cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
  257. {
  258. return m_HomeRelativeOutputPath;
  259. }
  260. //----------------------------------------------------------------------------
  261. void
  262. cmLocalUnixMakefileGenerator3
  263. ::WriteTargetRuleFiles(cmTarget& target)
  264. {
  265. // Create a directory for this target.
  266. std::string dir = this->GetTargetDirectory(target);
  267. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  268. // Generate the build-time dependencies file for this target.
  269. std::string depBase = dir;
  270. depBase += "/";
  271. depBase += target.GetName();
  272. // Construct the rule file name.
  273. std::string ruleFileName = dir;
  274. ruleFileName += "/build.make";
  275. // Open the rule file. This should be copy-if-different because the
  276. // rules may depend on this file itself.
  277. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  278. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  279. ruleFileStream.SetCopyIfDifferent(true);
  280. if(!ruleFileStream)
  281. {
  282. return;
  283. }
  284. this->WriteDisclaimer(ruleFileStream);
  285. this->WriteMakeVariables(ruleFileStream, HOME_OUTPUT);
  286. // Include the dependencies for the target.
  287. std::string depPath = dir;
  288. depPath += "/depend.make";
  289. depPath = this->ConvertToFullPath(depPath.c_str());
  290. depPath = this->Convert(depPath.c_str(),HOME_OUTPUT,MAKEFILE);
  291. ruleFileStream
  292. << "# Include any dependencies generated for this target.\n"
  293. << m_IncludeDirective << " "
  294. << depPath
  295. << "\n\n";
  296. // make sure the depend file exists
  297. if (!cmSystemTools::FileExists(depPath.c_str()))
  298. {
  299. // Write an empty dependency file.
  300. cmGeneratedFileStream depFileStream(depPath.c_str());
  301. depFileStream
  302. << "# Empty dependencies file for " << target.GetName() << ".\n"
  303. << "# This may be replaced when dependencies are built." << std::endl;
  304. }
  305. // First generate the object rule files. Save a list of all object
  306. // files for this target.
  307. std::vector<std::string> objects;
  308. std::vector<std::string> external_objects;
  309. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  310. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  311. source != sources.end(); ++source)
  312. {
  313. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  314. !(*source)->GetCustomCommand())
  315. {
  316. if(!m_GlobalGenerator->IgnoreFile((*source)->GetSourceExtension().c_str()))
  317. {
  318. // Generate this object file's rule file.
  319. this->WriteObjectRuleFiles(target, *(*source), objects,
  320. ruleFileStream);
  321. }
  322. else if((*source)->GetPropertyAsBool("EXTERNAL_OBJECT"))
  323. {
  324. // This is an external object file. Just add it.
  325. external_objects.push_back((*source)->GetFullPath());
  326. }
  327. }
  328. }
  329. // write the custom commands for this target
  330. std::vector<std::string> cleanFiles;
  331. // Look for files registered for cleaning in this directory.
  332. if(const char* additional_clean_files =
  333. m_Makefile->GetProperty("ADDITIONAL_MAKE_CLEAN_FILES"))
  334. {
  335. cmSystemTools::ExpandListArgument(additional_clean_files, cleanFiles);
  336. }
  337. this->WriteCustomCommands(target,ruleFileStream,cleanFiles);
  338. // Include the rule file for each object.
  339. std::string relPath = this->GetHomeRelativeOutputPath();
  340. std::string objTarget;
  341. // Write the rule for this target type.
  342. switch(target.GetType())
  343. {
  344. case cmTarget::STATIC_LIBRARY:
  345. this->WriteStaticLibraryRule(ruleFileStream, ruleFileName.c_str(),
  346. target, objects, external_objects,
  347. cleanFiles);
  348. break;
  349. case cmTarget::SHARED_LIBRARY:
  350. this->WriteSharedLibraryRule(ruleFileStream, ruleFileName.c_str(),
  351. target, objects, external_objects,
  352. cleanFiles);
  353. break;
  354. case cmTarget::MODULE_LIBRARY:
  355. this->WriteModuleLibraryRule(ruleFileStream, ruleFileName.c_str(),
  356. target, objects, external_objects,
  357. cleanFiles);
  358. break;
  359. case cmTarget::EXECUTABLE:
  360. this->WriteExecutableRule(ruleFileStream, ruleFileName.c_str(),
  361. target, objects, external_objects,
  362. cleanFiles);
  363. break;
  364. default:
  365. break;
  366. }
  367. // Write the requires target.
  368. this->WriteTargetRequiresRule(ruleFileStream, target, objects);
  369. // Write clean target
  370. this->WriteTargetCleanRule(ruleFileStream, target, cleanFiles);
  371. }
  372. //----------------------------------------------------------------------------
  373. void
  374. cmLocalUnixMakefileGenerator3
  375. ::WriteObjectDependRules(cmSourceFile& source,
  376. std::vector<std::string>& depends)
  377. {
  378. // Create the list of dependencies known at cmake time. These are
  379. // shared between the object file and dependency scanning rule.
  380. depends.push_back(source.GetFullPath());
  381. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  382. {
  383. std::vector<std::string> deps;
  384. cmSystemTools::ExpandListArgument(objectDeps, deps);
  385. for(std::vector<std::string>::iterator i = deps.begin();
  386. i != deps.end(); ++i)
  387. {
  388. depends.push_back(i->c_str());
  389. }
  390. }
  391. }
  392. //----------------------------------------------------------------------------
  393. void
  394. cmLocalUnixMakefileGenerator3
  395. ::WriteObjectBuildFile(std::string &obj,
  396. const char *lang,
  397. cmTarget& target,
  398. cmSourceFile& source,
  399. std::vector<std::string>& depends,
  400. std::ostream &ruleFileStream)
  401. {
  402. // Open the rule file for writing. This should be copy-if-different
  403. // because the rules may depend on this file itself.
  404. std::string ruleFileName = this->GetTargetDirectory(target);
  405. ruleFileName += "/build.make";
  406. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  407. // generate the depend scanning rule
  408. this->WriteObjectDependRules(source, depends);
  409. this->AppendRuleDepend(depends, ruleFileNameFull.c_str());
  410. // Write the build rule.
  411. // Build the set of compiler flags.
  412. std::string flags;
  413. // Add the export symbol definition for shared library objects.
  414. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  415. (target.GetType() == cmTarget::MODULE_LIBRARY));
  416. if(shared)
  417. {
  418. flags += "-D";
  419. if(const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  420. {
  421. flags += custom_export_name;
  422. }
  423. else
  424. {
  425. std::string in = target.GetName();
  426. in += "_EXPORTS";
  427. flags += cmSystemTools::MakeCindentifier(in.c_str());
  428. }
  429. }
  430. // Add flags from source file properties.
  431. this->AppendFlags(flags, source.GetProperty("COMPILE_FLAGS"));
  432. // Add language-specific flags.
  433. this->AddLanguageFlags(flags, lang);
  434. // Add shared-library flags if needed.
  435. this->AddSharedFlags(flags, lang, shared);
  436. // Add include directory flags.
  437. this->AppendFlags(flags, this->GetIncludeFlags(lang));
  438. // Get the output paths for source and object files.
  439. std::string sourceFile = source.GetFullPath();
  440. if(m_UseRelativePaths)
  441. {
  442. sourceFile = this->Convert(sourceFile.c_str(),HOME_OUTPUT);
  443. }
  444. sourceFile = this->Convert(sourceFile.c_str(),NONE,SHELL);
  445. std::string objectFile = this->Convert(obj.c_str(),START_OUTPUT,SHELL);
  446. // Construct the build message.
  447. std::string relativeObj = this->GetHomeRelativeOutputPath();
  448. relativeObj += obj;
  449. std::vector<std::string> commands;
  450. std::string buildEcho = "Building ";
  451. buildEcho += lang;
  452. buildEcho += " object ";
  453. buildEcho += relativeObj;
  454. this->AppendEcho(commands, buildEcho.c_str());
  455. // Construct the compile rules.
  456. std::string compileRuleVar = "CMAKE_";
  457. compileRuleVar += lang;
  458. compileRuleVar += "_COMPILE_OBJECT";
  459. std::string compileRule =
  460. m_Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  461. cmSystemTools::ExpandListArgument(compileRule, commands);
  462. // Expand placeholders in the commands.
  463. for(std::vector<std::string>::iterator i = commands.begin();
  464. i != commands.end(); ++i)
  465. {
  466. this->ExpandRuleVariables(*i,
  467. lang,
  468. 0, // no objects
  469. 0, // no target
  470. 0, // no link libs
  471. sourceFile.c_str(),
  472. relativeObj.c_str(),
  473. flags.c_str());
  474. }
  475. // Write the rule.
  476. this->WriteMakeRule(ruleFileStream, 0,
  477. relativeObj.c_str(), depends, commands);
  478. // If the language needs provides-requires mode, create the
  479. // corresponding targets.
  480. std::string objectRequires = relativeObj;
  481. objectRequires += ".requires";
  482. std::vector<std::string> no_commands;
  483. std::vector<std::string> p_depends;
  484. // always provide an empty requires target
  485. this->WriteMakeRule(ruleFileStream, 0,
  486. objectRequires.c_str(), p_depends, no_commands);
  487. // write a build rule to recursively build what this obj provides
  488. std::string objectProvides = relativeObj;
  489. objectProvides += ".provides";
  490. std::string temp = relativeObj;
  491. temp += ".provides.build";
  492. std::vector<std::string> r_commands;
  493. std::string tgtMakefileName = this->GetRelativeTargetDirectory(target);
  494. tgtMakefileName += "/build.make";
  495. r_commands.push_back(this->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  496. temp.c_str()));
  497. p_depends.clear();
  498. p_depends.push_back(objectRequires);
  499. this->WriteMakeRule(ruleFileStream, 0,
  500. objectProvides.c_str(), p_depends, r_commands);
  501. // write the provides.build rule dependency on the obj file
  502. p_depends.clear();
  503. p_depends.push_back(relativeObj);
  504. this->WriteMakeRule(ruleFileStream, 0,
  505. temp.c_str(), p_depends, no_commands);
  506. }
  507. //----------------------------------------------------------------------------
  508. void
  509. cmLocalUnixMakefileGenerator3
  510. ::WriteObjectRuleFiles(cmTarget& target, cmSourceFile& source,
  511. std::vector<std::string>& objects,
  512. std::ostream &ruleFileStream)
  513. {
  514. // Identify the language of the source file.
  515. const char* lang = this->GetSourceFileLanguage(source);
  516. if(!lang)
  517. {
  518. // If language is not known, this is an error.
  519. cmSystemTools::Error("Source file \"", source.GetFullPath().c_str(),
  520. "\" has unknown type.");
  521. return;
  522. }
  523. // Get the full path name of the object file.
  524. std::string obj = this->GetObjectFileName(target, source);
  525. // Avoid generating duplicate rules.
  526. if(m_ObjectFiles.find(obj) == m_ObjectFiles.end())
  527. {
  528. m_ObjectFiles.insert(obj);
  529. }
  530. else
  531. {
  532. cmOStringStream err;
  533. err << "Warning: Source file \""
  534. << source.GetSourceName().c_str() << "."
  535. << source.GetSourceExtension().c_str()
  536. << "\" is listed multiple times for target \"" << target.GetName()
  537. << "\".";
  538. cmSystemTools::Message(err.str().c_str(), "Warning");
  539. return;
  540. }
  541. // Create the directory containing the object file. This may be a
  542. // subdirectory under the target's directory.
  543. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  544. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  545. // Save this in the target's list of object files.
  546. objects.push_back(obj);
  547. std::string relativeObj = this->GetHomeRelativeOutputPath();
  548. relativeObj += obj;
  549. // we compute some depends when writing the depend.make that we will also
  550. // use in the build.make, same with depMakeFile
  551. std::vector<std::string> depends;
  552. std::string depMakeFile;
  553. // generate the build rule file
  554. this->WriteObjectBuildFile(obj, lang, target, source, depends,
  555. ruleFileStream);
  556. // The object file should be checked for dependency integrity.
  557. m_CheckDependFiles[target.GetName()][lang].insert(&source);
  558. // add this to the list of objects for this local generator
  559. m_LocalObjectFiles[cmSystemTools::GetFilenameName(obj)].push_back(&target);
  560. }
  561. //----------------------------------------------------------------------------
  562. void
  563. cmLocalUnixMakefileGenerator3
  564. ::GenerateCustomRuleFile(const cmCustomCommand& cc, const char *dir,
  565. std::ostream &ruleFileStream)
  566. {
  567. // Convert the output name to a relative path if possible.
  568. std::string output = this->Convert(cc.GetOutput(),START_OUTPUT);
  569. // Collect the commands.
  570. std::vector<std::string> commands;
  571. std::string preEcho = "Generating ";
  572. preEcho += output;
  573. this->AppendEcho(commands, preEcho.c_str());
  574. this->AppendCustomCommand(commands, cc);
  575. // Collect the dependencies.
  576. std::vector<std::string> depends;
  577. this->AppendCustomDepend(depends, cc);
  578. // Write the rule.
  579. const char* comment = 0;
  580. if(cc.GetComment() && *cc.GetComment())
  581. {
  582. comment = cc.GetComment();
  583. }
  584. this->WriteMakeRule(ruleFileStream, comment,
  585. cc.GetOutput(), depends, commands);
  586. }
  587. //----------------------------------------------------------------------------
  588. void
  589. cmLocalUnixMakefileGenerator3
  590. ::WriteUtilityRuleFiles(cmTarget& target)
  591. {
  592. // Create a directory for this target.
  593. std::string dir = this->GetTargetDirectory(target);
  594. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  595. // Construct the name of the rule file.
  596. std::string ruleFileName = dir;
  597. ruleFileName += "/build.make";
  598. // Open the rule file. This should be copy-if-different because the
  599. // rules may depend on this file itself.
  600. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  601. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  602. ruleFileStream.SetCopyIfDifferent(true);
  603. if(!ruleFileStream)
  604. {
  605. return;
  606. }
  607. this->WriteDisclaimer(ruleFileStream);
  608. this->WriteMakeVariables(ruleFileStream, HOME_OUTPUT);
  609. ruleFileStream
  610. << "# Utility rule file for " << target.GetName() << ".\n\n";
  611. // write the custom commands for this target
  612. std::vector<std::string> cleanFiles;
  613. if(const char* additional_clean_files =
  614. m_Makefile->GetProperty("ADDITIONAL_MAKE_CLEAN_FILES"))
  615. {
  616. cmSystemTools::ExpandListArgument(additional_clean_files, cleanFiles);
  617. }
  618. this->WriteCustomCommands(target,ruleFileStream, cleanFiles);
  619. // Collect the commands and dependencies.
  620. std::vector<std::string> commands;
  621. std::vector<std::string> depends;
  622. // Utility targets store their rules in pre- and post-build commands.
  623. this->AppendCustomDepends(depends, target.GetPreBuildCommands());
  624. this->AppendCustomDepends(depends, target.GetPostBuildCommands());
  625. this->AppendCustomCommands(commands, target.GetPreBuildCommands());
  626. this->AppendCustomCommands(commands, target.GetPostBuildCommands());
  627. // Add dependencies on targets that must be built first.
  628. this->AppendTargetDepends(depends, target);
  629. // Add a dependency on the rule file itself.
  630. std::string relPath = this->GetHomeRelativeOutputPath();
  631. std::string objTarget = relPath;
  632. objTarget += ruleFileName;
  633. this->AppendRuleDepend(depends, objTarget.c_str());
  634. // Write the rule.
  635. this->WriteMakeRule(ruleFileStream, 0,
  636. target.GetName(), depends, commands);
  637. // Write convenience targets.
  638. dir = m_Makefile->GetStartOutputDirectory();
  639. dir += "/";
  640. dir += this->GetTargetDirectory(target);
  641. std::string buildTargetRuleName = dir;
  642. buildTargetRuleName += "/build";
  643. buildTargetRuleName =
  644. this->Convert(buildTargetRuleName.c_str(),HOME_OUTPUT,MAKEFILE);
  645. this->WriteConvenienceRule(ruleFileStream, target.GetName(),
  646. buildTargetRuleName.c_str());
  647. // Write clean target
  648. this->WriteTargetCleanRule(ruleFileStream, target, cleanFiles);
  649. }
  650. //----------------------------------------------------------------------------
  651. bool
  652. cmLocalUnixMakefileGenerator3
  653. ::GenerateDependsMakeFile(const std::string& lang, const char* objFile)
  654. {
  655. // Construct a checker for the given language.
  656. std::auto_ptr<cmDepends>
  657. checker(this->GetDependsChecker(lang,false));
  658. if(checker.get())
  659. {
  660. // Check the dependencies. Ths is required because we need at least an
  661. // empty depends.make for make to include, so at cmake time the
  662. // ::Check() method will generate that if it does not exist
  663. checker->Check(objFile);
  664. return true;
  665. }
  666. return false;
  667. }
  668. //----------------------------------------------------------------------------
  669. void
  670. cmLocalUnixMakefileGenerator3
  671. ::WriteMakeRule(std::ostream& os,
  672. const char* comment,
  673. const char* target,
  674. const std::vector<std::string>& depends,
  675. const std::vector<std::string>& commands)
  676. {
  677. // Make sure there is a target.
  678. if(!target || !*target)
  679. {
  680. cmSystemTools::Error("No target for WriteMakeRule! called with comment: ",
  681. comment);
  682. return;
  683. }
  684. std::string replace;
  685. // Write the comment describing the rule in the makefile.
  686. if(comment)
  687. {
  688. replace = comment;
  689. std::string::size_type lpos = 0;
  690. std::string::size_type rpos;
  691. while((rpos = replace.find('\n', lpos)) != std::string::npos)
  692. {
  693. os << "# " << replace.substr(lpos, rpos-lpos) << "\n";
  694. lpos = rpos+1;
  695. }
  696. os << "# " << replace.substr(lpos) << "\n";
  697. }
  698. // Construct the left hand side of the rule.
  699. replace = target;
  700. std::string tgt = this->Convert(replace.c_str(),HOME_OUTPUT,MAKEFILE);
  701. tgt = this->ConvertToMakeTarget(tgt.c_str());
  702. const char* space = "";
  703. if(tgt.size() == 1)
  704. {
  705. // Add a space before the ":" to avoid drive letter confusion on
  706. // Windows.
  707. space = " ";
  708. }
  709. // Write the rule.
  710. if(depends.empty())
  711. {
  712. // No dependencies. The commands will always run.
  713. os << tgt.c_str() << space << ":\n";
  714. }
  715. else
  716. {
  717. // Split dependencies into multiple rule lines. This allows for
  718. // very long dependency lists even on older make implementations.
  719. for(std::vector<std::string>::const_iterator dep = depends.begin();
  720. dep != depends.end(); ++dep)
  721. {
  722. replace = *dep;
  723. replace = this->Convert(replace.c_str(),HOME_OUTPUT,MAKEFILE);
  724. replace = this->ConvertToMakeTarget(replace.c_str());
  725. os << tgt.c_str() << space << ": " << replace.c_str() << "\n";
  726. }
  727. }
  728. // Write the list of commands.
  729. for(std::vector<std::string>::const_iterator i = commands.begin();
  730. i != commands.end(); ++i)
  731. {
  732. replace = *i;
  733. os << "\t" << replace.c_str() << "\n";
  734. }
  735. os << "\n";
  736. }
  737. //----------------------------------------------------------------------------
  738. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  739. {
  740. os
  741. << "#======================================"
  742. << "=======================================\n";
  743. }
  744. //----------------------------------------------------------------------------
  745. void
  746. cmLocalUnixMakefileGenerator3
  747. ::WriteMakeVariables(std::ostream& makefileStream, RelativeRoot root)
  748. {
  749. this->WriteDivider(makefileStream);
  750. makefileStream
  751. << "# Set environment variables for the build.\n"
  752. << "\n";
  753. if(m_WindowsShell)
  754. {
  755. makefileStream
  756. << "!IF \"$(OS)\" == \"Windows_NT\"\n"
  757. << "NULL=\n"
  758. << "!ELSE\n"
  759. << "NULL=nul\n"
  760. << "!ENDIF\n";
  761. }
  762. else
  763. {
  764. makefileStream
  765. << "# The shell in which to execute make rules.\n"
  766. << "SHELL = /bin/sh\n"
  767. << "\n";
  768. }
  769. if(m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  770. {
  771. makefileStream
  772. << "# Produce verbose output by default.\n"
  773. << "VERBOSE = 1\n"
  774. << "\n";
  775. }
  776. std::string cmakecommand =
  777. this->ConvertToOutputForExisting(
  778. m_Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  779. makefileStream
  780. << "# The CMake executable.\n"
  781. << "CMAKE_COMMAND = "
  782. << this->Convert(cmakecommand.c_str(), root, MAKEFILE).c_str()
  783. << "\n"
  784. << "\n";
  785. makefileStream
  786. << "# The command to remove a file.\n"
  787. << "RM = "
  788. << this->Convert(cmakecommand.c_str(),root,SHELL).c_str()
  789. << " -E remove -f\n"
  790. << "\n";
  791. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  792. {
  793. makefileStream
  794. << "# The program to use to edit the cache.\n"
  795. << "CMAKE_EDIT_COMMAND = "
  796. << (this->ConvertToOutputForExisting(
  797. m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))) << "\n"
  798. << "\n";
  799. }
  800. makefileStream
  801. << "# The top-level source directory on which CMake was run.\n"
  802. << "CMAKE_SOURCE_DIR = "
  803. << this->Convert(m_Makefile->GetHomeDirectory(), root, SHELL)
  804. << "\n"
  805. << "\n";
  806. makefileStream
  807. << "# The top-level build directory on which CMake was run.\n"
  808. << "CMAKE_BINARY_DIR = "
  809. << this->Convert(m_Makefile->GetHomeOutputDirectory(), root, SHELL)
  810. << "\n"
  811. << "\n";
  812. }
  813. //----------------------------------------------------------------------------
  814. void
  815. cmLocalUnixMakefileGenerator3
  816. ::WriteSpecialTargetsTop(std::ostream& makefileStream)
  817. {
  818. this->WriteDivider(makefileStream);
  819. makefileStream
  820. << "# Special targets provided by cmake.\n"
  821. << "\n";
  822. // Write the main entry point target. This must be the VERY first
  823. // target so that make with no arguments will run it.
  824. {
  825. // Just depend on the all target to drive the build.
  826. std::vector<std::string> depends;
  827. std::vector<std::string> no_commands;
  828. depends.push_back("all");
  829. // Write the rule.
  830. this->WriteMakeRule(makefileStream,
  831. "Default target executed when no arguments are "
  832. "given to make.",
  833. "default_target",
  834. depends,
  835. no_commands);
  836. }
  837. // Write special "test" target to run ctest.
  838. if(m_Makefile->IsOn("CMAKE_TESTING_ENABLED"))
  839. {
  840. std::string ctest;
  841. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  842. {
  843. // We are building CMake itself. Use the ctest that comes with
  844. // this version of CMake instead of the one used to build it.
  845. ctest = m_ExecutableOutputPath;
  846. ctest += "ctest";
  847. ctest += cmSystemTools::GetExecutableExtension();
  848. ctest = this->Convert(ctest.c_str(),START_OUTPUT,SHELL);
  849. ctest += " --force-new-ctest-process";
  850. }
  851. else
  852. {
  853. // We are building another project. Use the ctest that comes with
  854. // the CMake building it.
  855. ctest = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  856. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  857. ctest += "/";
  858. ctest += "ctest";
  859. ctest += cmSystemTools::GetExecutableExtension();
  860. ctest = this->ConvertToOutputForExisting(ctest.c_str());
  861. }
  862. std::vector<std::string> no_depends;
  863. std::vector<std::string> commands;
  864. this->AppendEcho(commands, "Running tests...");
  865. ctest += " $(ARGS)";
  866. commands.push_back(ctest);
  867. this->WriteMakeRule(makefileStream,
  868. "Special rule to drive testing with ctest.",
  869. "test", no_depends, commands);
  870. }
  871. // Write special "install" target to run cmake_install.cmake script.
  872. {
  873. std::vector<std::string> depends;
  874. std::vector<std::string> commands;
  875. std::string cmd;
  876. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  877. {
  878. // We are building CMake itself. We cannot use the original
  879. // executable to install over itself.
  880. cmd = m_ExecutableOutputPath;
  881. cmd += "cmake";
  882. cmd = this->Convert(cmd.c_str(),START_OUTPUT,SHELL);
  883. }
  884. else
  885. {
  886. cmd = "$(CMAKE_COMMAND)";
  887. }
  888. cmd += " -P cmake_install.cmake";
  889. commands.push_back(cmd);
  890. const char* noall =
  891. m_Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  892. if(!noall || cmSystemTools::IsOff(noall))
  893. {
  894. // Drive the build before installing.
  895. depends.push_back("all");
  896. }
  897. this->WriteMakeRule(makefileStream,
  898. "Special rule to run installation script.",
  899. "install", depends, commands);
  900. }
  901. // Write special "rebuild_cache" target to re-run cmake.
  902. {
  903. std::vector<std::string> no_depends;
  904. std::vector<std::string> commands;
  905. this->AppendEcho(commands, "Running CMake to regenerate build system...");
  906. commands.push_back(
  907. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  908. this->WriteMakeRule(makefileStream,
  909. "Special rule to re-run CMake using make.",
  910. "rebuild_cache",
  911. no_depends,
  912. commands);
  913. }
  914. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  915. // Otherwise default to the interactive command-line interface.
  916. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  917. {
  918. std::vector<std::string> no_depends;
  919. std::vector<std::string> commands;
  920. this->AppendEcho(commands, "Running CMake cache editor...");
  921. commands.push_back(
  922. "$(CMAKE_EDIT_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  923. this->WriteMakeRule(makefileStream,
  924. "Special rule to re-run CMake cache editor using make.",
  925. "edit_cache",
  926. no_depends,
  927. commands);
  928. }
  929. else
  930. {
  931. std::vector<std::string> no_depends;
  932. std::vector<std::string> commands;
  933. this->AppendEcho(commands,
  934. "Running interactive CMake command-line interface...");
  935. commands.push_back(
  936. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  937. this->WriteMakeRule(makefileStream,
  938. "Special rule to re-run CMake cache editor using make.",
  939. "edit_cache",
  940. no_depends,
  941. commands);
  942. }
  943. }
  944. //----------------------------------------------------------------------------
  945. void
  946. cmLocalUnixMakefileGenerator3
  947. ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
  948. {
  949. this->WriteDivider(makefileStream);
  950. makefileStream
  951. << "# Special targets to cleanup operation of make.\n"
  952. << "\n";
  953. // Write special "cmake_check_build_system" target to run cmake with
  954. // the --check-build-system flag.
  955. {
  956. // Build command to run CMake to check if anything needs regenerating.
  957. std::string cmakefileName = "Makefile.cmake";
  958. std::string runRule =
  959. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  960. runRule += " --check-build-system ";
  961. runRule += this->Convert(cmakefileName.c_str(),NONE,SHELL);
  962. runRule += " 0";
  963. std::vector<std::string> no_depends;
  964. std::vector<std::string> commands;
  965. commands.push_back(runRule);
  966. this->WriteMakeRule(makefileStream,
  967. "Special rule to run CMake to check the build system "
  968. "integrity.\n"
  969. "No rule that depends on this can have "
  970. "commands that come from listfiles\n"
  971. "because they might be regenerated.",
  972. "cmake_check_build_system",
  973. no_depends,
  974. commands);
  975. }
  976. std::vector<std::string> no_commands;
  977. // Write special target to silence make output. This must be after
  978. // the default target in case VERBOSE is set (which changes the
  979. // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
  980. // "VERBOSE=1" to be added as a make variable which will change the
  981. // name of this special target. This gives a make-time choice to
  982. // the user.
  983. std::vector<std::string> no_depends;
  984. this->WriteMakeRule(makefileStream,
  985. "Suppress display of executed commands.",
  986. "$(VERBOSE).SILENT",
  987. no_depends,
  988. no_commands);
  989. // Special target to cleanup operation of make tool.
  990. std::vector<std::string> depends;
  991. this->WriteMakeRule(makefileStream,
  992. "Disable implicit rules so canoncical targets will work.",
  993. ".SUFFIXES",
  994. depends,
  995. no_commands);
  996. // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
  997. depends.push_back(".hpux_make_needs_suffix_list");
  998. this->WriteMakeRule(makefileStream, 0,
  999. ".SUFFIXES", depends, no_commands);
  1000. }
  1001. //----------------------------------------------------------------------------
  1002. void
  1003. cmLocalUnixMakefileGenerator3
  1004. ::WriteConvenienceRule(std::ostream& ruleFileStream,
  1005. const char* realTarget,
  1006. const char* helpTarget)
  1007. {
  1008. // A rule is only needed if the names are different.
  1009. if(strcmp(realTarget, helpTarget) != 0)
  1010. {
  1011. // The helper target depends on the real target.
  1012. std::vector<std::string> depends;
  1013. depends.push_back(realTarget);
  1014. // There are no commands.
  1015. std::vector<std::string> no_commands;
  1016. // Write the rule.
  1017. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  1018. helpTarget, depends, no_commands);
  1019. }
  1020. }
  1021. //----------------------------------------------------------------------------
  1022. void
  1023. cmLocalUnixMakefileGenerator3
  1024. ::WriteTargetRequiresRule(std::ostream& ruleFileStream, cmTarget& target,
  1025. const std::vector<std::string>& objects)
  1026. {
  1027. std::vector<std::string> depends;
  1028. std::vector<std::string> no_commands;
  1029. // Construct the name of the dependency generation target.
  1030. std::string depTarget = this->GetRelativeTargetDirectory(target);
  1031. depTarget += "/requires";
  1032. // This target drives dependency generation for all object files.
  1033. std::string relPath = this->GetHomeRelativeOutputPath();
  1034. std::string objTarget;
  1035. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1036. obj != objects.end(); ++obj)
  1037. {
  1038. objTarget = relPath;
  1039. objTarget += *obj;
  1040. objTarget += ".requires";
  1041. depends.push_back(objTarget);
  1042. }
  1043. // Write the rule.
  1044. this->WriteMakeRule(ruleFileStream, 0,
  1045. depTarget.c_str(), depends, no_commands);
  1046. }
  1047. //----------------------------------------------------------------------------
  1048. void
  1049. cmLocalUnixMakefileGenerator3
  1050. ::WriteExecutableRule(std::ostream& ruleFileStream,
  1051. const char* ruleFileName,
  1052. cmTarget& target,
  1053. const std::vector<std::string>& objects,
  1054. const std::vector<std::string>& external_objects,
  1055. std::vector<std::string>& cleanFiles)
  1056. {
  1057. // Write the dependency generation rule.
  1058. this->WriteTargetDependRule(ruleFileStream, target);
  1059. std::vector<std::string> commands;
  1060. std::string relPath = this->GetHomeRelativeOutputPath();
  1061. std::string objTarget;
  1062. // Build list of dependencies.
  1063. std::vector<std::string> depends;
  1064. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1065. obj != objects.end(); ++obj)
  1066. {
  1067. objTarget = relPath;
  1068. objTarget += *obj;
  1069. depends.push_back(objTarget);
  1070. }
  1071. // Add dependencies on targets that must be built first.
  1072. this->AppendTargetDepends(depends, target);
  1073. // Add a dependency on the rule file itself.
  1074. objTarget = relPath;
  1075. objTarget += ruleFileName;
  1076. this->AppendRuleDepend(depends, objTarget.c_str());
  1077. // Construct the full path to the executable that will be generated.
  1078. std::string targetFullPath = m_ExecutableOutputPath;
  1079. if(targetFullPath.length() == 0)
  1080. {
  1081. targetFullPath = m_Makefile->GetStartOutputDirectory();
  1082. targetFullPath += "/";
  1083. }
  1084. #ifdef __APPLE__
  1085. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  1086. {
  1087. // Make bundle directories
  1088. targetFullPath += target.GetName();
  1089. targetFullPath += ".app/Contents/MacOS/";
  1090. }
  1091. #endif
  1092. // do we have a different executable name?
  1093. if (target.GetProperty("OUTPUT_NAME"))
  1094. {
  1095. targetFullPath += target.GetProperty("OUTPUT_NAME");
  1096. }
  1097. else
  1098. {
  1099. targetFullPath += target.GetName();
  1100. }
  1101. targetFullPath += cmSystemTools::GetExecutableExtension();
  1102. // Convert to the output path to use in constructing commands.
  1103. std::string targetOutPath = this->Convert(targetFullPath.c_str(),HOME_OUTPUT,MAKEFILE);
  1104. // Get the language to use for linking this executable.
  1105. const char* linkLanguage =
  1106. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1107. // Make sure we have a link language.
  1108. if(!linkLanguage)
  1109. {
  1110. cmSystemTools::Error("Cannot determine link language for target \"",
  1111. target.GetName(), "\".");
  1112. return;
  1113. }
  1114. // Add the link message.
  1115. std::string buildEcho = "Linking ";
  1116. buildEcho += linkLanguage;
  1117. buildEcho += " executable ";
  1118. buildEcho += targetOutPath;
  1119. this->AppendEcho(commands, buildEcho.c_str());
  1120. // Build a list of compiler flags and linker flags.
  1121. std::string flags;
  1122. std::string linkFlags;
  1123. #if 0
  1124. // Loop over all libraries and see if all are shared
  1125. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  1126. int AllShared = 2; // 0 = false, 1 = true, 2 = unknown
  1127. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1128. lib != tlibs.end(); ++lib)
  1129. {
  1130. // look up the target if there is one
  1131. cmTarget *libtgt = m_GlobalGenerator->FindTarget(0,lib->first.c_str());
  1132. if (libtgt)
  1133. {
  1134. if (libtgt->GetType() != cmTarget::SHARED_LIBRARY)
  1135. {
  1136. AllShared = 0;
  1137. }
  1138. else if (AllShared == 2)
  1139. {
  1140. AllShared = 1;
  1141. }
  1142. }
  1143. }
  1144. // if all libs were shared then add the special borland flag for linking an
  1145. // executable to only shared libs
  1146. if(AllShared == 1)
  1147. {
  1148. this->AppendFlags
  1149. (linkFlags,m_Makefile->GetDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS"));
  1150. }
  1151. #endif
  1152. // Add flags to deal with shared libraries. Any library being
  1153. // linked in might be shared, so always use shared flags for an
  1154. // executable.
  1155. this->AddSharedFlags(linkFlags, linkLanguage, true);
  1156. // Add flags to create an executable.
  1157. this->AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS");
  1158. if(target.GetPropertyAsBool("WIN32_EXECUTABLE"))
  1159. {
  1160. this->AppendFlags(linkFlags,
  1161. m_Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  1162. }
  1163. else
  1164. {
  1165. this->AppendFlags(linkFlags,
  1166. m_Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  1167. }
  1168. // Add language-specific flags.
  1169. this->AddLanguageFlags(flags, linkLanguage);
  1170. // Add target-specific linker flags.
  1171. this->AppendFlags(linkFlags, target.GetProperty("LINK_FLAGS"));
  1172. // Add the pre-build and pre-link rules.
  1173. this->AppendCustomCommands(commands, target.GetPreBuildCommands());
  1174. this->AppendCustomCommands(commands, target.GetPreLinkCommands());
  1175. // Construct the main link rule.
  1176. std::string linkRuleVar = "CMAKE_";
  1177. linkRuleVar += linkLanguage;
  1178. linkRuleVar += "_LINK_EXECUTABLE";
  1179. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  1180. cmSystemTools::ExpandListArgument(linkRule, commands);
  1181. // Add the post-build rules.
  1182. this->AppendCustomCommands(commands, target.GetPostBuildCommands());
  1183. // Collect up flags to link in needed libraries.
  1184. cmOStringStream linklibs;
  1185. this->OutputLinkLibraries(linklibs, 0, target);
  1186. // Construct object file lists that may be needed to expand the
  1187. // rule.
  1188. std::string variableName;
  1189. std::string variableNameExternal;
  1190. this->WriteObjectsVariable(ruleFileStream, target, objects, external_objects,
  1191. variableName, variableNameExternal);
  1192. std::string buildObjs = "$(";
  1193. buildObjs += variableName;
  1194. buildObjs += ") $(";
  1195. buildObjs += variableNameExternal;
  1196. buildObjs += ")";
  1197. std::string cleanObjs = "$(";
  1198. cleanObjs += variableName;
  1199. cleanObjs += ")";
  1200. // Expand placeholders in the commands.
  1201. for(std::vector<std::string>::iterator i = commands.begin();
  1202. i != commands.end(); ++i)
  1203. {
  1204. this->ExpandRuleVariables(*i,
  1205. linkLanguage,
  1206. buildObjs.c_str(),
  1207. targetOutPath.c_str(),
  1208. linklibs.str().c_str(),
  1209. 0,
  1210. 0,
  1211. flags.c_str(),
  1212. 0,
  1213. 0,
  1214. 0,
  1215. linkFlags.c_str());
  1216. }
  1217. // Write the build rule.
  1218. this->WriteMakeRule(ruleFileStream, 0,
  1219. targetFullPath.c_str(), depends, commands);
  1220. // Write convenience targets.
  1221. std::string dir = m_Makefile->GetStartOutputDirectory();
  1222. dir += "/";
  1223. dir += this->GetTargetDirectory(target);
  1224. std::string buildTargetRuleName = dir;
  1225. buildTargetRuleName += "/build";
  1226. buildTargetRuleName =
  1227. this->Convert(buildTargetRuleName.c_str(),HOME_OUTPUT,MAKEFILE);
  1228. this->WriteConvenienceRule(ruleFileStream, targetFullPath.c_str(),
  1229. buildTargetRuleName.c_str());
  1230. cleanFiles.push_back(cleanObjs);
  1231. cleanFiles.push_back
  1232. (this->Convert(targetFullPath.c_str(),HOME_OUTPUT,MAKEFILE));
  1233. }
  1234. //----------------------------------------------------------------------------
  1235. void
  1236. cmLocalUnixMakefileGenerator3
  1237. ::WriteStaticLibraryRule(std::ostream& ruleFileStream,
  1238. const char* ruleFileName,
  1239. cmTarget& target,
  1240. const std::vector<std::string>& objects,
  1241. const std::vector<std::string>& external_objects,
  1242. std::vector<std::string>& cleanFiles)
  1243. {
  1244. const char* linkLanguage =
  1245. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1246. std::string linkRuleVar = "CMAKE_";
  1247. if (linkLanguage)
  1248. {
  1249. linkRuleVar += linkLanguage;
  1250. }
  1251. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  1252. std::string extraFlags;
  1253. this->AppendFlags(extraFlags, target.GetProperty("STATIC_LIBRARY_FLAGS"));
  1254. this->WriteLibraryRule(ruleFileStream, ruleFileName, target,
  1255. objects, external_objects,
  1256. linkRuleVar.c_str(), extraFlags.c_str(),cleanFiles);
  1257. }
  1258. //----------------------------------------------------------------------------
  1259. void
  1260. cmLocalUnixMakefileGenerator3
  1261. ::WriteSharedLibraryRule(std::ostream& ruleFileStream,
  1262. const char* ruleFileName,
  1263. cmTarget& target,
  1264. const std::vector<std::string>& objects,
  1265. const std::vector<std::string>& external_objects,
  1266. std::vector<std::string>& cleanFiles)
  1267. {
  1268. const char* linkLanguage =
  1269. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1270. std::string linkRuleVar = "CMAKE_";
  1271. if (linkLanguage)
  1272. {
  1273. linkRuleVar += linkLanguage;
  1274. }
  1275. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  1276. std::string extraFlags;
  1277. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  1278. this->AddConfigVariableFlags(extraFlags, "CMAKE_SHARED_LINKER_FLAGS");
  1279. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1280. {
  1281. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1282. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1283. i != sources.end(); ++i)
  1284. {
  1285. if((*i)->GetSourceExtension() == "def")
  1286. {
  1287. extraFlags += " ";
  1288. extraFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1289. extraFlags +=
  1290. this->Convert((*i)->GetFullPath().c_str(),HOME_OUTPUT,MAKEFILE);
  1291. }
  1292. }
  1293. }
  1294. this->WriteLibraryRule(ruleFileStream, ruleFileName, target,
  1295. objects, external_objects,
  1296. linkRuleVar.c_str(), extraFlags.c_str(), cleanFiles);
  1297. }
  1298. //----------------------------------------------------------------------------
  1299. void
  1300. cmLocalUnixMakefileGenerator3
  1301. ::WriteModuleLibraryRule(std::ostream& ruleFileStream,
  1302. const char* ruleFileName,
  1303. cmTarget& target,
  1304. const std::vector<std::string>& objects,
  1305. const std::vector<std::string>& external_objects,
  1306. std::vector<std::string>& cleanFiles)
  1307. {
  1308. const char* linkLanguage =
  1309. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1310. std::string linkRuleVar = "CMAKE_";
  1311. if (linkLanguage)
  1312. {
  1313. linkRuleVar += linkLanguage;
  1314. }
  1315. linkRuleVar += "_CREATE_SHARED_MODULE";
  1316. std::string extraFlags;
  1317. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  1318. this->AddConfigVariableFlags(extraFlags, "CMAKE_MODULE_LINKER_FLAGS");
  1319. // TODO: .def files should be supported here also.
  1320. this->WriteLibraryRule(ruleFileStream, ruleFileName, target,
  1321. objects, external_objects,
  1322. linkRuleVar.c_str(), extraFlags.c_str(), cleanFiles);
  1323. }
  1324. //----------------------------------------------------------------------------
  1325. void
  1326. cmLocalUnixMakefileGenerator3
  1327. ::WriteLibraryRule(std::ostream& ruleFileStream,
  1328. const char* ruleFileName,
  1329. cmTarget& target,
  1330. const std::vector<std::string>& objects,
  1331. const std::vector<std::string>& external_objects,
  1332. const char* linkRuleVar,
  1333. const char* extraFlags,
  1334. std::vector<std::string>& cleanFiles)
  1335. {
  1336. // Write the dependency generation rule.
  1337. this->WriteTargetDependRule(ruleFileStream, target);
  1338. // TODO: Merge the methods that call this method to avoid
  1339. // code duplication.
  1340. std::vector<std::string> commands;
  1341. std::string relPath = this->GetHomeRelativeOutputPath();
  1342. std::string objTarget;
  1343. // Build list of dependencies.
  1344. std::vector<std::string> depends;
  1345. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1346. obj != objects.end(); ++obj)
  1347. {
  1348. objTarget = relPath;
  1349. objTarget += *obj;
  1350. depends.push_back(objTarget);
  1351. }
  1352. // Add dependencies on targets that must be built first.
  1353. this->AppendTargetDepends(depends, target);
  1354. // Add a dependency on the rule file itself.
  1355. objTarget = relPath;
  1356. objTarget += ruleFileName;
  1357. this->AppendRuleDepend(depends, objTarget.c_str());
  1358. // from here up is the same for exe or lib
  1359. for(std::vector<std::string>::const_iterator obj = external_objects.begin();
  1360. obj != external_objects.end(); ++obj)
  1361. {
  1362. depends.push_back(*obj);
  1363. }
  1364. // Get the language to use for linking this library.
  1365. const char* linkLanguage =
  1366. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1367. // Make sure we have a link language.
  1368. if(!linkLanguage)
  1369. {
  1370. cmSystemTools::Error("Cannot determine link language for target \"",
  1371. target.GetName(), "\".");
  1372. return;
  1373. }
  1374. // Create set of linking flags.
  1375. std::string linkFlags;
  1376. this->AppendFlags(linkFlags, extraFlags);
  1377. // Construct the name of the library.
  1378. std::string targetName;
  1379. std::string targetNameSO;
  1380. std::string targetNameReal;
  1381. std::string targetNameBase;
  1382. target.GetLibraryNames(m_Makefile,
  1383. targetName, targetNameSO,
  1384. targetNameReal, targetNameBase);
  1385. // Construct the full path version of the names.
  1386. std::string outpath = m_LibraryOutputPath;
  1387. if(outpath.length() == 0)
  1388. {
  1389. outpath = m_Makefile->GetStartOutputDirectory();
  1390. outpath += "/";
  1391. }
  1392. std::string targetFullPath = outpath + targetName;
  1393. std::string targetFullPathSO = outpath + targetNameSO;
  1394. std::string targetFullPathReal = outpath + targetNameReal;
  1395. std::string targetFullPathBase = outpath + targetNameBase;
  1396. // Construct the output path version of the names for use in command
  1397. // arguments.
  1398. std::string targetOutPath =
  1399. this->Convert(targetFullPath.c_str(),HOME_OUTPUT,MAKEFILE);
  1400. std::string targetOutPathSO =
  1401. this->Convert(targetFullPathSO.c_str(),HOME_OUTPUT,MAKEFILE);
  1402. std::string targetOutPathReal =
  1403. this->Convert(targetFullPathReal.c_str(),HOME_OUTPUT,MAKEFILE);
  1404. std::string targetOutPathBase =
  1405. this->Convert(targetFullPathBase.c_str(),HOME_OUTPUT,MAKEFILE);
  1406. // Add the link message.
  1407. std::string buildEcho = "Linking ";
  1408. buildEcho += linkLanguage;
  1409. switch(target.GetType())
  1410. {
  1411. case cmTarget::STATIC_LIBRARY:
  1412. buildEcho += " static library "; break;
  1413. case cmTarget::SHARED_LIBRARY:
  1414. buildEcho += " shared library "; break;
  1415. case cmTarget::MODULE_LIBRARY:
  1416. buildEcho += " shared module "; break;
  1417. default:
  1418. buildEcho += " library "; break;
  1419. }
  1420. buildEcho += targetOutPath.c_str();
  1421. this->AppendEcho(commands, buildEcho.c_str());
  1422. // Construct a list of files associated with this library that may
  1423. // need to be cleaned.
  1424. std::vector<std::string> libCleanFiles;
  1425. {
  1426. std::string cleanStaticName;
  1427. std::string cleanSharedName;
  1428. std::string cleanSharedSOName;
  1429. std::string cleanSharedRealName;
  1430. target.GetLibraryCleanNames(m_Makefile,
  1431. cleanStaticName,
  1432. cleanSharedName,
  1433. cleanSharedSOName,
  1434. cleanSharedRealName);
  1435. std::string cleanFullStaticName = outpath + cleanStaticName;
  1436. std::string cleanFullSharedName = outpath + cleanSharedName;
  1437. std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
  1438. std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
  1439. libCleanFiles.push_back
  1440. (this->Convert(cleanFullStaticName.c_str(),HOME_OUTPUT,MAKEFILE));
  1441. if(cleanSharedRealName != cleanStaticName)
  1442. {
  1443. libCleanFiles.push_back
  1444. (this->Convert(cleanFullSharedRealName.c_str(),HOME_OUTPUT,MAKEFILE));
  1445. }
  1446. if(cleanSharedSOName != cleanStaticName &&
  1447. cleanSharedSOName != cleanSharedRealName)
  1448. {
  1449. libCleanFiles.push_back
  1450. (this->Convert(cleanFullSharedSOName.c_str(),HOME_OUTPUT,MAKEFILE));
  1451. }
  1452. if(cleanSharedName != cleanStaticName &&
  1453. cleanSharedName != cleanSharedSOName &&
  1454. cleanSharedName != cleanSharedRealName)
  1455. {
  1456. libCleanFiles.push_back
  1457. (this->Convert(cleanFullSharedName.c_str(),HOME_OUTPUT,MAKEFILE));
  1458. }
  1459. }
  1460. // Add a command to remove any existing files for this library.
  1461. this->AppendCleanCommand(commands, libCleanFiles);
  1462. // Add the pre-build and pre-link rules.
  1463. this->AppendCustomCommands(commands, target.GetPreBuildCommands());
  1464. this->AppendCustomCommands(commands, target.GetPreLinkCommands());
  1465. // Construct the main link rule.
  1466. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar);
  1467. cmSystemTools::ExpandListArgument(linkRule, commands);
  1468. // Add a rule to create necessary symlinks for the library.
  1469. if(targetOutPath != targetOutPathReal)
  1470. {
  1471. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  1472. symlink += targetOutPathReal;
  1473. symlink += " ";
  1474. symlink += targetOutPathSO;
  1475. symlink += " ";
  1476. symlink += targetOutPath;
  1477. commands.push_back(symlink);
  1478. }
  1479. // Add the post-build rules.
  1480. this->AppendCustomCommands(commands, target.GetPostBuildCommands());
  1481. // Collect up flags to link in needed libraries.
  1482. cmOStringStream linklibs;
  1483. this->OutputLinkLibraries(linklibs, target.GetName(), target);
  1484. // Construct object file lists that may be needed to expand the
  1485. // rule.
  1486. std::string variableName;
  1487. std::string variableNameExternal;
  1488. this->WriteObjectsVariable(ruleFileStream, target, objects, external_objects,
  1489. variableName, variableNameExternal);
  1490. std::string buildObjs = "$(";
  1491. buildObjs += variableName;
  1492. buildObjs += ") $(";
  1493. buildObjs += variableNameExternal;
  1494. buildObjs += ")";
  1495. std::string cleanObjs = "$(";
  1496. cleanObjs += variableName;
  1497. cleanObjs += ")";
  1498. // Expand placeholders in the commands.
  1499. for(std::vector<std::string>::iterator i = commands.begin();
  1500. i != commands.end(); ++i)
  1501. {
  1502. this->ExpandRuleVariables(*i,
  1503. linkLanguage,
  1504. buildObjs.c_str(),
  1505. targetOutPathReal.c_str(),
  1506. linklibs.str().c_str(),
  1507. 0, 0, 0, buildObjs.c_str(),
  1508. targetOutPathBase.c_str(),
  1509. targetNameSO.c_str(),
  1510. linkFlags.c_str());
  1511. }
  1512. // from here down is the same for exe or lib
  1513. // Write the build rule.
  1514. this->WriteMakeRule(ruleFileStream, 0,
  1515. targetFullPath.c_str(), depends, commands);
  1516. // Write convenience targets.
  1517. std::string dir = m_Makefile->GetStartOutputDirectory();
  1518. dir += "/";
  1519. dir += this->GetTargetDirectory(target);
  1520. std::string buildTargetRuleName = dir;
  1521. buildTargetRuleName += "/build";
  1522. buildTargetRuleName =
  1523. this->Convert(buildTargetRuleName.c_str(),HOME_OUTPUT,MAKEFILE);
  1524. this->WriteConvenienceRule(ruleFileStream, targetFullPath.c_str(),
  1525. buildTargetRuleName.c_str());
  1526. // Clean all the possible library names and symlinks and object files.
  1527. cleanFiles.insert(cleanFiles.end(),libCleanFiles.begin(),libCleanFiles.end());
  1528. cleanFiles.push_back(cleanObjs);
  1529. }
  1530. //----------------------------------------------------------------------------
  1531. void
  1532. cmLocalUnixMakefileGenerator3
  1533. ::WriteObjectsVariable(std::ostream& ruleFileStream,
  1534. cmTarget& target,
  1535. const std::vector<std::string>& objects,
  1536. const std::vector<std::string>& external_objects,
  1537. std::string& variableName,
  1538. std::string& variableNameExternal)
  1539. {
  1540. // Write a make variable assignment that lists all objects for the
  1541. // target.
  1542. variableName = this->CreateMakeVariable(target.GetName(), "_OBJECTS");
  1543. ruleFileStream
  1544. << "# Object files for target " << target.GetName() << "\n"
  1545. << variableName.c_str() << " =";
  1546. std::string relPath = this->GetHomeRelativeOutputPath();
  1547. std::string object;
  1548. for(std::vector<std::string>::const_iterator i = objects.begin();
  1549. i != objects.end(); ++i)
  1550. {
  1551. object = relPath;
  1552. object += *i;
  1553. ruleFileStream
  1554. << " \\\n"
  1555. << this->ConvertToQuotedOutputPath(object.c_str());
  1556. }
  1557. ruleFileStream
  1558. << "\n";
  1559. // Write a make variable assignment that lists all external objects
  1560. // for the target.
  1561. variableNameExternal = this->CreateMakeVariable(target.GetName(),
  1562. "_EXTERNAL_OBJECTS");
  1563. ruleFileStream
  1564. << "\n"
  1565. << "# External object files for target " << target.GetName() << "\n"
  1566. << variableNameExternal.c_str() << " =";
  1567. for(std::vector<std::string>::const_iterator i = external_objects.begin();
  1568. i != external_objects.end(); ++i)
  1569. {
  1570. object = this->Convert(i->c_str(),HOME_OUTPUT);
  1571. ruleFileStream
  1572. << " \\\n"
  1573. << this->ConvertToQuotedOutputPath(object.c_str());
  1574. }
  1575. ruleFileStream
  1576. << "\n"
  1577. << "\n";
  1578. }
  1579. //----------------------------------------------------------------------------
  1580. void
  1581. cmLocalUnixMakefileGenerator3
  1582. ::WriteTargetDependRule(std::ostream& ruleFileStream, cmTarget& target)
  1583. {
  1584. // must write the targets depend info file
  1585. std::string dir = this->GetTargetDirectory(target);
  1586. std::string infoFileName = dir;
  1587. infoFileName += "/DependInfo.cmake";
  1588. std::string ruleFileNameFull = this->ConvertToFullPath(infoFileName);
  1589. cmGeneratedFileStream infoFileStream(ruleFileNameFull.c_str());
  1590. infoFileStream.SetCopyIfDifferent(true);
  1591. if(!infoFileStream)
  1592. {
  1593. return;
  1594. }
  1595. cmGlobalUnixMakefileGenerator3 *gg =
  1596. static_cast<cmGlobalUnixMakefileGenerator3 *>(m_GlobalGenerator);
  1597. this->WriteDependLanguageInfo(infoFileStream,target);
  1598. // and now write the rule to use it
  1599. std::vector<std::string> depends;
  1600. std::vector<std::string> commands;
  1601. // Construct the name of the dependency generation target.
  1602. std::string depTarget = this->GetRelativeTargetDirectory(target);
  1603. depTarget += "/depend";
  1604. std::string depMark = depTarget;
  1605. depMark += ".make.mark";
  1606. depends.push_back(depMark);
  1607. this->WriteMakeRule(ruleFileStream, 0,
  1608. depTarget.c_str(), depends, commands);
  1609. depends.clear();
  1610. // Write the dependency generation rule.
  1611. std::string depEcho = "Scanning dependencies of target ";
  1612. depEcho += target.GetName();
  1613. this->AppendEcho(commands, depEcho.c_str());
  1614. // Add a command to call CMake to scan dependencies. CMake will
  1615. // touch the corresponding depends file after scanning dependencies.
  1616. cmOStringStream depCmd;
  1617. // TODO: Account for source file properties and directory-level
  1618. // definitions when scanning for dependencies.
  1619. depCmd << "$(CMAKE_COMMAND) -E cmake_depends "
  1620. << " \""
  1621. << m_GlobalGenerator->GetName() << "\" "
  1622. << this->Convert(m_Makefile->GetHomeOutputDirectory(),FULL,SHELL)
  1623. << " "
  1624. << this->Convert(m_Makefile->GetStartOutputDirectory(),FULL,SHELL)
  1625. << " "
  1626. << this->Convert(ruleFileNameFull.c_str(),FULL,SHELL);
  1627. commands.push_back(depCmd.str());
  1628. // Write the rule.
  1629. this->WriteMakeRule(ruleFileStream, 0,
  1630. depMark.c_str(), depends, commands);
  1631. }
  1632. //----------------------------------------------------------------------------
  1633. void
  1634. cmLocalUnixMakefileGenerator3
  1635. ::WriteTargetCleanRule(std::ostream& ruleFileStream,
  1636. cmTarget& target,
  1637. const std::vector<std::string>& files)
  1638. {
  1639. std::vector<std::string> no_depends;
  1640. std::vector<std::string> commands;
  1641. // Construct the clean target name.
  1642. std::string cleanTarget = this->GetRelativeTargetDirectory(target);
  1643. cleanTarget += "/clean";
  1644. // Construct the clean command.
  1645. this->AppendCleanCommand(commands, files);
  1646. // Write the rule.
  1647. this->WriteMakeRule(ruleFileStream, 0,
  1648. cleanTarget.c_str(), no_depends, commands);
  1649. }
  1650. //----------------------------------------------------------------------------
  1651. void
  1652. cmLocalUnixMakefileGenerator3
  1653. ::WriteCMakeArgument(std::ostream& os, const char* s)
  1654. {
  1655. // Write the given string to the stream with escaping to get it back
  1656. // into CMake through the lexical scanner.
  1657. os << "\"";
  1658. for(const char* c = s; *c; ++c)
  1659. {
  1660. if(*c == '\\')
  1661. {
  1662. os << "\\\\";
  1663. }
  1664. else if(*c == '"')
  1665. {
  1666. os << "\\\"";
  1667. }
  1668. else
  1669. {
  1670. os << *c;
  1671. }
  1672. }
  1673. os << "\"";
  1674. }
  1675. //----------------------------------------------------------------------------
  1676. std::string
  1677. cmLocalUnixMakefileGenerator3::GetTargetDirectory(cmTarget& target)
  1678. {
  1679. std::string dir;
  1680. // Put a prefix on the name if one is given by the CMake code.
  1681. if(const char* prefix = m_Makefile->GetDefinition("CMAKE_TARGET_DIR_PREFIX"))
  1682. {
  1683. dir += prefix;
  1684. }
  1685. dir += target.GetName();
  1686. dir += ".dir";
  1687. return dir;
  1688. }
  1689. //----------------------------------------------------------------------------
  1690. std::string
  1691. cmLocalUnixMakefileGenerator3::GetRelativeTargetDirectory(cmTarget& target)
  1692. {
  1693. std::string dir = m_Makefile->GetStartOutputDirectory();
  1694. dir += "/";
  1695. dir += this->GetTargetDirectory(target);
  1696. dir = cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(), dir.c_str());
  1697. return this->Convert(dir.c_str(),NONE,MAKEFILE);
  1698. }
  1699. //----------------------------------------------------------------------------
  1700. std::string
  1701. cmLocalUnixMakefileGenerator3
  1702. ::GetSubdirTargetName(const char* pass, const char* subdir)
  1703. {
  1704. // Convert the subdirectory name to a relative path to keep it short.
  1705. std::string reldir = this->Convert(subdir,START_OUTPUT);
  1706. // Convert the subdirectory name to a valid make target name.
  1707. std::string s = pass;
  1708. s += "_";
  1709. s += reldir;
  1710. // Replace "../" with 3 underscores. This allows one .. at the beginning.
  1711. size_t pos = s.find("../");
  1712. if(pos != std::string::npos)
  1713. {
  1714. s.replace(pos, 3, "___");
  1715. }
  1716. // Replace "/" directory separators with a single underscore.
  1717. while((pos = s.find('/')) != std::string::npos)
  1718. {
  1719. s.replace(pos, 1, "_");
  1720. }
  1721. // Replace ":" drive specifier with a single underscore
  1722. while((pos = s.find(':')) != std::string::npos)
  1723. {
  1724. s.replace(pos, 1, "_");
  1725. }
  1726. return s;
  1727. }
  1728. //----------------------------------------------------------------------------
  1729. std::string
  1730. cmLocalUnixMakefileGenerator3
  1731. ::GetObjectFileName(cmTarget& target,
  1732. const cmSourceFile& source)
  1733. {
  1734. // If the full path to the source file includes this directory,
  1735. // we want to use the relative path for the filename of the
  1736. // object file. Otherwise, we will use just the filename
  1737. // portion.
  1738. std::string objectName;
  1739. if((cmSystemTools::GetFilenamePath(
  1740. source.GetFullPath()).find(
  1741. m_Makefile->GetCurrentDirectory()) == 0)
  1742. || (cmSystemTools::GetFilenamePath(
  1743. source.GetFullPath()).find(
  1744. m_Makefile->GetStartOutputDirectory()) == 0))
  1745. {
  1746. objectName = source.GetSourceName();
  1747. }
  1748. else
  1749. {
  1750. objectName = cmSystemTools::GetFilenameName(source.GetSourceName());
  1751. }
  1752. // Append the object file extension.
  1753. objectName +=
  1754. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  1755. source.GetSourceExtension().c_str());
  1756. // Convert to a safe name.
  1757. objectName = this->CreateSafeUniqueObjectFileName(objectName.c_str());
  1758. // Prepend the target directory.
  1759. std::string obj = this->GetTargetDirectory(target);
  1760. obj += "/";
  1761. obj += objectName;
  1762. return obj;
  1763. }
  1764. //----------------------------------------------------------------------------
  1765. const char*
  1766. cmLocalUnixMakefileGenerator3
  1767. ::GetSourceFileLanguage(const cmSourceFile& source)
  1768. {
  1769. // Identify the language of the source file.
  1770. return (m_GlobalGenerator
  1771. ->GetLanguageFromExtension(source.GetSourceExtension().c_str()));
  1772. }
  1773. //----------------------------------------------------------------------------
  1774. std::string
  1775. cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p)
  1776. {
  1777. // Split the path into its components.
  1778. std::vector<std::string> components;
  1779. cmSystemTools::SplitPath(p, components);
  1780. // Return an empty path if there are no components.
  1781. if(components.empty())
  1782. {
  1783. return "\"\"";
  1784. }
  1785. // Choose a slash direction and fix root component.
  1786. const char* slash = "/";
  1787. #if defined(_WIN32) && !defined(__CYGWIN__)
  1788. if(!cmSystemTools::GetForceUnixPaths())
  1789. {
  1790. slash = "\\";
  1791. for(std::string::iterator i = components[0].begin();
  1792. i != components[0].end(); ++i)
  1793. {
  1794. if(*i == '/')
  1795. {
  1796. *i = '\\';
  1797. }
  1798. }
  1799. }
  1800. #endif
  1801. // Begin the quoted result with the root component.
  1802. std::string result = "\"";
  1803. result += components[0];
  1804. // Now add the rest of the components separated by the proper slash
  1805. // direction for this platform.
  1806. bool first = true;
  1807. for(unsigned int i=1; i < components.size(); ++i)
  1808. {
  1809. // Only the last component can be empty to avoid double slashes.
  1810. if(components[i].length() > 0 || (i == (components.size()-1)))
  1811. {
  1812. if(!first)
  1813. {
  1814. result += slash;
  1815. }
  1816. result += components[i];
  1817. first = false;
  1818. }
  1819. }
  1820. // Close the quoted result.
  1821. result += "\"";
  1822. return result;
  1823. }
  1824. //----------------------------------------------------------------------------
  1825. void
  1826. cmLocalUnixMakefileGenerator3
  1827. ::AppendTargetDepends(std::vector<std::string>& depends,
  1828. cmTarget& target)
  1829. {
  1830. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  1831. {
  1832. return;
  1833. }
  1834. // Keep track of dependencies already listed.
  1835. std::set<cmStdString> emitted;
  1836. // A target should not depend on itself.
  1837. emitted.insert(target.GetName());
  1838. // Loop over all library dependencies.
  1839. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  1840. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1841. lib != tlibs.end(); ++lib)
  1842. {
  1843. // Don't emit the same library twice for this target.
  1844. if(emitted.insert(lib->first).second)
  1845. {
  1846. // Add this dependency.
  1847. this->AppendAnyDepend(depends, lib->first.c_str());
  1848. }
  1849. }
  1850. }
  1851. //----------------------------------------------------------------------------
  1852. void
  1853. cmLocalUnixMakefileGenerator3
  1854. ::AppendAnyDepend(std::vector<std::string>& depends, const char* name,
  1855. bool assume_unknown_is_file)
  1856. {
  1857. // There are a few cases for the name of the target:
  1858. // - CMake target in this directory: depend on it.
  1859. // - CMake target in another directory: depend and add jump-and-build.
  1860. // - Full path to a file: depend on it.
  1861. // - Other format (like -lm): do nothing.
  1862. // If it is an executable or library target there will be a
  1863. // definition for it.
  1864. std::string dirVar = name;
  1865. dirVar += "_CMAKE_PATH";
  1866. const char* dir = m_Makefile->GetDefinition(dirVar.c_str());
  1867. if(dir && *dir)
  1868. {
  1869. // This is a CMake target somewhere in this project.
  1870. // Get the type of the library. If it does not have a type then
  1871. // it is an executable.
  1872. std::string typeVar = name;
  1873. typeVar += "_LIBRARY_TYPE";
  1874. const char* libType = m_Makefile->GetDefinition(typeVar.c_str());
  1875. // Get the output path for this target type.
  1876. std::string tgtOutputPath;
  1877. if(libType)
  1878. {
  1879. tgtOutputPath = m_LibraryOutputPath;
  1880. }
  1881. else
  1882. {
  1883. tgtOutputPath = m_ExecutableOutputPath;
  1884. }
  1885. // Get the path to the target.
  1886. std::string tgtPath;
  1887. if(tgtOutputPath.size())
  1888. {
  1889. tgtPath = tgtOutputPath;
  1890. }
  1891. else
  1892. {
  1893. tgtPath = dir;
  1894. tgtPath += "/";
  1895. }
  1896. // Add the name of the targets's file. This depends on the type
  1897. // of the target.
  1898. std::string prefix;
  1899. std::string suffix;
  1900. if(!libType)
  1901. {
  1902. suffix = cmSystemTools::GetExecutableExtension();
  1903. }
  1904. else if(strcmp(libType, "SHARED") == 0)
  1905. {
  1906. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1907. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1908. }
  1909. else if(strcmp(libType, "MODULE") == 0)
  1910. {
  1911. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1912. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1913. }
  1914. else if(strcmp(libType, "STATIC") == 0)
  1915. {
  1916. prefix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1917. suffix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1918. }
  1919. tgtPath += prefix;
  1920. tgtPath += name;
  1921. tgtPath += suffix;
  1922. // Add a dependency on the target.
  1923. depends.push_back(tgtPath.c_str());
  1924. }
  1925. else if(m_Makefile->GetTargets().find(name) !=
  1926. m_Makefile->GetTargets().end())
  1927. {
  1928. // This is a CMake target that is not an executable or library.
  1929. // It must be in this directory, so just depend on the name
  1930. // directly.
  1931. depends.push_back(name);
  1932. }
  1933. else if(cmSystemTools::FileIsFullPath(name))
  1934. {
  1935. // This is a path to a file. Just trust the listfile author that
  1936. // it will be present or there is a rule to build it.
  1937. depends.push_back(cmSystemTools::CollapseFullPath(name));
  1938. }
  1939. else if(assume_unknown_is_file)
  1940. {
  1941. // Just assume this is a file or make target that will be present.
  1942. depends.push_back(name);
  1943. }
  1944. }
  1945. //----------------------------------------------------------------------------
  1946. void
  1947. cmLocalUnixMakefileGenerator3
  1948. ::AppendRuleDepend(std::vector<std::string>& depends,
  1949. const char* ruleFileName)
  1950. {
  1951. // Add a dependency on the rule file itself unless an option to skip
  1952. // it is specifically enabled by the user or project.
  1953. const char* nodep = m_Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
  1954. if(!nodep || cmSystemTools::IsOff(nodep))
  1955. {
  1956. depends.push_back(ruleFileName);
  1957. }
  1958. }
  1959. //----------------------------------------------------------------------------
  1960. void
  1961. cmLocalUnixMakefileGenerator3
  1962. ::AppendCustomDepends(std::vector<std::string>& depends,
  1963. const std::vector<cmCustomCommand>& ccs)
  1964. {
  1965. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  1966. i != ccs.end(); ++i)
  1967. {
  1968. this->AppendCustomDepend(depends, *i);
  1969. }
  1970. }
  1971. //----------------------------------------------------------------------------
  1972. void
  1973. cmLocalUnixMakefileGenerator3
  1974. ::AppendCustomDepend(std::vector<std::string>& depends,
  1975. const cmCustomCommand& cc)
  1976. {
  1977. for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
  1978. d != cc.GetDepends().end(); ++d)
  1979. {
  1980. // Add this dependency.
  1981. this->AppendAnyDepend(depends, d->c_str());
  1982. }
  1983. }
  1984. //----------------------------------------------------------------------------
  1985. void
  1986. cmLocalUnixMakefileGenerator3
  1987. ::AppendCustomCommands(std::vector<std::string>& commands,
  1988. const std::vector<cmCustomCommand>& ccs)
  1989. {
  1990. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  1991. i != ccs.end(); ++i)
  1992. {
  1993. this->AppendCustomCommand(commands, *i);
  1994. }
  1995. }
  1996. //----------------------------------------------------------------------------
  1997. void
  1998. cmLocalUnixMakefileGenerator3
  1999. ::AppendCustomCommand(std::vector<std::string>& commands,
  2000. const cmCustomCommand& cc)
  2001. {
  2002. // TODO: Convert outputs/dependencies (arguments?) to relative paths.
  2003. std::vector<std::string> commands1;
  2004. // Add each command line to the set of commands.
  2005. for(cmCustomCommandLines::const_iterator cl = cc.GetCommandLines().begin();
  2006. cl != cc.GetCommandLines().end(); ++cl)
  2007. {
  2008. // Build the command line in a single string.
  2009. const cmCustomCommandLine& commandLine = *cl;
  2010. std::string cmd = commandLine[0];
  2011. if (cmd.size())
  2012. {
  2013. cmSystemTools::ReplaceString(cmd, "/./", "/");
  2014. cmd = this->Convert(cmd.c_str(),START_OUTPUT);
  2015. if(cmd.find("/") == cmd.npos &&
  2016. commandLine[0].find("/") != cmd.npos)
  2017. {
  2018. // Add a leading "./" for executables in the current directory.
  2019. cmd = "./" + cmd;
  2020. }
  2021. cmd = this->Convert(cmd.c_str(),NONE,SHELL);
  2022. for(unsigned int j=1; j < commandLine.size(); ++j)
  2023. {
  2024. cmd += " ";
  2025. cmd += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  2026. }
  2027. commands1.push_back(cmd);
  2028. }
  2029. }
  2030. // stick this group of commands into a cd of the proper path
  2031. // Build the jump-and-build command list.
  2032. if(m_WindowsShell)
  2033. {
  2034. // On Windows we must perform each step separately and then jump
  2035. // back because the shell keeps the working directory between
  2036. // commands.
  2037. std::string cmd = "cd ";
  2038. cmd += this->ConvertToOutputForExisting(m_Makefile->GetStartOutputDirectory());
  2039. commands.push_back(cmd);
  2040. // push back the custom commands
  2041. commands.insert(commands.end(), commands1.begin(), commands1.end());
  2042. // Jump back to the home directory.
  2043. cmd = "cd ";
  2044. cmd += this->ConvertToOutputForExisting(m_Makefile->GetHomeOutputDirectory());
  2045. commands.push_back(cmd);
  2046. }
  2047. else
  2048. {
  2049. // On UNIX we must construct a single shell command to jump and
  2050. // build because make resets the directory between each command.
  2051. std::string cmd = "cd ";
  2052. cmd += this->ConvertToOutputForExisting(m_Makefile->GetStartOutputDirectory());
  2053. // add the commands
  2054. unsigned int i;
  2055. for (i = 0; i < commands1.size(); ++i)
  2056. {
  2057. cmd += " && ";
  2058. cmd += commands1[i];
  2059. }
  2060. // Add the command as a single line.
  2061. commands.push_back(cmd);
  2062. }
  2063. }
  2064. //----------------------------------------------------------------------------
  2065. void
  2066. cmLocalUnixMakefileGenerator3
  2067. ::AppendCleanCommand(std::vector<std::string>& commands,
  2068. const std::vector<std::string>& files)
  2069. {
  2070. if(!files.empty())
  2071. {
  2072. std::string remove = "$(CMAKE_COMMAND) -E remove -f";
  2073. for(std::vector<std::string>::const_iterator f = files.begin();
  2074. f != files.end(); ++f)
  2075. {
  2076. remove += " ";
  2077. remove += this->Convert(f->c_str(),START_OUTPUT,SHELL);
  2078. }
  2079. commands.push_back(remove);
  2080. }
  2081. }
  2082. //----------------------------------------------------------------------------
  2083. void
  2084. cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
  2085. const char* text)
  2086. {
  2087. // Echo one line at a time.
  2088. std::string line;
  2089. for(const char* c = text;; ++c)
  2090. {
  2091. if(*c == '\n' || *c == '\0')
  2092. {
  2093. // Avoid writing a blank last line on end-of-string.
  2094. if(*c != '\0' || !line.empty())
  2095. {
  2096. // Add a command to echo this line.
  2097. std::string cmd = "@echo ";
  2098. if(m_EchoNeedsQuote)
  2099. {
  2100. cmd += "\"";
  2101. }
  2102. cmd += line;
  2103. if(m_EchoNeedsQuote)
  2104. {
  2105. cmd += "\"";
  2106. }
  2107. commands.push_back(cmd);
  2108. }
  2109. // Reset the line to emtpy.
  2110. line = "";
  2111. // Terminate on end-of-string.
  2112. if(*c == '\0')
  2113. {
  2114. return;
  2115. }
  2116. }
  2117. else if(*c != '\r')
  2118. {
  2119. // Append this character to the current line.
  2120. line += *c;
  2121. }
  2122. }
  2123. }
  2124. //============================================================================
  2125. //----------------------------------------------------------------------------
  2126. bool
  2127. cmLocalUnixMakefileGenerator3::SamePath(const char* path1, const char* path2)
  2128. {
  2129. if (strcmp(path1, path2) == 0)
  2130. {
  2131. return true;
  2132. }
  2133. #if defined(_WIN32) || defined(__APPLE__)
  2134. return
  2135. (cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  2136. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2)));
  2137. #else
  2138. return false;
  2139. #endif
  2140. }
  2141. //----------------------------------------------------------------------------
  2142. // take a tgt path and convert it into a make target, it could be full, or relative
  2143. std::string
  2144. cmLocalUnixMakefileGenerator3
  2145. ::ConvertToMakeTarget(const char* tgt)
  2146. {
  2147. // Make targets should not have a leading './' for a file in the
  2148. // directory containing the makefile.
  2149. std::string ret = tgt;
  2150. if(ret.size() > 2 && (ret[0] == '.') &&
  2151. ( (ret[1] == '/') || ret[1] == '\\'))
  2152. {
  2153. std::string upath = ret;
  2154. cmSystemTools::ConvertToUnixSlashes(upath);
  2155. if(upath.find(2, '/') == upath.npos)
  2156. {
  2157. ret = ret.substr(2, ret.size()-2);
  2158. }
  2159. }
  2160. return ret;
  2161. }
  2162. //----------------------------------------------------------------------------
  2163. std::string&
  2164. cmLocalUnixMakefileGenerator3::CreateSafeUniqueObjectFileName(const char* sin)
  2165. {
  2166. if ( m_Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES") )
  2167. {
  2168. std::map<cmStdString,cmStdString>::iterator it = m_UniqueObjectNamesMap.find(sin);
  2169. if ( it == m_UniqueObjectNamesMap.end() )
  2170. {
  2171. std::string ssin = sin;
  2172. bool done;
  2173. int cc = 0;
  2174. char rpstr[100];
  2175. sprintf(rpstr, "_p_");
  2176. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2177. std::string sssin = sin;
  2178. do
  2179. {
  2180. done = true;
  2181. for ( it = m_UniqueObjectNamesMap.begin();
  2182. it != m_UniqueObjectNamesMap.end();
  2183. ++ it )
  2184. {
  2185. if ( it->second == ssin )
  2186. {
  2187. done = false;
  2188. }
  2189. }
  2190. if ( done )
  2191. {
  2192. break;
  2193. }
  2194. sssin = ssin;
  2195. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2196. sprintf(rpstr, "_p%d_", cc++);
  2197. }
  2198. while ( !done );
  2199. m_UniqueObjectNamesMap[sin] = ssin;
  2200. }
  2201. }
  2202. else
  2203. {
  2204. m_UniqueObjectNamesMap[sin] = sin;
  2205. }
  2206. return m_UniqueObjectNamesMap[sin];
  2207. }
  2208. //----------------------------------------------------------------------------
  2209. std::string
  2210. cmLocalUnixMakefileGenerator3
  2211. ::CreateMakeVariable(const char* sin, const char* s2in)
  2212. {
  2213. std::string s = sin;
  2214. std::string s2 = s2in;
  2215. std::string unmodified = s;
  2216. unmodified += s2;
  2217. // if there is no restriction on the length of make variables
  2218. // and there are no "." charactors in the string, then return the
  2219. // unmodified combination.
  2220. if(!m_MakefileVariableSize && unmodified.find('.') == s.npos)
  2221. {
  2222. return unmodified;
  2223. }
  2224. // see if the variable has been defined before and return
  2225. // the modified version of the variable
  2226. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  2227. if(i != m_MakeVariableMap.end())
  2228. {
  2229. return i->second;
  2230. }
  2231. // start with the unmodified variable
  2232. std::string ret = unmodified;
  2233. // if this there is no value for m_MakefileVariableSize then
  2234. // the string must have bad characters in it
  2235. if(!m_MakefileVariableSize)
  2236. {
  2237. cmSystemTools::ReplaceString(ret, ".", "_");
  2238. int ni = 0;
  2239. char buffer[5];
  2240. // make sure the _ version is not already used, if
  2241. // it is used then add number to the end of the variable
  2242. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2243. {
  2244. ++ni;
  2245. sprintf(buffer, "%04d", ni);
  2246. ret = unmodified + buffer;
  2247. }
  2248. m_ShortMakeVariableMap[ret] = "1";
  2249. m_MakeVariableMap[unmodified] = ret;
  2250. return ret;
  2251. }
  2252. // if the string is greater the 32 chars it is an invalid vairable name
  2253. // for borland make
  2254. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  2255. {
  2256. int keep = m_MakefileVariableSize - 8;
  2257. int size = keep + 3;
  2258. std::string str1 = s;
  2259. std::string str2 = s2;
  2260. // we must shorten the combined string by 4 charactors
  2261. // keep no more than 24 charactors from the second string
  2262. if(static_cast<int>(str2.size()) > keep)
  2263. {
  2264. str2 = str2.substr(0, keep);
  2265. }
  2266. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  2267. {
  2268. str1 = str1.substr(0, size - str2.size());
  2269. }
  2270. char buffer[5];
  2271. int ni = 0;
  2272. sprintf(buffer, "%04d", ni);
  2273. ret = str1 + str2 + buffer;
  2274. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2275. {
  2276. ++ni;
  2277. sprintf(buffer, "%04d", ni);
  2278. ret = str1 + str2 + buffer;
  2279. }
  2280. if(ni == 1000)
  2281. {
  2282. cmSystemTools::Error("Borland makefile variable length too long");
  2283. return unmodified;
  2284. }
  2285. // once an unused variable is found
  2286. m_ShortMakeVariableMap[ret] = "1";
  2287. }
  2288. // always make an entry into the unmodified to variable map
  2289. m_MakeVariableMap[unmodified] = ret;
  2290. return ret;
  2291. }
  2292. //============================================================================
  2293. //----------------------------------------------------------------------------
  2294. cmDepends*
  2295. cmLocalUnixMakefileGenerator3::GetDependsChecker(const std::string& lang,
  2296. bool verbose)
  2297. {
  2298. cmDepends *ret = 0;
  2299. if(lang == "C" || lang == "CXX" || lang == "RC")
  2300. {
  2301. ret = new cmDependsC();
  2302. }
  2303. #ifdef CMAKE_BUILD_WITH_CMAKE
  2304. else if(lang == "Fortran")
  2305. {
  2306. ret = new cmDependsFortran();
  2307. }
  2308. else if(lang == "Java")
  2309. {
  2310. ret = new cmDependsJava();
  2311. }
  2312. #endif
  2313. if (ret)
  2314. {
  2315. ret->SetVerbose(verbose);
  2316. }
  2317. return ret;
  2318. }
  2319. //----------------------------------------------------------------------------
  2320. bool
  2321. cmLocalUnixMakefileGenerator3
  2322. ::ScanDependencies(std::vector<std::string> const& args)
  2323. {
  2324. // Format of arguments is:
  2325. // $(CMAKE_COMMAND), cmake_depends, GeneratorName, home_output_dir, start_output_dir, info file
  2326. // The caller has ensured that all required arguments exist.
  2327. // The info file for this target
  2328. std::string const& infoFile = args[5];
  2329. // Read the directory information file.
  2330. cmake cm;
  2331. cmGlobalGenerator gg;
  2332. gg.SetCMakeInstance(&cm);
  2333. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  2334. lg->SetGlobalGenerator(&gg);
  2335. cmMakefile* mf = lg->GetMakefile();
  2336. mf->SetHomeOutputDirectory(args[3].c_str());
  2337. mf->SetStartOutputDirectory(args[4].c_str());
  2338. lg->SetupPathConversions();
  2339. bool haveDirectoryInfo = false;
  2340. std::string dirInfoFile = args[4];
  2341. dirInfoFile += "/CMakeDirectoryInformation.cmake";
  2342. if(mf->ReadListFile(0, dirInfoFile.c_str()) &&
  2343. !cmSystemTools::GetErrorOccuredFlag())
  2344. {
  2345. haveDirectoryInfo = true;
  2346. }
  2347. // read in the target info file
  2348. if(!mf->ReadListFile(0, infoFile.c_str()) ||
  2349. cmSystemTools::GetErrorOccuredFlag())
  2350. {
  2351. cmSystemTools::Error("Target DependInfo.cmake file not found");
  2352. }
  2353. // Test whether we need to force Unix paths.
  2354. if(haveDirectoryInfo)
  2355. {
  2356. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  2357. {
  2358. if(!cmSystemTools::IsOff(force))
  2359. {
  2360. cmSystemTools::SetForceUnixPaths(true);
  2361. }
  2362. }
  2363. }
  2364. else
  2365. {
  2366. cmSystemTools::Error("Directory Information file not found");
  2367. }
  2368. // create the file stream for the depends file
  2369. std::string dir = cmSystemTools::GetFilenamePath(infoFile);
  2370. dir += "/depend.make";
  2371. // Open the rule file. This should be copy-if-different because the
  2372. // rules may depend on this file itself.
  2373. std::string ruleFileNameFull = dir;
  2374. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  2375. ruleFileStream.SetCopyIfDifferent(true);
  2376. if(!ruleFileStream)
  2377. {
  2378. return false;
  2379. }
  2380. this->WriteDisclaimer(ruleFileStream);
  2381. // for each language we need to scan, scan it
  2382. const char *langStr = mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES");
  2383. std::vector<std::string> langs;
  2384. cmSystemTools::ExpandListArgument(langStr, langs);
  2385. for (std::vector<std::string>::iterator li =
  2386. langs.begin(); li != langs.end(); ++li)
  2387. {
  2388. // construct the checker
  2389. std::string lang = li->c_str();
  2390. // Get the set of include directories.
  2391. std::vector<std::string> includes;
  2392. if(haveDirectoryInfo)
  2393. {
  2394. std::string includePathVar = "CMAKE_";
  2395. includePathVar += lang;
  2396. includePathVar += "_INCLUDE_PATH";
  2397. if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
  2398. {
  2399. cmSystemTools::ExpandListArgument(includePath, includes);
  2400. }
  2401. }
  2402. // Get the include file regular expression.
  2403. std::string includeRegexScan = "^.*$";
  2404. std::string includeRegexComplain = "^$";
  2405. if(haveDirectoryInfo)
  2406. {
  2407. std::string scanRegexVar = "CMAKE_";
  2408. scanRegexVar += lang;
  2409. scanRegexVar += "_INCLUDE_REGEX_SCAN";
  2410. if(const char* scanRegex = mf->GetDefinition(scanRegexVar.c_str()))
  2411. {
  2412. includeRegexScan = scanRegex;
  2413. }
  2414. std::string complainRegexVar = "CMAKE_";
  2415. complainRegexVar += lang;
  2416. complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
  2417. if(const char* complainRegex = mf->GetDefinition(complainRegexVar.c_str()))
  2418. {
  2419. includeRegexComplain = complainRegex;
  2420. }
  2421. }
  2422. // Create the scanner for this language
  2423. cmDepends *scanner = 0;
  2424. if(lang == "C" || lang == "CXX" || lang == "RC")
  2425. {
  2426. // TODO: Handle RC (resource files) dependencies correctly.
  2427. scanner = new cmDependsC(includes,
  2428. includeRegexScan.c_str(),
  2429. includeRegexComplain.c_str());
  2430. }
  2431. #ifdef CMAKE_BUILD_WITH_CMAKE
  2432. else if(lang == "Fortran")
  2433. {
  2434. scanner = new cmDependsFortran(includes);
  2435. }
  2436. else if(lang == "Java")
  2437. {
  2438. scanner = new cmDependsJava();
  2439. }
  2440. #endif
  2441. // for each file we need to scan
  2442. std::string srcLang = "CMAKE_DEPENDS_CHECK_";
  2443. srcLang += lang;
  2444. const char *srcStr = mf->GetSafeDefinition(srcLang.c_str());
  2445. std::vector<std::string> srcs;
  2446. cmSystemTools::ExpandListArgument(srcStr, srcs);
  2447. for (std::vector<std::string>::iterator si =
  2448. srcs.begin(); si != srcs.end(); ++si)
  2449. {
  2450. std::string &src = *si;
  2451. ++si;
  2452. // make sure the object file is relative to home output
  2453. std::string obj = *si;
  2454. obj = lg->Convert(obj.c_str(),HOME_OUTPUT,MAKEFILE);
  2455. scanner->Write(src.c_str(),obj.c_str(),ruleFileStream);
  2456. }
  2457. // free the scanner for this language
  2458. if (scanner)
  2459. {
  2460. delete scanner;
  2461. }
  2462. }
  2463. // dependencies were generated, so touch the mark file
  2464. dir += ".mark";
  2465. std::ofstream fmark(dir.c_str());
  2466. fmark << "Dependencies updated>" << std::endl;
  2467. return true;
  2468. }
  2469. //----------------------------------------------------------------------------
  2470. void cmLocalUnixMakefileGenerator3::WriteLocalMakefile()
  2471. {
  2472. // only write the local Makefile if we are not at the top
  2473. if (!m_Parent)
  2474. {
  2475. return;
  2476. }
  2477. // generate the includes
  2478. std::string ruleFileName = "Makefile";
  2479. // Open the rule file. This should be copy-if-different because the
  2480. // rules may depend on this file itself.
  2481. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  2482. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  2483. ruleFileStream.SetCopyIfDifferent(true);
  2484. if(!ruleFileStream)
  2485. {
  2486. return;
  2487. }
  2488. this->WriteDisclaimer(ruleFileStream);
  2489. this->WriteMakeVariables(ruleFileStream, START_OUTPUT);
  2490. this->WriteSpecialTargetsTop(ruleFileStream);
  2491. std::vector<std::string> depends;
  2492. std::vector<std::string> commands;
  2493. // Write the all rule.
  2494. std::string dir = m_Makefile->GetStartOutputDirectory();
  2495. dir += "/directorystart";
  2496. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  2497. this->CreateJumpCommand(commands,"Makefile2",dir);
  2498. this->WriteMakeRule(ruleFileStream, "The main all target", "all", depends, commands);
  2499. // Write the clean rule.
  2500. dir = m_Makefile->GetStartOutputDirectory();
  2501. dir += "/clean";
  2502. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  2503. commands.clear();
  2504. this->CreateJumpCommand(commands,"Makefile2",dir);
  2505. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean", depends, commands);
  2506. // write our targets, and while doing it collect up the object
  2507. // file rules
  2508. this->WriteLocalMakefileTargets(ruleFileStream);
  2509. // now write out the object rules
  2510. // for each object file name
  2511. for (std::map<cmStdString,std::vector<cmTarget *> >::iterator lo =
  2512. m_LocalObjectFiles.begin();
  2513. lo != m_LocalObjectFiles.end(); ++lo)
  2514. {
  2515. commands.clear();
  2516. // for each target using the object file
  2517. for (std::vector<cmTarget *>::iterator to =
  2518. lo->second.begin(); to != lo->second.end(); ++to)
  2519. {
  2520. std::string tgtMakefileName = this->GetRelativeTargetDirectory(**to);
  2521. std::string targetName = tgtMakefileName;
  2522. tgtMakefileName += "/build.make";
  2523. targetName += "/";
  2524. targetName += lo->first.c_str();
  2525. this->CreateJumpCommand(commands,tgtMakefileName.c_str(),targetName);
  2526. }
  2527. this->WriteMakeRule(ruleFileStream,
  2528. "target for object file",
  2529. lo->first.c_str(), depends, commands);
  2530. }
  2531. this->WriteHelpRule(ruleFileStream);
  2532. }
  2533. void cmLocalUnixMakefileGenerator3
  2534. ::WriteLocalMakefileTargets(std::ostream& ruleFileStream)
  2535. {
  2536. std::vector<std::string> depends;
  2537. std::vector<std::string> commands;
  2538. // for each target we just provide a rule to cd up to the top and do a make
  2539. // on the target
  2540. cmTargets& targets = m_Makefile->GetTargets();
  2541. std::string localName;
  2542. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  2543. {
  2544. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  2545. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  2546. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2547. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  2548. (t->second.GetType() == cmTarget::UTILITY))
  2549. {
  2550. // Add a rule to build the target by name.
  2551. localName = this->GetRelativeTargetDirectory(t->second);
  2552. localName += "/rule";
  2553. commands.clear();
  2554. depends.clear();
  2555. this->CreateJumpCommand(commands,"Makefile2",localName);
  2556. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  2557. localName.c_str(), depends, commands);
  2558. // Add a target with the canonical name (no prefix, suffix or path).
  2559. if(localName != t->second.GetName())
  2560. {
  2561. commands.clear();
  2562. depends.push_back(localName);
  2563. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  2564. t->second.GetName(), depends, commands);
  2565. }
  2566. }
  2567. }
  2568. }
  2569. void cmLocalUnixMakefileGenerator3
  2570. ::CreateJumpCommand(std::vector<std::string>& commands,
  2571. const char *MakefileName,
  2572. std::string& localName)
  2573. {
  2574. if(m_WindowsShell)
  2575. {
  2576. // On Windows we must perform each step separately and then change
  2577. // back because the shell keeps the working directory between
  2578. // commands.
  2579. std::string cmd = "cd ";
  2580. cmd += this->ConvertToOutputForExisting
  2581. (m_Makefile->GetHomeOutputDirectory());
  2582. commands.push_back(cmd);
  2583. // Build the target for this pass.
  2584. commands.push_back(this->GetRecursiveMakeCall
  2585. (MakefileName,localName.c_str()));
  2586. // Change back to the starting directory. Any trailing slash must be
  2587. // removed to avoid problems with Borland Make.
  2588. std::string back =
  2589. cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(),
  2590. m_Makefile->GetStartOutputDirectory());
  2591. if(back.size() && back[back.size()-1] == '/')
  2592. {
  2593. back = back.substr(0, back.size()-1);
  2594. }
  2595. cmd = "cd ";
  2596. cmd += this->ConvertToOutputForExisting(back.c_str());
  2597. commands.push_back(cmd);
  2598. }
  2599. else
  2600. {
  2601. // On UNIX we must construct a single shell command to change
  2602. // directory and build because make resets the directory between
  2603. // each command.
  2604. std::string cmd = "cd ";
  2605. cmd += this->ConvertToOutputForExisting(m_Makefile->GetHomeOutputDirectory());
  2606. // Build the target for this pass.
  2607. cmd += " && ";
  2608. cmd += this->GetRecursiveMakeCall(MakefileName,localName.c_str());
  2609. // Add the command as a single line.
  2610. commands.push_back(cmd);
  2611. }
  2612. }
  2613. //----------------------------------------------------------------------------
  2614. void cmLocalUnixMakefileGenerator3::CheckDependencies(cmMakefile* mf,
  2615. bool verbose,
  2616. bool clear)
  2617. {
  2618. // Get the list of target files to check
  2619. const char* infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
  2620. if(!infoDef)
  2621. {
  2622. return;
  2623. }
  2624. std::vector<std::string> files;
  2625. cmSystemTools::ExpandListArgument(infoDef, files);
  2626. // For each info file run the check
  2627. cmDependsC checker;
  2628. checker.SetVerbose(verbose);
  2629. for(std::vector<std::string>::iterator l = files.begin();
  2630. l != files.end(); ++l)
  2631. {
  2632. // either clear or check the files
  2633. std::string dependFile = cmSystemTools::GetFilenamePath(l->c_str());
  2634. dependFile += "/depend.make";
  2635. if (clear)
  2636. {
  2637. checker.Clear(dependFile.c_str());
  2638. }
  2639. else
  2640. {
  2641. checker.Check(dependFile.c_str());
  2642. }
  2643. }
  2644. }
  2645. //----------------------------------------------------------------------------
  2646. std::string
  2647. cmLocalUnixMakefileGenerator3
  2648. ::GetRecursiveMakeCall(const char *Makefile, const char* tgt)
  2649. {
  2650. // Call make on the given file.
  2651. std::string cmd;
  2652. cmd += "$(MAKE) -f ";
  2653. cmd += Makefile;
  2654. cmd += " ";
  2655. // Passg down verbosity level.
  2656. if(this->GetMakeSilentFlag().size())
  2657. {
  2658. cmd += this->GetMakeSilentFlag();
  2659. cmd += " ";
  2660. }
  2661. // Most unix makes will pass the command line flags to make down to
  2662. // sub-invoked makes via an environment variable. However, some
  2663. // makes do not support that, so you have to pass the flags
  2664. // explicitly.
  2665. if(this->GetPassMakeflags())
  2666. {
  2667. cmd += "-$(MAKEFLAGS) ";
  2668. }
  2669. // Add the target.
  2670. if (tgt && tgt[0] != '\0')
  2671. {
  2672. std::string tgt2 = this->Convert(tgt,HOME_OUTPUT,MAKEFILE);
  2673. tgt2 = this->ConvertToMakeTarget(tgt2.c_str());
  2674. cmd += tgt2;
  2675. }
  2676. return cmd;
  2677. }
  2678. //----------------------------------------------------------------------------
  2679. void
  2680. cmLocalUnixMakefileGenerator3::WriteHelpRule(std::ostream& ruleFileStream)
  2681. {
  2682. // add the help target
  2683. std::string path;
  2684. std::vector<std::string> no_depends;
  2685. std::vector<std::string> commands;
  2686. this->AppendEcho(commands,
  2687. "The following are some of the valid targets for this Makefile:");
  2688. this->AppendEcho(commands,"... all (the default if no target is provided)");
  2689. this->AppendEcho(commands,"... clean");
  2690. this->AppendEcho(commands,"... install");
  2691. this->AppendEcho(commands,"... rebuild_cache");
  2692. // Keep track of targets already listed.
  2693. std::set<cmStdString> emittedTargets;
  2694. // for each target Generate the rule files for each target.
  2695. cmTargets& targets = this->GetMakefile()->GetTargets();
  2696. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  2697. {
  2698. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  2699. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  2700. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2701. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  2702. (t->second.GetType() == cmTarget::UTILITY))
  2703. {
  2704. if(emittedTargets.insert(t->second.GetName()).second)
  2705. {
  2706. path = "... ";
  2707. path += t->second.GetName();
  2708. this->AppendEcho(commands,path.c_str());
  2709. }
  2710. }
  2711. }
  2712. this->WriteMakeRule(ruleFileStream, "Help Target",
  2713. "help:",
  2714. no_depends, commands);
  2715. ruleFileStream << "\n\n";
  2716. }
  2717. void cmLocalUnixMakefileGenerator3
  2718. ::WriteDependLanguageInfo(std::ostream& cmakefileStream, cmTarget &target)
  2719. {
  2720. // now write all the language stuff
  2721. // Set the set of files to check for dependency integrity.
  2722. std::set<cmStdString> checkSetLangs;
  2723. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  2724. checkSet = this->GetIntegrityCheckSet()[target.GetName()];
  2725. for(std::map<cmStdString,
  2726. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  2727. l = checkSet.begin(); l != checkSet.end(); ++l)
  2728. {
  2729. checkSetLangs.insert(l->first);
  2730. }
  2731. // list the languages
  2732. cmakefileStream
  2733. << "# The set of files whose dependency integrity should be checked:\n";
  2734. cmakefileStream
  2735. << "SET(CMAKE_DEPENDS_LANGUAGES\n";
  2736. for(std::set<cmStdString>::iterator
  2737. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  2738. {
  2739. cmakefileStream << " \"" << l->c_str() << "\"\n";
  2740. }
  2741. cmakefileStream << " )\n";
  2742. // now list the files for each language
  2743. for(std::set<cmStdString>::iterator
  2744. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  2745. {
  2746. cmakefileStream
  2747. << "SET(CMAKE_DEPENDS_CHECK_" << l->c_str() << "\n";
  2748. // get the check set for this local gen and language
  2749. cmLocalUnixMakefileGenerator3::IntegrityCheckSet iCheckSet =
  2750. checkSet[*l];
  2751. // for each file
  2752. for(cmLocalUnixMakefileGenerator3::IntegrityCheckSet::const_iterator
  2753. csIter = iCheckSet.begin();
  2754. csIter != iCheckSet.end(); ++csIter)
  2755. {
  2756. cmakefileStream << " \"" << (*csIter)->GetFullPath() << "\"\n";
  2757. // Get the full path name of the object file.
  2758. std::string obj = this->GetObjectFileName(target, **csIter);
  2759. cmakefileStream << " \"" <<
  2760. this->Convert(obj.c_str(),
  2761. cmLocalGenerator::FULL).c_str() << "\"\n";
  2762. }
  2763. cmakefileStream << " )\n";
  2764. }
  2765. }