init.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. local library_base_folders = "extern/"
  2. local function extern_path(local_path)
  3. if local_path:sub(1,1) == '/' then
  4. -- TODO : Windows absolute path check
  5. print(([[[WARN] "%s" is not a local path.]]):format(local_path))
  6. local_path = local_path:sub(2)
  7. end
  8. return library_base_folders .. local_path
  9. end
  10. --- Common function I use to structure projects according to the three-stage
  11. -- configuration pattern I usually see : Debug, optimized Debug and Release.
  12. -- Sometimes I see also a Final version, but it almost is a copy of the Release
  13. -- stage.
  14. local function base_workspace_settings()
  15. language "C++"
  16. configurations {"Debug", "DebugOpt", "Release"}
  17. location "build"
  18. architecture "x86_64"
  19. flags { "MultiProcessorCompile" }
  20. filter "configurations:Debug"
  21. defines {"DEBUG"}
  22. symbols "On"
  23. warnings "Extra"
  24. filter "configurations:DebugOpt"
  25. defines {"DEBUG"}
  26. symbols "On"
  27. optimize "Debug"
  28. warnings "Extra"
  29. filter "configurations:Release"
  30. defines {"NDEBUG"}
  31. optimize "On"
  32. warnings "Extra"
  33. filter {}
  34. end
  35. local function link_against_modules(modules)
  36. for _, mod in pairs(modules) do
  37. links(mod.name)
  38. end
  39. for _, mod in pairs(modules) do
  40. includedirs(mod.public_includes)
  41. end
  42. end
  43. --- An attempt at making a common ground for multiple libraries I'd use
  44. -- in various projects.
  45. local function library_module(lib_name, folder)
  46. local mod = {
  47. name = lib_name,
  48. folder = folder or lib_name,
  49. links = {},
  50. win_links = {},
  51. linux_links = {},
  52. files = {},
  53. win_files = {},
  54. linux_files = {},
  55. defines = {},
  56. win_defines = {},
  57. linux_defines = {},
  58. -- Separating private/public includes helps
  59. -- to reuse the public when needed.
  60. private_includes = {},
  61. win_private_includes = {},
  62. linux_private_includes = {},
  63. public_includes = {},
  64. module_dependencies = {}
  65. }
  66. function mod:_convert_and_store_path(destination, paths)
  67. local path_prefix = library_base_folders .. self.folder .. "/"
  68. for k, path in pairs(paths) do
  69. table.insert(destination, path_prefix .. path)
  70. end
  71. end
  72. function mod:use_private_includes(local_private_paths)
  73. self:_convert_and_store_path(self.private_includes, local_private_paths)
  74. return self
  75. end
  76. function mod:use_win_private_includes(local_win_private_paths)
  77. self:_convert_and_store_path(self.win_private_includes, local_win_private_paths)
  78. return self
  79. end
  80. function mod:use_linux_private_includes(local_linux_private_paths)
  81. self:_convert_and_store_path(self.linux_private_includes, local_linux_private_paths)
  82. return self
  83. end
  84. function mod:use_public_includes(local_public_paths)
  85. self:_convert_and_store_path(self.public_includes, local_public_paths)
  86. return self
  87. end
  88. function mod:use_files(local_file_paths)
  89. self:_convert_and_store_path(self.files, local_file_paths)
  90. return self
  91. end
  92. function mod:use_win_files(local_file_paths)
  93. self:_convert_and_store_path(self.win_files, local_file_paths)
  94. return self
  95. end
  96. function mod:use_linux_files(local_file_paths)
  97. self:_convert_and_store_path(self.linux_files, local_file_paths)
  98. return self
  99. end
  100. function mod:use_defines(defines)
  101. self.defines = defines
  102. return self
  103. end
  104. function mod:use_win_defines(defines)
  105. self.win_defines = defines
  106. return self
  107. end
  108. function mod:use_linux_defines(defines)
  109. self.linux_defines = defines
  110. return self
  111. end
  112. function mod:require_module(module)
  113. self.module_dependencies:insert(module)
  114. end
  115. function mod:use_links(links)
  116. self.links = links
  117. return self
  118. end
  119. function mod:use_win_links(links)
  120. self.win_links = links
  121. return self
  122. end
  123. function mod:use_linux_links(links)
  124. self.linux_links = links
  125. return self
  126. end
  127. --- Calls premake's code to add the module's project to the current
  128. -- workspace.
  129. function mod:use(use_kind)
  130. -- TODO: handle dependencies
  131. project(self.name)
  132. kind(use_kind)
  133. defines(self.defines)
  134. files(self.files)
  135. includedirs(self.public_includes)
  136. includedirs(self.private_includes)
  137. links(self.links)
  138. filter "system:windows"
  139. defines(self.win_defines)
  140. files(self.win_files)
  141. includedirs(self.win_private_includes)
  142. links(self.win_links)
  143. filter "system:linux"
  144. defines(self.linux_defines)
  145. files(self.linux_files)
  146. includedirs(self.linux_private_includes)
  147. links(self.linux_links)
  148. filter {}
  149. end
  150. return mod
  151. end
  152. return {
  153. _VERSION = "0.0.1",
  154. base_workspace_settings = base_workspace_settings,
  155. library_module = library_module,
  156. link_against_modules = link_against_modules
  157. }