当前位置:网站首页>Proficient in xmake

Proficient in xmake

2022-06-11 19:34:00 fqbqrr

01
Download the release
can xmake update To automatically update .
02 Create and compile projects
xmake create establish Languages The empty project .
The default is C++, Generate

add_rules("mode.debug", "mode.release") 
// Optional , Two build modes 
target("test") 
// Sub engineering modules 
    set_kind("binary") 
//exe
    add_files("src/*.cpp")

The default with Release Pattern , Switch mode :

$ xmake f -m debug
$ xmake
//f Indicates configuration .

xmake --help There are more abbreviations .
xmake create --help, Yes Various types of works . Such as c++( Default ),-l Setup language . Yes go/rust/dlang/....
use -t To specify the Template type .
Such as , be based on C Static library

xmake create -l c -t static test

be based on qt:

xmake create -l c++ -t qt.quickapp test
...

A focus on c/c++ Language .

Operation target

$ xmake run

use add_runenvs To set up The goal is Of environment variable .

target("test")
    set_kind("binary")
    add_files("src/*.c")
    add_runenvs("PATH", "/tmp/bin", "xxx/bin")
    add_runenvs("LD_LIBRARY_PATH", "/tmp/lib", "xxx/lib")
// Support multiple values .

on_run Run scripts automatically ,
xmake run -d Debugging program .

Common settings

target("demo", {
    kind = "binary", files = "src/*.c"})

One line compilation src All of the following c file . Generate demo. For the above Streamlining How to write it . This is more recommended :

target("demo") 
    set_kind("binary") 
    add_files("src/*.c")

set_kind(), Set up binary,static,shared, by static state / dynamic / Binary system .
Jiahong (add_defines()) Equivalent to -DXXX.
is_plat() Judge the platform .
Multiple The goal is , It's alone .
Global settings , Put it on the outside .

--  Global settings 
add_defines("TEST")
// Jiahong 
if is_arch("arm64", "armv7") then
    add_defines("ARM")
end

You can use target_end() End of the mandatory target Subdomain , Switch back to the local area .
from add_cflags( only c), add_cxflags(c/c++), add_cxxflags(c++) To set compilation options . By the user Judge Compiling platform . Such as

add_cflags("-g", "-O2", "-DDEBUG")
if is_plat("windows") then
    add_cflags("/MT")
end

With gcc Defined as standard , When incompatible , Automatically transformation , When there is no matching value , Ignore .
Disable flag .

add_cflags("-g", "-O2", {
    force = true})
// mandatory , To force parameters to be passed in .

xmake -v View compilation details .

Add Library Directory

target("test")
    set_kind("binary")
    add_includedirs("/usr/local/include")
    add_links("pthread")
    add_linkdirs("/usr/local/lib")
// Header Directory , Library name , The library catalog .

add_syslinks() For the system link library .add_links() It is not a system .

target("test") 
    set_kind("binary") 
    add_links("A", "B") 
    add_syslinks("pthread")

Into the -lA -lB -lpthread such . System Put the final .
Set language standards :set_languages("c99", "c++11").

Built in optimization

Compile optimized configuration :none,fast,faster,fastest,smallest,aggressive

set_optimize("fastest")

xmake Internal mapping . So it is very convenient and cross platform .
Different compilation modes , We need to judge :

if is_mode("debug") then
    set_symbols("debug")
    set_optimize("none")
end
if is_mode("release") then
    set_symbols("hidden")
    set_strip("all")
    if is_plat("iphoneos", "android") then
        set_optimize("smallest")
    else
        set_optimize("fastest")
    end
end

too troublesome , So use Built in rules To simplify setup .

add_rules("mode.release", "mode.debug")

Customizable :

add_rules("mode.release", "mode.debug") 
if is_mode("release") then 
    set_optimize("fastest") 
end

Switch to debug mode :

xmake f -m debug; xmake

Add source file

add_files()

Add various source files :C/Cpp/d/.....obj, .a/.lib It's fine too , Such as .

add_files("src/test_*.c") 
add_files("src/xxx/**.cpp") 
add_files("src/asm/*.S", "src/objc/**/hello.m")

* Single stage ,** Multi level directory . It can also filter .

add_files("src/**.c|impl/*.c")
// barring impl Below directory .
add_files("src/*.cpp|test.cpp|hello.cpp|xx_*.cpp")
// The following are all exclusions 

Finer control

target("test")
    add_defines("TEST1")
    add_files("src/*.c")
    add_files("test/*.c", "test2/test2.c", {
    defines = "TEST2", languages = "c99", includedirs = ".", cflags = "-O0"})

stay add_files Last parameter , Incoming configuration surface , To control the specified file Compilation options .

target("test") 
    -- ... 
    add_files("src/test/*.md", {
    rule = "markdown"})
// Add a rule to the specified file .

mandatory :

add_files("src/*.c", {
    force = {
    cxflags = "-DTEST", mflags = "-framework xxx"}})

Also delete the file :

target("test") 
    add_files("src/*.c") 
    del_files("src/test.c")
// And | The effect is the same .

such :

target("test") 
    add_files("src/**.c") 
    del_files("src/test*.c") 
    del_files("src/subdir/*.c|xxx.c") 
//xxx.c, Under double negation , Express reservation .
    if is_plat("iphoneos") then 
        add_files("xxx.m") 
    end

Android platform
ndk Catalog :

xmake f -p android --ndk=~/downloads/android-ndk-r19c $ xmake
// Pass in 

-p android Switch to Android . Compiling Android native . Support binary,static,shared
xmake f/config Only for the current project .
Set the global usage :

$ xmake g --ndk=~/xxx/android-ndk-r19c
//ANDROID_NDK_HOME=
$ xmake f -p android --ndk=xxx --ndk_sdkver=16
//api edition 
$ xmake f -p android --ndk=xxx -a arm64-v8a
// Change the structure 

The architecture has :armv7-a,arm64-v8a,armv5te,mips,mips64,i386,x86_64
use is_plat("android") To judge Android .

target("test") 
    set_kind("shared") 
    add_files("src/*.c") 
    if is_plat("android") then 
        add_defines("ANDROID") 
        add_syslinks("log") 
    end

Development qt

$ xmake f --qt=/home/xxx/qtsdk
// Manual configuration 
$ xmake g --qt=/home/xxx/qtsdk
// Make the whole situation 
$ xmake create -t qt.quickapp test
//qt Fast 

Generate

target("test")
    add_rules("qt.quickapp")
// Use this built-in rule , add ` link / sign / Contains the directory ` etc. 
    add_headerfiles("src/*.h")
    add_files("src/*.cpp") 
    add_files("src/qml.qrc")
//
$ xmake run
// function 

stay qt.quickapp In the maintenance *.qrc The rules .

$ xmake create -t qt.widgetapp test
// Create project . Generate 
target("qt_widgetapp")
    add_rules("qt.widgetapp")
    add_files("src/*.cpp") 
    add_files("src/mainwindow.ui")
    add_files("src/mainwindow.h")  
 --  Add with `Q_OBJECT` Of `meta` The header file 
// Static version 
$ xmake create -t qt.widgetapp_static test
-- become  add_rules("qt.widgetapp_static")

also :

-qt.console:c++
-qt.quickapp:c++
-qt.quickapp_static:c++
-qt.shared:c++
-qt.static:c++
-qt.widgetapp:c++
-qt.widgetapp_static:c++

Also set --android_sdk Location .

$ xmake install
// install qtapk To equipment 

use -m/--mode= To switch modes . When loading targets , Judge

xmake f --mode=xxx

Set up , use is_mode() To judge .debug Then disable Optimize Enable Symbol , and release Enable Optimize , Ban Symbol .
Customizable :

if is_mode("release") then
    set_symbols("debug")
end
// Or add additional compilation flags :
if is_mode("release") then
    add_cflags("-fomit-frame-pointer")
end

User configuration takes precedence . Users can also Fully customized :

--  If the current compilation mode is debug
if is_mode("debug") then

    --  add to DEBUG Compile macro 
    add_defines("DEBUG")

    --  Enable debug symbols 
    set_symbols("debug")

    --  Disable optimization 
    set_optimize("none")
end

--  If it is release perhaps profile Pattern 
if is_mode("release", "profile") then
    --  If it is release Pattern 
    if is_mode("release") then
        --  Hidden symbols 
        set_symbols("hidden")
        -- strip All symbols 
        set_strip("all")
        --  Ignore frame pointer 
        add_cxflags("-fomit-frame-pointer")
        add_mxflags("-fomit-frame-pointer")

    --  If it is profile Pattern 
    else
        --  Enable debug symbols 
        set_symbols("debug")
    end

    --  Add extensions 
    add_vectorexts("sse2", "sse3", "ssse3", "mmx")
end

Inside :

add_rules("mode.debug")
//==
if is_mode("debug") then
    set_symbols("debug")
    set_optimize("none")
end
// and 
add_rules("mode.release")
//==
if is_mode("release") then
    set_symbols("hidden")
    set_optimize("fastest")
    set_strip("all")
end
// Used to check memory 
add_rules("mode.check")
//==
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
//
add_rules("mode.profile")
//==
// Used to analyze performance 
if is_mode("profile") then
    set_symbols("debug")
    add_cxflags("-pg")
    add_ldflags("-pg")
end
// Analysis coverage 
add_rules("mode.coverage")
//===
if is_mode("coverage") then
    add_cxflags("--coverage")
    add_mxflags("--coverage")
    add_ldflags("--coverage")
end

$(mode) As a variable , Enable according to different modes :

target("test")
    set_kind("binary")
    add_files("src/*.c")
    add_links("xxx_$(mode)")
// Add link library 
target("test")
    set_kind("binary")
    add_files("src/*.c")
    add_linkdirs("lib/$(mode)")
// Search path 
    add_links("xxx")

use get_config("mode") obtain Pattern Value .
Customize , It works :

on_load(function (target)
    if is_mode("release") then
        print(get_config("mode"), "$(mode)")
    end
end)
原网站

版权声明
本文为[fqbqrr]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011816581206.html