xmake v2.6.5 發佈,遠程編譯支持

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/MacOS/Windows 上,實現跨平臺編譯,例如:在 Linux 上編譯運行 Windows 程序,在 Windows 上編譯運行 macOS/Linux 程序。

相比 ssh 遠程登入編譯,它更加的穩定,使用更加流暢,不會因爲網絡不穩定導致 ssh 終端輸入卡頓,也可以實現本地快速編輯代碼文件。

甚至我們可以在 vs/sublime/vscode/idea 等編輯器和IDE 中無縫實現遠程編譯,而不需要依賴 IDE 本身對遠程編譯的支持力度。

開啓服務

$ xmake service
<remote_build_server>: listening 0.0.0.0:90091 ..

我們也可以開啓服務的同時,回顯詳細日誌信息。

$ xmake service -vD
<remote_build_server>: listening 0.0.0.0:90091 ..

以 Daemon 模式開啓服務

$ xmake service --start
$ xmake service --restart
$ xmake service --stop

配置服務端

我們首先,運行 xmake service 命令,它會自動生成一個默認的 service.conf 配置文件,存儲到 ~/.xmake/service.conf

然後,我們編輯它,修復服務器的監聽端口(可選)。

{
    logfile = "/Users/ruki/.xmake/service/logs.txt",
    remote_build = {
        server = {
            listen = "0.0.0.0:90091"
        }
    }
}

配置客戶端

我們還是編輯這個文件 ~/.xmake/service.conf,配置客戶端需要連接的服務器地址。

{
    logfile = "/Users/ruki/.xmake/service/logs.txt",
    remote_build = {
        client = {
            connect = "192.168.56.101:90091",
        }
    }
}

導入給定的配置文件

我們也可以通過下面的命令,導入指定的配置文件。

$ xmake service --config=/tmp/service.conf

連接遠程的服務器

接下來,我們只需要進入需要遠程編譯的工程根目錄,執行 xmake service --connect 命令,進行連接。

$ xmake create test
$ cd test
$ xmake service --connect 
<remote_build_client>: connect 192.168.56.110:90091 ..
<remote_build_client>: connected!
<remote_build_client>: sync files in 192.168.56.110:90091 ..
Scanning files ..
Comparing 3 files ..
    [+]: src/main.cpp
    [+]: .gitignore
    [+]: xmake.lua
3 files has been changed!
Archiving files ..
Uploading files with 1372 bytes ..
<remote_build_client>: sync files ok!

遠程構建工程

連接成功後,我們就可以像正常本地編譯一樣,進行遠程編譯。

$ xmake
<remote_build_client>: run xmake in 192.168.56.110:90091 ..
checking for platform ... macosx
checking for architecture ... x86_64
checking for Xcode directory ... /Applications/Xcode.app
checking for Codesign Identity of Xcode ... Apple Development: [email protected] (T3NA4MRVPU)
checking for SDK version of Xcode for macosx (x86_64) ... 11.3
checking for Minimal target version of Xcode for macosx (x86_64) ... 11.4
[ 25%]: ccache compiling.release src/main.cpp
[ 50%]: linking.release test
[100%]: build ok!
<remote_build_client>: run command ok!

遠程運行目標程序

我們也可以像本地運行調試那樣,遠程運行調試編譯的目標程序。

$ xmake run
<remote_build_client>: run xmake run in 192.168.56.110:90091 ..
hello world!
<remote_build_client>: run command ok!

遠程重建工程

$ xmake -rv
<remote_build_client>: run xmake -rv in 192.168.56.110:90091 ..
[ 25%]: ccache compiling.release src/main.cpp
/usr/local/bin/ccache /usr/bin/xcrun -sdk macosx clang -c -Qunused-arguments -arch x86_64 -mmacosx-version-min=11.4 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build/.objs/test/macosx/x86_64/release/src/main.cpp.o src/main.cpp
[ 50%]: linking.release test
"/usr/bin/xcrun -sdk macosx clang++" -o build/macosx/x86_64/release/test build/.objs/test/macosx/x86_64/release/src/main.cpp.o -arch x86_64 -mmacosx-version-min=11.4 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -stdlib=libc++ -Wl,-x -lz
[100%]: build ok!
<remote_build_client>: run command ok!

遠程配置編譯參數

$ xmake f --xxx --yy

手動同步工程文件

連接的時候,會自動同步一次代碼,後期代碼改動,可以執行此命令來手動同步改動的文件。

$ xmake service --sync
<remote_build_client>: sync files in 192.168.56.110:90091 ..
Scanning files ..
Comparing 3 files ..
    [+]: src/main.cpp
    [+]: .gitignore
    [+]: xmake.lua
3 files has been changed!
Archiving files ..
Uploading files with 1372 bytes ..
<remote_build_client>: sync files ok!

斷開遠程連接

針對當前工程,斷開連接,這僅僅影響當前工程,其他項目還是可以同時連接和編譯。

$ xmake service --disconnect
<remote_build_client>: disconnect 192.168.56.110:90091 ..
<remote_build_client>: disconnected!

查看服務器日誌

$ xmake service --logs

清理遠程服務緩存和構建文件

我們也可以手動清理遠程的任何緩存和構建生成的文件。

$ cd projectdir
$ xmake service --clean

改進 Cargo 包依賴

在之前的版本中,我們已經可以通過 add_requires("cargo::base64") 去單獨集成每個 cargo 包,用於編譯 rust 項目,以及與 C/C++ 的混合編譯,例如:

add_rules("mode.release", "mode.debug")
add_requires("cargo::base64 0.13.0")
add_requires("cargo::flate2 1.0.17", {configs = {features = "zlib"}})

target("test")
    set_kind("binary")
    add_files("src/main.rs")
    add_packages("cargo::base64", "cargo::flate2")

但是上面的方式會有一個問題:

如果依賴很多,並且有幾個依賴都共同依賴了相同的子依賴,那麼會出現重定義問題,因此如果我們使用完整的 Cargo.toml 去管理依賴就不會存在這個問題。

例如:

add_rules("mode.release", "mode.debug")
add_requires("cargo::test", {configs = {cargo_toml = path.join(os.projectdir(), "Cargo.toml")}})

target("test")
    set_kind("binary")
    add_files("src/main.rs")
    add_packages("cargo::test")

然後,我們就可以在 Cargo.toml 中集成所有需要的依賴,讓 Rust 自己去分析依賴關係,避免重複的子依賴衝突。

完整例子見:cargo_deps_with_toml

當然,如果用戶的依賴比較單一,那麼之前的集成方式還是完全可用。

爲什麼使用 Xmake 編譯 Rust?

這個時候,肯定會有人問,既然都用了 Cargo.toml 和 Cargo 了,爲什麼還要在 xmake.lua 中去配置呢,直接 Cargo 編譯不就好了麼。

如果我們是在用 Xmake 開發 C/C++ 項目,但是需要引入一些 Rust 子模塊給 C/C++ 項目使用,那麼就可以藉助這種方式,快速方便地在 C/C++ 中調用 Rust 庫和代碼。

更多關於 C/C++ 中調用 Rust 代碼庫的說明,見:使用 cxxbridge 在 C/C++ 中調用 Rust

支持源文件分組

新版本,我們提供了一個新接口 add_filegroups,用於對 vs/vsxmake/cmakelists generator 生成的工程文件進行源文件分組展示。

如果不設置分組展示,Xmake 也會默認按照樹狀模式展示,但是有些極端情況下,目錄層級顯示不是很好,例如:

target("test")
    set_kind("binary")
    add_files("../../../../src/**.cpp")

目前主要支持兩種展示模式:

  • plain: 平坦模式
  • tree: 樹形展示,這也是默認模式

另外,它也支持對 add_headerfiles 添加的文件進行分組。

設置分組並指定根目錄

target("test")
    set_kind("binary")
    add_files("../../../../src/**.cpp")
    add_filegroups("group1/group2", {rootdir = "../../../../"})

設置分組並指定文件匹配模式

target("test")
    set_kind("binary")
    add_files("../../../../src/**.cpp")
    add_filegroups("group1/group2", {rootdir = "../../../../", files = {"src/**.cpp"}})

作爲平坦模式展示

這種模式下,所有源文件忽略嵌套的目錄層級,在分組下同一層級展示。

target("test")
    set_kind("binary")
    add_files("../../../../src/**.cpp")
    add_filegroups("group1/group2", {rootdir = "../../../../", mode = "plain"})

包版本選擇支持 Git Commit

Xmake 的包依賴管理接口 add_requires 支持版本語義選擇,分支選擇,例如:

add_requires("tbox 1.6.1")
add_requires("tbox >=1.6.1")
add_requires("tbox master")

但是,之前的版本,我們還不支持從 Git Commit 中選擇版本,而現在我們也支持上了。

add_requires("tbox e807230557aac69e4d583c75626e3a7ebdb922f8")

只要,這個包的配置中帶有 Git url,就能從 Commit 中選擇版本。

更好地支持 iOS 模擬器編譯

如果要編譯 iOS 平臺目標程序,之前可以使用如下配置,僅僅通過切換 arch,就能分別編譯真機,模擬器版本程序。

$ xmake f -p iphoneos [-a armv7|armv7s|arm64|i386|x86_64]
$ xmake

但是由於 M1 設備上模擬器也支持 arm64 架構,因此之前單純從 arch 去區分是否爲模擬器,已無法滿足需求。
因此,在新版本中,我們新增了一個參數配置去區分是否爲模擬器目標。

$ xmake f -p iphoneos --appledev=simulator
$ xmake f -p watchos --appledev=simulator
$ xmake f -p appletvos --appledev=simulator

而如果沒有指定 --appledev= 參數,默認就是編譯真機程序,當然,之前的模式也是完全兼容的。

更新內容

新特性

  • #2138: 支持模板包
  • #2185: 添加 --appledev=simulator 去改進 Apple 模擬器目標編譯支持
  • #2227: 改進 cargo 包,支持指定 Cargo.toml 文件
  • 改進 add_requires 支持 git command 作爲版本
  • #622: 支持遠程編譯
  • #2282: 添加 add_filegroups 接口爲 vs/vsxmake/cmake generator 增加文件組支持

改進

  • #2137: 改進 path 模塊
  • macOS 下,減少 50% 的 Xmake 二進制文件大小
  • 改進 tools/autoconf,cmake 去更好地支持工具鏈切換
  • #2221: 改進註冊表 api 去支持 unicode
  • #2225: 增加對 protobuf 的依賴分析和構建支持
  • #2265: 排序 CMakeLists.txt
  • 改進 os.files 的文件遍歷速度

Bugs 修復

  • #2233: 修復 c++ modules 依賴
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章