cmLocalUnixMakefileGenerator3.cxx 103 KB

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