cmLocalUnixMakefileGenerator3.cxx 93 KB

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