xmake Mastering 8: compilation mode switch

xmake Lua is a lightweight modern c / c ++ project build tools based on the main features are: grammar simple and approachable, to provide a more readable project maintenance, building experience to achieve consistent cross-platform behavior.

In this article we will detail at how to switch debug / release and other commonly used building mode in the project build process, as well as other custom compilation mode.

Debug and release mode

Generally, if we are through xmake createproject command created a line compiler will automatically add the configuration rules in xmake.lua inside, as follows:

add_rules("mode.release", "mode.debug")
target("hello")
    set_kind("binary")
    add_files("src/*.c")

Through the add_rulesinterface, we default to the release and debug two commonly used built-in rules, they will be included with some compiler flags associated with the corresponding pattern at compile time to turn on debugging optimized for publishing or compiling.

If only the implementation of the xmakeorder, no additional configuration, the default will be release compiled equivalent to:

$ xmake f -m release
$ xmake
[  0%]: ccache compiling.release src/main.cpp
[100%]: linking.release test
build ok!

If we want to switch to debug compilation mode, you only need:

$ xmake f -m debug
$ xmake
[  0%]: ccache compiling.debug src/main.cpp
[100%]: linking.debug test
build ok!

The above -m/--mode=parameter is used to set the compilation mode, will talk mode.releaseand mode.debugdo associate the two rules.

So, how are they linked on it? We can first look at the internal implementation of these two rules:

rule("mode.debug")
    after_load(function (target)
        if is_mode("debug") then
            if not target:get("symbols") then
                target:set("symbols", "debug")
            end
            if not target:get("optimize") then
                target:set("optimize", "none")
            end
        end
    end)

rule("mode.release")
    after_load(function (target)
        if is_mode("release") then
            if not target:get("symbols") and target:targetkind() ~= "shared" then
                target:set("symbols", "hidden")
            end
            if not target:get("optimize") then
                if is_plat("android", "iphoneos") then
                    target:set("optimize", "smallest")
                else
                    target:set("optimize", "fastest")
                end
            end
            if not target:get("strip") then
                target:set("strip", "all")
            end
        end
    end)

It can be seen that, in the loading phase target, xmake be trying to determine the user xmake f --mode=xxxconfiguration parameters, if by is_mode()the acquired debug mode is interface, then disabled and enabled optimization related output symbols.
And if it is release mode, it will turn off compiler optimization and strip all debugging symbols.

Customized mode configuration

Of course, these two rules compiler built-in default configuration settings, can only meet the needs of most conventional scenario, if the user wants some personal customization of build configurations in different compilation mode, you need to do it yourself in xmake.lua judgment.

For example, we also want to enable the debug symbols in the release, then you only need:

if is_mode("release") then
    set_symbols("debug")
end

Or add some extra compiler flags:

if is_mode("release") then
    add_cflags("-fomit-frame-pointer")
end

Note: If you own configuration and mode.releasebuilt-in configuration conflicts, takes precedence over user settings.

Of course, we can not completely by add_rules("mode.debug", "mode.release")adding default configuration rules, allowing users to fully control their own mode configuration:

-- 如果当前编译模式是debug
if is_mode("debug") then

    -- 添加DEBUG编译宏
    add_defines("DEBUG")

    -- 启用调试符号
    set_symbols("debug")

    -- 禁用优化
    set_optimize("none")
end

-- 如果是release或者profile模式
if is_mode("release", "profile") then

    -- 如果是release模式
    if is_mode("release") then

        -- 隐藏符号
        set_symbols("hidden")

        -- strip所有符号
        set_strip("all")

        -- 忽略帧指针
        add_cxflags("-fomit-frame-pointer")
        add_mxflags("-fomit-frame-pointer")

    -- 如果是profile模式
    else
        -- 启用调试符号
        set_symbols("debug")
    end

    -- 添加扩展指令集
    add_vectorexts("sse2", "sse3", "ssse3", "mmx")
end

Other built-in schema rules

By way of example above, we see that in addition to debug / release mode, also added a configuration profile determines a pattern, in fact xmake also provides a corresponding model built, what we look at particular:

mode.debug

Add configuration rules debug compilation mode for the current project xmake.lua, such as:

add_rules("mode.debug")

It is equivalent to:

if is_mode("debug") then
    set_symbols("debug")
    set_optimize("none")
end

We can: xmake f -m debugto switch to this compilation mode.

mode.release

Add configuration rules release compilation mode for the current project xmake.lua, such as:

add_rules("mode.release")

It is equivalent to:

if is_mode("release") then
    set_symbols("hidden")
    set_optimize("fastest")
    set_strip("all")
end

We can: xmake f -m releaseto switch to this compilation mode.

mode.check

Xmake.lua add to the current project configuration rules check compilation mode, generally used for memory test, for example:

add_rules("mode.check")

It is equivalent to:

if is_mode("check") then
    set_symbols("debug")
    set_optimize("none")
    add_cxflags("-fsanitize=address", "-ftrapv")
    add_mxflags("-fsanitize=address", "-ftrapv")
    add_ldflags("-fsanitize=address")
end

We can: xmake f -m checkto switch to this compilation mode.

mode.profile

Xmake.lua add to the current project configuration rule profile compiled mode, generally used for performance analysis, such as:

add_rules("mode.profile")

It is equivalent to:

if is_mode("profile") then
    set_symbols("debug")
    add_cxflags("-pg")
    add_ldflags("-pg")
end

We can: xmake f -m profileto switch to this compilation mode.

mode.coverage

Xmake.lua add to the current project configuration rules coverage compilation mode, generally used for coverage analysis, such as:

add_rules("mode.coverage")

It is equivalent to:

if is_mode("coverage") then
    add_cxflags("--coverage")
    add_mxflags("--coverage")
    add_ldflags("--coverage")
end

We can: xmake f -m coverageto switch to this compilation mode.

Note: The generated files are generally a gcno obj directory corresponding oh, it is necessary to go from the build directory.

Expand their compilation mode

xmake mode configuration, and is not fixed, the user can freely pass and configuration, as long as the xmake f -m/--mode=xxxincoming and xmake.lua mode value inside is_mode("xxx")the energy corresponding to the line.

For example, we set up a their own unique compilation mode my_modecan be configured directly from the command line switches;

$ xmake f -m my_mode
$ xmake
[  0%]: ccache compiling.my_mode src/main.cpp
[100%]: linking.my_mode test
build ok!

Then xmake.lua inside the corresponding values ​​can be determined:

if is_mode("my_mode") then
    add_defines("ENABLE_MY_MODE")
end

Variable usage patterns

We can also pass directly in the configuration mode variable value $(mode), such as selecting a different libraries according to different modes:

target("test")
    set_kind("binary")
    add_files("src/*.c")
    add_links("xxx_$(mode)")

The above configuration, if it is compiled in debug mode will select the link: libxxx_debug.athe library, and the next release will be a link libxxx_release.a, of course, we can also set the library search path, according to the library to select the corresponding directory.

target("test")
    set_kind("binary")
    add_files("src/*.c")
    add_linkdirs("lib/$(mode)")
    add_links("xxx")

In addition, we can get_config("mode")configure the value of direct access to the incoming pattern, and these types of acquisition mode, the custom script is equally effective Oh, for example:

target("test")
    set_kind("binary")
    add_files("src/*.c")
    on_load(function (target)
        if is_mode("release") then
            print(get_config("mode"), "$(mode)")
        end
    end)

Original: https://tboox.org/cn/2019/12/05/quickstart-8-switch-build-mode/xmake

Guess you like

Origin www.cnblogs.com/tboox/p/11994383.html