init.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 selective_dependency_link(module_entries)
  36. for _, mod in pairs(module_entries) do
  37. if mod[1] ~= "None" then
  38. links(mod.name)
  39. end
  40. end
  41. for _, mod in pairs(module_entries) do
  42. includedirs(mod[1].public_includes)
  43. end
  44. end
  45. local function link_against_modules(modules)
  46. for _, mod in pairs(modules) do
  47. links(mod.name)
  48. end
  49. for _, mod in pairs(modules) do
  50. includedirs(mod.public_includes)
  51. end
  52. for _, mod in pairs(modules) do
  53. selective_dependency_link(mod.module_dependencies)
  54. end
  55. end
  56. --- An attempt at making a common ground for multiple libraries I'd use
  57. -- in various projects.
  58. local function library_module(lib_name, folder)
  59. local mod = {
  60. name = lib_name,
  61. folder = folder or lib_name,
  62. links = {},
  63. win_links = {},
  64. linux_links = {},
  65. files = {},
  66. win_files = {},
  67. linux_files = {},
  68. defines = {},
  69. win_defines = {},
  70. linux_defines = {},
  71. -- Separating private/public includes helps
  72. -- to reuse the public when needed.
  73. private_includes = {},
  74. win_private_includes = {},
  75. linux_private_includes = {},
  76. public_includes = {},
  77. module_dependencies = {}
  78. }
  79. function mod:_convert_and_store_path(destination, paths)
  80. local path_prefix = library_base_folders .. self.folder .. "/"
  81. for k, path in pairs(paths) do
  82. table.insert(destination, path_prefix .. path)
  83. end
  84. end
  85. function mod:use_private_includes(local_private_paths)
  86. self:_convert_and_store_path(self.private_includes, local_private_paths)
  87. return self
  88. end
  89. function mod:use_win_private_includes(local_win_private_paths)
  90. self:_convert_and_store_path(self.win_private_includes, local_win_private_paths)
  91. return self
  92. end
  93. function mod:use_linux_private_includes(local_linux_private_paths)
  94. self:_convert_and_store_path(self.linux_private_includes, local_linux_private_paths)
  95. return self
  96. end
  97. function mod:use_public_includes(local_public_paths)
  98. self:_convert_and_store_path(self.public_includes, local_public_paths)
  99. return self
  100. end
  101. function mod:use_files(local_file_paths)
  102. self:_convert_and_store_path(self.files, local_file_paths)
  103. return self
  104. end
  105. function mod:use_win_files(local_file_paths)
  106. self:_convert_and_store_path(self.win_files, local_file_paths)
  107. return self
  108. end
  109. function mod:use_linux_files(local_file_paths)
  110. self:_convert_and_store_path(self.linux_files, local_file_paths)
  111. return self
  112. end
  113. function mod:use_defines(defines)
  114. self.defines = defines
  115. return self
  116. end
  117. function mod:use_win_defines(defines)
  118. self.win_defines = defines
  119. return self
  120. end
  121. function mod:use_linux_defines(defines)
  122. self.linux_defines = defines
  123. return self
  124. end
  125. function mod:require_module(module, link_type)
  126. table.insert(self.module_dependencies, {module, link_type})
  127. return self
  128. end
  129. function mod:use_links(links)
  130. self.links = links
  131. return self
  132. end
  133. function mod:use_win_links(links)
  134. self.win_links = links
  135. return self
  136. end
  137. function mod:use_linux_links(links)
  138. self.linux_links = links
  139. return self
  140. end
  141. --- Calls premake's code to add the module's project to the current
  142. -- workspace.
  143. function mod:use(use_kind)
  144. -- TODO: handle dependencies
  145. project(self.name)
  146. kind(use_kind)
  147. defines(self.defines)
  148. files(self.files)
  149. includedirs(self.public_includes)
  150. includedirs(self.private_includes)
  151. links(self.links)
  152. selective_dependency_link(self.module_dependencies)
  153. filter "system:windows"
  154. defines(self.win_defines)
  155. files(self.win_files)
  156. includedirs(self.win_private_includes)
  157. links(self.win_links)
  158. filter "system:linux"
  159. defines(self.linux_defines)
  160. files(self.linux_files)
  161. includedirs(self.linux_private_includes)
  162. links(self.linux_links)
  163. filter {}
  164. end
  165. return mod
  166. end
  167. return {
  168. _VERSION = "0.0.1",
  169. base_workspace_settings = base_workspace_settings,
  170. library_module = library_module,
  171. link_against_modules = link_against_modules
  172. }