@@ -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 >)
221221endif ()
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
239244if (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 )
0 commit comments