This shows how to build a OCaml project containing a library in ./lib/ using OCamlLex and Menhir, a binary in ./bin/ that uses the library and tests of the library in ./test/ using Buck 2.
All you need to do to be able to build this project, is to install a new Opam switch and the needed packages.
In this directory ocamllex_menhir_example
, run the following command to create the Opam switch and install the packages configured in the file ./third-party/dromedary.json:
Use the actual path to dromedary.py
instead of just dromedary.py
in the example.
python3 dromedary.py -o third-party/BUCK third-party/dromedary.json
Then, the Opam switch's environment must be activated:
opam switch ./third-party
eval $(opam env --switch=./third-party --set-switch)
Now all needed packages should be installed and set up, so you can build and run the executable:
buck2 run //:bin
and the tests:
buck2 run //:test
buck2 clean
- deletes all generated files (in the directory./buck-out/v2/
).buck2 targets //...
- lists all available targets, including all Opam packages. The packages have a prefix ofroot//third-party
.buck2 build //...
- builds all targets.buck2 run //:bin
- run the generated executable. This is an alias forbuck2 run //bin:ocamllex_menhir_example
.buck2 run //:test
- run the generated executable. This is an alias forbuck2 run //test:ocamllex_menhir_example
.
List only the targets of ocamllex_menhir_example
, without the Opam packages:
> buck2 targets //lib: //bin: //test: //:
File changed: root//README.md
Build ID: 58742ba4-60db-418a-9d02-1439c7e4112f
Jobs completed: 2. Time elapsed: 0.0s.
root//:bin
root//:lib
root//:test
root//bin:ocamllex_menhir_example
root//lib:ocamllex_menhir_example
root//lib:ocamllex_menhir_example.mli
root//lib:ocamllex_menhir_example__
root//test:ocamllex_menhir_example
Run the generated executable:
> buck2 run //:bin
File changed: root//README.md
Build ID: 4ff6fa61-4b10-4744-8d88-583a7894fd90
Jobs completed: 3. Time elapsed: 0.0s.
BUILD SUCCEEDED
Some examples:
1 + 2 * 5 = 11
1.486 - 2.5 / 0.123 = -18.839203252
let a = 5 in a / 3 = 1
let a = 4.0 - 5.2 in let b = a * 0.123 in let c = 5.6 in a + b + c = 4.2524
Run the tests:
> buck2 run //:test
File changed: root//README.md
Build ID: 3d781d47-672c-4f79-9810-13042b8641d2
Jobs completed: 3. Time elapsed: 0.0s.
BUILD SUCCEEDED
qcheck random seed: 896189154
Testing `Interpreter Tests'.
This run has ID `WNG915K9'.
[OK] Fixed terms and results 0 1 - 1 = 0.
[OK] Fixed terms and results 1 2.0 * 1.75 - 4.5 = -1.0.
[OK] Fixed terms and results 2 let a = 5.21 in a - 0.21 = 5.0.
[OK] Generated Terms 0 n - m.
[OK] Generated Terms 1 n + m.
[OK] Generated Terms 2 n * m.
[OK] Generated Terms 3 n / m.
Full test results in `./OCaml-Buck-2-Examples/ocamllex_menhir_example/_build/_tests/Interpreter Tests'.
Test Successful in 0.002s. 7 tests run.
-
./.buckroot - this marks the directory as the root of the Buck 2 directories. In real examples you might want to put all your projects in subdirectories of the Buck 2 root directory. This is an empty file.
-
./.buckconfig - Contains the configuration generated by
buck2 init --git
. Set the execution platform to the default one of the prelude.[build] execution_platforms = prelude//platforms:default
-
./toolchains/BUCK - The configuration of the toolchains to use. You always need C++ (
system_cxx_toolchain
) and Python (system_python_bootstrap_toolchain
). The OCaml tool chain issystem_ocaml_toolchain
. -
./third-party/ - This directory contains all third party dependencies and their configuration, like Opam packages. In a bigger Buck 2 configuration you want to use one subdirectory for each language or for each project, e.g.
third-party/ocaml
for all OCaml project dependencies orthird-party/ocamllex_menhir_example
for the dependencies ofocamllex_menhir_example
, if for example Opam package versions differ from project to project. -
./third-party/dromedary.json - This is the configuration of the Opam switch to create and the packages to install.
-
./third-party/BUCK - The configuration of the Opam packages. Generated by the
ocaml-script
dromedary.py
, and should not be edited manually. -
./BUCK - The main Buck 2 configuration file. Contains shorter aliases for the
bin
andtest
targets. -
./bin/BUCK - The rules to build the binary.
-
./lib/BUCK - The rules to build the library.
-
./test/BUCK - The rules to build the test.
-
./prelude/ - Contains all toolchains and rules for all supported languages of Buck 2. A Git submodule.
-
./.merlin - Merlin configuration file for Merlin and the OCaml-LSP