How-To: Buck 2 and OCaml - Build system

When I run buck2 run //:bin in basic_example I get the following error

BUILD FAILED
From load at implicit location

Caused by:
    0: From load at prelude/prelude.bzl:8
    1: From load at prelude/native.bzl:30
    2: From load at prelude/rules.bzl:9
    3: Error evaluating module: `prelude//decls/common.bzl`
    4: error: String literals are not allowed in type expressions: `"PluginKind"`
         --> prelude/decls/common.bzl:23:27
          |
       23 |     uses_plugins = field([list["PluginKind"], None], None),
          |                           ^^^^^^^^^^^^^^^^^^
          |

Oh, yeah. I’ve forgot about this problem: The Buck 2 version and the version of the prelude must be compatible. You can’t use an old version of the prelude with a new version of Buck 2 and vice versa. So, updating the prelude to the newest version (if your Buck 2 executable is the newest version) should get rid of that.

I do not know if running buck2 git --init still clones the latest version of the prelude or the “correct one”, see Github Issue: Make it easier to align the binary and prelude versions

Got it. No just doing buck2 git --init doesn’t update the submodules I’ll try with an updated version

A git pull in each prelude subdirectory should update to the latest prelude, or a git pull --recurse-submodules (or git submodule update --recursive) in the project troot. GitHub - facebook/buck2-prelude: Prelude for the Buck2 project

I did a git submodule update --recursive before you suggested it, that doesn’t help either.

@Release-Candidate I pulled in buck2-prelude inside the basic example and I am getting the following error :

╰─ buck2 run //:bin
File changed: root//buck2-prelude
File changed: prelude//
Action failed: root//bin:basic_example (ocaml_link_native)
Local command returned non-zero exit code 2
Reproduce locally: `env -- 'BUCK_SCRATCH_PATH=buck-out/v2/tmp/root/5e32bd4ff23abcc6/ocaml_link_native' buck-out/v2/gen/r ...<omitted>... td -cclib -lpthread -o buck-out/v2/gen/root/904931f735703749/bin/__basic_example__/basic_example.opt (run `buck2 log what-failed` to get the full command)`
stdout:
stderr:
clang++: error: invalid linker name in argument '-fuse-ld=lld'
File "caml_startup", line 1:
Error: Error during linking (exit code 1)
Build ID: 21efb1e2-74e1-4ab1-8b17-34799d8d1e68
Jobs completed: 100. Time elapsed: 0.8s.
Cache hits: 0%. Commands: 10 (cached: 0, remote: 0, local: 10)
BUILD FAILED
Failed to build 'root//bin:basic_example (prelude//platforms:default#904931f735703749)'

LLVM’s linker, lld is missing. I guess this is an extra package, named lld or something like that (it is on Debian and Ubuntu).

1 Like

I thought lld came with clang, apparently not

@Release-Candidate thanks for continuous help, finally it succeeded!

I think I’ll write a script for all the things I did and try and automate it. Thanks a ton!

I’m glad to hear that!