# Building on FreeBSD

# Install dependencies

Install dependencies with pkg.

sudo pkg install autoconf automake bash cmake coreutils docbook-xsl dpkg fakeroot findutils gettext git gmake gnugrep gnupg gsed gtar libtool libplist ncurses openssl patch perl5 pkgconf po4a python39 wget zstd

You'll also need to install triehash

wget -O triehash https://raw.githubusercontent.com/julian-klode/triehash/main/triehash.pl
gsed -i 's|#!/usr/bin/perl -w|#!/usr/bin/env perl -w|g' triehash
sudo install -m755 triehash /usr/local/bin

# Install toolchain

# Automatic install

This script (opens new window) will setup your SDKs, cctools-port with your iOS toolchain, and other dependencies needed.

To run the script

wget -qO- https://gist.githubusercontent.com/asdfugil/8d629aa1d9f5080a4c6c868b47600879/raw/d845a136b5e1d447d8bd5a042199f8a309d684ab/procursus-toolchain-fbsd.sh | bash

Then, add the following to your shell's startup file (such as ~/.profile)

export PATH=${HOME}/.local/bin:${PATH}
export GNUBINDIR=${HOME}/.local/libexec/gnubin

# Manual install

If you want to install the toolchain manually for whatever reason, begin with exporting the location where you want to install the toolchain.

export PREFIX=${HOME}/.local
# or
# export PREFIX=/usr/local

Then, create a directory of GNU binaries

COREUTILS='basename cat chgrp chmod chown chroot cksum comm cp csplit cut date dd df dir dircolors dirname du echo env expand expr factor false fmt fold groups head hostid hostname id install join kill link ln logname ls md5sum mkdir mkfifo mknod mv nice nl nohup od paste pathchk pinky pr printenv printf ptx pwd readlink rm rmdir seq sha1sum shred sleep sort split stat stty su sum sync tac tail tee test touch tr true tsort tty uname unexpand uniq unlink uptime users vdir wc who whoami yes'
OTHER_GNU_BINS='make tar sed patch find'
export GNUBINDIR="${PREFIX}/libexec/gnubin"
mkdir -p "${GNUBINDIR}"
for util in $COREUTILS $OTHER_GNU_BINS; do
        ln -sfn /usr/local/bin/g$util "${GNUBINDIR}/${util}" 
done
ln -sfn /usr/local/bin/grep "${GNUBINDIR}/grep"

Temporarily add it to PATH

export PATH="${GNUBINDIR}":"${PATH}"

# Install libtapi

export TAPI_VERSION=1100.0.11
wget -O /tmp/libtapi.tar.gz https://github.com/tpoechtrager/apple-libtapi/archive/refs/heads/${TAPI_VERSION}.tar.gz
tar -xf /tmp/libtapi.tar.gz
mkdir -p apple-libtapi-${TAPI_VERSION}/build
pushd apple-libtapi-${TAPI_VERSION}/build
  TAPI_INCLUDE_FIX="-I $PWD/../src/llvm/projects/clang/include "
  TAPI_INCLUDE_FIX+="-I $PWD/projects/clang/include "

  cmake ../src/llvm \
    -DCMAKE_CXX_FLAGS="${TAPI_INCLUDE_FIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${PREFIX} \
    -DTAPI_FULL_VERSION="${TAPI_VERSION}" \
    -DLLVM_ENABLE_PROJECTS="libtapi" \
    -DTAPI_INCLUDE_TESTS=OFF \
    -DLLVM_INCLUDE_TESTS=OFF

  make -j$(sysctl -n hw.ncpu) clangBasic libtapi
  make -j$(sysctl -n hw.ncpu) install-libtapi install-tapi-headers
popd

# Installing cctools

git clone https://github.com/tpoechtrager/cctools-port.git
cd cctools-port/cctools
# Build for arm64
./configure \
  --prefix=${PREFIX} \
  --with-libtapi=${PREFIX} \
  --target=aarch64-apple-darwin
make -j$(sysctl -n hw.ncpu)
make install
make clean

# Build for x86_64
./configure \
  --prefix=${PREFIX} \
  --with-libtapi=${PREFIX} \
  --target=x86_64-apple-darwin
make -j$(sysctl -n hw.ncpu)
make install
make clean
cd ../..

# Installing the clang wrapper

wget -P /tmp https://github.com/ProcursusTeam/Procursus/raw/main/build_tools/wrapper.c

mkdir -p ${PREFIX}/bin

cc -O2 -Wall -Wextra -Wno-address -Wno-incompatible-pointer-types -pedantic \
  /tmp/wrapper.c \
  -o ${PREFIX}/bin/aarch64-apple-darwin

ln -f ${PREFIX}/bin/aarch64-apple-darwin ${PREFIX}/bin/x86_64-apple-darwin

rm /tmp/wrapper.c

# Installing ldid

git clone https://github.com/ProcursusTeam/ldid.git
cd ldid
make -j$(nproc)
make -j$(nproc) install
cd ..

# Get the SDKs

mkdir -p ~/cctools/{SDK,MacOSX-SDKs,iOS-SDKs}
git clone --depth=1 https://github.com/xybp888/iOS-SDKs ~/cctools/iOS-SDKs
# Theae SDKs already includes C++ headers and symbols for Private Frameworks, so you can use them as-is.
# You might want to use a newer SDK here
ln -s ~/cctools/iOS-SDKs/iPhoneOS14.5.sdk ~/cctools/SDK/iPhoneOS.sdk
# You also need macOS SDKs
git clone --depth=1 https://github.com/phracker/MacOSX-SDKs ~/cctools/MacOSX-SDKs
ln -s ~/cctools/MacOSX-SDKs/MacOSX11.3.sdk ~/cctools/SDK/MacOSX.sdk 
cd ~/cctools/SDK
wget -P /tmp https://cdn.discordapp.com/attachments/672628720497852459/871756626066018354/AppleTVOS.sdk.zst https://cdn.discordapp.com/attachments/672628720497852459/871756640754470933/WatchOS.sdk.zst
zstd -d /tmp/{AppleTVOS,WatchOS}.sdk.zst
rm /tmp/{AppleTVOS,WatchOS}.sdk.zst
cd ../..

# Building Procursus

Clone the Procursus repository

git clone --recursive https://github.com/ProcursusTeam/Procursus.git

Next, you should remove ${GNUBINDIR} from your PATH because many things in FreeBSD do not expect GNU binaries

The easiest way is to restart your shell.

Then, add this to ~/.profile or the equivalent for your shell.

# Add our toolchain to the path.
export PATH=${PREFIX}/bin:${PATH}
# This tells the Procursus makefile where to find the GNU binaries
export GNUBINDIR="${PREFIX}/libexec/gnubin"

After that, run the above commands in your current shell or restart your shell.

Then, try to build bash

cd Procursus
gmake bash-package

If the build is successful, congrats! You should now be able to compile packages from Procursus. Take note that because you're not using macOS, it's likely that some packages (particularly those that need Go, Python, and/or NodeJS) will fail to compile. You will need a macOS enabled computer to compile those.

In addition, there are a few things that you might find useful.

# Extras

# Building Go Packages

Currently unsupported.

# Building Rust Packages

If you want to build Rust packages, you'll need to install rustup and install the following targets

Installing rustup

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# iOS arm64
rustup target add aarch64-apple-ios

# macOS arm64
rustup target add aarch64-apple-darwin

# macOS x86_64
rustup target add x86_64-apple-darwin

# Tell rust to use our toolchain
cat <<EOF >>${HOME}/.cargo/config
[target.x86_64-apple-darwin]
linker = "x86_64-apple-darwin-clang"
ar = "x86_64-apple-darwin-ar"

                                                                                        [target.aarch64-apple-darwin]
linker = "aarch64-apple-darwin-clang"
ar = "aarch64-apple-darwin-ar"

[target.aarch64-apple-ios]
linker = "aarch64-apple-darwin-clang"
ar = "aarch64-apple-darwin-ar"

[build]
rustflags = ["-C", "link-args=-shared"]
EOF
Last Updated: 2/13/2023, 5:03:54 PM