Skip to content

Instantly share code, notes, and snippets.

@LowByteFox
Last active November 19, 2023 11:06
Show Gist options
  • Save LowByteFox/a4e86c64aa3eaae6f79efb293faefe74 to your computer and use it in GitHub Desktop.
Save LowByteFox/a4e86c64aa3eaae6f79efb293faefe74 to your computer and use it in GitHub Desktop.
Compiling bun on FreeBSD
  1. Install FreeBSD 13.2 in a VM and create dummy account, call it bun for example
  2. Install these packages as root using pkg install

ccache cmake git go libiconv libtool gmake ninja pkgconf python rust unzip ruby automake bash llvm16 llvm17 gcc13-dev npm icu libsysinfo

  1. Change bun's shell to /usr/local/bin/bash using chsh ( I recommend installing neovim and running the command as EDITOR=nvim chsh )

now always login as bun user

  1. Clone these repos - there is space between urls, git clone each one separately

https://github.com/Bun-BSD/codegen.git https://github.com/oven-sh/WebKit.git https://github.com/ziglang/zig.git https://github.com/Bun-BSD/bun.git Now we will compile all deps, make sure you are in home directory each time

  1. Building WebKit
    1. In your home directory create webkit.sh and write this into it
    cmake . -B WebKitBuild/Debug \
    -DPORT=JSCOnly \
    -DENABLE_STATIC_JSC=ON \
    -DENABLE_SINGLE_THREADED_VM_ENTRY_SCOPE=ON \
    -DCMAKE_BUILD_TYPE=Debug \
    -DENABLE_BUN_SKIP_FAILING_ASSERTIONS=ON \
    -DUSE_THIN_ARCHIVES=OFF \
    -DENABLE_FTL_JIT=ON \
    -DCMAKE_C_COMPILER="$(which clang16)" \
    -DCMAKE_CXX_COMPILER="$(which clang++16)" \
    -DUSE_BUN_JSC_ADDITIONS=ON \
    -DCMAKE_C_FLAGS="-I /usr/local/include/ -L /usr/local/lib/ -lsysinfo" \
    -DCMAKE_CXX_FLAGS="-I /usr/local/include/ -L /usr/local/lib/ -lsysinfo" \
    -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld" \
    -DCMAKE_AR="$(which llvm-ar)" \
    -DCMAKE_RANLIB="$(which llvm-ranlib)" \
    -DALLOW_LINE_AND_COLUMN_NUMBER_IN_BUILTINS=ON \
    -G Ninja \
    -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0 \
    -DPTHREAD_JIT_PERMISSIONS_API=1 \
     -DUSE_PTHREAD_JIT_PERMISSIONS_API=ON \
    -DENABLE_REMOTE_INSPECTOR=ON
    1. In your home directory create webkitbuninstall.sh and write this into it
    mkdir -p bun
    mkdir -p bun/include
    mkdir -p bun/include/JavaScriptCore
    cp cmakeconfig.h bun/include/
    
    find JavaScriptCore/Headers/JavaScriptCore/ -name "*.h" -exec cp {} bun/include/JavaScriptCore/ \;
    find JavaScriptCore/PrivateHeaders/JavaScriptCore/ -name "*.h" -exec cp {} bun/include/JavaScriptCore/ \;
    cp -r bmalloc/Headers/bmalloc bun/include/
    cp -r WTF/Headers/wtf bun/include/wtf/
    
    mkdir -p bun/Source/JavaScriptCore/
    cp -r ../../Source/JavaScriptCore/Scripts bun/Source/JavaScriptCore
    cp ../../Source/JavaScriptCore/create_hash_table bun/Source/JavaScriptCore 
    
    echo "Copying fat libJavaScriptCore.a"
    mv lib bun/
    for i in libicui18n.a libicutest.a libicuuc.a libicudata.a libicuio.a libicutu.a; do
        cp "/usr/local/lib/${i}" bun/lib/
    done
    
    rm -f bun/lib/*.so

    Both are shell scripts, make them executable with chmod +x

    1. Change directory into WebKit and run ~/webkit.sh
    2. Change directory into WebKitBuild/Debug and run ninja jsc

    This way you'll build webkit for bun

    1. While being in the directory run ~/webkitbuninstall.sh
    2. Once the script has done executing run cp -r bun ~/bun-webkit
    3. Now you have built WebKit used by bun, feel free to remove WebKit
  2. Building Zig
    1. Change directory into zig and create directory build
    2. Change directory into build and run

    cmake .. -DZIG_STATIC_LLVM=ON -DCMAKE_PREFIX_PATH="/usr/local/llvm17;/usr/local"

    1. Compile zig using make
    2. Once zig is compiled, run make install and zig should be installed inside stage3 directory
    3. Run command echo $PATH if it contains something like /home/bun/bin, you are on good track

    If it does not contain the directory, run this command echo 'export PATH="$PATH:$HOME/bin"' >> "~/.bashrc" and then run . ~/.bashrc

    1. Create directory ~/bin and copy bin and lib from stage3 into ~/bin/
    2. Change directory into ~/bin/ and run ln -s bin/zig zig
    3. Now by running zig version you should see something like 0.12.0-dev.1646+4e212f165

    This is not exact version bun needs bun it can compile with it Now if you want to save space, get rid of zig source code

  3. Building Bun
    1. Change directory into bun and run git checkout Bun-BSD/freebsd-port
    2. Execute ./scripts/setup.sh
    3. Execute gmake node-fallbacks && gmake all-js
    4. Execute pushd ~/codegen/ && tar xf codegen.tar && popd
    5. Configure cmake with this command

    cmake -S . -G Ninja -B build -DCMAKE_BUILD_TYPE=Debug -DNO_CODEGEN=1 -DUSE_DEBUG_JSC=1 -DCMAKE_CXX_COMPILER="clang++16" -DCMAKE_C_COMPILER="clang16" -DCMAKE_EXE_LINKER_FLAGS="-L /usr/local/lib/gcc13/ -L /usr/local/lib/ -lsysinfo -Wl,-undefined,error" -DCMAKE_CXX_FLAGS="-I /usr/local/include/"

    1. Execute cp -r ~/codegen/build-codegen-freebsd-x64/* build/
    2. Execute ninja -C build and when it starts to compile zig code, interrupt it with ctrl + c
    3. Execute rm -rf build/bun-webkit/ && cp -r ~/bun-webkit/ build/bun-webkit

    Now you should have our custom built webkit

    1. Now just run ninja -C build and everything should compile fine

Now you should have bun binary in build/bun-debug If I missed something or made mistake, tell me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment