cmake_minimum_required(VERSION 3.20)
project(bitbybit_occt_history LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT OCCT_DIR)
  message(FATAL_ERROR "OCCT_DIR must point to an Emscripten OCCT build containing OpenCASCADEConfig.cmake")
endif()

find_package(OpenCASCADE REQUIRED CONFIG PATHS "${OCCT_DIR}" NO_DEFAULT_PATH)

add_executable(bitbybit_occt_history occt_history.cpp)
target_include_directories(bitbybit_occt_history PRIVATE "${OCCT_DIR}/include/opencascade")
target_link_libraries(bitbybit_occt_history PRIVATE TKDESTEP TKXSBase TKDE TKBO TKBRep TKTopAlgo TKPrim TKShHealing TKGeomAlgo TKGeomBase TKG3d TKG2d TKMath TKernel)
if(FREECAD_WASM_SDK_DIR)
  if(NOT FREECAD_NAMING_BRIDGE_SOURCE OR NOT EXISTS "${FREECAD_NAMING_BRIDGE_SOURCE}")
    message(FATAL_ERROR "FREECAD_NAMING_BRIDGE_SOURCE must point to the verified FreeCAD private naming bridge source.")
  endif()
  if(NOT FREECAD_NAMING_INCLUDE_DIRS OR NOT FREECAD_NAMING_LIBRARIES)
    message(FATAL_ERROR "FREECAD_NAMING_INCLUDE_DIRS and FREECAD_NAMING_LIBRARIES are required for a FreeCAD-linked Worker.")
  endif()
  target_sources(bitbybit_occt_history PRIVATE "${FREECAD_NAMING_BRIDGE_SOURCE}")
  target_include_directories(bitbybit_occt_history PRIVATE ${FREECAD_NAMING_INCLUDE_DIRS})
  target_link_libraries(bitbybit_occt_history PRIVATE ${FREECAD_NAMING_LIBRARIES})
  target_compile_definitions(bitbybit_occt_history PRIVATE BITBYBIT_FREECAD_NAMING_LINKED=1)
endif()
target_compile_options(bitbybit_occt_history PRIVATE -fexceptions -O2)
target_link_options(bitbybit_occt_history PRIVATE
  --bind
  -fexceptions
  -sMODULARIZE=1
  -sEXPORT_ES6=1
  -sENVIRONMENT=web,worker,node
  -sALLOW_MEMORY_GROWTH=1
  -sINITIAL_MEMORY=134217728
  -sMAXIMUM_MEMORY=2147483648
  -sNO_EXIT_RUNTIME=1
)
set_target_properties(bitbybit_occt_history PROPERTIES OUTPUT_NAME "bitbybit-occt-history")
