cmLocalUnixMakefileGenerator2.cxx 102 KB

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