cmLocalUnixMakefileGenerator3.cxx 103 KB

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