cmLocalUnixMakefileGenerator2.cxx 103 KB

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