cmLocalUnixMakefileGenerator3.cxx 95 KB

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