cmLocalUnixMakefileGenerator3.cxx 106 KB

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