Skip to content
Open
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
77 changes: 45 additions & 32 deletions cpp/cmake/barretenberg.cmake
Original file line number Diff line number Diff line change
@@ -1,53 +1,66 @@
# Here we Set up barretenberg as an ExternalProject
# - Point to its source and build directories
# - Construct its `configure` and `build` command lines
# - include its `src/` in `search path for includes
# - Depend on specific libraries from barretenberg
#
# If barretenberg's cmake files change, its configure and build are triggered
# If barretenberg's source files change, build is triggered

include(ExternalProject)

# Reference barretenberg artifacts (like library archives) via this dir:
# 🛡️ PATH SAFETY: Ensure BBERG_DIR is an absolute path to avoid relative path hell
# inside ExternalProject execution.
get_filename_component(BBERG_ABS_DIR ${BBERG_DIR} ABSOLUTE)

# 🏗️ CONFIGURATION: Determine build directories and targets based on architecture.
if (WASM)
set(BBERG_BUILD_DIR ${BBERG_DIR}/build-wasm)
set(BBERG_BUILD_DIR ${BBERG_ABS_DIR}/build-wasm)
set(BBERG_TARGETS --target barretenberg --target env --target primitives.wasm)
else()
set(BBERG_BUILD_DIR ${BBERG_DIR}/build)
set(BBERG_BUILD_DIR ${BBERG_ABS_DIR}/build)
set(BBERG_TARGETS --target barretenberg --target env)
endif()

# Set default preset if not provided
if(NOT CMAKE_BBERG_PRESET)
set(CMAKE_BBERG_PRESET default)
endif()

# Naming: Project: Barretenberg, Libraries: barretenberg, env
# Need BUILD_ALWAYS to ensure that barretenberg is automatically reconfigured when its CMake files change
# "Enabling this option forces the build step to always be run. This can be the easiest way to robustly
# ensure that the external project's own build dependencies are evaluated rather than relying on the
# default success timestamp-based method." - https://cmake.org/cmake/help/latest/module/ExternalProject.html
# 📦 EXTERNAL PROJECT DEFINITION
# We use BUILD_IN_SOURCE because we are relying on 'cmake --preset' to handle
# the build directory generation internally within barretenberg's structure.
ExternalProject_Add(Barretenberg
SOURCE_DIR ${BBERG_DIR}
SOURCE_DIR ${BBERG_ABS_DIR}
BUILD_IN_SOURCE TRUE
BUILD_ALWAYS TRUE
UPDATE_COMMAND ""
INSTALL_COMMAND ""
CONFIGURE_COMMAND ${CMAKE_COMMAND} --preset ${CMAKE_BBERG_PRESET} -DSERIALIZE_CANARY=${SERIALIZE_CANARY} -DENABLE_ASAN=${ENABLE_ASAN} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DUSE_TURBO=${USE_TURBO}
BUILD_ALWAYS TRUE # ⚠️ Performance Note: This checks the sub-project on every build.
UPDATE_COMMAND "" # Prevent git fetch/pull on every build
INSTALL_COMMAND "" # We consume from build dir, no install needed

# Pass necessary flags to the sub-project
CONFIGURE_COMMAND ${CMAKE_COMMAND} --preset ${CMAKE_BBERG_PRESET}
-DSERIALIZE_CANARY=${SERIALIZE_CANARY}
-DENABLE_ASAN=${ENABLE_ASAN}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DUSE_TURBO=${USE_TURBO}

BUILD_COMMAND ${CMAKE_COMMAND} --build --preset ${CMAKE_BBERG_PRESET} ${BBERG_TARGETS}
# byproducts needed by ninja generator (not needed by make)
BUILD_BYPRODUCTS ${BBERG_BUILD_DIR}/lib/libbarretenberg.a ${BBERG_BUILD_DIR}/lib/libenv.a)

# Ninja generator requirement: explicit byproducts
BUILD_BYPRODUCTS
${BBERG_BUILD_DIR}/lib/libbarretenberg.a
${BBERG_BUILD_DIR}/lib/libenv.a
)

include_directories(${BBERG_DIR}/src)
# 🚀 MODERN CMAKE: IMPORTED TARGETS
# Instead of global include_directories(), we attach the include path
# directly to the library target.

# Add the imported barretenberg and env libraries, point to their library archives,
# and add a dependency of these libraries on the imported project
# --- Library: barretenberg ---
add_library(barretenberg STATIC IMPORTED)
set_target_properties(barretenberg PROPERTIES IMPORTED_LOCATION ${BBERG_BUILD_DIR}/lib/libbarretenberg.a)
set_target_properties(barretenberg PROPERTIES
IMPORTED_LOCATION ${BBERG_BUILD_DIR}/lib/libbarretenberg.a
# ✨ MAGIC: Only targets linking to 'barretenberg' will see these headers.
INTERFACE_INCLUDE_DIRECTORIES ${BBERG_ABS_DIR}/src
)
add_dependencies(barretenberg Barretenberg)

# env is needed for logstr in native executables and wasm tests
# It is otherwise omitted from wasm to prevent use of C++ logstr instead of imported/Typescript
# --- Library: env ---
add_library(env STATIC IMPORTED)
set_target_properties(env PROPERTIES IMPORTED_LOCATION ${BBERG_BUILD_DIR}/lib/libenv.a)
add_dependencies(env Barretenberg)
set_target_properties(env PROPERTIES
IMPORTED_LOCATION ${BBERG_BUILD_DIR}/lib/libenv.a
# Env likely needs the same headers available
INTERFACE_INCLUDE_DIRECTORIES ${BBERG_ABS_DIR}/src
)
add_dependencies(env Barretenberg)