Skip to content

Commit 80e1fa6

Browse files
committed
fix(bb): split WASM target — barretenberg static lib + barretenberg_wasm_bin exec
`bb` (built by the wasm-threads preset) links against the `barretenberg` target. Previously under WASM, `barretenberg` was an `add_executable` and the link line emits `-lbarretenberg`, which wasm-ld fails to resolve. Split the target: `barretenberg` is always a static library (so `bb` and other consumers can link it), and the WASM bundle is produced by a new `barretenberg_wasm_bin` executable target with `OUTPUT_NAME barretenberg` (so artifacts on disk remain `barretenberg.js` / `barretenberg.wasm`). Also pin bb / bb-avm WASM link options: -sNODERAWFS=1, -sPROXY_TO_PTHREAD=0, -sALLOW_BLOCKING_ON_MAIN_THREAD=1 — bb has main() and reads CRS from disk; the toolchain default proxies main onto a worker which silently zeros file ops under NODERAWFS (Emscripten #19330).
1 parent fd5757a commit 80e1fa6

2 files changed

Lines changed: 54 additions & 22 deletions

File tree

barretenberg/cpp/src/CMakeLists.txt

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,18 @@ if(NOT WASM AND NOT FUZZING AND NOT BB_LITE)
220220
list(APPEND BARRETENBERG_TARGET_OBJECTS $<TARGET_OBJECTS:world_state_objects>)
221221
endif()
222222

223-
if(NOT WASM)
224-
add_library(
225-
barretenberg
226-
STATIC
227-
${BARRETENBERG_TARGET_OBJECTS}
228-
)
223+
# `barretenberg` is the canonical static library of all core proving objects.
224+
# Native consumers (bb, tests, benches, FFI) link it directly. Under WASM the
225+
# bbapi bundle is a separate executable target (`barretenberg_wasm_bin`) that
226+
# also links it; we cannot collapse the two because under WASM `bb` itself is
227+
# also built as a wasm executable that needs the static archive.
228+
add_library(
229+
barretenberg
230+
STATIC
231+
${BARRETENBERG_TARGET_OBJECTS}
232+
)
229233

234+
if(NOT WASM)
230235
add_library(
231236
bb-external
232237
STATIC
@@ -238,24 +243,27 @@ endif()
238243

239244
if(WASM)
240245
# Under Emscripten the executable suffix is `.js` (set by the toolchain),
241-
# and Emscripten emits a sibling `.wasm` next to it. We therefore name the
242-
# CMake target `barretenberg` -- the artifacts on disk are
246+
# and Emscripten emits a sibling `.wasm` next to it. The CMake target name
247+
# is `barretenberg_wasm_bin` (to avoid clashing with the static library
248+
# `barretenberg`); OUTPUT_NAME pins the on-disk artifact to
243249
# `bin/barretenberg.js` + `bin/barretenberg.wasm`.
250+
# The same OBJECT-library object files are also packaged into the static
251+
# `barretenberg` lib that `bb` links against. We pass them as sources
252+
# directly (rather than linking the static lib) because -sEXPORT_ALL=1
253+
# only exports symbols that exist in the link, and the static-archive
254+
# selection step would drop everything not directly referenced.
244255
add_executable(
245-
barretenberg
256+
barretenberg_wasm_bin
246257
${BARRETENBERG_TARGET_OBJECTS}
247-
# env provides logstr / throw_or_abort_impl. Outside WASM these are
248-
# supplied by the consumer linking libbarretenberg + env; the WASM
249-
# exec is the artifact we ship, so it must bundle them. Without
250-
# this, -sEXPORT_ALL=1 hard-errors at link time on the unresolved
251-
# WASM_IMPORT-decorated declarations.
258+
# env provides logstr / throw_or_abort_impl; the WASM bundle is the
259+
# artifact we ship, so it must include them directly.
252260
$<TARGET_OBJECTS:env_objects>
253-
# This is an object library, so doesn't need _objects.
254261
$<TARGET_OBJECTS:vm2_stub>
255262
)
263+
set_target_properties(barretenberg_wasm_bin PROPERTIES OUTPUT_NAME barretenberg)
256264

257265
target_link_options(
258-
barretenberg
266+
barretenberg_wasm_bin
259267
PRIVATE
260268
# `-sEXPORT_ALL=1` keeps the WASM_EXPORT symbols on the Module object.
261269
# The toolchain already sets MODULARIZE / EXPORT_ES6 / EXPORT_NAME /
@@ -278,14 +286,14 @@ if(WASM)
278286
# invoke `cmake --build --target barretenberg.wasm`; that resolves to the
279287
# main executable target which produces `barretenberg.wasm` as a sibling
280288
# of `barretenberg.js`.
281-
add_custom_target(barretenberg.wasm ALL DEPENDS barretenberg)
282-
add_custom_target(barretenberg-debug.wasm ALL DEPENDS barretenberg)
289+
add_custom_target(barretenberg.wasm ALL DEPENDS barretenberg_wasm_bin)
290+
add_custom_target(barretenberg-debug.wasm ALL DEPENDS barretenberg_wasm_bin)
283291

284292
# Gzipped artifacts for the bb.js packaging step.
285293
add_custom_command(
286294
OUTPUT ${CMAKE_BINARY_DIR}/bin/barretenberg.wasm.gz
287295
COMMAND gzip -kf ${CMAKE_BINARY_DIR}/bin/barretenberg.wasm
288-
DEPENDS barretenberg
296+
DEPENDS barretenberg_wasm_bin
289297
COMMENT "Creating gzipped version of barretenberg.wasm"
290298
)
291299

@@ -297,7 +305,7 @@ if(WASM)
297305
add_custom_command(
298306
OUTPUT ${CMAKE_BINARY_DIR}/bin/barretenberg-debug.wasm.gz
299307
COMMAND gzip -kf -c ${CMAKE_BINARY_DIR}/bin/barretenberg.wasm > ${CMAKE_BINARY_DIR}/bin/barretenberg-debug.wasm.gz
300-
DEPENDS barretenberg
308+
DEPENDS barretenberg_wasm_bin
301309
COMMENT "Creating gzipped debug copy of barretenberg.wasm"
302310
)
303311

@@ -308,12 +316,12 @@ if(WASM)
308316

309317
if(ENABLE_STACKTRACES)
310318
target_link_libraries(
311-
barretenberg
319+
barretenberg_wasm_bin
312320
PUBLIC
313321
Backward::Interface
314322
)
315323
target_link_options(
316-
barretenberg
324+
barretenberg_wasm_bin
317325
PRIVATE
318326
-ldw -lelf
319327
)

barretenberg/cpp/src/barretenberg/bb/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ if (NOT(FUZZING))
2525
if(NOT WASM AND NOT BB_LITE)
2626
target_link_libraries(bb PRIVATE ipc)
2727
endif()
28+
if(WASM)
29+
# bb has a main() and reads CRS / msgpack inputs from the host
30+
# filesystem. The toolchain default PROXY_TO_PTHREAD migrates main
31+
# onto a worker, which combined with NODERAWFS makes every file op
32+
# silently return zero (Emscripten #19330). Mirror the bench
33+
# override block in cmake/module.cmake so `bb prove ...` actually
34+
# sees the disk it was pointed at.
35+
target_link_options(
36+
bb
37+
PRIVATE
38+
"SHELL:-sNODERAWFS=1"
39+
"SHELL:-sPROXY_TO_PTHREAD=0"
40+
"SHELL:-sALLOW_BLOCKING_ON_MAIN_THREAD=1"
41+
)
42+
endif()
2843
if(ENABLE_STACKTRACES)
2944
target_link_libraries(
3045
bb
@@ -65,6 +80,15 @@ if (NOT(FUZZING))
6580
if(NOT WASM AND NOT BB_LITE)
6681
target_link_libraries(bb-avm PRIVATE ipc)
6782
endif()
83+
if(WASM)
84+
target_link_options(
85+
bb-avm
86+
PRIVATE
87+
"SHELL:-sNODERAWFS=1"
88+
"SHELL:-sPROXY_TO_PTHREAD=0"
89+
"SHELL:-sALLOW_BLOCKING_ON_MAIN_THREAD=1"
90+
)
91+
endif()
6892
if(ENABLE_STACKTRACES)
6993
target_link_libraries(
7094
bb-avm

0 commit comments

Comments
 (0)