Xmake: 现在可以支持远程编译了,有玩 C/C++ 的同学可以试试

讨论 未结 0 36
waruqi waruqi
waruqi waruqi 会员 2022年4月25日 07:16 发表
<p><a href="https://github.com/xmake-io/xmake" rel="nofollow">Xmake</a> 是一个基于 Lua 的轻量级跨平台构建工具。</p> <p>它非常的轻量,没有任何依赖,因为它内置了 Lua 运行时。</p> <p>它使用 xmake.lua 维护项目构建,相比 makefile/CMakeLists.txt ,配置语法更加简洁直观,对新手非常友好,短时间内就能快速入门,能够让用户把更多的精力集中在实际的项目开发上。</p> <p>我们能够使用它像 Make/Ninja 那样可以直接编译项目,也可以像 CMake/Meson 那样生成工程文件,另外它还有内置的包管理系统来帮助用户解决 C/C++ 依赖库的集成使用问题。</p> <p>目前,Xmake 主要用于 C/C++ 项目的构建,但是同时也支持其他 native 语言的构建,可以实现跟 C/C++ 进行混合编译,同时编译速度也是非常的快,可以跟 Ninja 持平。</p> <pre><code>Xmake = Build backend + Project Generator + Package Manager </code></pre> <ul> <li><a href="https://github.com/xmake-io/xmake" rel="nofollow">项目源码</a></li> <li><a href="https://xmake.io/#/zh-cn/" rel="nofollow">官方文档</a></li> <li><a href="https://xmake.io/#/zh-cn/about/course" rel="nofollow">入门课程</a></li> </ul> <h2>前言</h2> <p>尽管 VSCode 等现代化的编辑器都内置支持远程编译,但是不是所有编辑器都支持,通过 Xmake 内置的远程编译特性,我们可以再全端任意编辑器(甚至在 vim/emacs 上),IDE (包括 vs )上快速地编辑代码,然后进行远程编译和运行。</p> <p>例如,我们可以像本地编译一样,实现在 macOS 、linux 上编译 Windows 程序,并远程运行,也可以在 Windows 上远程编译调试 linux/macOS 程序。</p> <p>目前支持初步支持,还有很多细节体验有待改进,欢迎大家试用 Xmake 新版本。</p> <h2>新特性介绍</h2> <h3>远程编译支持</h3> <p>新版本提供了远程编译支持,我们可以通过它可以远程服务器上编译代码,远程运行和调试。</p> <p>服务器可以部署在 Linux/MacOS/Windows 上,实现跨平台编译,例如:在 Linux 上编译运行 Windows 程序,在 Windows 上编译运行 macOS/Linux 程序。</p> <p>相比 ssh 远程登入编译,它更加的稳定,使用更加流畅,不会因为网络不稳定导致 ssh 终端输入卡顿,也可以实现本地快速编辑代码文件。</p> <p>甚至我们可以在 vs/sublime/vscode/idea 等编辑器和 IDE 中无缝实现远程编译,而不需要依赖 IDE 本身对远程编译的支持力度。</p> <h4>开启服务</h4> <pre><code class="language-console">$ xmake service &lt;remote_build_server&gt;: listening 0.0.0.0:90091 .. </code></pre> <p>我们也可以开启服务的同时,回显详细日志信息。</p> <pre><code class="language-console">$ xmake service -vD &lt;remote_build_server&gt;: listening 0.0.0.0:90091 .. </code></pre> <h4>以 Daemon 模式开启服务</h4> <pre><code class="language-console">$ xmake service --start $ xmake service --restart $ xmake service --stop </code></pre> <h4>配置服务端</h4> <p>我们首先,运行 <code>xmake service</code> 命令,它会自动生成一个默认的 <code>service.conf</code> 配置文件,存储到 <code>~/.xmake/service.conf</code>。</p> <p>然后,我们编辑它,修复服务器的监听端口(可选)。</p> <pre><code class="language-lua">{ logfile = "/Users/ruki/.xmake/service/logs.txt", remote_build = { server = { listen = "0.0.0.0:90091" } } } </code></pre> <h4>配置客户端</h4> <p>我们还是编辑这个文件 <code>~/.xmake/service.conf</code>,配置客户端需要连接的服务器地址。</p> <pre><code class="language-lua">{ logfile = "/Users/ruki/.xmake/service/logs.txt", remote_build = { client = { connect = "192.168.56.101:90091", } } } </code></pre> <h4>导入给定的配置文件</h4> <p>我们也可以通过下面的命令,导入指定的配置文件。</p> <pre><code class="language-console">$ xmake service --config=/tmp/service.conf </code></pre> <h4>连接远程的服务器</h4> <p>接下来,我们只需要进入需要远程编译的工程根目录,执行 <code>xmake service --connect</code> 命令,进行连接。</p> <pre><code class="language-console">$ xmake create test $ cd test $ xmake service --connect &lt;remote_build_client&gt;: connect 192.168.56.110:90091 .. &lt;remote_build_client&gt;: connected! &lt;remote_build_client&gt;: 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 .. &lt;remote_build_client&gt;: sync files ok! </code></pre> <h4>远程构建工程</h4> <p>连接成功后,我们就可以像正常本地编译一样,进行远程编译。</p> <pre><code class="language-console">$ xmake &lt;remote_build_client&gt;: 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: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc8b9d8e898d95bc9b919d9590d29f9391">[email&nbsp;protected]</a> (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! &lt;remote_build_client&gt;: run command ok! </code></pre> <h4>远程运行目标程序</h4> <p>我们也可以像本地运行调试那样,远程运行调试编译的目标程序。</p> <pre><code class="language-console">$ xmake run &lt;remote_build_client&gt;: run xmake run in 192.168.56.110:90091 .. hello world! &lt;remote_build_client&gt;: run command ok! </code></pre> <h4>远程重建工程</h4> <pre><code class="language-console">$ xmake -rv &lt;remote_build_client&gt;: 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! &lt;remote_build_client&gt;: run command ok! </code></pre> <h4>远程配置编译参数</h4> <pre><code class="language-console">$ xmake f --xxx --yy </code></pre> <h4>手动同步工程文件</h4> <p>连接的时候,会自动同步一次代码,后期代码改动,可以执行此命令来手动同步改动的文件。</p> <pre><code class="language-console">$ xmake service --sync &lt;remote_build_client&gt;: 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 .. &lt;remote_build_client&gt;: sync files ok! </code></pre> <h4>断开远程连接</h4> <p>针对当前工程,断开连接,这仅仅影响当前工程,其他项目还是可以同时连接和编译。</p> <pre><code class="language-console">$ xmake service --disconnect &lt;remote_build_client&gt;: disconnect 192.168.56.110:90091 .. &lt;remote_build_client&gt;: disconnected! </code></pre> <h4>查看服务器日志</h4> <pre><code class="language-console">$ xmake service --logs </code></pre> <h4>清理远程服务缓存和构建文件</h4> <p>我们也可以手动清理远程的任何缓存和构建生成的文件。</p> <pre><code class="language-console">$ cd projectdir $ xmake service --clean </code></pre> <h3>改进 Cargo 包依赖</h3> <p>在之前的版本中,我们已经可以通过 <code>add_requires("cargo::base64")</code> 去单独集成每个 cargo 包,用于编译 rust 项目,以及与 C/C++ 的混合编译,例如:</p> <pre><code class="language-lua">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") </code></pre> <p>但是上面的方式会有一个问题:</p> <p>如果依赖很多,并且有几个依赖都共同依赖了相同的子依赖,那么会出现重定义问题,因此如果我们使用完整的 Cargo.toml 去管理依赖就不会存在这个问题。</p> <p>例如:</p> <pre><code class="language-lua">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") </code></pre> <p>然后,我们就可以在 Cargo.toml 中集成所有需要的依赖,让 Rust 自己去分析依赖关系,避免重复的子依赖冲突。</p> <p>完整例子见:<a href="https://github.com/xmake-io/xmake/blob/dev/tests/projects/rust/cargo_deps_with_toml/xmake.lua" rel="nofollow">cargo_deps_with_toml</a></p> <p>当然,如果用户的依赖比较单一,那么之前的集成方式还是完全可用。</p> <h4>为什么使用 Xmake 编译 Rust?</h4> <p>这个时候,肯定会有人问,既然都用了 Cargo.toml 和 Cargo 了,为什么还要在 xmake.lua 中去配置呢,直接 Cargo 编译不就好了么。</p> <p>如果我们是在用 Xmake 开发 C/C++ 项目,但是需要引入一些 Rust 子模块给 C/C++ 项目使用,那么就可以借助这种方式,快速方便地在 C/C++ 中调用 Rust 库和代码。</p> <p>更多关于 C/C++ 中调用 Rust 代码库的说明,见:<a href="https://xmake.io/#/zh-cn/guide/project_examples?id=%e4%bd%bf%e7%94%a8-cxxbridge-%e5%9c%a8-c-%e4%b8%ad%e8%b0%83%e7%94%a8-rust" rel="nofollow">使用 cxxbridge 在 C/C++ 中调用 Rust</a></p> <h3>支持源文件分组</h3> <p>新版本,我们提供了一个新接口 <code>add_filegroups</code>,用于对 vs/vsxmake/cmakelists generator 生成的工程文件进行源文件分组展示。</p> <p>如果不设置分组展示,Xmake 也会默认按照树状模式展示,但是有些极端情况下,目录层级显示不是很好,例如:</p> <pre><code class="language-lua">target("test") set_kind("binary") add_files("../../../../src/**.cpp") </code></pre> <p><img alt="" class="embedded_image" loading="lazy" referrerpolicy="no-referrer" rel="noreferrer" src="https://xmake.io/assets/img/manual/filegroup1.png"></p> <p>目前主要支持两种展示模式:</p> <ul> <li>plain: 平坦模式</li> <li>tree: 树形展示,这也是默认模式</li> </ul> <p>另外,它也支持对 <code>add_headerfiles</code> 添加的文件进行分组。</p> <h4>设置分组并指定根目录</h4> <pre><code class="language-lua">target("test") set_kind("binary") add_files("../../../../src/**.cpp") add_filegroups("group1/group2", {rootdir = "../../../../"}) </code></pre> <p><img alt="" class="embedded_image" loading="lazy" referrerpolicy="no-referrer" rel="noreferrer" src="https://xmake.io/assets/img/manual/filegroup2.png"></p> <h4>设置分组并指定文件匹配模式</h4> <pre><code class="language-lua">target("test") set_kind("binary") add_files("../../../../src/**.cpp") add_filegroups("group1/group2", {rootdir = "../../../../", files = {"src/**.cpp"}}) </code></pre> <h4>作为平坦模式展示</h4> <p>这种模式下,所有源文件忽略嵌套的目录层级,在分组下同一层级展示。</p> <pre><code class="language-lua">target("test") set_kind("binary") add_files("../../../../src/**.cpp") add_filegroups("group1/group2", {rootdir = "../../../../", mode = "plain"}) </code></pre> <p><img alt="" class="embedded_image" loading="lazy" referrerpolicy="no-referrer" rel="noreferrer" src="https://xmake.io/assets/img/manual/filegroup3.png"></p> <h3>包版本选择支持 Git Commit</h3> <p>Xmake 的包依赖管理接口 <code>add_requires</code> 支持版本语义选择,分支选择,例如:</p> <pre><code class="language-lua">add_requires("tbox 1.6.1") add_requires("tbox &gt;=1.6.1") add_requires("tbox master") </code></pre> <p>但是,之前的版本,我们还不支持从 Git Commit 中选择版本,而现在我们也支持上了。</p> <pre><code class="language-lua">add_requires("tbox e807230557aac69e4d583c75626e3a7ebdb922f8") </code></pre> <p>只要,这个包的配置中带有 Git url ,就能从 Commit 中选择版本。</p> <h3>更好地支持 iOS 模拟器编译</h3> <p>如果要编译 iOS 平台目标程序,之前可以使用如下配置,仅仅通过切换 arch ,就能分别编译真机,模拟器版本程序。</p> <pre><code class="language-bash">$ xmake f -p iphoneos [-a armv7|armv7s|arm64|i386|x86_64] $ xmake </code></pre> <p>但是由于 M1 设备上模拟器也支持 arm64 架构,因此之前单纯从 arch 去区分是否为模拟器,已无法满足需求。 因此,在新版本中,我们新增了一个参数配置去区分是否为模拟器目标。</p> <pre><code class="language-bash">$ xmake f -p iphoneos --appledev=simulator $ xmake f -p watchos --appledev=simulator $ xmake f -p appletvos --appledev=simulator </code></pre> <p>而如果没有指定 <code>--appledev=</code> 参数,默认就是编译真机程序,当然,之前的模式也是完全兼容的。</p> <h2>更新内容</h2> <h3>新特性</h3> <ul> <li><a href="https://github.com/xmake-io/xmake/issues/2138" rel="nofollow">#2138</a>: 支持模板包</li> <li><a href="https://github.com/xmake-io/xmake/issues/2185" rel="nofollow">#2185</a>: 添加 <code>--appledev=simulator</code> 去改进 Apple 模拟器目标编译支持</li> <li><a href="https://github.com/xmake-io/xmake/issues/2227" rel="nofollow">#2227</a>: 改进 cargo 包,支持指定 Cargo.toml 文件</li> <li>改进 <code>add_requires</code> 支持 git command 作为版本</li> <li><a href="https://github.com/xmake-io/xmake/issues/622" rel="nofollow">#622</a>: 支持远程编译</li> <li><a href="https://github.com/xmake-io/xmake/issues/2282" rel="nofollow">#2282</a>: 添加 <code>add_filegroups</code> 接口为 vs/vsxmake/cmake generator 增加文件组支持</li> </ul> <h3>改进</h3> <ul> <li><a href="https://github.com/xmake-io/xmake/pull/2137" rel="nofollow">#2137</a>: 改进 path 模块</li> <li>macOS 下,减少 50% 的 Xmake 二进制文件大小</li> <li>改进 tools/autoconf,cmake 去更好地支持工具链切换</li> <li><a href="https://github.com/xmake-io/xmake/pull/2221" rel="nofollow">#2221</a>: 改进注册表 api 去支持 unicode</li> <li><a href="https://github.com/xmake-io/xmake/issues/2225" rel="nofollow">#2225</a>: 增加对 protobuf 的依赖分析和构建支持</li> <li><a href="https://github.com/xmake-io/xmake/issues/2265" rel="nofollow">#2265</a>: 排序 CMakeLists.txt</li> <li>改进 os.files 的文件遍历速度</li> </ul> <h3>Bugs 修复</h3> <ul> <li><a href="https://github.com/xmake-io/xmake/issues/2233" rel="nofollow">#2233</a>: 修复 c++ modules 依赖</li> </ul>
收藏(0)  分享
相关标签: 灌水交流
注意:本文归作者所有,未经作者允许,不得转载
0个回复
  • 消灭零回复