cmLocalUnixMakefileGenerator2.cxx 107 KB

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