Skip to content

Building Codex on FreeBSD #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,51 @@ File: `C:/Users/<username>/AppData/Roaming/Code/User/settings.json`

It is possible that nim-codex can be built and run on other platforms supported by the [Nim](https://nim-lang.org/) language: BSD family, older versions of Windows, etc. There has not been sufficient experimentation with nim-codex on such platforms, so instructions are not provided. Community contributions to these docs and our build system are welcome!


#### FreeBSD

1. Ensure that the following system packages are installed:

```shell
pkg install bash git gmake gcc cmake rust
```

2. Run the normal build

```shell
gmake update
gmake
```

3. The build will fail in compiling the wasmer-vm-2.3.0 crate with messages looking something like:

```
/home/evenson/work/nim-codex/vendor/nim-circom-compat/circomcompat.nim(20, 9) Warning: rust error> error: Unsupported platform [User]
/home/evenson/work/nim-codex/vendor/nim-circom-compat/circomcompat.nim(20, 9) Warning: rust error> --> /home/evenson/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmer-vm-2.3.0/src/trap/traphandlers.rs:305:21 [User]
/home/evenson/work/nim-codex/vendor/nim-circom-compat/circomcompat.nim(20, 9) Warning: rust error> | [User]
/home/evenson/work/nim-codex/vendor/nim-circom-compat/circomcompat.nim(20, 9) Warning: rust error> 305 | compile_error!("Unsupported platform"); [User]
/home/evenson/work/nim-codex/vendor/nim-circom-compat/circomcompat.nim(20, 9) Warning: rust error> | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [User]
/home/evenson/work/nim-codex/vendor/nim-circom-compat/circomcompat.nim(20, 9) Warning: rust error> [User]
/home/evenson/work/nim-codex/vendor/nim-circom-compat/circomcompat.nim(20, 9) Warning: rust error> error: could not compile `wasmer-vm` (lib) due to 1 previous error [User]
/home/evenson/work/nim-codex/vendor/nim-circom-compat/circomcompat.nim(20, 9) Warning: rust error> warning: build failed, waiting for other jobs to finish... [User]
```

After the build fails, patch the Rust target under <file:.cargo/> with

```shell
patch -p1 < $NIM_CODEX_ROOT/docs/notes/wasmer-vm-2.3.0-freebsd-amd64.patch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an already patched version of wasmer that builds on FreeBSD, I would very much like to avoid the manual patching. Other than that, this is great!

```

where NIM_CODEX_ROOT is set to the local location of the nim-codex repository.

4. Restart the build with

```shell
gmake
```

Voila! A Codex binary for FreeBSD!

## Repository

In Bash run
Expand Down
11 changes: 11 additions & 0 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ when defined(windows):
# because these require direct manipulations of the stdout File object.
switch("define", "chronicles_colors=off")

# Fix problems linking <file:vendor/nim-circom-compat/> under FreeBSD
#
# Symptom: codex executable fails to start due to undefined symbol `kinfo_getvmmap`
#
# Under FreeBSD the `kinfo_*` symbols are found in /usr/lib/libutil.a,
# so this is presumably a problem with the circom-compat-ffi Rust
# crate, and fixing things there would be a preferred solution, but
# adding this change allows codex to run without updating Rust dependencies.
when defined(freebsd):
switch("passL", "-lutil")

# This helps especially for 32-bit x86, which sans SSE2 and newer instructions
# requires quite roundabout code generation for cryptography, and other 64-bit
# and larger arithmetic use cases, along with register starvation issues. When
Expand Down
12 changes: 12 additions & 0 deletions docs/notes/wasmer-vm-2.3.0-freebsd-amd64.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--- a/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmer-vm-2.3.0/src/trap/traphandlers.rs.orig 1973-11-29 21:33:09.000000000 +0000
+++ b/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmer-vm-2.3.0/src/trap/traphandlers.rs 2024-09-24 06:35:50.966457000 +0000
@@ -274,6 +274,9 @@
} else if #[cfg(all(target_os = "freebsd", target_arch = "x86"))] {
pc = context.uc_mcontext.mc_rip as usize;
sp = context.uc_mcontext.mc_rsp as usize;
+ } else if #[cfg(all(target_os = "freebsd", target_arch = "x86_64"))] {
+ pc = context.uc_mcontext.mc_rip as usize;
+ sp = context.uc_mcontext.mc_rsp as usize;
} else if #[cfg(all(target_vendor = "apple", target_arch = "x86_64"))] {
pc = (*context.uc_mcontext).__ss.__rip as usize;
sp = (*context.uc_mcontext).__ss.__rsp as usize;