Apache License 2.0

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度解析 Xrepo:跨平台的 C/C++ 包管理器

项目背景与痛点

在 C/C++ 生态中,跨平台的构建和包管理一直以来是一个棘手的问题。不同的操作系统有各自的编译工具和链接库,这使得多平台项目的维护变得复杂。xmake-io/xrepo 应运而生,它旨在为 C/C++ 开发者提供一个高效、稳定的跨平台包管理工具,解决多平台编建中的繁琐细节。

传统的构建工具如 CMake 和 autotools,尽管功能强大,但在跨平台编译上的表现却不尽如人意,尤其是在自动化和标准化编译流程方面。xrepo 通过 Xmake 框架,把包的安装和管理系统化,方便开发者配置他们的构建流程,使跨平台的开发更加轻便。

核心技术揭秘

xrepo 核心依靠 Xmake,这是其技术栈的核心。Xmake 是一个高级的跨平台构建工具,在编译流程设计和包管理上有着独特优势。xrepo 在 Xmake 的基础上实现了一个综合性的包管理系统,使得 C/C++ 项目的配置和安装流程更加顺滑。具体的技术细节如下:

graph TD A[用户发起安装请求] --> B[读取 xmake.lua] B --> C[计算依赖包] C --> D{"解决本地依赖包?"} D -->|"已解决"| E[安装本地依赖] D -->|"未解决"| F[在线查找依赖包] F --> G[下载依赖包] G --> H[编译依赖包] H --> I[缓存依赖包] I --> J[安装依赖包] J --> K[配置项目] K --> L[执行项目]

整个流程从 xmake.lua 配置文件开始,计算依赖,解决本地包和外部依赖,再下载、编译和安装。每一步都极为关键,并且高度自动化,减少了人为干预。

功能亮点与差异

xrepo 最大的亮点在于其完整的手动或自动构建流程。不仅仅在于跨平台支持,更重要的是自动化安装和编译依赖项的方式。对于普通开发者,xrepo 的配置简单直接;对于复杂的企业级应用场景,Xmake 提供了强大的插件机制来扩展和调整包管理及构建流程。

应用场景与落地建议

在实际生产环境中,使用 xrepo 进行跨平台 C/C++ 开发需要注意多个细节。首先是环境依赖,由于 xrepo 集成了多个平台的依赖包,跨平台构建环境的要求相对较低,但仍需验证每种平台上的可用性。其次是缓存机制,利用 xrepo 自带的缓存系统,可以显著提高编译速度,快速迭代应用代码。

适合使用 xrepo 的场景包括但不限于跨平台开发、自动化 CI/CD 集成、大规模 C/C++ 库和框架开发支持。

综合评价

优点:

  • 简洁易用的配置语言:借助 Xmake,使得项目配置简单快速,快速上手。
  • 强大的包管理能力:采用 Xmake 架构,便于跨平台和自动化处理复杂的包依赖。
  • 高效的构建流程:集成缓存系统,大幅加速构建过程。

缺点:

  • 文档仍需完善:虽然项目开发已经开始,但完整详细的文档和示例仍需补充。
  • 社区支持需强化:作为新开库,社区活跃度与用户反馈仍需提升。

总的来说,xrepo 为 C/C++ 跨平台项目提供了宝贵的支持,尽管仍有改进空间,但它无疑为现代 C/C++ 开发人员提供了一个强大的工具。

关于

GitHub 原始描述(英文)

🗂️ A cross-platform C/C++ package manager based on Xmake

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

语言占比

Batchfile
Shell

贡献者

README

xrepo

A cross-platform C/C++ package manager based on Xmake

Supporting the project

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. 🙏 [Become a sponsor]

Introduction (中文)

xrepo is a cross-platform C/C++ package manager based on Xmake.

It is based on the runtime provided by xmake, but it is a complete and independent package management program. Compared with package managers such as vcpkg/homebrew, xrepo can provide C/C++ packages for more platforms and architectures at the same time.

And it also supports multi-version semantic selection. In addition, it is also a decentralized distributed repository. It not only provides the official xmake-repo repository, It also supports users to build multiple private repositorys.

At the same time, xrepo also supports installing packages from third-party package managers such as vcpkg/homebrew/conan, and provides unified and consistent library link information to facilitate integration and docking with third-party projects.

If you want to know more, please refer to: Documents, Github and Gitee

Installation

We only need install xmake to use the xrepo command. About the installation of xmake, we can see: Xmake Installation Document.

Supported platforms

  • Windows (x86, x64)
  • macOS (i386, x86_64, arm64)
  • Linux (i386, x86_64, cross-toolchains …)
  • *BSD (i386, x86_64)
  • Android (x86, x86_64, armeabi, armeabi-v7a, arm64-v8a)
  • iOS (armv7, armv7s, arm64, i386, x86_64)
  • MSYS (i386, x86_64)
  • MinGW (i386, x86_64, arm, arm64)
  • Cross Toolchains

Supported package repositories

  • Official package repository xmake-repo (tbox >1.6.1)
  • User-built repositories
  • Conan (conan::openssl/1.1.1g)
  • Vcpkg (vcpkg:ffmpeg)
  • Homebrew/Linuxbrew (brew::pcre2/libpcre2-8)
  • Pacman on archlinux/msys2 (pacman::libcurl)
  • Clib (clib::clibs/[email protected])
  • Dub (dub::log 0.4.3)

Suppory distributed repository

In addition to directly retrieving the installation package from the official repository: xmake-repo.

We can also add any number of self-built repositories, and even completely isolate the external network, and only maintain the installation and integration of private packages on the company’s internal network.

Just use the following command to add your own repository address:

$ xrepo add-repo myrepo https://github.com/mygroup/myrepo

Seamless integration with xmake project

add_requires("tbox >1.6.1", "libuv master", "vcpkg::ffmpeg", "brew::pcre2/libpcre2-8")
add_requires("conan::openssl/1.1.1g", {alias = "openssl", optional = true, debug = true})
target("test")
     set_kind("binary")
     add_files("src/*.c")
     add_packages("tbox", "libuv", "vcpkg::ffmpeg", "brew::pcre2/libpcre2-8", "openssl")

The following is the overall architecture and compilation process integrated with xmake.

Get started

Installation package

Basic usage
$ xrepo install zlib tbox
Install the specified version package
$ xrepo install "zlib 1.2.x"
$ xrepo install "zlib >=1.2.0"
Install the specified platform package
$ xrepo install -p iphoneos -a arm64 zlib
$ xrepo install -p android [--ndk=/xxx] zlib
$ xrepo install -p mingw [--mingw=/xxx] zlib
$ xrepo install -p cross --sdk=/xxx/arm-linux-musleabi-cross zlib
Install the debug package
$ xrepo install -m debug zlib
Install the package with dynamic library
$ xrepo install -k shared zlib
Install the specified configuration package
$ xrepo install -f "vs_runtime='MD'" zlib
$ xrepo install -f "regex=true,thread=true" boost
Install packages from third-party package manager
$ xrepo install brew::zlib
$ xrepo install vcpkg::zlib
$ xrepo install conan::zlib/1.2.11
$ xrepo install pacman:libpng
$ xrepo install dub:log

Find the library information of the package

$ xrepo fetch pcre2
{
  {
    linkdirs = {
      "/usr/local/Cellar/pcre2/10.33/lib"
    },
    links = {
      "pcre2-8"
    },
    defines = {
      "PCRE2_CODE_UNIT_WIDTH=8"
    },
    includedirs = "/usr/local/Cellar/pcre2/10.33/include"
  }
}
$ xrepo fetch --ldflags openssl
-L/Users/ruki/.xmake/packages/o/openssl/1.1.1/d639b7d6e3244216b403b39df5101abf/lib -lcrypto -lssl
$ xrepo fetch --cflags openssl
-I/Users/ruki/.xmake/packages/o/openssl/1.1.1/d639b7d6e3244216b403b39df5101abf/include
$ xrepo fetch -p [iphoneos|android] --cflags "zlib 1.2.x"
-I/Users/ruki/.xmake/packages/z/zlib/1.2.11/df72d410e7e14391b1a4375d868a240c/include
$ xrepo fetch --cflags --ldflags conan::zlib/1.2.11
-I/Users/ruki/.conan/data/zlib/1.2.11/_/_/package/f74366f76f700cc6e991285892ad7a23c30e6d47/include -L/Users/ruki/.conan/data/zlib/1.2.11/_/_/package/f74366f76f700cc6e991285892ad7a23c30e6d47/lib -lz

Import and export packages

xrepo can quickly export the installed packages, including the corresponding library files, header files, and so on.

$ xrepo export -o /tmp/output zlib

You can also import the previously exported installation package on other machines to implement package migration.

$ xrepo import -i /xxx/packagedir zlib

Search supported packages

$ xrepo search zlib "pcr*"
    zlib:
      -> zlib: A Massively Spiffy Yet Delicately Unobtrusive Compression Library (in xmake-repo)
    pcr*:
      -> pcre2: A Perl Compatible Regular Expressions Library (in xmake-repo)
      -> pcre: A Perl Compatible Regular Expressions Library (in xmake-repo)

In addition, you can now search for their packages from third-party package managers such as vcpkg, conan, conda, and apt. You only need to add the corresponding package namespace, for example:

$ xrepo search vcpkg::pcre
The package names:
     vcpkg::pcre:
       -> vcpkg::pcre-8.44#8: Perl Compatible Regular Expressions
       -> vcpkg::pcre2-10.35#2: PCRE2 is a re-working of the original Perl Compatible Regular Expressions library
$ xrepo search conan::openssl
The package names:
     conan::openssl:
       -> conan::openssl/1.1.1g:
       -> conan::openssl/1.1.1h:

Show package environment information

$ xrepo env --show luajit
{
   OLDPWD = "/mnt/tbox",
   HOME = "/home/ruki",
   PATH = "/home/ruki/.xmake/packages/l/luajit/2.1.0-beta3/fbac76d823b844f0b91abf3df0a3bc61/bin:/tmp:/tmp/arm-linux-musleabi-cross/bin:~/.local/bin: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
   TERM = "xterm",
   PWD = "/mnt/xmake",
   XMAKE_PROGRAM_DIR = "/mnt/xmake/xmake",
   HOSTNAME = "e6edd61ff1ab",
   LD_LIBRARY_PATH = "/home/ruki/.xmake/packages/l/luajit/2.1.0-beta3/fbac76d823b844f0b91abf3df0a3bc61/lib",
   SHLVL = "1",
   _ = "/mnt/xmake/scripts/xrepo.sh"
}

Load package environment and run commands

$ xrepo env luajit
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
JIT: ON SSE2 SSE3 SSE4.1 BMI2 fold cse dce fwd dse narrow loop abc sink fuse
>
$ xrepo env -b "luajit 2.x" luajit
$ xrepo env -p iphoneos -b "zlib,libpng,luajit 2.x" cmake ..

Package virtual environment

Enter the virtual environment

We can customize some package configurations by adding the xmake.lua file in the current directory, and then enter the specific package virtual environment.

add_requires("zlib 1.2.11")
add_requires("python 3.x", "luajit")
$ xrepo env shell
> python --version
> luajit --version

We can also configure and load the corresponding toolchain environment in xmake.lua, for example, load the VS compilation environment.

set_toolchains("msvc")
Manage virtual environments

We can use the following command to register the specified virtual environment configuration globally to the system for quick switching.

$ xrepo env --add /tmp/base.lua

At this time, we have saved a global virtual environment called base, and we can view it through the list command.

$ xrepo env --list
/Users/ruki/.xmake/envs:
  -base
envs(1) found!

We can also delete it.

$ xrepo env --remove base
Switch global virtual environment

If we register multiple virtual environments, we can also switch them quickly.

$ xrepo env -b base shell
> python --version

Or directly load the specified virtual environment to run specific commands

$ xrepo env -b base python --version

xrepo env -b/--bind is to bind the specified virtual environment. For more details, see: #1762

Show the given package information

$ xrepo info zlib
The package info of project:
    require(zlib):
      -> description: A Massively Spiffy Yet Delicately Unobtrusive Compression Library
      -> version: 1.2.11
      -> urls:
         -> http://zlib.net/zlib-1.2.11.tar.gz
            -> c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1
         -> https://downloads.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
            -> c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1
      -> repo: xmake-repo https://gitee.com/tboox/xmake-repo.git master
      -> cachedir: /Users/ruki/.xmake/cache/packages/2010/z/zlib/1.2.11
      -> installdir: /Users/ruki/.xmake/packages/z/zlib/1.2.11/d639b7d6e3244216b403b39df5101abf
      -> searchdirs:
      -> searchnames: zlib-1.2.11.tar.gz
      -> fetchinfo: 1.2.11, system
          -> version: 1.2.11
          -> links: z
          -> linkdirs: /usr/local/Cellar/zlib/1.2.11/lib
          -> includedirs: /usr/local/Cellar/zlib/1.2.11/include
      -> platforms: iphoneos, mingw@windows, macosx, mingw@linux,macosx, android@linux,macosx, windows, linux
      -> requires:
         -> plat: macosx
         -> arch: x86_64
         -> configs:
            -> debug: false
            -> vs_runtime: MT
            -> shared: false
      -> configs:
      -> configs (builtin):
         -> debug: Enable debug symbols. (default: false)
         -> shared: Enable shared library. (default: false)
         -> cflags: Set the C compiler flags.
         -> cxflags: Set the C/C++ compiler flags.
         -> cxxflags: Set the C++ compiler flags.
         -> asflags: Set the assembler flags.
         -> vs_runtime: Set vs compiler runtime. (default: MT)
            -> values: {"MT","MD"}

Uninstall all packages

We can use the following command to batch uninstall and delete the installed packages, supporting pattern matching:

$ xrepo remove --all
$ xrepo remove --all zlib pcr*

Contacts

评论

暂无评论