cmLocalUnixMakefileGenerator3.cxx 99 KB

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