cmLocalUnixMakefileGenerator3.cxx 94 KB

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