cmLocalUnixMakefileGenerator3.cxx 99 KB

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