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

請問 option 裡面的 add_cxxsnippet 和 add_csnippet 使用方法? #322

Closed
orzuk-lmj opened this Issue Jan 10, 2019 · 7 comments

Comments

Projects
None yet
2 participants
@orzuk-lmj
Copy link
Contributor

orzuk-lmj commented Jan 10, 2019

  • 目前使用的是git上的 xmake SHA-1: 2b5599a 這個版本

  • 測試腳本為:

    option("TestOP")
        add_cxxsnippet(
           [[
               constexpr int f(int x) {
                   int sum=0;
                   for (int i=0; i<=x; ++i)
                       sum += i;
                   return sum;
               }
               constexpr int x = f(5);
               static_assert(x == 15);
           ]]
        )
  • 錯誤訊息是

    xmake/bin/core/base/os.lua:705: add_cxxsnippet(nil): invalid value type!
    
    stack traceback:
        [C]: in function 'error'
        [...xmake\bin/core/base/os.lua:705]: in function 'raise'
        [...xmake\bin/core/base/interpreter.lua:1301]: in function 'add_cxxsnippet'
        [...sources\xmake.lua:41]: in main chunk
@waruqi

This comment has been minimized.

Copy link
Member

waruqi commented Jan 10, 2019

你需要设置先cxsnippet的名字,例如

option("TestOP")
    set_languages("c++14")
    add_cxxsnippet("constexpr", [[
           constexpr int f(int x) {
               int sum=0;
               for (int i=0; i<=x; ++i)
                   sum += i;
               return sum;
           }
           constexpr int x = f(5);
           static_assert(x == 15);
       ]])

不过之前的版本check_cxsnippets处理和回显信息上,确实有点小问题,我改进了下,你可以更新下dev分支后再试试,上面的设置。。

文档上可能没更新这块,我一会改下。。

@waruqi

This comment has been minimized.

Copy link
Member

waruqi commented Jan 10, 2019

不过如果一些常用特性,可以直接用has_feature,这个不需要尝试编译,检测速度非常快,可以看看 https://xmake.io/#/zh/manual?id=compiler-features

之后我可以考虑在option上增加 add_features来快速检测编译器特性

@orzuk-lmj

This comment has been minimized.

Copy link
Contributor Author

orzuk-lmj commented Jan 10, 2019

感謝,我正在移植一些第三方庫,使用option去檢測可使用的功能片段,然後用 set_config_header 寫出設定檔。過程還算順利,真心覺得XMAKE很讚。

但是目前還遇到一個小問題,打算檢測當前target是否為shared,若是則添加 #defined BUILD_DLL 1 到config.h 內,暫時卡在 target:on_load 內不知道如何寫 add_defines_h

@waruqi

This comment has been minimized.

Copy link
Member

waruqi commented Jan 10, 2019

你试试

target("test")
    set_kind("$(kind)")
    set_config_header("config.h")
    on_load(function (target)
        if target:targetkind() == "shared" then
            target:add("defines_h", "BUILD_DLL=1")
        end
    end)
@orzuk-lmj

This comment has been minimized.

Copy link
Contributor Author

orzuk-lmj commented Jan 10, 2019

感謝

@orzuk-lmj orzuk-lmj closed this Jan 10, 2019

@waruqi waruqi added this to the v2.2.4 milestone Jan 11, 2019

@waruqi waruqi added the improvement label Jan 11, 2019

@waruqi waruqi reopened this Jan 11, 2019

@waruqi

This comment has been minimized.

Copy link
Member

waruqi commented Jan 11, 2019

我新增了 add_features 接口到option,用于快速检测一些编译器特性,不需要尝试编译,速度会非常快,你可以更新dev分支版本试试:

option("TestOP")
    set_languages("c++14")
    add_features("c_static_assert", "cxx_constexpr")
@waruqi

This comment has been minimized.

Copy link
Member

waruqi commented Jan 11, 2019

并且我重命名了下add_cxxsnippetadd_csnippet接口到add_cxxsnippetsadd_csnippets,因为这个接口也是支持添加多个代码片段的,应该尽量按照命名规范,之前设计的时候没考虑周到。。

例如:

option("TestOP")
    set_languages("c++14")
    add_csnippets("name", "code snippet")
    add_cxxsnippets {constexpr =  "code snippet1", xxxx = "code snippet2", zzzz = "code snippet3"}

当然之前的接口,还是保留向下兼容的,使用的时候,会给出一个警告信息,建议尽量使用我刚提供的新接口。

由于add_cxxsnippet目前用的人非常少,预计迭代一个大版本后(2.3.x),我才会移除它。。

相关文档我会在v2.2.4发版前更新。

@waruqi waruqi added the new feature label Jan 11, 2019

@waruqi waruqi closed this Jan 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.