cmLocalUnixMakefileGenerator3.cxx 103 KB

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