M1 Mac で古いバージョンの numpy をビルドする

# 環境
M1 Macbook Pro (2021)
OS: MacOS X Monterey
Python バージョン: 3.9.9


# 問題

M1 Mac に古いバージョンの numpy をインストールしようとしたところ，事前ビルド済みのバイナリが存在しないためにローカルでビルドが走ったが，エラーメッセージが出てビルドが失敗した．

## 詳細

python で `pip install` を行うと，コンパイルが必要なパッケージに関しては，使用している OS とバージョンに応じて事前にビルドされたバイナリがダウンロードされ，高速にパッケージをインストールできる仕組みになっている．

しかし，事前ビルド済みのバイナリがない場合は，ローカルでビルドが行われる．

今回インストールしたい `numpy==1.19.3` がリリースされたのは2020年10月のことで，M1 Mac がリリースされた2020年11月より前なので，当時存在しなかった OS に対して事前ビルド済みのバイナリは作られていない．

今回は以下のようにコマンドで numpy をインストールしようとしたところ， C のコンパイルでエラーが発生し，ビルドが失敗した．

## コマンド

```bash
$ pip install numpy==1.19.3
```

## エラーメッセージ

```
<中略>

building 'numpy.core._multiarray_umath' extension
  compiling C sources
  C compiler: clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include

  creating build/temp.macosx-12.1-arm64-3.9/numpy/core/src/multiarray
  creating build/temp.macosx-12.1-arm64-3.9/numpy/core/src/umath
  creating build/temp.macosx-12.1-arm64-3.9/build/src.macosx-12.1-arm64-3.9/numpy/core/src/umath
  creating build/temp.macosx-12.1-arm64-3.9/build/src.macosx-12.1-arm64-3.9/numpy/core/src/common
  creating build/temp.macosx-12.1-arm64-3.9/private
  creating build/temp.macosx-12.1-arm64-3.9/private/var
  creating build/temp.macosx-12.1-arm64-3.9/private/var/folders
  creating build/temp.macosx-12.1-arm64-3.9/private/var/folders/h8
  creating build/temp.macosx-12.1-arm64-3.9/private/var/folders/h8/yzgn6yw92b3dx8g990_w73800000gp
  creating build/temp.macosx-12.1-arm64-3.9/private/var/folders/h8/yzgn6yw92b3dx8g990_w73800000gp/T
  creating build/temp.macosx-12.1-arm64-3.9/private/var/folders/h8/yzgn6yw92b3dx8g990_w73800000gp/T/pip-install-trn_r2lp
  creating build/temp.macosx-12.1-arm64-3.9/private/var/folders/h8/yzgn6yw92b3dx8g990_w73800000gp/T/pip-install-trn_r2lp/numpy_7552e917d2674e7f8beac383fc2b50df
  creating build/temp.macosx-12.1-arm64-3.9/private/var/folders/h8/yzgn6yw92b3dx8g990_w73800000gp/T/pip-install-trn_r2lp/numpy_7552e917d2674e7f8beac383fc2b50df/numpy
  creating build/temp.macosx-12.1-arm64-3.9/private/var/folders/h8/yzgn6yw92b3dx8g990_w73800000gp/T/pip-install-trn_r2lp/numpy_7552e917d2674e7f8beac383fc2b50df/numpy/_build_utils
  creating build/temp.macosx-12.1-arm64-3.9/private/var/folders/h8/yzgn6yw92b3dx8g990_w73800000gp/T/pip-install-trn_r2lp/numpy_7552e917d2674e7f8beac383fc2b50df/numpy/_build_utils/src
  compile options: '-DNPY_INTERNAL_BUILD=1 -DHAVE_NPY_CONFIG_H=1 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -D_LARGEFILE64_SOURCE=1 -DNO_ATLAS_INFO=3 -DHAVE_CBLAS -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/src/umath -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/src/npymath -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/src/common -Inumpy/core/include -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/include/numpy -Inumpy/core/src/common -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/Users/myuser/myrepo/venv/include -I/Users/myuser/.pyenv/versions/3.9.9/include/python3.9 -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/src/common -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/src/npymath -c'
  extra options: '-faltivec -I/System/Library/Frameworks/vecLib.framework/Headers'
  clang: numpy/core/src/multiarray/alloc.c
  clang: numpy/core/src/multiarray/array_assign_scalar.c
  clang: numpy/core/src/multiarray/common.c
  clang: numpy/core/src/multiarray/conversion_utils.c
  clang: numpy/core/src/multiarray/datetime_strings.c
  clang: numpy/core/src/multiarray/buffer.c
  clang: numpy/core/src/multiarray/descriptor.c
  clang: build/src.macosx-12.1-arm64-3.9/numpy/core/src/multiarray/einsum.c
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: build/src.macosx-12.1-arm64-3.9/numpy/core/src/multiarray/lowlevel_strided_loops.c
  clang: numpy/core/src/multiarray/hashdescr.c
  clang: numpy/core/src/multiarray/multiarraymodule.c
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: numpy/core/src/multiarray/nditer_constr.c
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: numpy/core/src/multiarray/refcount.c
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: numpy/core/src/multiarray/scalarapi.c
  clang: numpy/core/src/multiarray/temp_elide.c
  clang: numpy/core/src/multiarray/vdot.c
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: numpy/core/src/umath/ufunc_object.c
  clang: build/src.macosx-12.1-arm64-3.9/numpy/core/src/umath/loops.c
  clang: numpy/core/src/umath/ufunc_type_resolution.c
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: build/src.macosx-12.1-arm64-3.9/numpy/core/src/npymath/ieee754.c
  clang: numpy/core/src/common/array_assign.c
  clang: numpy/core/src/common/ucsnarrow.c
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: build/src.macosx-12.1-arm64-3.9/numpy/core/src/common/npy_cpu_features.c
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: /private/var/folders/h8/yzgn6yw92b3dx8g990_w73800000gp/T/pip-install-trn_r2lp/numpy_7552e917d2674e7f8beac383fc2b50df/numpy/_build_utils/src/apple_sgemv_fix.c
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
  error: Command "clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -DNPY_INTERNAL_BUILD=1 -DHAVE_NPY_CONFIG_H=1 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -D_LARGEFILE64_SOURCE=1 -DNO_ATLAS_INFO=3 -DHAVE_CBLAS -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/src/umath -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/src/npymath -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/src/common -Inumpy/core/include -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/include/numpy -Inumpy/core/src/common -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/Users/myuser/myrepo/venv/include -I/Users/myuser/.pyenv/versions/3.9.9/include/python3.9 -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/src/common -Ibuild/src.macosx-12.1-arm64-3.9/numpy/core/src/npymath -c numpy/core/src/multiarray/array_assign_scalar.c -o build/temp.macosx-12.1-arm64-3.9/numpy/core/src/multiarray/array_assign_scalar.o -MMD -MF build/temp.macosx-12.1-arm64-3.9/numpy/core/src/multiarray/array_assign_scalar.o.d -faltivec -I/System/Library/Frameworks/vecLib.framework/Headers" failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for numpy
Failed to build numpy
ERROR: Could not build wheels for numpy, which is required to install pyproject.toml-based projects
```

# 解決方法
OpenBLAS というパッケージをインストールし，インストール先のパスを環境変数に指定して `pip install` を行う．

```bash
$ brew install openblas
$ OPENBLAS="$(brew --prefix openblas)" pip install numpy==1.19.3
```

無事にビルドできた．

# 備考
上記の方法は python==3.9.7 では失敗したので， pyenv 等で最新の python バージョンを使用することも検討されたい．

その時のエラーメッセージ

```
AssertionError: would build wheel with unsupported tag ('cp39', 'cp39', 'macosx_11_0_arm64')
  ----------------------------------------
  ERROR: Failed building wheel for numpy
Failed to build numpy
ERROR: Could not build wheels for numpy, which is required to install pyproject.toml-based projects
```

# 参考文献
https://github.com/numpy/numpy/issues/17807#issuecomment-731014921