cmLocalUnixMakefileGenerator3.cxx 93 KB

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