0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pv (Pipe Viewer) を Universal Binary としてビルドする

Last updated at Posted at 2025-04-10

前記事で、Pipe Viewerpvコマンド)を利用しました。

ここで、普通に pv コマンドをインストールすると、インストールされた pv バイナリは、

  • そのホストマシンのCPUと同じCPUアーキテクチャ用 (x86_64 or arm64)
  • 動作するOSバージョンはそのホストマシンの macOS バージョン以降

となってしまいます。

ビルドされる pv バイナリが、できるだけ広範な環境で動くようにするには、Universal Binary としてビルドし、また動作OSバージョンを低めに設定するのが望ましいでしょう。

そのために、次のようなシェルスクリプトを用意するとよいでしょう。

#!/bin/bash

set -eux

PV_VERSION="1.9.31"
PV_URL="https://www.ivarch.com/programs/sources/pv-${PV_VERSION}.tar.gz"

ORIGDIR="$(pwd)"
WORKDIR="${ORIGDIR}/pv_build"
mkdir -p "${WORKDIR}"
cd "${WORKDIR}"

curl -fL -o "pv-${PV_VERSION}.tar.gz" "${PV_URL}"
tar xzf "pv-${PV_VERSION}.tar.gz"
cd "pv-${PV_VERSION}"

# Apple Clang を明示指定
export CC="/usr/bin/clang"
export CXX="/usr/bin/clang++"
export LD="/usr/bin/clang"

# macOS 10.13 以降(Intel) / macOS 11 以降(Apple Silicon) 対応で Universal build をビルド
export MACOSX_DEPLOYMENT_TARGET="10.13"
export CFLAGS="-arch x86_64 -arch arm64 -mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET} -Wno-implicit-function-declaration"
export LDFLAGS="-arch x86_64 -arch arm64 -mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET}"

./configure --prefix="${WORKDIR}/pv_inst" --disable-dependency-tracking
make -j4
make install

cp -p "${WORKDIR}/pv_inst/bin/pv" "${ORIGDIR}"

echo "=== Build completed! ==="
lipo -info "${ORIGDIR}/pv"

-arch x86_64 -arch arm64 で Universal Binary の生成を指定し、-mmacosx-version-min=10.13 で動作OS要件を指定します。(Apple Silicon 対応のOSは最低でも macOS 11.0 Big Sur なので、arm64 バイナリでは 10.13 を指定しても 11.0 となります。)

これらのオプションに対応させるために、コンパイラ・リンカには Apple Clang を明示指定する必要があります。(Homebrew や MacPorts で GCC がインストールされていると、デフォルトではその GCC が呼び出されてしまう可能性があります。)それが CC, CXX, LD の指定の部分です。

このスクリプトを実行すると、カレントディレクトリに Universal Binary の pv コマンドが生成されているはずです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?