cmLocalUnixMakefileGenerator3.cxx 99 KB

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