Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some builtin help functions for includes(), e.g. check_xxx #342

Closed
13 tasks done
waruqi opened this issue Feb 6, 2019 · 7 comments
Closed
13 tasks done

Add some builtin help functions for includes(), e.g. check_xxx #342

waruqi opened this issue Feb 6, 2019 · 7 comments
Milestone

Comments

@waruqi
Copy link
Member

waruqi commented Feb 6, 2019

Add some builtin help functions to simplify checking code using option("xx")

References

Roadmap

includes("xxx.lua")

  • check_cfuncs.lua
  • check_cincludes.lua
  • check_csnippets.lua
  • check_ctypes.lua
  • check_cflags.lua
  • check_cxxfuncs.lua
  • check_cxxincludes.lua
  • check_cxxsnippets.lua
  • check_cxxtypes.lua
  • check_cxxflags.lua
  • check_features.lua
  • check_links.lua
  • find_and_add_packages.lua

The search pathes of including lua scripts

projectdir/xmake.lua

includes("check_features.lua")
target("test")
    check_features("HAS_CONSTEXPR", "cxx_constexpr")

Search pathes

  1. find projectdir/check_features.lua (current project directory)
  2. find xmake/includes/check_features.lua (xmake program directory)
@waruqi waruqi added this to the v2.2.4 milestone Feb 6, 2019
@waruqi
Copy link
Member Author

waruqi commented Feb 6, 2019

Examples

Check links, ctype, includes and features and write macro definitions to the config.h file.

includes("check_links.lua")
includes("check_ctypes.lua")
includes("check_cfuncs.lua")
includes("check_features.lua")
includes("check_csnippets.lua")
includes("check_cincludes.lua")

target("test")
    set_kind("binary")
    add_files("*.c")
    add_configfiles("config.h.in")

    configvar_check_ctypes("HAS_WCHAR", "wchar_t")
    configvar_check_cincludes("HAS_STRING_H", "string.h")
    configvar_check_cincludes("HAS_STRING_AND_STDIO_H", {"string.h", "stdio.h"})
    configvar_check_ctypes("HAS_WCHAR_AND_FLOAT", {"wchar_t", "float"})
    configvar_check_links("HAS_PTHREAD", {"pthread", "m", "dl"})
    configvar_check_csnippets("HAS_STATIC_ASSERT", "_Static_assert(1, \"\");")
    configvar_check_cfuncs("HAS_SETJMP", "setjmp", {includes = {"signal.h", "setjmp.h"}})
    configvar_check_features("HAS_CONSTEXPR", "cxx_constexpr")
    configvar_check_features("HAS_CONSEXPR_AND_STATIC_ASSERT", {"cxx_constexpr", "c_static_assert"}, {languages = "c++11"})

config.h.in

${define HAS_STRING_H}
${define HAS_STRING_AND_STDIO_H}
${define HAS_WCHAR}
${define HAS_WCHAR_AND_FLOAT}
${define HAS_PTHREAD}
${define HAS_STATIC_ASSERT}
${define HAS_SETJMP}
${define HAS_CONSTEXPR}
${define HAS_CONSEXPR_AND_STATIC_ASSERT}

config.h

/* #undef HAS_STRING_H */
#define HAS_STRING_AND_STDIO_H 1
/* #undef HAS_WCHAR */
/* #undef HAS_WCHAR_AND_FLOAT */
#define HAS_PTHREAD 1
#define HAS_STATIC_ASSERT 1
#define HAS_SETJMP 1
/* #undef HAS_CONSTEXPR */
#define HAS_CONSEXPR_AND_STATIC_ASSERT 1

@waruqi
Copy link
Member Author

waruqi commented Feb 6, 2019

Examples

Check links, ctype, includes and features and add macro definitions to the given target.

includes("check_links.lua")
includes("check_ctypes.lua")
includes("check_cfuncs.lua")
includes("check_features.lua")
includes("check_csnippets.lua")
includes("check_cincludes.lua")

target("test")
    set_kind("binary")
    add_files("*.c")

    check_ctypes("HAS_WCHAR", "wchar_t")
    check_cincludes("HAS_STRING_H", "string.h")
    check_cincludes("HAS_STRING_AND_STDIO_H", {"string.h", "stdio.h"})
    check_ctypes("HAS_WCHAR_AND_FLOAT", {"wchar_t", "float"})
    check_links("HAS_PTHREAD", {"pthread", "m", "dl"})
    check_csnippets("HAS_STATIC_ASSERT", "_Static_assert(1, \"\");")
    check_cfuncs("HAS_SETJMP", "setjmp", {includes = {"signal.h", "setjmp.h"}})
    check_features("HAS_CONSTEXPR", "cxx_constexpr")
    check_features("HAS_CONSEXPR_AND_STATIC_ASSERT", {"cxx_constexpr", "c_static_assert"}, {languages = "c++11"})
/usr/local/bin/ccache xcrun -sdk macosx clang -c -DHAS_STRING_H -DHAS_STATIC_ASSERT -DHAS_CONSTEXPR -Qunused-arguments -arch
x86_64 -fpascal-strings -fmessage-length=0 -isysroot /Applications/Xcode.app/Contents/Dev
eloper/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -o build/.objs/test/macos
x/x86_64/release/./main.c.o main.c

@waruqi
Copy link
Member Author

waruqi commented Feb 6, 2019

Implemention

-- check c funcs and add macro definition 
--
-- the function syntax
--  - sigsetjmp
--  - sigsetjmp((void*)0, 0)
--  - sigsetjmp{sigsetjmp((void*)0, 0);}
--  - sigsetjmp{int a = 0; sigsetjmp((void*)a, a);}
--
-- e.g.
--
-- check_cfuncs("HAS_SETJMP", "setjmp", {includes = {"signal.h", "setjmp.h"}, links = {}})
-- check_cfuncs("HAS_SETJMP", {"setjmp", "sigsetjmp{sigsetjmp((void*)0, 0);}"})
--
function check_cfuncs(definition, funcs, opt)
    opt = opt or {}
    option(opt.name or definition)
        add_cfuncs(funcs)
        add_defines(definition)
        if opt.links then
            add_links(opt.links)
        end
        if opt.includes then
            add_cincludes(opt.includes)
        end
        if opt.languages then
            set_languages(opt.languages)
        end
        if opt.cflags then
            add_cflags(opt.cflags)
        end
        if opt.cflags then
            add_cxflags(opt.cxflags)
        end
    option_end()
    add_options(opt.name or definition)
end

-- check c funcs and add macro definition to the configuration files 
--
-- e.g.
--
-- configvar_check_cfuncs("HAS_SETJMP", "setjmp", {includes = {"signal.h", "setjmp.h"}, links = {}})
-- configvar_check_cfuncs("HAS_SETJMP", {"setjmp", "sigsetjmp{sigsetjmp((void*)0, 0);}"})
--
function configvar_check_cfuncs(definition, funcs, opt)
    opt = opt or {}
    option(opt.name or definition)
        add_cfuncs(funcs)
        set_configvar(definition, 1)
        if opt.links then
            add_links(opt.links)
        end
        if opt.includes then
            add_cincludes(opt.includes)
        end
        if opt.languages then
            set_languages(opt.languages)
        end
        if opt.cflags then
            add_cflags(opt.cflags)
        end
        if opt.cxflags then
            add_cxflags(opt.cxflags)
        end
    option_end()
    add_options(opt.name or definition)
end

@waruqi
Copy link
Member Author

waruqi commented Feb 12, 2019

Set the custom alias:

    configvar_check_cincludes("HAS_STRING_H", "string.h", {name = "string_h"})

show:

checking string_h ...

@waruqi waruqi changed the title Add some builtin help functions for includes() Add some builtin help functions for includes(), e.g. check_xxx Feb 17, 2019
@waruqi
Copy link
Member Author

waruqi commented Feb 17, 2019

includes("check_cflags.lua")
includes("check_cxxflags.lua")

target("test")   
    check_cxxflags("HAS_SSE2", "-msse2")
    configvar_check_cflags("HAS_SSE2", "-msse2")

@waruqi
Copy link
Member Author

waruqi commented Feb 18, 2019

Custom variable value

    configvar_check_cflags("SSE=2", "-msse2")

config.h.in

#define SSE ${default SSE 0}

config.h

#define SSE 2
// #define SSE 0

@waruqi
Copy link
Member Author

waruqi commented Feb 19, 2019

 includes("find_and_add_packages.lua")
 target("test")
     set_kind("binary")
     add_files("src/*.c")
     find_and_add_packages("brew::pcre2/libpcre2-8", "zlib")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant