release 0.1.6
[liba.git] / xmake.lua
blob7ca894cd7b9ea4267408048676d496a7d7a1e980
1 ---@diagnostic disable
2 -- set project name
3 set_project("liba")
5 -- set xmake minimum version
6 set_xmakever("2.5.0")
8 -- set project version
9 set_version("0.1.6", { build = "%Y%m%d" })
11 -- option: liba-cxx
12 option("liba-cxx")
13 set_default(true)
14 set_showmenu(true)
15 set_category("liba")
16 set_description("Enable/Disable c++")
17 option_end()
19 -- option: warning
20 option("warning")
21 set_default(false)
22 set_showmenu(true)
23 set_description("Enable/Disable warnings")
24 option_end()
26 if has_config("warning") then
27 -- set warning everything
28 set_warnings("everything")
29 -- disable some compiler errors
30 if is_plat("windows") then
31 add_cxflags("/wd4464", "/wd4514", "/wd4710", "/wd4711", "/wd4820", "/wd5039", "/wd5045")
32 end
33 add_cflags("-Wno-declaration-after-statement")
34 add_cxxflags("-Wno-c++98-compat-pedantic")
35 add_cxflags("-Wno-unsafe-buffer-usage")
36 end
38 -- add build modes
39 add_rules("mode.check", "mode.debug", "mode.release")
40 if is_mode("check") and not is_plat("mingw") then
41 local flags = {
42 "-fsanitize=address,undefined",
43 "-fsanitize-recover=address",
44 "-fno-omit-frame-pointer",
45 "-fsanitize=leak",
47 add_cxflags(flags)
48 add_ldflags(flags)
49 end
51 -- option: float
52 option("liba-float")
53 set_default("8")
54 set_showmenu(true)
55 set_category("liba")
56 set_values("4", "8", "16")
57 set_description("floating-point number bytes")
58 option_end()
60 -- option: rpath
61 option("liba-rpath")
62 set_showmenu(true)
63 set_category("liba")
64 set_description("dynamic library search path")
65 option_end()
67 if xmake.version():ge("2.8.5") then
68 includes("@builtin/check")
69 else
70 includes("check_csnippets.lua")
71 includes("check_cincludes.lua")
72 includes("check_cfuncs.lua")
73 end
75 configvar_check_sizeof = configvar_check_sizeof
76 or function(define_name, type_name)
77 configvar_check_csnippets(
78 define_name,
79 'printf("%u", sizeof(' .. type_name .. "));return 0;",
80 { output = true, number = true }
82 end
84 target("a")
85 -- make as a collection of objects
86 set_kind("object")
87 -- detect c code functions
88 configvar_check_sizeof("A_SIZE_POINTER", "void *")
89 configvar_check_csnippets(
90 "A_BYTE_ORDER",
91 'int x = 1; puts(*(char *)&x ? "1234" : "4321");',
92 { output = true, number = true }
94 float = get_config("liba-float")
95 function check_math(funcs, opt)
96 for i, func in pairs(funcs) do
97 local have = "A_HAVE_" .. string.upper(func)
98 if float == "16" then
99 func = func .. "l"
101 if float == "4" then
102 func = func .. "f"
104 configvar_check_cfuncs(have, func, opt)
107 local funcs = { "expm1", "log1p", "hypot", "atan2" }
108 check_math(funcs, { includes = "math.h" })
109 -- stylua: ignore
110 local funcs = { "csqrt",
111 "cpow", "cexp", "clog",
112 "csin", "ccos", "ctan",
113 "csinh", "ccosh", "ctanh",
114 "casin", "cacos", "catan",
115 "casinh", "cacosh", "catanh",
117 check_math(funcs, { includes = "complex.h" })
118 -- set the auto-generated a.xmake.h
119 a_have_h = path.relative(os.projectdir() .. "/$(buildir)/a.xmake.h", "include/a")
120 add_defines('A_HAVE_H="' .. a_have_h .. '"', { public = true })
121 set_configvar("XMAKE_VERSION", tostring(xmake.version()))
122 set_configvar("A_SIZE_FLOAT", float, { quote = false })
123 add_configfiles("include/a.xmake.h.in")
124 -- add include directories
125 add_includedirs("include", { public = true })
126 -- set export library symbols
127 add_defines("A_EXPORTS")
128 -- add the common source files
129 add_files("src/**.c")
130 if not table.empty(os.files("src/**.cpp")) and has_config("liba-cxx") then
131 add_files("src/**.cpp")
133 -- add the platform options
134 rpath = get_config("liba-rpath")
135 if rpath then
136 add_linkdirs(rpath, { public = true })
137 add_rpathdirs(rpath, { public = true })
139 if not is_plat("windows", "mingw") then
140 add_syslinks("m", { public = true })
141 add_cxflags("-fPIC")
143 after_build(function(target)
144 import("core.tool.linker")
145 local object = target:objectfiles()
146 local output = path.join(target:targetdir(), "liba.o")
147 local program = linker.linkargv("binary", "cc", object, output, { target = target })
148 try({
149 function()
150 os.runv(program, table.join("-nostdlib", "-o", output, "-r", object))
151 end,
153 end)
154 after_install(function(target)
155 local output = path.join(target:installdir(), "lib", "liba.o")
156 os.trycp(path.join(target:targetdir(), "liba.o"), output)
157 end)
158 after_clean(function(target)
159 os.tryrm(path.join(target:targetdir(), "liba.o"))
160 end)
161 target_end()
163 target("alib")
164 set_basename("a")
165 -- make as a static library
166 set_kind("static")
167 -- add the header files for installing
168 add_headerfiles("include/(**.h)")
169 add_headerfiles("$(buildir)/a.xmake.h", { prefixdir = "a" })
170 if not table.empty(os.files("include/**.hpp")) and has_config("liba-cxx") then
171 add_headerfiles("include/(**.hpp)")
173 on_load(function(target)
174 target:set("links", target:targetfile(), { interface = true })
175 end)
176 after_install(function(target)
177 if target:installdir() then
178 local cur = "#if defined(A_HAVE_H)"
179 local new = '#include "a.xmake.h"\n' .. cur
180 local includedir = path.join(target:installdir(), "include")
181 io.replace(path.join(includedir, "a", "a.h"), cur, new, { plain = true })
183 end)
184 -- add the dependent target
185 add_deps("a")
186 target_end()
188 target("liba")
189 -- make as a shared library
190 set_kind("shared")
191 -- add the platform options
192 if not is_plat("windows") then
193 set_prefixname("lib")
194 set_basename("a")
196 on_load(function(target)
197 local liba = target:targetfile()
198 if target:is_plat("windows") then
199 liba = liba:gsub(".dll", ".lib")
200 elseif target:is_plat("mingw") then
201 liba = liba .. ".a"
203 target:set("links", liba, { interface = true })
204 end)
205 add_defines("A_IMPORTS", { interface = true })
206 -- add the dependent target
207 add_deps("a")
208 target_end()
210 -- option: liba-rust
211 option("liba-rust")
212 set_default(false)
213 set_showmenu(true)
214 set_category("liba")
215 set_description("Enable/Disable rust")
216 option_end()
218 if has_config("liba-rust") then
219 target("ars")
220 add_deps("a")
221 set_kind("static")
222 set_basename("liba")
223 add_files("src/lib.rs")
224 if get_config("liba-float") == "4" then
225 add_rcflags('--cfg=feature="float"')
227 target_end()
230 -- include module sources
231 if not table.empty(os.files("*/xmake.lua")) then
232 includes("*/xmake.lua")