cmLocalUnixMakefileGenerator3.cxx 96 KB

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