clang.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. --
  2. -- clang.lua
  3. -- Clang toolset adapter for Premake
  4. -- Copyright (c) 2013 Jason Perkins and the Premake project
  5. --
  6. local p = premake
  7. p.tools.clang = {}
  8. local clang = p.tools.clang
  9. local gcc = p.tools.gcc
  10. local config = p.config
  11. --
  12. -- Build a list of flags for the C preprocessor corresponding to the
  13. -- settings in a particular project configuration.
  14. --
  15. -- @param cfg
  16. -- The project configuration.
  17. -- @return
  18. -- An array of C preprocessor flags.
  19. --
  20. function clang.getcppflags(cfg)
  21. -- Just pass through to GCC for now
  22. local flags = gcc.getcppflags(cfg)
  23. return flags
  24. end
  25. --
  26. -- Build a list of C compiler flags corresponding to the settings in
  27. -- a particular project configuration. These flags are exclusive
  28. -- of the C++ compiler flags, there is no overlap.
  29. --
  30. -- @param cfg
  31. -- The project configuration.
  32. -- @return
  33. -- An array of C compiler flags.
  34. --
  35. clang.shared = {
  36. architecture = gcc.shared.architecture,
  37. flags = gcc.shared.flags,
  38. floatingpoint = {
  39. Fast = "-ffast-math",
  40. },
  41. strictaliasing = gcc.shared.strictaliasing,
  42. optimize = {
  43. Off = "-O0",
  44. On = "-O2",
  45. Debug = "-O0",
  46. Full = "-O3",
  47. Size = "-Os",
  48. Speed = "-O3",
  49. },
  50. pic = gcc.shared.pic,
  51. vectorextensions = gcc.shared.vectorextensions,
  52. isaextensions = gcc.shared.isaextensions,
  53. warnings = gcc.shared.warnings,
  54. symbols = gcc.shared.symbols,
  55. unsignedchar = gcc.shared.unsignedchar,
  56. omitframepointer = gcc.shared.omitframepointer,
  57. compileas = gcc.shared.compileas
  58. }
  59. clang.cflags = table.merge(gcc.cflags, {
  60. })
  61. function clang.getcflags(cfg)
  62. local shared = config.mapFlags(cfg, clang.shared)
  63. local cflags = config.mapFlags(cfg, clang.cflags)
  64. local flags = table.join(shared, cflags)
  65. flags = table.join(flags, clang.getwarnings(cfg), clang.getsystemversionflags(cfg))
  66. return flags
  67. end
  68. function clang.getwarnings(cfg)
  69. return gcc.getwarnings(cfg)
  70. end
  71. --
  72. -- Returns C/C++ system version related build flags
  73. --
  74. function clang.getsystemversionflags(cfg)
  75. local flags = {}
  76. if cfg.system == p.MACOSX or cfg.system == p.IOS then
  77. local minVersion = p.project.systemversion(cfg)
  78. if minVersion ~= nil then
  79. local name = iif(cfg.system == p.MACOSX, "macosx", "iphoneos")
  80. table.insert (flags, "-m" .. name .. "-version-min=" .. p.project.systemversion(cfg))
  81. end
  82. end
  83. return flags
  84. end
  85. --
  86. -- Build a list of C++ compiler flags corresponding to the settings
  87. -- in a particular project configuration. These flags are exclusive
  88. -- of the C compiler flags, there is no overlap.
  89. --
  90. -- @param cfg
  91. -- The project configuration.
  92. -- @return
  93. -- An array of C++ compiler flags.
  94. --
  95. clang.cxxflags = table.merge(gcc.cxxflags, {
  96. })
  97. function clang.getcxxflags(cfg)
  98. local shared = config.mapFlags(cfg, clang.shared)
  99. local cxxflags = config.mapFlags(cfg, clang.cxxflags)
  100. local flags = table.join(shared, cxxflags)
  101. flags = table.join(flags, clang.getwarnings(cfg), clang.getsystemversionflags(cfg))
  102. return flags
  103. end
  104. --
  105. -- Returns a list of defined preprocessor symbols, decorated for
  106. -- the compiler command line.
  107. --
  108. -- @param defines
  109. -- An array of preprocessor symbols to define; as an array of
  110. -- string values.
  111. -- @return
  112. -- An array of symbols with the appropriate flag decorations.
  113. --
  114. function clang.getdefines(defines)
  115. -- Just pass through to GCC for now
  116. local flags = gcc.getdefines(defines)
  117. return flags
  118. end
  119. function clang.getundefines(undefines)
  120. -- Just pass through to GCC for now
  121. local flags = gcc.getundefines(undefines)
  122. return flags
  123. end
  124. --
  125. -- Returns a list of forced include files, decorated for the compiler
  126. -- command line.
  127. --
  128. -- @param cfg
  129. -- The project configuration.
  130. -- @return
  131. -- An array of force include files with the appropriate flags.
  132. --
  133. function clang.getforceincludes(cfg)
  134. -- Just pass through to GCC for now
  135. local flags = gcc.getforceincludes(cfg)
  136. return flags
  137. end
  138. --
  139. -- Returns a list of include file search directories, decorated for
  140. -- the compiler command line.
  141. --
  142. -- @param cfg
  143. -- The project configuration.
  144. -- @param dirs
  145. -- An array of include file search directories; as an array of
  146. -- string values.
  147. -- @return
  148. -- An array of symbols with the appropriate flag decorations.
  149. --
  150. function clang.getincludedirs(cfg, dirs, sysdirs, frameworkdirs)
  151. -- Just pass through to GCC for now
  152. local flags = gcc.getincludedirs(cfg, dirs, sysdirs, frameworkdirs)
  153. return flags
  154. end
  155. clang.getrunpathdirs = gcc.getrunpathdirs
  156. --
  157. -- get the right output flag.
  158. --
  159. function clang.getsharedlibarg(cfg)
  160. return gcc.getsharedlibarg(cfg)
  161. end
  162. --
  163. -- Build a list of linker flags corresponding to the settings in
  164. -- a particular project configuration.
  165. --
  166. -- @param cfg
  167. -- The project configuration.
  168. -- @return
  169. -- An array of linker flags.
  170. --
  171. clang.ldflags = {
  172. architecture = {
  173. x86 = "-m32",
  174. x86_64 = "-m64",
  175. },
  176. flags = {
  177. LinkTimeOptimization = "-flto",
  178. },
  179. kind = {
  180. SharedLib = function(cfg)
  181. local r = { clang.getsharedlibarg(cfg) }
  182. if cfg.system == "windows" and not cfg.flags.NoImportLib then
  183. table.insert(r, '-Wl,--out-implib="' .. cfg.linktarget.relpath .. '"')
  184. elseif cfg.system == p.LINUX then
  185. table.insert(r, '-Wl,-soname=' .. p.quoted(cfg.linktarget.name))
  186. elseif table.contains(os.getSystemTags(cfg.system), "darwin") then
  187. table.insert(r, '-Wl,-install_name,' .. p.quoted('@rpath/' .. cfg.linktarget.name))
  188. end
  189. return r
  190. end,
  191. WindowedApp = function(cfg)
  192. if cfg.system == p.WINDOWS then return "-mwindows" end
  193. end,
  194. },
  195. system = {
  196. wii = "$(MACHDEP)",
  197. }
  198. }
  199. function clang.getldflags(cfg)
  200. local flags = config.mapFlags(cfg, clang.ldflags)
  201. return flags
  202. end
  203. --
  204. -- Build a list of additional library directories for a particular
  205. -- project configuration, decorated for the tool command line.
  206. --
  207. -- @param cfg
  208. -- The project configuration.
  209. -- @return
  210. -- An array of decorated additional library directories.
  211. --
  212. function clang.getLibraryDirectories(cfg)
  213. -- Just pass through to GCC for now
  214. local flags = gcc.getLibraryDirectories(cfg)
  215. return flags
  216. end
  217. --
  218. -- Build a list of libraries to be linked for a particular project
  219. -- configuration, decorated for the linker command line.
  220. --
  221. -- @param cfg
  222. -- The project configuration.
  223. -- @param systemOnly
  224. -- Boolean flag indicating whether to link only system libraries,
  225. -- or system libraries and sibling projects as well.
  226. -- @return
  227. -- A list of libraries to link, decorated for the linker.
  228. --
  229. function clang.getlinks(cfg, systemonly, nogroups)
  230. return gcc.getlinks(cfg, systemonly, nogroups)
  231. end
  232. --
  233. -- Return a list of makefile-specific configuration rules. This will
  234. -- be going away when I get a chance to overhaul these adapters.
  235. --
  236. -- @param cfg
  237. -- The project configuration.
  238. -- @return
  239. -- A list of additional makefile rules.
  240. --
  241. function clang.getmakesettings(cfg)
  242. -- Just pass through to GCC for now
  243. local flags = gcc.getmakesettings(cfg)
  244. return flags
  245. end
  246. --
  247. -- Retrieves the executable command name for a tool, based on the
  248. -- provided configuration and the operating environment. I will
  249. -- be moving these into global configuration blocks when I get
  250. -- the chance.
  251. --
  252. -- @param cfg
  253. -- The configuration to query.
  254. -- @param tool
  255. -- The tool to fetch, one of "cc" for the C compiler, "cxx" for
  256. -- the C++ compiler, or "ar" for the static linker.
  257. -- @return
  258. -- The executable command name for a tool, or nil if the system's
  259. -- default value should be used.
  260. --
  261. clang.tools = {
  262. cc = "clang",
  263. cxx = "clang++",
  264. ar = function(cfg) return iif(cfg.flags.LinkTimeOptimization, "llvm-ar", "ar") end
  265. }
  266. function clang.gettoolname(cfg, tool)
  267. local value = clang.tools[tool]
  268. if type(value) == "function" then
  269. value = value(cfg)
  270. end
  271. return value
  272. end