gmake2_makefile.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. --
  2. -- gmake2_makefile.lua
  3. -- Generate a C/C++ project makefile.
  4. -- (c) 2016-2017 Jason Perkins, Blizzard Entertainment and the Premake project
  5. --
  6. local p = premake
  7. local gmake2 = p.modules.gmake2
  8. gmake2.makefile = {}
  9. local makefile = gmake2.makefile
  10. local project = p.project
  11. local config = p.config
  12. local fileconfig = p.fileconfig
  13. ---
  14. -- Add namespace for element definition lists for p.callArray()
  15. ---
  16. makefile.elements = {}
  17. --
  18. -- Generate a GNU make makefile project makefile.
  19. --
  20. makefile.elements.makefile = function(prj)
  21. return {
  22. gmake2.header,
  23. gmake2.phonyRules,
  24. makefile.configs,
  25. makefile.targetRules
  26. }
  27. end
  28. function makefile.generate(prj)
  29. p.eol("\n")
  30. p.callArray(makefile.elements.makefile, prj)
  31. end
  32. makefile.elements.configuration = function(cfg)
  33. return {
  34. gmake2.target,
  35. gmake2.buildCommands,
  36. gmake2.cleanCommands,
  37. }
  38. end
  39. function makefile.configs(prj)
  40. local first = true
  41. for cfg in project.eachconfig(prj) do
  42. -- identify the toolset used by this configurations (would be nicer if
  43. -- this were computed and stored with the configuration up front)
  44. local toolset = p.tools[cfg.toolset or "gcc"]
  45. if not toolset then
  46. error("Invalid toolset '" .. cfg.toolset .. "'")
  47. end
  48. if first then
  49. _x('ifeq ($(config),%s)', cfg.shortname)
  50. first = false
  51. else
  52. _x('else ifeq ($(config),%s)', cfg.shortname)
  53. end
  54. p.callArray(makefile.elements.configuration, cfg, toolset)
  55. _p('')
  56. end
  57. if not first then
  58. _p('else')
  59. _p(' $(error "invalid configuration $(config)")')
  60. _p('endif')
  61. _p('')
  62. end
  63. end
  64. function makefile.targetRules(prj)
  65. _p('$(TARGET):')
  66. _p('\t$(BUILDCMDS)')
  67. _p('')
  68. _p('clean:')
  69. _p('\t$(CLEANCMDS)')
  70. _p('')
  71. end
  72. function gmake2.buildCommands(cfg)
  73. _p(' define BUILDCMDS')
  74. local steps = cfg.buildcommands
  75. if #steps > 0 then
  76. steps = os.translateCommandsAndPaths(steps, cfg.project.basedir, cfg.project.location)
  77. _p('\t@echo Running build commands')
  78. _p('\t%s', table.implode(steps, "", "", "\n\t"))
  79. end
  80. _p(' endef')
  81. end
  82. function gmake2.cleanCommands(cfg)
  83. _p(' define CLEANCMDS')
  84. local steps = cfg.cleancommands
  85. if #steps > 0 then
  86. steps = os.translateCommandsAndPaths(steps, cfg.project.basedir, cfg.project.location)
  87. _p('\t@echo Running clean commands')
  88. _p('\t%s', table.implode(steps, "", "", "\n\t"))
  89. end
  90. _p(' endef')
  91. end