cmLocalUnixMakefileGenerator3.cxx 95 KB

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