cmLocalUnixMakefileGenerator3.cxx 100 KB

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