xmake v2.6.2 發布,新增 Linux 內核驅動模塊構建支持
Xmake 是一個基于 Lua 的輕量級跨平臺構建工具。
它非常的輕量,沒有任何依賴,因為它內置了 Lua 運行時。
它使用 xmake.lua 維護項目構建,相比 makefile/CMakeLists.txt,配置語法更加簡潔直觀,對新手非常友好,短時間內就能快速入門,能夠讓用戶把更多的精力集中在實際的項目開發上。
我們能夠使用它像 Make/Ninja 那樣可以直接編譯項目,也可以像 CMake/Meson 那樣生成工程文件,另外它還有內置的包管理系統來幫助用戶解決 C/C++ 依賴庫的集成使用問題。
目前,Xmake 主要用于 C/C++ 項目的構建,但是同時也支持其他 native 語言的構建,可以實現跟 C/C++ 進行混合編譯,同時編譯速度也是非常的快,可以跟 Ninja 持平。
Xmake = Build backend + Project Generator + Package Manager
新版本改動
這個版本主要新增兩大特性:
- Linux 內核驅動模塊的構建支持
- 分組構建和批量運行支持,可用于實現
Run all tests功能
剩下的主要是一些零散的功能改進和 Bugs 修復,可以看下文末的更新內容明細,一些比較大的改動,下面也會逐一說明。
新特性介紹
構建 Linux 內核驅動模塊
Xmake 也許是首個提供 Linux 內核驅動開發 內置支持的第三方構建工具了。
盡管網上也有介紹 CMake 如何去配置構建 linux 驅動,但是大都是通過 add_custom_command 方式自定義各種命令,然后執行 echo 去自己拼接生成 Linux 的 Makefile 文件。
也就是說,其實還是依賴 Linux 內核源碼的 Makefile 來執行的構建,因此如果想自己追加一些編譯配置和宏定義都會非常麻煩。
而使用 Xmake,我們可以提供更加靈活的可配置性,更加簡單的配置文件,以及一鍵編譯、自動依賴拉取集成、Linux kernel 源碼自動下載集成,內核驅動交叉編譯等特性。
Hello World
我們通過一個最簡單的字符驅動來直觀感受下。
首先,我們準備一個 Hello World 驅動代碼,例如:
add_requires("linux-headers", {configs = {driver_modules = true}})
target("hello")
add_rules("platform.linux.driver")
add_files("src/*.c")
add_packages("linux-headers")
set_license("GPL-2.0")
它的配置非常簡單,只需要配置上支持模塊的 linux-headers 包,然后應用 platform.linux.driver 構建規則就行了。
然后直接執行 xmake 命令,一鍵編譯,生成內核驅動模塊 hello.ko。
$ xmake
[ 20%]: ccache compiling.release src/add.c
[ 20%]: ccache compiling.release src/hello.c
[ 60%]: linking.release build/linux/x86_64/release/hello.ko
[100%]: build ok!
簡單么,也許你會說,這跟直接用 Makefile 配置,然后 make 編譯也沒啥太大區別么。
那么重點來了,Xmake 是會自動幫你拉取指定版本依賴內核源碼,make 不會,CMake 也不會,用戶必須通過 sudo apt install linux-headers-generic 自己安裝它們。
但是 Xmake 不需要,上面的一鍵編譯,我其實省略了部分輸出,實際上是這樣的。
$ xmake
note: install or modify (m) these packages (pass -y to skip confirm)?
in xmake-repo:
-> m4 1.4.19 [from:linux-headers,bison,flex,elfutils]
-> flex 2.6.4 [from:bc,linux-headers]
-> bison 3.8.2 [from:bc,linux-headers]
-> ed 1.17 [from:bc,linux-headers]
-> texinfo 6.7 [from:bc,linux-headers]
-> bc 1.07.1 [from:linux-headers]
-> pkg-config 0.29.2 [from:linux-headers]
-> openssl 1.1.1l [private, from:linux-headers]
-> elfutils 0.183 [private, from:linux-headers]
-> linux-headers 5.10.46 [driver_modules:y]
please input: y (y/n/m)
=> download https://github.com/xmake-mirror/ed/archive/refs/tags/1.17.tar.gz .. ok
=> install ed 1.17 .. ok
=> download https://ftp.gnu.org/gnu/m4/m4-1.4.19.tar.xz .. ok
=> download https://ftp.gnu.org/gnu/texinfo/texinfo-6.7.tar.xz .. ok
=> download https://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar.gz .. ok
=> download https://github.com/openssl/openssl/archive/OpenSSL_1_1_1l.zip .. ok
=> install m4 1.4.19 .. ok
=> download https://github.com/westes/flex/releases/download/v2.6.4/flex-2.6.4.tar.gz .. ok
=> install texinfo 6.7 .. ok
=> install pkg-config 0.29.2 .. ok
=> download http://ftp.gnu.org/gnu/bison/bison-3.8.2.tar.gz .. ok
=> install flex 2.6.4 .. ok
=> download https://sourceware.org/elfutils/ftp/0.183/elfutils-0.183.tar.bz2 .. ok
=> install elfutils 0.183 .. ok
=> install bison 3.8.2 .. ok
=> download https://ftp.gnu.org/gnu/bc/bc-1.07.1.tar.gz .. ok
=> install bc 1.07.1 .. ok
=> install openssl 1.1.1l .. ok
=> download https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.46.tar.xz .. ok
=> install linux-headers 5.10.46 .. ok
[ 16%]: ccache compiling.release src/add.c
[ 16%]: ccache compiling.release src/hello.c
[ 16%]: ccache compiling.release src/hello.mod.c
[ 66%]: linking.release build/linux/x86_64/release/hello.ko
[100%]: build ok!
首次編譯,Xmake 會拉取所有依賴,如果用戶自己已經通過 apt 等第三方包管理安裝了它們,Xmake 也會優先用系統已經安裝的版本,省去下載安裝過程。
也就是說,不管在哪個環境,用戶都不需要關心如何去搭建內核驅動開發環境,僅僅只需要一個 xmake 命令,就能搞定一切。
而這些依賴拉取,都是通過 add_requires("linux-headers", {configs = {driver_modules = true}}) 配置包來實現的。
另外,我們也可以看完整構建命令參數。
$ xmake -v
[ 20%]: ccache compiling.release src/add.c
/usr/bin/ccache /usr/bin/gcc -c -m64 -O2 -std=gnu89 -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include/generated -I/usr/src/linux-headers-5.11.0-41-generic/include -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include/uapi -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include/generated/uapi -I/usr/src/linux-headers-5.11.0-41-generic/include/uapi -I/usr/src/linux-headers-5.11.0-41-generic/include/generated/uapi -D__KERNEL__ -DMODULE -DKBUILD_MODNAME=\"hello\" -DCONFIG_X86_X32_ABI -isystem /usr/lib/gcc/x86_64-linux-gnu/10/include -include /usr/src/linux-headers-5.11.0-41-generic/include/linux/kconfig.h -include /usr/src/linux-headers-5.11.0-41-generic/include/linux/compiler_types.h -nostdinc -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -mindirect-branch=thunk-extern -mindirect-branch-register -mrecord-mcount -fmacro-prefix-map=./= -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -fcf-protection=none -falign-jumps=1 -falign-loops=1 -fno-asynchronous-unwind-tables -fno-jump-tables -fno-delete-null-pointer-checks -fno-allow-store-data-races -fno-reorder-blocks -fno-ipa-cp-clone -fno-partial-inlining -fstack-protector-strong -fno-inline-functions-called-once -falign-functions=32 -fno-strict-overflow -fno-stack-check -fconserve-stack -DKBUILD_BASENAME=\"add\" -o build/.objs/hello/linux/x86_64/release/src/add.c.o src/add.c
[ 20%]: ccache compiling.release src/hello.c
/usr/bin/ccache /usr/bin/gcc -c -m64 -O2 -std=gnu89 -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include/generated -I/usr/src/linux-headers-5.11.0-41-generic/include -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include/uapi -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include/generated/uapi -I/usr/src/linux-headers-5.11.0-41-generic/include/uapi -I/usr/src/linux-headers-5.11.0-41-generic/include/generated/uapi -D__KERNEL__ -DMODULE -DKBUILD_MODNAME=\"hello\" -DCONFIG_X86_X32_ABI -isystem /usr/lib/gcc/x86_64-linux-gnu/10/include -include /usr/src/linux-headers-5.11.0-41-generic/include/linux/kconfig.h -include /usr/src/linux-headers-5.11.0-41-generic/include/linux/compiler_types.h -nostdinc -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -mindirect-branch=thunk-extern -mindirect-branch-register -mrecord-mcount -fmacro-prefix-map=./= -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -fcf-protection=none -falign-jumps=1 -falign-loops=1 -fno-asynchronous-unwind-tables -fno-jump-tables -fno-delete-null-pointer-checks -fno-allow-store-data-races -fno-reorder-blocks -fno-ipa-cp-clone -fno-partial-inlining -fstack-protector-strong -fno-inline-functions-called-once -falign-functions=32 -fno-strict-overflow -fno-stack-check -fconserve-stack -DKBUILD_BASENAME=\"hello\" -o build/.objs/hello/linux/x86_64/release/src/hello.c.o src/hello.c
[ 60%]: linking.release build/linux/x86_64/release/hello.ko
/usr/bin/ld -m elf_x86_64 -r -o build/.objs/hello/linux/x86_64/release/build/linux/x86_64/release/hello.ko.o build/.objs/hello/linux/x86_64/release/src/add.c.o build/.objs/hello/linux/x86_64/release/src/hello.c.o
/usr/src/linux-headers-5.11.0-41-generic/scripts/mod/modpost -m -a -o build/.objs/hello/linux/x86_64/release/build/linux/x86_64/release/Module.symvers -e -N -T -
WARNING: modpost: Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.
/usr/bin/ccache /usr/bin/gcc -c -m64 -O2 -std=gnu89 -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include/generated -I/usr/src/linux-headers-5.11.0-41-generic/include -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include/uapi -I/usr/src/linux-headers-5.11.0-41-generic/arch/x86/include/generated/uapi -I/usr/src/linux-headers-5.11.0-41-generic/include/uapi -I/usr/src/linux-headers-5.11.0-41-generic/include/generated/uapi -D__KERNEL__ -DMODULE -DKBUILD_MODNAME=\"hello\" -DCONFIG_X86_X32_ABI -isystem /usr/lib/gcc/x86_64-linux-gnu/10/include -include /usr/src/linux-headers-5.11.0-41-generic/include/linux/kconfig.h -include /usr/src/linux-headers-5.11.0-41-generic/include/linux/compiler_types.h -nostdinc -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -mindirect-branch=thunk-extern -mindirect-branch-register -mrecord-mcount -fmacro-prefix-map=./= -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -fcf-protection=none -falign-jumps=1 -falign-loops=1 -fno-asynchronous-unwind-tables -fno-jump-tables -fno-delete-null-pointer-checks -fno-allow-store-data-races -fno-reorder-blocks -fno-ipa-cp-clone -fno-partial-inlining -fstack-protector-strong -fno-inline-functions-called-once -falign-functions=32 -fno-strict-overflow -fno-stack-check -fconserve-stack -o build/.objs/hello/linux/x86_64/release/build/linux/x86_64/release/hello.ko.mod.o build/.objs/hello/linux/x86_64/release/build/linux/x86_64/release/hello.ko.mod.c
/usr/bin/ld -m elf_x86_64 -r --build-id=sha1 -T /usr/src/linux-headers-5.11.0-41-generic/scripts/module.lds -o build/linux/x86_64/release/hello.ko build/.objs/hello/linux/x86_64/release/build/linux/x86_64/release/hello.ko.o build/.objs/hello/linux/x86_64/release/build/linux/x86_64/release/hello.ko.mod.o
使用特定版本的內核源碼
我們也可以指定版本語義規則,選取自己需要的內核源碼作為構建源。
add_requires("linux-headers 5.9.x", {configs = {driver_modules = true}})
交叉編譯
我們也支持內核驅動模塊的交叉編譯,比如在 Linux x86_64 上使用交叉編譯工具鏈來構建 Linux Arm/Arm64 的驅動模塊。
我們只需要準備好自己的交叉編譯工具鏈,通過 --sdk= 指定它的根目錄,然后配置切換到 -p cross 平臺, 最后指定需要構建的架構 arm/arm64 即可。
同樣的,我們不用關心如何準備 linux-headers 去支持交叉編譯,Xmake 的依賴包管理會幫你準本好一切,拉取構建支持對應架構的內核源碼。
這里用到的交叉工具鏈,可以從這里下載: Download toolchains
更多,交叉編譯配置文檔,見:配置交叉編譯
注:目前僅僅支持 arm/arm64 交叉編譯架構,后續會支持更多的平臺架構。
構建 Arm 驅動模塊
$ xmake f -p cross -a arm --sdk=/mnt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf -c
$ xmake
[ 20%]: ccache compiling.release src/add.c
[ 20%]: ccache compiling.release src/hello.c
[ 60%]: linking.release build/cross/arm/release/hello.ko
[100%]: build ok!
構建 Arm64 驅動模塊
$ xmake f -p cross -a arm64 --sdk=/mnt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu -c
$ xmake
[ 20%]: ccache compiling.release src/add.c
[ 20%]: ccache compiling.release src/hello.c
[ 60%]: linking.release build/cross/arm64/release/hello.ko
[100%]: build ok!
分組批量構建和運行支持
早期,我們已經支持了通過 set_group 設置目標分組,實現 vs/vsxmake 工程在 vs 下的源文件分組管理展示。
但是,這個分組僅限于這個特性,沒有用于其他地方,而新版本中,我們繼續改進利用分組特性,實現指定構建一批目標程序,以及批量運行一批目標程序。
這通常有什么用呢,比如可以用來提供 Run all tests 和 Run all benchmarks 等功能。
編譯指定一批目標程序
我們可以使用 set_group() 將給定的目標標記為 test/benchmark/... 并使用 set_default(false) 禁用來默認構建它。
這樣,默認情況下 Xmake 不會去構建它們,但是我們可以通過 xmake -g xxx 命令就能指定構建一批目標程序了。
比如,我們可以使用此功能來構建所有測試。
target("test1")
set_kind("binary")
set_default(false)
set_group("test")
add_files("src/*.cpp")
target("test2")
set_kind("binary")
set_default(false)
set_group("test")
add_files("src/*.cpp")
$ xmake -g test
$ xmake --group=test
運行指定一批目標程序
我們也可以通過設置分組,來指定運行所有帶有 test 分組的測試程序。
這通常非常有用,在此之前想要實現 Run all tests 功能,我們只能通過定義一個 task("tests") 在里面調用 os.exec 去挨個執行測試目標,配置比較繁瑣,對用戶要求比較高。
而現在,我們只需要對需要執行的測試目標程序標記為 set_group("test"),然后就可以批量運行它們了。
$ xmake run -g test
$ xmake run --group=test
支持分組模式匹配
另外,我們還可以支持分組的模式匹配,非常的靈活:
$ xmake build -g test_*
$ xmake run -g test/foo_*
$ xmake build -g bench*
$ xmake run -g bench*
更多信息見:#1913
改進 CMake 包源的查找和集成
之前的版本中,我們提供了 find_package("cmake::xxx") 來查找 cmake 內部的包,但是這種方式對于用戶集成使用還是很繁瑣。
因此,新版本中,我們進一步改進它,通過 add_requires 來實現統一快速的 cmake 包集成。
add_requires("cmake::ZLIB", {alias = "zlib", system = true})
target("test")
set_kind("binary")
add_files("src/*.c")
add_packages("zlib")
我們指定 system = true 告訴 xmake 強制從系統中調用 cmake 查找包,如果找不到,不再走安裝邏輯,因為 cmake 沒有提供類似 vcpkg/conan 等包管理器的安裝功能,
只提供了包查找特性。
指定版本
add_requires("cmake::OpenCV 4.1.1", {system = true})
指定組件
add_requires("cmake::Boost", {system = true, configs = {components = {"regex", "system"}})}
預設開關
add_requires("cmake::Boost", {system = true, configs = {components = {"regex", "system"},
presets = {Boost_USE_STATIC_LIB = true}}})
相當于內部調用 find_package 查找包之前,在 CMakeLists.txt 中預定義一些配置,控制 find_package 的查找策略和狀態。
set(Boost_USE_STATIC_LIB ON) -- will be used in FindBoost.cmake
find_package(Boost REQUIRED COMPONENTS regex system)
設置環境變量
add_requires("cmake::OpenCV", {system = true, configs = {envs = {CMAKE_PREFIX_PATH = "xxx"}}})
指定自定義 FindFoo.cmake 模塊腳本目錄
mydir/cmake_modules/FindFoo.cmake
add_requires("cmake::Foo", {system = true, configs = {moduledirs = "mydir/cmake_modules"}})
相關 issues: #1632
xmake-idea 插件更新
xmake-idea 這個插件由于個人時間和精力的關系,一直沒有花時間去維護更新,而 IDEA 插件的兼容性問題有非常多,只要一段時間不用,就無法在新的 Idea/Clion 上正常使用。
最近,我花了點時間,修復了一些兼容性問題,比如 Windows 上創建工程會卡死的問題,新版本 Clion 無法安裝等問題。
目前,最新版本應該可以在全平臺正常使用了。
另外一些值得提起的事情
年終總結
這是 2021 年我發布的最后一個版本,這一年下來,經歷了很多,Xmake 也在逐漸成長為一個更加強大的構建工具。
到今年年底,Xmake 總共收獲了 4.2k stars,處理了 1.9k 的 issues/pr,超過 8k 多次 commits。
而官方的包管理倉庫 xmake-repo 也已經收錄了近 500+ 的常用依賴包。
感謝
感謝各位貢獻者對 xmake-repo 倉庫 和 Xmake 的貢獻,完整貢獻者列表見:Contributors。
也非常感謝大家對 Xmake 的贊助的支持,使得我能夠有足夠的動力去持續維護,完整捐助列表見:Sponsors。
更新內容
新特性
改進
- #1872: 支持轉義 set_configvar 中字符串值
- #1888: 改進 windows 安裝器,避免錯誤刪除其他安裝目錄下的文件
- #1895: 改進
plugin.vsxmake.autoupdate規則 - #1893: 改進探測 icc 和 ifort 工具鏈
- #1905: 改進 msvc 對 external 頭文件搜索探測支持
- #1904: 改進 vs201x 工程生成器
- 添加
XMAKE_THEME環境變量去切換主題配置 - #1907: 添加
-f/--force參數使得xmake create可以在費控目錄被強制創建 - #1917: 改進 find_package 和配置

Xmake 是一個基于 Lua 的輕量級跨平臺構建工具。
它非常的輕量,沒有任何依賴,因為它內置了 Lua 運行時。
它使用 xmake.lua 維護項目構建,相比 makefile/CMakeLists.txt,配置語法更加簡潔直觀,對新手非常友好,短時間內就能快速入門,能夠讓用戶把更多的精力集中在實際的項目開發上。
我們能夠使用它像 Make/Ninja 那樣可以直接編譯項目,也可以像 CMake/Meson 那樣生成工程文件,另外它還有內置的包管理系統來幫助用戶解決 C/C++ 依賴庫的集成使用問題。
目前,Xmake 主要用于 C/C++ 項目的構建,但是同時也支持其他 native 語言的構建,可以實現跟 C/C++ 進行混合編譯,同時編譯速度也是非常的快,可以跟 Ninja 持平。
浙公網安備 33010602011771號