Skip to content

Commit

Permalink
Merge pull request #235 from tboox/check
Browse files Browse the repository at this point in the history
Improve to check interfaces
  • Loading branch information
waruqi authored Sep 13, 2023
2 parents dfede90 + f6267af commit 848051b
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 236 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/mingw_msys2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
- name: Prepare
shell: msys2 {0}
run: |
git clone https://github.com/xmake-io/xmake.git --recurse-submodules -b dev
cd xmake
git clone https://github.com/xmake-io/xmake.git --recurse-submodules -b dev xmakesrc
cd xmakesrc
./configure
make build
make install PREFIX=${{ matrix.prefix }}
Expand Down
2 changes: 1 addition & 1 deletion src/tbox/micro.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@ target("tbox")
end

-- check interfaces
check_interfaces()
on_config("check_interfaces")
2 changes: 1 addition & 1 deletion src/tbox/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,4 @@ target("tbox")
end

-- check interfaces
check_interfaces()
on_config("check_interfaces")
231 changes: 0 additions & 231 deletions src/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,237 +136,6 @@ for idx, require_name in ipairs({"zlib", "sqlite3", "mysql", "mbedtls 2.13.*", "
end
end

-- get function name
--
-- sigsetjmp
-- sigsetjmp((void*)0, 0)
-- sigsetjmp{sigsetjmp((void*)0, 0);}
-- sigsetjmp{int a = 0; sigsetjmp((void*)a, a);}
--
function get_function_name(func)
local name, code = string.match(func, "(.+){(.+)}")
if code == nil then
local pos = func:find("%(")
if pos then
name = func:sub(1, pos - 1)
else
name = func
end
end
return name:trim()
end

-- check c functions in the given module
function check_module_cfuncs(module, includes, ...)
for _, func in ipairs({...}) do
local funcname = get_function_name(func)
configvar_check_cfuncs(("TB_CONFIG_%s_HAVE_%s"):format(module:upper(), funcname:upper()), func,
{name = module .. "_" .. funcname,
includes = includes,
defines = "_GNU_SOURCE=1",
warnings = "error",
languages = stdc})
end
end

-- check c snippet in the given module
function check_module_csnippet(module, includes, name, snippet, opt)
configvar_check_csnippets(("TB_CONFIG_%s_HAVE_%s"):format(module:upper(), name:upper()), snippet, table.join({name = module .. "_" .. name, includes = includes, defines = "_GNU_SOURCE=1", languages = stdc}, opt))
end

-- check interfaces
function check_interfaces()

-- check the interfaces for libc
check_module_cfuncs("libc", {"string.h", "stdlib.h"},
"memcpy",
"memset",
"memmove",
"memcmp",
"memmem",
"strcat",
"strncat",
"strcpy",
"strncpy",
"strlcpy",
"strlen",
"strnlen",
"strstr",
"strchr",
"strrchr",
"strcasestr",
"strcmp",
"strcasecmp",
"strncmp",
"strncasecmp")
check_module_cfuncs("libc", {"wchar.h", "stdlib.h"},
"wcscat",
"wcsncat",
"wcscpy",
"wcsncpy",
"wcslcpy",
"wcslen",
"wcsnlen",
"wcsstr",
"wcscasestr",
"wcscmp",
"wcscasecmp",
"wcsncmp",
"wcsncasecmp",
"wcstombs",
"mbstowcs")
check_module_cfuncs("libc", "time.h", "gmtime", "mktime", "localtime")
check_module_cfuncs("libc", "sys/time.h", "gettimeofday")
check_module_cfuncs("libc", {"signal.h", "setjmp.h"}, "signal", "setjmp", "sigsetjmp{sigjmp_buf buf; sigsetjmp(buf, 0);}", "kill")
check_module_cfuncs("libc", "execinfo.h", "backtrace")
check_module_cfuncs("libc", "locale.h", "setlocale")
check_module_cfuncs("libc", "stdio.h", "fputs", "fgets", "fgetc", "ungetc", "fputc", "fread", "fwrite")
check_module_cfuncs("libc", "stdlib.h", "srandom", "random")

-- add the interfaces for libm
check_module_cfuncs("libm", "math.h",
"sincos",
"sincosf",
"log2",
"log2f",
"sqrt",
"sqrtf",
"acos",
"acosf",
"asin",
"asinf",
"pow",
"powf",
"fmod",
"fmodf",
"tan",
"tanf",
"atan",
"atanf",
"atan2",
"atan2f",
"cos",
"cosf",
"sin",
"sinf",
"exp",
"expf")

-- add the interfaces for posix
if not is_plat("windows") then
check_module_cfuncs("posix", {"poll.h", "sys/socket.h"}, "poll")
check_module_cfuncs("posix", {"sys/select.h"}, "select")
check_module_cfuncs("posix", "pthread.h",
"pthread_mutex_init",
"pthread_create",
"pthread_setspecific",
"pthread_getspecific",
"pthread_key_create",
"pthread_key_delete",
"pthread_setaffinity_np") -- need _GNU_SOURCE
check_module_cfuncs("posix", {"sys/socket.h", "fcntl.h"}, "socket")
check_module_cfuncs("posix", "dirent.h", "opendir")
check_module_cfuncs("posix", "dlfcn.h", "dlopen")
check_module_cfuncs("posix", {"sys/stat.h", "fcntl.h"}, "open", "stat64", "lstat64")
check_module_cfuncs("posix", "unistd.h", "gethostname")
check_module_cfuncs("posix", "ifaddrs.h", "getifaddrs")
check_module_cfuncs("posix", "semaphore.h", "sem_init")
check_module_cfuncs("posix", "unistd.h", "getpagesize", "sysconf")
check_module_cfuncs("posix", "sched.h", "sched_yield", "sched_setaffinity") -- need _GNU_SOURCE
check_module_cfuncs("posix", "regex.h", "regcomp", "regexec")
check_module_cfuncs("posix", "sys/uio.h", "readv", "writev", "preadv", "pwritev")
check_module_cfuncs("posix", "unistd.h", "pread64", "pwrite64")
check_module_cfuncs("posix", "unistd.h", "fdatasync")
check_module_cfuncs("posix", "copyfile.h", "copyfile")
check_module_cfuncs("posix", "sys/sendfile.h", "sendfile")
check_module_cfuncs("posix", "sys/epoll.h", "epoll_create", "epoll_wait")
check_module_cfuncs("posix", "spawn.h", "posix_spawnp", "posix_spawn_file_actions_addchdir_np")
check_module_cfuncs("posix", "unistd.h", "execvp", "execvpe", "fork", "vfork")
check_module_cfuncs("posix", "sys/wait.h", "waitpid")
check_module_cfuncs("posix", "unistd.h", "getdtablesize")
check_module_cfuncs("posix", "sys/resource.h", "getrlimit")
check_module_cfuncs("posix", "netdb.h", "getaddrinfo", "getnameinfo", "gethostbyname", "gethostbyaddr")
check_module_cfuncs("posix", "fcntl.h", "fcntl")
check_module_cfuncs("posix", "unistd.h", "pipe", "pipe2")
check_module_cfuncs("posix", "sys/stat.h", "mkfifo")
check_module_cfuncs("posix", "sys/mman.h", "mmap")
check_module_cfuncs("posix", "sys/stat.h", "futimens", "utimensat")
end

-- add the interfaces for windows/msvc
if is_plat("windows") then
for _, mo in ipairs({"", "_nf", "_acq", "_rel"}) do
check_module_csnippet("windows", "windows.h", "_InterlockedExchange" .. mo, format([[
LONG _InterlockedExchange%s(LONG volatile* Destination, LONG Value);
#pragma intrinsic(_InterlockedExchange%s)
void test() {
_InterlockedExchange%s(0, 0);
}]], mo, mo, mo), {cxflags = "-WX -W3"})
check_module_csnippet("windows", "windows.h", "_InterlockedExchange8" .. mo, format([[
CHAR _InterlockedExchange8%s(CHAR volatile* Destination, CHAR Value);
#pragma intrinsic(_InterlockedExchange8%s)
void test() {
_InterlockedExchange8%s(0, 0);
}]], mo, mo, mo), {cxflags = "-WX -W3"})
check_module_csnippet("windows", "windows.h", "_InterlockedOr8" .. mo, format([[
CHAR _InterlockedOr8%s(CHAR volatile* Destination, CHAR Value);
#pragma intrinsic(_InterlockedOr8%s)
void test() {
_InterlockedOr8%s(0, 0);
}]], mo, mo, mo), {cxflags = "-WX -W3"})
check_module_csnippet("windows", "windows.h", "_InterlockedExchangeAdd" .. mo, format([[
LONG _InterlockedExchangeAdd%s(LONG volatile* Destination, LONG Value);
#pragma intrinsic(_InterlockedExchangeAdd%s)
void test() {
_InterlockedExchangeAdd%s(0, 0);
}]], mo, mo, mo), {cxflags = "-WX -W3"})
check_module_csnippet("windows", "windows.h", "_InterlockedExchangeAdd64" .. mo, format([[
__int64 _InterlockedExchangeAdd64%s(__int64 volatile* Destination, __int64 Value);
#pragma intrinsic(_InterlockedExchangeAdd64%s)
void test() {
_InterlockedExchangeAdd64%s(0, 0);
}]], mo, mo, mo), {cxflags = "-WX -W3"})
check_module_csnippet("windows", "windows.h", "_InterlockedCompareExchange" .. mo, format([[
LONG _InterlockedCompareExchange%s(LONG volatile* Destination, LONG Exchange, LONG Comperand);
#pragma intrinsic(_InterlockedCompareExchange%s)
void test() {
_InterlockedCompareExchange%s(0, 0, 0);
}]], mo, mo, mo), {cxflags = "-WX -W3"})
check_module_csnippet("windows", "windows.h", "_InterlockedCompareExchange64" .. mo, format([[
__int64 _InterlockedCompareExchange64%s(__int64 volatile* Destination, __int64 Exchange, __int64 Comperand);
#pragma intrinsic(_InterlockedCompareExchange64%s)
void test() {
_InterlockedCompareExchange64%s(0, 0, 0);
}]], mo, mo, mo), {cxflags = "-WX -W3"})
end
end

-- add the interfaces for bsd
if not is_plat("windows") then
check_module_cfuncs("bsd", {"sys/file.h", "fcntl.h"}, "flock")
end

-- add the interfaces for systemv
if not is_plat("windows") then
check_module_cfuncs("systemv", {"sys/sem.h", "sys/ipc.h"}, "semget", "semtimedop")
end

-- add the interfaces for linux
if is_plat("linux", "android") then
check_module_cfuncs("linux", {"sys/inotify.h"}, "inotify_init")
end

-- add the interfaces for valgrind
check_module_cfuncs("valgrind", "valgrind/valgrind.h", "VALGRIND_STACK_REGISTER(0, 0)")

-- check __thread keyword
configvar_check_csnippets("TB_CONFIG_KEYWORD_HAVE__thread", "__thread int a = 0;", {name = "keyword_thread", links = "pthread", languages = stdc})
configvar_check_csnippets("TB_CONFIG_KEYWORD_HAVE_Thread_local", "_Thread_local int a = 0;", {name = "keyword_thread_local", links = "pthread", languages = stdc})

-- check anonymous union feature
configvar_check_csnippets("TB_CONFIG_FEATURE_HAVE_ANONYMOUS_UNION", "void test() { struct __st { union {int dummy;};} a; a.dummy = 1; }", {name = "feature_anonymous_union", languages = stdc})
end

-- include project directories
includes(format("tbox/%s.lua", (has_config("micro") and "micro" or "xmake")))
if has_config("demo") then
Expand Down
5 changes: 4 additions & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set_project("tbox")

-- set xmake minimum version
set_xmakever("2.6.1")
set_xmakever("2.8.2")

-- set project version
set_version("1.7.4", {build = "%Y%m%d", soname = true})
Expand All @@ -18,6 +18,9 @@ set_languages(stdc)
set_configvar("_GNU_SOURCE", 1)
set_configvar("_REENTRANT", 1)

-- add module directories
add_moduledirs("xmake")

-- disable some compiler errors
add_cxflags("-Wno-error=deprecated-declarations", "-fno-strict-aliasing", "-Wno-error=expansion-to-defined", "-Wno-error=empty-body")
add_mxflags("-Wno-error=deprecated-declarations", "-fno-strict-aliasing", "-Wno-error=expansion-to-defined", "-Wno-error=empty-body")
Expand Down
Loading

0 comments on commit 848051b

Please sign in to comment.