|
| 1 | +cmake_minimum_required(VERSION 3.14) |
| 2 | +project(echo_c_bindings C) |
| 3 | + |
| 4 | +set(CMAKE_C_STANDARD 11) |
| 5 | +set(CMAKE_C_STANDARD_REQUIRED ON) |
| 6 | + |
| 7 | +# ── Locate the repository root (contains ffi.nimble) ───────────────────────── |
| 8 | +set(_search_dir "${CMAKE_CURRENT_SOURCE_DIR}") |
| 9 | +set(REPO_ROOT "") |
| 10 | +foreach(_i RANGE 10) |
| 11 | + if(EXISTS "${_search_dir}/ffi.nimble") |
| 12 | + set(REPO_ROOT "${_search_dir}") |
| 13 | + break() |
| 14 | + endif() |
| 15 | + get_filename_component(_search_dir "${_search_dir}" DIRECTORY) |
| 16 | +endforeach() |
| 17 | +if("${REPO_ROOT}" STREQUAL "") |
| 18 | + message(FATAL_ERROR "Cannot find repo root (no ffi.nimble in any ancestor)") |
| 19 | +endif() |
| 20 | + |
| 21 | +# Build the Nim dylib + vendored TinyCBOR (shared with the C++ backend). |
| 22 | +set(NIM_FFI_LIB echo) |
| 23 | +set(NIM_FFI_SRC ../echo.nim) |
| 24 | +include("${REPO_ROOT}/ffi/codegen/templates/nim_ffi_lib.cmake") |
| 25 | + |
| 26 | +find_package(Threads REQUIRED) |
| 27 | + |
| 28 | +add_library(echo_headers INTERFACE) |
| 29 | +target_include_directories(echo_headers INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}") |
| 30 | +target_link_libraries(echo_headers INTERFACE echo tinycbor Threads::Threads) |
| 31 | +# The generated header is async (no blocking helper), but consumer code that |
| 32 | +# waits on a result callback typically uses nanosleep / pthreads, which need a |
| 33 | +# POSIX feature level that strict `-std=c11` hides. Define it for consumers. |
| 34 | +target_compile_definitions(echo_headers INTERFACE _POSIX_C_SOURCE=200809L) |
| 35 | + |
| 36 | +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/main.c") |
| 37 | + add_executable(echo_example main.c) |
| 38 | + target_link_libraries(echo_example PRIVATE echo_headers) |
| 39 | + add_dependencies(echo_example echo_nim_lib) |
| 40 | + if(CMAKE_SYSTEM_NAME STREQUAL "Windows") |
| 41 | + add_custom_command(TARGET echo_example POST_BUILD |
| 42 | + COMMAND "${CMAKE_COMMAND}" -E copy_if_different |
| 43 | + "${echo_RUNTIME_LIB}" |
| 44 | + "$<TARGET_FILE_DIR:echo_example>" |
| 45 | + COMMENT "Staging echo.dll next to echo_example.exe") |
| 46 | + endif() |
| 47 | +endif() |
0 commit comments