Recently, I’ve decided to switch from Homebrew to Macports on my MacBook (M2), for no particular reasons other than to see how FreeBSD-inspired system works. Overall it’s nice and fast.
But Homebrew is the mainstream option, and as in programming in general, when you go outside the path, one should be prepared to see weird things. I’m also trying out the non-mainstream languages, and wanted to build something in Odin. Weirdness from going non-mainstream had just been squared.
For some reason the Odin compiler from ports could not find vendor libraries:
$ odin run .
ld: library 'raylib' not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Rather than investigating why and submitting fixes to the Macports as one should, I’ve decided to build Odin from git repo. That would also get me faster updates and allow to hopefully/hypothetically look at the source code of the compiler in the future.
However, basic instructions on installing Odin from source posted on the website don’t quite work, if you’re on non-Homebrew system. Here is the flow that worked for me.
Installing llvm-18
sudo port install llvm-18
To add proper symlinks, select llvm-18 as the default llvm (in my case it was printed as mp-llvm-18
):
sudo port select llvm
After that plain llvm-config
should exist and work.
Fixing the build
At this point make
would still fail:
$ make
./build_odin.sh debug
+ clang++ src/main.cpp src/libtommath.cpp -Wno-switch -Wno-macro-redefined -Wno-unused-value '-DODIN_VERSION_RAW="dev-2024-07"' '-DGIT_SHA="3a75a8dd1"' -std=c++14 -I/opt/local/libexec/llvm-18/include -std=c++17 -stdlib=libc++ -fno-exceptions -funwind-tables -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -L/opt/local/libexec/llvm-18/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -g -pthread -lm -lstdc++ -liconv -ldl -framework System -lLLVM -o odin
ld: warning: ignoring duplicate libraries: '-lc++'
+ set +x
dyld[37203]: Library not loaded: @rpath/libLLVM.dylib
Referenced from: <F651AB76-7D8A-309C-857B-EC851EECDD06> /Users/mt/src/Odin/odin
Reason: no LC_RPATH's found
./build_odin.sh: line 146: 37203 Abort trap: 6 ./odin run examples/demo -vet -strict-style -- Hellope World
make: *** [debug] Error 134
I’m not quite sure why that happens. Searching the interwebz, I found a small patch to the build file:
diff --git a/build_odin.sh b/build_odin.sh
index 125b9335a..208ed4567 100755
--- a/build_odin.sh
+++ b/build_odin.sh
@@ -79,7 +79,7 @@ Darwin)
echo "Warning: MacOSX.sdk not found."
fi
- CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags) ${darwin_sysroot}"
+ CXXFLAGS="$CXXFLAGS -I/opt/local/include -L/opt/local/lib -rpath /opt/local/libexec/llvm-18/lib $($LLVM_CONFIG --cxxflags --ldflags) ${darwin_sysroot}"
LDFLAGS="$LDFLAGS -liconv -ldl -framework System -lLLVM"
;;
FreeBSD)
Namely, add the -I/opt/local/include -L/opt/local/lib -rpath /opt/local/libexec/llvm-18/lib
to CXXFLAGS. At this moment I don’t know what rpath
is (seems like “runtime path”?), but I’ve postponed this learning moment for the future me.
I think that build script with this patch would not work on Homebrew machines, so I’m also not submitting it into the Odin repo back. Also, the patch is literally a hack with hardcoding paths to specific llvm version, surely there should be a better way.
After the patch, make
builds without any problems and we get an odin compiler built on M2 Mac that knows how to find the vendored raylib
.