cmLocalUnixMakefileGenerator2.cxx 105 KB

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