Skip to content

Commit

Permalink
feat!: Remove TOOLCHAIN logic and replace with CMake presets (#162)
Browse files Browse the repository at this point in the history
* feat: Add CMake presets to project

* fix!: Remove toolchain file and make bootstrap pick correct preset

* fix!: Rework toolchain files to be proper cross-compile files

* chore: Update dockerfiles to use presets

* chore: Rework compare_honk_bench to work with presets (need to double check)

* chore: Update docs to use presets throughout

* chore: Remove docs about parallel command since presets enable maximum jobs

* chore: Rework toolchain docs to call out cross-compiling

* Ignore CMakeUserPresets.json as per adrian

* Add CMake Tools VS Code plugin config.

* Set ninja as the default generator

* Export compile commands used for clangd
- add clangd setting to ignore GCC-specific flags

* add ninja to docker images

---------

Co-authored-by: codygunton <codygunton@gmail.com>
Co-authored-by: adr1anh <adrian.hamelink@gmail.com>
  • Loading branch information
3 people authored Mar 2, 2023
1 parent 7ae381e commit 09db0be
Show file tree
Hide file tree
Showing 32 changed files with 333 additions and 188 deletions.
65 changes: 35 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ cd cpp
./bootstrap.sh
```

### Parallelise the build

Use the `--parallel` option to `cmake --build <path>` to parallelize builds. This is roughly equivalent to `make -j$(nproc)` but is more portable.

### Formatting

Code is formatted using `clang-format` and the `./cpp/format.sh` script which is called via a git pre-commit hook.
Expand All @@ -43,21 +39,24 @@ If you've installed the C++ Vscode extension you should configure it to format o

Each module has its own tests. e.g. To build and run `ecc` tests:

```
cmake --build . --parallel --target ecc_tests
```bash
# Replace the `default` preset with whichever preset you want to use
cmake --build --preset default --target ecc_tests
cd build
./bin/ecc_tests
```

A shorthand for the above is:

```
cmake --build . --parallel --target run_ecc_tests
```bash
# Replace the `default` preset with whichever preset you want to use
cmake --build --preset default --target run_ecc_tests
```

Running the entire suite of tests using `ctest`:

```
cmake --build . --parallel --target test
```bash
cmake --build --preset default --target test
```

You can run specific tests, e.g.
Expand All @@ -70,15 +69,18 @@ You can run specific tests, e.g.

Some modules have benchmarks. The build targets are named `<module_name>_bench`. To build and run, for example `ecc` benchmarks.

```
cmake --build . --parallel --target ecc_bench
./src/aztec/ecc/ecc_bench
```bash
# Replace the `default` preset with whichever preset you want to use
cmake --build --preset default --target ecc_bench
cd build
./bin/ecc_bench
```

A shorthand for the above is:

```
cmake --build . --parallel --target run_ecc_bench
```bash
# Replace the `default` preset with whichever preset you want to use
cmake --build --preset default --target run_ecc_bench
```

### CMake Build Options
Expand All @@ -91,17 +93,19 @@ CMake can be passed various build options on its command line:
- `-DMULTITHREADING=ON | OFF`: Enable/disable multithreading using OpenMP.
- `-DTESTING=ON | OFF`: Enable/disable building of tests.
- `-DBENCHMARK=ON | OFF`: Enable/disable building of benchmarks.
- `-DTOOLCHAIN=<filename in ./cmake/toolchains>`: Use one of the preconfigured toolchains.
- `-DFUZZING=ON | OFF`: Enable building various fuzzers.

If you are cross-compiling, you can use a preconfigured toolchain file:

- `-DCMAKE_TOOLCHAIN_FILE=<filename in ./cmake/toolchains>`: Use one of the preconfigured toolchains.

### WASM build

To build:

```
mkdir build-wasm && cd build-wasm
cmake -DTOOLCHAIN=wasm-linux-clang ..
cmake --build . --parallel --target barretenberg.wasm
```bash
cmake --preset wasm
cmake --build --preset wasm --target barretenberg.wasm
```

The resulting wasm binary will be at `./build-wasm/bin/barretenberg.wasm`.
Expand All @@ -114,8 +118,8 @@ curl https://wasmtime.dev/install.sh -sSf | bash

Tests can be built and run like:

```
cmake --build . --parallel --target ecc_tests
```bash
cmake --build --preset wasm --target ecc_tests
wasmtime --dir=.. ./bin/ecc_tests
```

Expand All @@ -124,11 +128,12 @@ wasmtime --dir=.. ./bin/ecc_tests
For detailed instructions look in cpp/docs/Fuzzing.md

To build:

```bash
cmake --preset fuzzing
cmake --build --preset fuzzing
```
mkdir build-fuzzing && cd build-fuzzing
cmake -DTOOLCHAIN=x86_64-linux-clang -DFUZZING=ON ..
cmake --build . --parallel
```

Fuzzing build turns off building tests and benchmarks, since they are incompatible with libfuzzer interface.

To turn on address sanitizer add `-DADDRESS_SANITIZER=ON`. Note that address sanitizer can be used to explore crashes.
Expand All @@ -139,10 +144,10 @@ Note that the fuzzer can be orders of magnitude slower with ASan (2-3x slower) o
### Test coverage build

To build:
```
mkdir build-coverage && cd build-coverage
cmake -DTOOLCHAIN=x86_64-linux-clang -DCOVERAGE=ON -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --parallel

```bash
cmake --preset coverage
cmake --build --preset coverage
```

Then run tests (on the mainframe always use taskset and nice to limit your influence on the server. Profiling instrumentation is very heavy):
Expand Down
7 changes: 6 additions & 1 deletion barretenberg.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@
],
"[cpp]": {
"editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
}
},
"cmake.configureArgs": [
"--preset clang15",
"-G Ninja",
],
"cmake.useCMakePresets": "auto",
},
}
2 changes: 2 additions & 0 deletions cpp/.clangd
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
CompileFlags: # Tweak the parse settings
Remove: -fconstexpr-ops-limit=*
---
# Applies all barretenberg source files
If:
Expand Down
3 changes: 2 additions & 1 deletion cpp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ build*/
src/wasi-sdk-*
src/aztec/proof_system/proving_key/fixtures
src/aztec/rollup/proofs/*/fixtures
srs_db/*/*/transcript*
srs_db/*/*/transcript*
CMakeUserPresets.json
9 changes: 5 additions & 4 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

cmake_minimum_required(VERSION 3.24)

include(cmake/toolchain.cmake)

set(PROJECT_VERSION 0.1.0)
project(Barretenberg
DESCRIPTION "BN254 elliptic curve library, and PLONK SNARK prover"
Expand All @@ -20,8 +18,9 @@ option(DISABLE_TBB "Intel Thread Building Blocks" ON)
option(COVERAGE "Enable collecting coverage from tests" OFF)
option(ENABLE_HEAVY_TESTS "Enable heavy tests when collecting coverage" OFF)

if(ARM)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
message(STATUS "Compiling for ARM.")
set(ARM ON)
set(DISABLE_ASM ON)
set(DISABLE_ADX ON)
set(RUN_HAVE_STD_REGEX 0)
Expand Down Expand Up @@ -54,12 +53,14 @@ if(FUZZING)
set(TESTING OFF)
endif()

if(WASM)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "wasm32")
message(STATUS "Compiling for WebAssembly.")
set(WASM ON)
set(DISABLE_ASM ON)
set(MULTITHREADING OFF)
set(BENCHMARKS OFF)
set(DISABLE_TBB 1)
add_compile_definitions(_WASI_EMULATED_PROCESS_CLOCKS=1)
endif()

set(CMAKE_C_STANDARD 11)
Expand Down
Loading

0 comments on commit 09db0be

Please sign in to comment.