| Replace V with Nim 472eb49 nandithebull 8h ago | 1 | # Shared CMake logic for every platform that builds the Nim library from |
| 2 | # source. Included by android/, linux/ and windows/. |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 3 | cmake_minimum_required(VERSION 3.15) |
| Replace V with Nim 472eb49 nandithebull 8h ago | 4 | project(nimflutter LANGUAGES C) |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 5 | |
| Replace V with Nim 472eb49 nandithebull 8h ago | 6 | find_program(NIM_EXECUTABLE nim REQUIRED |
| 7 | DOC "The Nim compiler. Set -DNIM_EXECUTABLE=/path/to/nim to override.") |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 8 | |
| Replace V with Nim 472eb49 nandithebull 8h ago | 9 | set(NF_SRC "${CMAKE_CURRENT_LIST_DIR}/nimflutter.nim") |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 10 | |
| Replace V with Nim 472eb49 nandithebull 8h ago | 11 | # Nim cross-compiles by emitting C, so the reliable pattern is: |
| 12 | # Nim source -> C (Nim's job) C -> object/library (the toolchain's) |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 13 | # This keeps the NDK / MSVC / clang in charge of ABI, sysroot and flags. |
| Replace V with Nim 472eb49 nandithebull 8h ago | 14 | # |
| 15 | # Nim writes one .c per module rather than a single file, and the set is only |
| 16 | # known after it runs — so it runs at CONFIGURE time and the results are |
| 17 | # globbed, instead of the add_custom_command a single-file emitter allows. |
| 18 | set(NF_NIMCACHE "${CMAKE_CURRENT_BINARY_DIR}/nimcache") |
| 19 | |
| 20 | execute_process( |
| 21 | COMMAND "${NIM_EXECUTABLE}" c --compileOnly --nimcache:${NF_NIMCACHE} |
| 22 | --mm:arc -d:danger --threads:on --noMain "${NF_SRC}" |
| 23 | RESULT_VARIABLE NF_NIM_RESULT |
| 24 | OUTPUT_VARIABLE NF_NIM_OUTPUT |
| 25 | ERROR_VARIABLE NF_NIM_OUTPUT) |
| 26 | if(NOT NF_NIM_RESULT EQUAL 0) |
| 27 | message(FATAL_ERROR "nim failed to emit C:\n${NF_NIM_OUTPUT}") |
| 28 | endif() |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 29 | |
| Replace V with Nim 472eb49 nandithebull 8h ago | 30 | file(GLOB NF_GENERATED_C "${NF_NIMCACHE}/*.c") |
| 31 | if(NF_GENERATED_C STREQUAL "") |
| 32 | message(FATAL_ERROR "nim produced no C in ${NF_NIMCACHE}") |
| 33 | endif() |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 34 | |
| Replace V with Nim 472eb49 nandithebull 8h ago | 35 | add_library(nimflutter SHARED ${NF_GENERATED_C}) |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 36 | |
| Replace V with Nim 472eb49 nandithebull 8h ago | 37 | # nimbase.h lives in Nim's own lib/, alongside the compiler. |
| 38 | get_filename_component(NF_NIM_BIN "${NIM_EXECUTABLE}" DIRECTORY) |
| 39 | get_filename_component(NF_NIM_ROOT "${NF_NIM_BIN}" DIRECTORY) |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 40 | |
| Replace V with Nim 472eb49 nandithebull 8h ago | 41 | target_include_directories(nimflutter PUBLIC |
| 42 | "${CMAKE_CURRENT_LIST_DIR}" |
| 43 | "${NF_NIM_ROOT}/lib") |
| 44 | |
| 45 | # Nim's generated C is machine output: silence the noise, keep real errors. |
| Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 10h ago | 46 | # |
| 47 | # -O2 is set unconditionally, including in Debug. This library is a numeric |
| 48 | # kernel with no Dart-visible debugger story of its own, and at -O0 the |
| 49 | # Mandelbrot path runs several times slower than the equivalent Dart — which |
| 50 | # makes a debug run of the example wildly misrepresent the bridge. |
| 51 | if(MSVC) |
| Replace V with Nim 472eb49 nandithebull 8h ago | 52 | target_compile_options(nimflutter PRIVATE /O2) |
| Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 10h ago | 53 | else() |
| Replace V with Nim 472eb49 nandithebull 8h ago | 54 | target_compile_options(nimflutter PRIVATE |
| Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 10h ago | 55 | -O2 -w -fPIC -Wno-int-conversion -Wno-incompatible-pointer-types) |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 56 | endif() |
| 57 | |
| 58 | if(ANDROID) |
| Replace V with Nim 472eb49 nandithebull 8h ago | 59 | target_link_libraries(nimflutter PRIVATE log) |
| 60 | endif() |
| 61 | |
| 62 | # Nim's threading support needs pthreads where the platform has them. |
| 63 | find_package(Threads) |
| 64 | if(Threads_FOUND AND NOT ANDROID AND NOT WIN32) |
| 65 | target_link_libraries(nimflutter PRIVATE Threads::Threads) |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 66 | endif() |
| 67 | |
| Replace V with Nim 472eb49 nandithebull 8h ago | 68 | set_target_properties(nimflutter PROPERTIES |
| 69 | OUTPUT_NAME "nimflutter" |
| Initial commit: V -> Flutter FFI plugin ad0ccda nandi 14h ago | 70 | C_VISIBILITY_PRESET hidden) |