cmLocalUnixMakefileGenerator3.cxx 98 KB

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