🐕

macOS 15.4でsqlcのビルドに失敗する原因と対策

に公開

macOS 15.4環境で sqlc をビルドしようとした際、エラーが発生してビルドに失敗する現象に遭遇しました。本記事では、その原因と解決策についてまとめます。

発生したエラー

以下は、sqlc バージョン1.28.0をインストールしようとした際に表示されたエラーです。

$ go install github.com/sqlc-dev/sqlc/cmd/sqlc@v1.28.0
# github.com/pganalyze/pg_query_go/v5/parser
src_port_snprintf.c:374:1: error: static declaration of 'strchrnul' follows non-static declaration
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_string.h:198:9: note: previous declaration is here
src_port_snprintf.c:438:27: warning: 'strchrnul' is only available on macOS 15.4 or newer [-Wunguarded-availability-new]
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_string.h:198:9: note: 'strchrnul' has been marked as being introduced in macOS 15.4 here, but the deployment target is macOS 15.0.0
src_port_snprintf.c:438:27: note: enclose 'strchrnul' in a __builtin_available check to silence this warning

なお、v1.29.0 以降ではこの問題は解消されており、正常にインストールできます。

$ go install github.com/sqlc-dev/sqlc/cmd/sqlc@v1.29.0
# => 成功します

検証環境

  • OS: macOS Sequoia 15.4.1 arm64
  • Host: MacBook Air (M1, 2020)

原因

この問題は、pg_query_go という依存ライブラリが、macOS 15.4で新たに追加された strchrnul() 関数を利用しようとしたことに起因しています。
実際、sqlcのIssue #3916pg_query_goのIssue #132 でも報告されており、すでに修正されています。

直接的な原因はPostgreSQL本体のconfigureチェックの誤りでした。
macOS 15.4ではstrchrnul()関数が利用可能になりましたが、この関数はMACOSX_DEPLOYMENT_TARGET >= 15.4という条件下でのみ利用できます。
デフフォルトではMACOSX_DEPLOYMENT_TARGET15.0 に固定されるため、15.4 の新APIは本来利用することができません。
しかし、従来のconfigureでは関数の存在のみをチェックしているため、デフォルトのデプロイメントターゲット(15.0)のままでも使えると判断してしまい、実際のリンクやランタイムで未定義シンボルエラーが発生し、ビルドが失敗します。

これは こちらのコミット で修正されています。

Fix detection and handling of strchrnul() for macOS 15.4.

As of 15.4, macOS has strchrnul(), but access to it is blocked behind
a check for MACOSX_DEPLOYMENT_TARGET >= 15.4. But our does-it-link
configure check finds it, so we try to use it, and fail with the
present default deployment target (namely 15.0). This accounts for
today's buildfarm failures on indri and sifaka.

この修正は pganalyze/libpg_query 17-6.1.0 で取り込まれています。

Update to Postgres 17.4, and add recent patches scheduled for Postgres 17.5 (not yet released)
Notably, this pulls in support for macOS 15.4 which defines strchrnul in its standard library, fixing builds on up-to-date macOS versions.

解決策

1. バージョンを最新にする

最も簡単な解決策は、sqlcpg_query_goのバージョンを最新にアップデートすることです。これにより、macOS 15.4でも問題なくビルドできるようになります。

2. ビルドフラグを指定する

バージョンアップが難しい場合は、以下のようにCGOのフラグを設定し、明示的にmacOS 15.4をターゲットにしてビルドすることで回避できます。

$ export CGO_CFLAGS="-DHAVE_STRCHRNUL -mmacosx-version-min=15.4"
$ export MACOSX_DEPLOYMENT_TARGET="15.4"
$ go install github.com/sqlc-dev/sqlc/cmd/sqlc@v1.28.0

参考リンク

GitHubで編集を提案

Discussion