cmLocalUnixMakefileGenerator2.cxx 105 KB

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