Raspberry Pi Pico 開発環境を Cygwin 上に構築してみた

## Arm 用 gcc toolchain を cygwin 上で build

Raspberry Pi Pico の開発を Windows 上でやるなら、Visual Studio からインストールするか、なんなら WSL2 上に環境作るかだと思います。

本来 Cygwin上でやる必要もないはずなんですが、色々あって、arm-eabi のtoolchain の build をやってみました。
ただし、やりたかったのは Raspberry Pi Pico で bare metal のプログラム作りたかっただけなので、他の用途で使えるかどうかはわかりません。

Cygwin 側で必要なのは、
gcc, bison, flex, autoconf, automake, libexpat-devel, libgmp-devel, libisl-devel, libelf-devel, libiconv-devel, libmpc-devel, libmpfr-devel, zlib-devel
あたりだと思います。
ソースは、以下のサイトから gcc-arm-none-eabi-10.3-2021.10-src.tar.bz2 をダウンロードしてきます。

https://developer.arm.com/downloads/-/gnu-rm

解凍し、gcc-arm-none-eabi-10.3-2021.10/build ディレクトリを作って、そこに以下のスクリプトを置いて実行します。(/usr/local/arm 以下にインストールする想定で、/usr/local/arm/bin にはパス通しておく)

```build.sh
#!/bin/bash
export BUILDDIR=`pwd`
export INSTDIR=/usr/local/arm
 
for i in bin gcc1 gcc2 newlib1 newlib2 gdb; do
    rm -rf build_$i
    mkdir build_$i
done
rm -rf target-libs
mkdir target-libs
 
# binutils
cd build_bin
../binutils/configure --target=arm-none-eabi --prefix=$INSTDIR --disable-nls --disable-werror --disable-sim --disable-gdb --enable-interwork --enable-plugins --with-sysroot=$INSTDIR/arm-none-eabi
make
make install
cd ..

# gcc path1
cd build_gcc1
../gcc/configure --target=arm-none-eabi --prefix=$INSTDIR --enable-languages=c,c++ --enable-plugins --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --disable-tls --with-newlib --with-headers=yes --disable-werror --with-sysroot=$INSTDIR/arm-none-eabi "--with-host-libstdcxx=static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm"
make INHIBIT_LIBC_CFLAGS="-DUSE_TM_CLONE_REGISTRY=0"
make install
cd ..
 
# gcc path2
cd build_gcc2
../gcc/configure --target=arm-none-eabi --prefix=$BUILDDIR/target-libs --enable-languages=c,c++ --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-libstdcxx-verbose --disable-nls --disable-shared --disable-threads --disable-tls --with-newlib --with-headers=yes --disable-werror --with-sysroot=$BUILDDIR/target-libs/arm-none-eabi "--with-host-libstdcxx=static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm"
make CXXFLAGS_FOR_TARGET="-g -Os -ffunction-sections -fdata-sections -fno-exceptions"
make install
cd ..
 
# newlib path1
cd build_newlib1
export CFLAGS_FOR_TARGET='-g -O2 -ffunction-sections -fdata-sections'
../newlib/configure --target=arm-none-eabi --prefix=$INSTDIR --enable-newlib-io-long-long --enable-newlib-io-c99-formats --enable-newlib-reent-check-verify --enable-newlib-register-fini --enable-newlib-retargetable-locking --disable-newlib-supplied-syscalls --disable-nls --disable-werror
make
make install
export CFLAGS_FOR_TARGET=
cd ..
 
# newlib path2
cd build_newlib2
export CFLAGS_FOR_TARGET='-g -Os -ffunction-sections -fdata-sections'
../newlib/configure --target=arm-none-eabi --prefix=$BUILDDIR/target-libs --disable-newlib-supplied-syscalls --enable-newlib-reent-check-verify --enable-newlib-reent-small --enable-newlib-retargetable-locking --disable-newlib-fvwrite-in-streamio --disable-newlib-fseek-optimization --disable-newlib-wide-orient --enable-newlib-nano-malloc --disable-newlib-unbuf-stream-opt --enable-lite-exit --enable-newlib-global-atexit --enable-newlib-nano-formatted-io --disable-nls --disable-werror
make
make install
export CFLAGS_FOR_TARGET=
cd ..
 
copy_multi_libs() {
    local -a multilibs
    local multilib
    local multi_dir
    local src_prefix
    local dst_prefix
    local src_dir
    local dst_dir
    local target_gcc
 
    for arg in "$@" ; do
        eval "${arg// /\\ }"
    done
 
    multilibs=( $("${target_gcc}" -print-multi-lib 2>/dev/null) )
    for multilib in "${multilibs[@]}" ; do
        multi_dir="${multilib%%;*}"
        src_dir=${src_prefix}/${multi_dir}
        dst_dir=${dst_prefix}/${multi_dir}
        cp -f "${src_dir}/libstdc++.a" "${dst_dir}/libstdc++_nano.a"
        cp -f "${src_dir}/libsupc++.a" "${dst_dir}/libsupc++_nano.a"
        cp -f "${src_dir}/libc.a" "${dst_dir}/libc_nano.a"
        cp -f "${src_dir}/libg.a" "${dst_dir}/libg_nano.a"
        cp -f "${src_dir}/librdimon.a" "${dst_dir}/librdimon_nano.a"
        cp -f "${src_dir}/nano.specs" "${dst_dir}/"
        cp -f "${src_dir}/rdimon.specs" "${dst_dir}/"
        cp -f "${src_dir}/nosys.specs" "${dst_dir}/"
        cp -f "${src_dir}/"*crt0.o "${dst_dir}/"
    done
}
 
copy_multi_libs src_prefix="$BUILDDIR/target-libs/arm-none-eabi/lib" dst_prefix="$INSTDIR/arm-none-eabi/lib" target_gcc="$BUILDDIR/target-libs/bin/arm-none-eabi-gcc"
 
mkdir $INSTDIR/arm-none-eabi/include/newlib-nano
cp $BUILDDIR/target-libs/arm-none-eabi/include/newlib.h $INSTDIR/arm-none-eabi/include/newlib-nano/
 
# gdb
cd build_gdb
../gdb/configre --target=arm-none-eabi --prefix=$INSTDIR --disable-nls --disable-sim --disable-gas --disable-binutils --disable-ld --disable-gprof --with-libexpat --with-lzma=no
make
make install
cd ..
```

以上。
SDK のセットアップ後に試したところ、一応 cygwin の cmake,make とともに使って、ちゃんと動いてるようです。