FreeBSD Specific Notes

FreeBSD cross compiler depends on binutils same as Linux cross-compiler, but unlike Linux cross-compiler, FreeBSD cross-compiler does not depend on glibc. It depends on FreeBSD own libc instead, and FreeBSD libc cannot be built in cross-environment from Windows. So, you have to grab it from installed FreeBSD system.

Also FreeBSD cross compiler is based on clang rather than gcc, which is available for Windows in pre-built binary form.

So, building cross-compiler for FreeBSD is much easier than for Linux, because we can bypass Cygwin environment entirely. Actually we need to build binutils only.

Overview of Windows-to-FreeBSD cross-compiler preparation process:

  1. Install FreeBSD of your target version into the virtual machine
  2. Grab FreeBSD include files (/usr/include), FreeBSD libs (/lib + /libexec + /usr/lib) from FreeBSD virtual machine. Make sure to dereference symlinks when creating archive (tar -L -zcf ...)
  3. Download binutils-2.35 from ftp://ftp.gnu.org/. FreeBSD-native binutils-2.17.50 can't be built under MinGW, the following error message is generated on the configure stage:
    configure: creating ./config.status
    config.status: creating Makefile
    config.status: creating testsuite/Makefile
    config.status: error: cannot find input file: testsuite/Makefile.in
    make[1]: *** [configure-libiberty] Error 1
    make[1]: Leaving directory `/opt/crosstool/src/build-binutils'
    make: *** [all] Error 2
    
  4. Prepare MinGW virtual machine as for Linux cross-compiler, but please note that 32-bit FreeBSD target is called "i386-unknown-freebsd", 64-bit FreeBSD target is called "x86_64-unknown-freebsd". Unpack files grabbed from FreeBSD into /opt/crosstool/i386-unknown-freebsd or /opt/crosstool/x86_64-unknown-freebsd.
  5. Before building binutils, you need to add the following lines to C:\MinGW\include\errno.h:
    #define EOVERFLOW  ERANGE
    #define ENOTSUP    ENOSYS
    
  6. Binutils are built using the following script (replace "TARGET=i386-unknown-freebsd" with "TARGET=x86_64-unknown-freebsd" when done and run again):
    #!/bin/bash
    export PATH=.:/usr/local/bin:/mingw/bin:/bin
    export PATH=$PATH:/c/WINDOWS/system32:/c/WINDOWS
    export PATH=$PATH:/c/WINDOWS/System32/Wbem
    export TARGET=i386-unknown-freebsd
    export PREFIX=/opt/crosstool/$TARGET
    
    cd /opt/crosstool/src/build-binutils
    find . -delete
    LDFLAGS="-Wl,-s -Wl,-static -static -static-libgcc" \
      ../binutils-2.35/configure --target=$TARGET --prefix=$PREFIX \
        --with-sysroot=$PREFIX --disable-nls --disable-werror
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to configure binutils ==="
        exit 1
    fi
    make all MAKEINFO=true
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to make binutils ==="
        exit 1
    fi
    make install MAKEINFO=true
    if [ "$?" -ne "0" ]; then
        echo "=== build script: failed to install binutils ==="
        exit 1
    fi
    echo "=== build script: done ==="
    exit 0
    
  7. If the following error occurs during compilation:
    ../../binutils-2.35/libctf/ctf-create.c: In function 'ctf_add_function':
    ../../binutils-2.35/libctf/ctf-create.c:1076:32: error:
      'EOVERFLOW' undeclared (first use in this function)
         return (ctf_set_errno (fp, EOVERFLOW));
                                    ^
    ../../binutils-2.35/libctf/ctf-create.c:1076:32: note:
      each undeclared identifier is reported only once for each function it appears in
    make[3]: *** [ctf-create.lo] Error 1
    make[3]: Leaving directory `/opt/crosstool/src/build-binutils/libctf'
    make[2]: *** [all] Error 2
    make[2]: Leaving directory `/opt/crosstool/src/build-binutils/libctf'
    make[1]: *** [all-libctf] Error 2
    make[1]: Leaving directory `/opt/crosstool/src/build-binutils'
    make: *** [all] Error 2
    
    then "#define EOVERFLOW ERANGE" line wasn't added to the C:\MinGW\include\errno.h file (see above).
  8. If the following error occurs during compilation:
    ../../binutils-2.35/libctf/ctf-subr.c: In function 'ctf_version':
    ../../binutils-2.35/libctf/ctf-subr.c:147:12: error:
      'ENOTSUP' undeclared (first use in this function)
        errno = ENOTSUP;
                ^
    ../../binutils-2.35/libctf/ctf-subr.c:147:12: note:
      each undeclared identifier is reported only once for each function it appears in
    make[3]: *** [ctf-subr.lo] Error 1
    make[3]: Leaving directory `/opt/crosstool/src/build-binutils/libctf'
    make[2]: *** [all] Error 2
    make[2]: Leaving directory `/opt/crosstool/src/build-binutils/libctf'
    make[1]: *** [all-libctf] Error 2
    make[1]: Leaving directory `/opt/crosstool/src/build-binutils'
    make: *** [all] Error 2
    
    then "#define ENOTSUP ENOSYS" line wasn't added to the C:\MinGW\include\errno.h file (see above).
  9. When binutils are ready, download LLVM 6.0.1 and install it to "C:\Program Files\LLVM" in the MinGW virtual machine, then copy folders as shown below:
    "C:\Program Files\LLVM\bin"
      -> C:\MinGW\msys\1.0\opt\crosstool\i386-unknown-freebsd\i386-unknown-freebsd\bin
      -> C:\MinGW\msys\1.0\opt\crosstool\x86_64-unknown-freebsd\x86_64-unknown-freebsd\bin
    "C:\Program Files\LLVM\lib\clang"
      -> C:\MinGW\msys\1.0\opt\crosstool\i386-unknown-freebsd\i386-unknown-freebsd\lib\clang
      -> C:\MinGW\msys\1.0\opt\crosstool\x86_64-unknown-freebsd\x86_64-unknown-freebsd\lib\clang
    
  10. LLVM target is i386-unknown-freebsd12.0 or x86_64-unknown-freebsd12.0.


>> Look at screenshots or buy already prepared cross-compiler (€10) to save your time.