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

Simplify getting output of c/c++ snippets #1547

Closed
xq114 opened this issue Aug 5, 2021 · 2 comments
Closed

Simplify getting output of c/c++ snippets #1547

xq114 opened this issue Aug 5, 2021 · 2 comments

Comments

@xq114
Copy link
Contributor

xq114 commented Aug 5, 2021

你在什么场景下需要该功能?

目前xmake只能用check_c[xx]snippet测试一段代码能否正常编译,但无法比较容易的获取一段代码的输出,只能写一个target在after_build里面获取变量;然而使用这种方法时,必须还要保证先编译运行测试代码,再配置其他target,并且还要调整install时的规则,相当麻烦

例如,在编译时打印size type:

> xmake
checking sizeof int: 4
checking sizeof long: 4
checking sizeof long long: 8
....

就没法很好的实现;测试dll存在性

#include <windows.h>
HMODULE m = LoadLibrary("vulkan-1.dll");
FARPROC test = GetProcAddress(m, "vkCreateShader");

也没法很好解决(必须运行时才能发现找到的dll是否符合要求)

还有很多一系列场景都与运行一段代码有关

描述可能的解决方案

include("run_c[xx]snippets")
local ok, result = run_c[xx]snippets([[snippet]], {cxflags = "..."})

描述你认为的候选方案

include("check_c[xx]snippets")
local ok, result = check_c[xx]snippets([[snippet]], {output = true, cxflags = "..."})
@waruqi waruqi added this to the v2.5.7 milestone Aug 7, 2021
@waruqi
Copy link
Member

waruqi commented Aug 9, 2021

初步支持了,可以先试下:#1562

添加 {tryrun = true}{output = true} 两个选项给 add_csnippets/add_cxxnippets

option("test")
    add_csnippets("HAS_INT_4", "return (sizeof(int) == 4)? 0 : -1;", {tryrun = true})

option("test")
    add_csnippets("INT_SIZE", 'printf("%d", sizeof(int)); return 0;', {output = true, number = true})

仅仅测试运行,不捕获输出

tryrun = true

运行并且捕获输出

output = true

注: 设置为捕获输出,当前 option 不能再设置其他 snippets

我们也可以通过 is_config 获取绑定到option的输出

if is_config("test", "8") tben
    -- xxx
end

辅助接口

includes("check_csnippets.lua")

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

    check_csnippets("HAS_INT_4", "return (sizeof(int) == 4)? 0 : -1;", {tryrun = true})
    check_csnippets("INT_SIZE", 'printf("%d", sizeof(int)); return 0;', {output = true, number = true})
    configvar_check_csnippets("HAS_LONG_8", "return (sizeof(long) == 8)? 0 : -1;", {tryrun = true})
    configvar_check_csnippets("PTR_SIZE", 'printf("%d", sizeof(void*)); return 0;', {output = true, number = true})

设置为 output = true,自动追加 stdio.h 所以不用额外设置

tryrun 模式,最后必须有个 return 0; 或者 return -1; 来返回 main 的 exitcode 用于控制检测状态。

如果额外设置了 number = true,那么会作为数值定义而不是字符串,#define INT_SIZE 4

@waruqi
Copy link
Member

waruqi commented Aug 9, 2021

@waruqi waruqi closed this as completed Aug 9, 2021
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

2 participants