cmLocalUnixMakefileGenerator2.cxx 104 KB

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