Xrepo:一個現代化的跨平臺 C/C++ 包管理器

waruqi發表於2020-11-25

xrepo 是一個基於 Xmake 的跨平臺 C/C++ 包管理器。

它基於 xmake 提供的執行時,但卻是一個完整獨立的包管理程式,相比 vcpkg/homebrew 此類包管理器,xrepo 能夠同時提供更多平臺和架構的 C/C++ 包。

並且還支援多版本語義選擇,另外它還是一個去中心化的分散式倉庫,不僅僅提供了官方的 xmake-repo 倉庫,還支援使用者自建多個私有倉庫。

同時,xrepo 也支援從 vcpkg/homebrew/conan 等第三方包管理器中安裝包,並提供統一一致的庫連結資訊,方便與第三方專案的整合對接。

如果你想要了解更多,請參考:線上文件, Github 以及 Gitee

安裝

我們只需要安裝上 xmake 就可以使用 xrepo 命令,關於 xmake 的安裝,我們可以看下:xmake 安裝文件

支援平臺

  • 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

支援的包管理倉庫

  • 官方自建倉庫 xmake-repo (tbox >1.6.1)
  • 使用者自建倉庫
  • Conan (conan::openssl/1.1.1g)
  • Vcpkg (vcpkg:ffmpeg)
  • Homebrew/Linuxbrew (brew::pcre2/libpcre2-8)
  • Pacman on archlinux/msys2 (pacman::libcurl)
  • Clib (clib::clibs/bytes@0.0.4)
  • Dub (dub::log 0.4.3)

分散式倉庫支援

除了可以直接從官方倉庫:xmake-repo 檢索安裝包之外,
我們還可以新增任意多個自建的倉庫,甚至可以完全隔離外網,僅僅在公司內部網路維護私有包的安裝整合。

只需要通過下面的命令,新增上自己的倉庫地址:

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

與 xmake 的工程無縫整合

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")

下面是與 xmake 整合的整體架構和編譯流程。

Xrepo:一個現代化的跨平臺 C/C++ 包管理器

快速上手

安裝包

基本使用

$ xrepo install zlib tbox

安裝指定版本包

完整支援 Semantic Versioning (語義版本)。

$ xrepo install "zlib 1.2.x"
$ xrepo install "zlib >=1.2.0"

安裝指定平臺包

$ 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

安裝除錯版本包

$ xrepo install -m debug zlib

安裝動態庫版本包

$ xrepo install -k shared zlib

安裝指定配置包

$ xrepo install -f "vs_runtime=MD" zlib
$ xrepo install -f "regex=true,thread=true" boost

安裝第三方包管理器的包

$ xrepo install brew::zlib
$ xrepo install vcpkg::zlib
$ xrepo install conan::zlib/1.2.11
$ xrepo install pacman:libpng
$ xrepo install dub:log

查詢包的庫使用資訊

$ 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

匯出安裝後的包

xrepo 可以快速匯出已經安裝後的包,包括對應的庫檔案,標頭檔案等等。

$ xrepo export -o /tmp/output zlib

搜尋支援的包

$ 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)

檢視包環境資訊

$ 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"
}

載入執行包環境並執行命令

$ 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 ..

檢視包資訊

$ 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"}

https://tboox.org/cn/2020/11/15/xrepo-new-command/

相關文章