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

Proficient in xmake2

2022-06-11 19:34:00 fqbqrr

Tool chains have corresponding include/lib Catalog , It's similar :

    - bin
       -  Tools 
       -  Tools 
       - ...
   - lib
       - libxxx.a
   - include
       - xxx.h

structure . You can use :

$ xmake f -p cross --sdk=/home/toolchains_sdkdir

To compile .-p cross Indicates cross compilation ,--sdk= Tool chain root directory . commonly xmake Automatic processing .
Manual adjustment :

$ xmake f -p linux --sdk=/home/toolchains_sdkdir --bin=/usr/opt/bin 
//bin Catalog 
$ xmake

use --cross= Configure compilation Tools Prefix .

$ xmake f -p linux --sdk=/usr/toolsdk --bin=/opt/bin --cross=armv7-linux-
$ xmake f -p linux --sdk=/user/toolsdk --cc=armv7-linux-clang --cxx=armv7-linux-clang++
// Continue adding options 

--cc by c.--cxx by C++ compiler . Yes CC/CXX Environment variables , environment variable first .

xmake f --cxx=clang++@/home/xxx/c++mips.exe
// Equivalent 
$ xmake f -p linux --sdk=/user/toolsdk --ld=armv7-linux-clang++ --sh=armv7-linux-clang++ --ar=armv7-linux-ar

ld The linker ,sh share ,ar Static library , Again LD/SH/AR environment variable .
use --includedirs and --linkdirs and --links To add Search for route .
use : perhaps ;( window ) To segment .
use --cflags,--cxxflags,--ldflags,--shflags and --arflags set an option ,as For assembler .

$ xmake f -p mingw
$ xmake -v
//
$ xmake g --mingw=/home/mingwsdk 
$ xmake f -p mingw 
$ xmake

Switching platform .
Set up tool chain , No need Manual Appoint .

target("test")
    set_kind("binary")
    set_toolchain("cxx", "clang")
    set_toolchain("ld", "clang++")

Set up To configure Parameters , Equivalent to xmake f:

set_config("cflags", "-DTEST")
set_config("sdk", "/home/xxx/tooksdk")
set_config("cc", "gcc")
set_config("ld", "g++")

Also available -p/--plat= Custom platform .

$ xmake f -p linux --sdk=/usr/local/arm-xxx-gcc/ 
$ xmake

Lin Cao platform .
The goal is Sub project .

add_links("tbox")
add_linkdirs("lib")
add_includedirs("include")
// Put a common one outside 
target("test1")
    set_kind("binary")
    add_files("src/test1/*.c")
    
target("test2")
    set_kind("binary")
    add_files("src/test2/*.c")

Different items , individualization . use add_deps Set dependencies .

target("foo")
    set_kind("static")
    add_files("*.c")
    add_defines("FOO", {
    public = true})
    add_includedirs("foo/inc", {
    public = true})

target("test1")
    set_kind("binary")
    add_deps("foo")
    add_files("test1/*.c")
    
target("test2")
    set_kind("binary")
    add_deps("foo")
    add_files("test2/*.c")

inheritable linkdirs,links,includedirs as well as defines Such as relationship , And automatically process the compilation sequence . rely on Or cascading .

add_deps("dep1", "dep2", {
    inherit = false})
// Non inherited dependencies .
add_includedirs("inc1", {
    public = true})
// Public directory .

Yes private( Default , private )/public( Open )/interface( Only be dependent on ) Three .
add_includedirs, add_defines, add_cflags etc. , Both support visibility .

Big project

Sub project , use xmake.lua, use includes Import .

$ xmake build test1
$ xmake run test3
$ xmake install demo1
// There is no need to switch to a subdirectory to operate .

root xmake.lua Put general configuration .

--  Define the project 
set_project("tbox")
set_xmakever("2.3.2")
set_version("1.6.5", {
    build = "%Y%m%d%H%M"})

--  Common signs 
set_warnings("all", "error")
set_languages("c99")
add_cxflags("-Wno-error=deprecated-declarations", "-fno-strict-aliasing", "-Wno-error=expansion-to-defined")
add_mxflags("-Wno-error=deprecated-declarations", "-fno-strict-aliasing", "-Wno-error=expansion-to-defined")

--  Build a model 
add_rules("mode.release", "mode.debug")

--  Include subprojects 
includes("test", "demo")

Sub configuration It only affects Myself and the lower class .
subprojects Straddlable xmake.lua Rely solely on .

target("demo1")
    set_kind("binary")
    add_files("demo1/*.c")
    add_deps("test1")
// Rely on your peers . Priority compilation is dependent , The dependent party automatically links to the corresponding library 

add_files,add_includedirs It's all relative to subprojects Catalog . Just consider the current directory .

projectdir
  - test
    - xmake.lua
    - test1/ *.c
    - test2/ *.c
//
target("test1")
    add_files("test1/*.c")
target("test2")
    add_files("test2/*.c")
// such .

use $(projectdir) Root directory .
includes Is the global interface .

includes("test1", "test2")
// To do this , Can't put ` The goal is ` Inside 
target("test")
    set_kind("static")
    add_files("test/*.c")

direct includes("test1") Just reference the directory .
Batch :

includes("test/*/xmake.lua")
// recursive 
includes("test/**/xmake.lua")

Description domain The configuration file . Complex scripts in Script domain Write in .
on_xxx,after_xxx,before_xxx All are script fields .
You can use import Interface import xmake All kinds of built-in lua modular , And users lua Script

target("test")
    set_kind("binary")
    add_files("src/*.c")
    on_load("modules.test.load")
    on_install("modules.test.install")
// Script in a separate file .

More complex... Can be configured .
A separate lua Script file to main Main entrance .

import("core.project.config")
import("mymodule")
// Import built-in / Self writing extension .

function main(target)
    if is_plat("linux", "macosx") then
        target:add("links", "pthread", "m", "dl")
    end
end
// The main entrance 

on_load stay Load target When to start , Only once . and Description domain May be executed more than once .
also ,on/after/before_build/install/package/run Stage .
Script Available for Customize (on_build,on_run..) Script , Developing a plug-in , Development templates , Expansion platform , Custom tasks, etc .

import("core.base.option")
import("core.base.task")
// use . Split the path .
function main()
    
    --  Take parameter options 
    print(option.get("version"))

    --  Run tasks and plug-ins 
    task.run("hello")
end
//
import("modules.hello1")
import("modules.hello2")
// Custom module , nothing _ Prefixes are exported 

Import the specified

import("hello3", {
    rootdir = "/home/xxx/modules"})
// Catalog .

Specify alias

import("core.platform.platform", {
    alias = "p"}) 
function main() 
    --  So it can be used p To call `platform` Modular `plats` Interface , Take all `xmake` List of supported platforms 
    print(p.plats())
end

new

import("xxx.xxx", {
    try = true, anonymous = true})

try Failure , Return without error , anonymous Do not introduce the current domain .
test lua.

function main() 
    print("hello xmake!") 
end
// function .
$ xmake lua /tmp/test.lua
$ xmake lua lib.detect.find_tool gcc
// Fast execution .
$ xmake lua
// Interactive mode 

Import extension module

> task = import("core.project.task")
// Interactive mode 
> task.run("hello")
hello xmake!

use target:set,target:add To add The goal is attribute .set_,add_ The former can be used to dynamically configure . There are many other interfaces . Such as name , take , Target file , The target type , Dependents , rely on , Options , Options , Bags , package , Source list .
on_link Handle When linking action .on_build Build time ,

target("test")
    on_build("windows", function (target)
        print("build for windows")
    end)
// It can also be targeted at platforms .

on_build_file Custom compilation script .
on_build_files A group of .

target("test")
    set_kind("binary")
    add_files("src/*.c")
    on_build_files(function (target, sourcebatch, opt)
    end)
//sourcebatch To express .

sourcebatch There are methods sourcekind/sourcefiles/objectfiles/dependfiles
on_clean Cleaning action .
on_package pack ,on_install install ,on_uninstall uninstall .on_run function apk etc.
also target:before_xxx and target:after_xxx Series hooks .
xmake Built-in module , Such as os,io operation . The following functions will be omitted .

原网站

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