cmLocalUnixMakefileGenerator2.cxx 104 KB

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