Published on

Build Static Lib Package Under ArchLinux

Authors
  • avatar
    Name
    ttyS3
    Twitter

通常情况下,Arch 下的包不像 RHEL 系那样有单独的静态库 (RHEL系命名风格一般是 libXXX-static),但并不是说Arch下面的包都没有静态库,这个得看情况。比如 /lib/libresolv.a, /lib/librt.a 属于 GNU libc (glibc包), /lib/libstdc++.a 属于 gcc 包。

libgit2 包为例,ArchLinux 源里是没有提供静态库的。相对的, Ubuntu 有提供静态库的 package, 甚至连 ArlpineLinux 也有提供。 Fedora 下的 libgit2 好像没有静态lib。Ubuntu 是直接放在 libgit2-dev 包里 (/usr/lib/x86_64-linux-gnu/libgit2.a)

由于Arch源里其实已经有libgit2了,因此我们对 PKGBUILD 文件作做修改即可编译相应的静态库。

首先我们安装 asp, 如果没有安装的话: sudo pacman -S --needed asp

然后获取Arch构建源码: asp checkout libgit2

修改之后执行 makepkg 即可。

如果不用asp, 也可以手动用git clone源码。

进入包页面 https://archlinux.org/packages/extra/x86_64/libgit2/ 然后 点击 Source Files 即可查看源码。然后我们就跳到了 https://github.com/archlinux/svntogit-packages/tree/packages/libgit2/trunk

clone仓库然后 checkout 到 packages/libgit2/trunk 分支即可。(对于 Arch 这种用分支来存包源码的行为,老灯表示有点迷啊,可能跟它从svn转过来有关?)

PKGBUILD 文件如下:

# Maintainer: Lukas Fleischer <[email protected]>
# Contributor: David Runge <[email protected]>
# Contributor: Hilton Medeiros <[email protected]>
# Contributor: Dave Reisner <[email protected]>

pkgname=libgit2
pkgver=1.1.0
pkgrel=1
epoch=1
pkgdesc='A linkable library for Git'
arch=('x86_64')
url="https://libgit2.github.com/"
depends=('glibc' 'http-parser' 'openssl' 'pcre' 'zlib')
makedepends=('cmake' 'libssh2' 'python')
provides=('libgit2.so')
license=('GPL2')
source=("$pkgname-$pkgver.tar.gz::https://github.com/libgit2/libgit2/archive/v${pkgver}.tar.gz"
        "${pkgname}-0.99.0-remove_http-parse_incompatible_tests.patch")
sha512sums=('347bb68900181b44fa58a0417506c91383adb965607fce049a5b4c57ac9cc286e0a140d164c339b50fb6cd6951f47757c2917a2df44ba004bfaa4fb643946bb8'
            'e73072424c9c1870eaaf93b3451295ef7333b59f6cb8a6897dd690b69a20aaeb70f00d15a692c1d9e0745d5ef16bbbb912fbd570d8bc83ca0b7d57f32025bf94')
b2sums=('2a1c1f71d2a2e06448c78eb46028fdcfd59682dccf2365851c4bd059cdd78842320f9a5ba7345e761611a5b4eba634faf2e26cc669097da0ba2e1c832c23059f'
        'cdca2012f772afea99436faa02f80697dc9042a6eb5ae14f8ee8ba9e100a65b936cdfaf84ec0361543c70859375c823a25cfee52b0face40b8dea2ec2cf1de59')

prepare() {
  cd "$pkgname-$pkgver"
  # removing tests that are only compatible with the (modified) vendored
  # version of http-parser, but not with upstream http-parser
  patch -Np1 -i "../${pkgname}-0.99.0-remove_http-parse_incompatible_tests.patch"
}


build() {
  cd "$pkgname-$pkgver"
  cmake -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_BUILD_TYPE='None' \
        -DUSE_HTTP_PARSER=system \
        -DTHREADSAFE=ON \
        -Wno-dev \
        -B build \
        -S .
  make -C build VERBOSE=1
}

check() {
  cd "$pkgname-$pkgver"
  make -C build test VERBOSE=1
}

package() {
  depends+=('libssh2.so')
  cd "$pkgname-$pkgver"
  make -C build DESTDIR="$pkgdir" install
  install -vDm 644 {AUTHORS,README.md} \
    -t "${pkgdir}/usr/share/doc/${pkgname}"
}

我们稍做修改:

diff --git a/trunk/PKGBUILD b/trunk/PKGBUILD
index 9192069..9eb6a7a 100644
--- a/trunk/PKGBUILD
+++ b/trunk/PKGBUILD
@@ -3,38 +3,41 @@
 # Contributor: Hilton Medeiros <[email protected]>
 # Contributor: Dave Reisner <[email protected]>
 
-pkgname=libgit2
+pkgname=libgit2-static
+srcname=libgit2
 pkgver=1.1.0
 pkgrel=1
 epoch=1
-pkgdesc='A linkable library for Git'
+pkgdesc='libgit2 static library for cross compile purpose'
 arch=('x86_64')
 url="https://libgit2.github.com/"
-depends=('glibc' 'http-parser' 'openssl' 'pcre' 'zlib')
-makedepends=('cmake' 'libssh2' 'python')
-provides=('libgit2.so')
+depends=('glibc' 'http-parser' 'pcre' 'zlib' 'libgit2')
+makedepends=('cmake' 'python')
+provides=('libgit2.a')
 license=('GPL2')
-source=("$pkgname-$pkgver.tar.gz::https://github.com/libgit2/libgit2/archive/v${pkgver}.tar.gz"
-        "${pkgname}-0.99.0-remove_http-parse_incompatible_tests.patch")
+source=("$srcname-$pkgver.tar.gz::https://github.com/libgit2/libgit2/archive/v${pkgver}.tar.gz"
+        "${srcname}-0.99.0-remove_http-parse_incompatible_tests.patch")
 sha512sums=('347bb68900181b44fa58a0417506c91383adb965607fce049a5b4c57ac9cc286e0a140d164c339b50fb6cd6951f47757c2917a2df44ba004bfaa4fb643946bb8'
             'e73072424c9c1870eaaf93b3451295ef7333b59f6cb8a6897dd690b69a20aaeb70f00d15a692c1d9e0745d5ef16bbbb912fbd570d8bc83ca0b7d57f32025bf94')
 b2sums=('2a1c1f71d2a2e06448c78eb46028fdcfd59682dccf2365851c4bd059cdd78842320f9a5ba7345e761611a5b4eba634faf2e26cc669097da0ba2e1c832c23059f'
         'cdca2012f772afea99436faa02f80697dc9042a6eb5ae14f8ee8ba9e100a65b936cdfaf84ec0361543c70859375c823a25cfee52b0face40b8dea2ec2cf1de59')
 
 prepare() {
-  cd "$pkgname-$pkgver"
+  cd "$srcname-$pkgver"
   # removing tests that are only compatible with the (modified) vendored
   # version of http-parser, but not with upstream http-parser
-  patch -Np1 -i "../${pkgname}-0.99.0-remove_http-parse_incompatible_tests.patch"
+  patch -Np1 -i "../${srcname}-0.99.0-remove_http-parse_incompatible_tests.patch"
 }
 
 
 build() {
-  cd "$pkgname-$pkgver"
+  cd "$srcname-$pkgver"
   cmake -DCMAKE_INSTALL_PREFIX=/usr \
         -DCMAKE_BUILD_TYPE='None' \
         -DUSE_HTTP_PARSER=system \
         -DTHREADSAFE=ON \
+        -DBUILD_SHARED_LIBS=OFF \
+        -DUSE_SSH=OFF \
         -Wno-dev \
         -B build \
         -S .
@@ -42,14 +45,11 @@ build() {
 }
 
 check() {
-  cd "$pkgname-$pkgver"
+  cd "$srcname-$pkgver"
   make -C build test VERBOSE=1
 }
 
 package() {
-  depends+=('libssh2.so')
-  cd "$pkgname-$pkgver"
-  make -C build DESTDIR="$pkgdir" install
-  install -vDm 644 {AUTHORS,README.md} \
-    -t "${pkgdir}/usr/share/doc/${pkgname}"
+  cd "$srcname-$pkgver"
+  install -vD -m755 ./build/libgit2.a $pkgdir/usr/lib/libgit2.a
 }

完整文件:

# Maintainer: Lukas Fleischer <[email protected]>
# Contributor: David Runge <[email protected]>
# Contributor: Hilton Medeiros <[email protected]>
# Contributor: Dave Reisner <[email protected]>

pkgname=libgit2-static
srcname=libgit2
pkgver=1.1.0
pkgrel=1
epoch=1
pkgdesc='libgit2 static library for cross compile purpose'
arch=('x86_64')
url="https://libgit2.github.com/"
depends=('glibc' 'http-parser' 'pcre' 'zlib' 'libgit2')
makedepends=('cmake' 'python')
provides=('libgit2.a')
license=('GPL2')
source=("$srcname-$pkgver.tar.gz::https://github.com/libgit2/libgit2/archive/v${pkgver}.tar.gz"
        "${srcname}-0.99.0-remove_http-parse_incompatible_tests.patch")
sha512sums=('347bb68900181b44fa58a0417506c91383adb965607fce049a5b4c57ac9cc286e0a140d164c339b50fb6cd6951f47757c2917a2df44ba004bfaa4fb643946bb8'
            'e73072424c9c1870eaaf93b3451295ef7333b59f6cb8a6897dd690b69a20aaeb70f00d15a692c1d9e0745d5ef16bbbb912fbd570d8bc83ca0b7d57f32025bf94')
b2sums=('2a1c1f71d2a2e06448c78eb46028fdcfd59682dccf2365851c4bd059cdd78842320f9a5ba7345e761611a5b4eba634faf2e26cc669097da0ba2e1c832c23059f'
        'cdca2012f772afea99436faa02f80697dc9042a6eb5ae14f8ee8ba9e100a65b936cdfaf84ec0361543c70859375c823a25cfee52b0face40b8dea2ec2cf1de59')

prepare() {
  cd "$srcname-$pkgver"
  # removing tests that are only compatible with the (modified) vendored
  # version of http-parser, but not with upstream http-parser
  patch -Np1 -i "../${srcname}-0.99.0-remove_http-parse_incompatible_tests.patch"
}


build() {
  cd "$srcname-$pkgver"
  cmake -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_BUILD_TYPE='None' \
        -DUSE_HTTP_PARSER=system \
        -DTHREADSAFE=ON \
        -DBUILD_SHARED_LIBS=OFF \
        -DUSE_SSH=OFF \
        -Wno-dev \
        -B build \
        -S .
  make -C build VERBOSE=1
}

check() {
  cd "$srcname-$pkgver"
  make -C build test VERBOSE=1
}

package() {
  cd "$srcname-$pkgver"
  install -vD -m755 ./build/libgit2.a $pkgdir/usr/lib/libgit2.a
}

Refs

https://github.com/libgit2/libgit2

https://pkgs.alpinelinux.org/package/edge/community/x86_64/libgit2-static

https://packages.ubuntu.com/focal/amd64/libgit2-dev/filelist

https://wiki.archlinux.org/index.php/Arch_Build_System#Retrieve_PKGBUILD_source_using_Git

https://archlinux.org/packages/extra/x86_64/libgit2/