cmLocalUnixMakefileGenerator3.cxx 100 KB

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