cmake_minimum_required(VERSION 3.0)
project (model_plant_sim)


if(NOT MODEL_NAME)
    message(FATAL_ERROR "MODEL_NAME must be defined when generating the build files with CMake.")
endif()

set( RTS_LIB_NAME ${MODEL_NAME})
set( PYBIND_LIB_NAME ${MODEL_NAME}_pybind)

set(RCG_SRC $ENV{RCG_SRC})

set(RCG_BUILDD $ENV{RCG_BUILDD})

message (STATUS "Using ${RCG_BUILDD} as the build directory to look for model libraries.")

if(NOT EXISTS ${RCG_BUILDD}/models/${MODEL_NAME}/userspace/UserspaceVars.cmake )
    message(FATAL_ERROR "${RCG_BUILDD}/models/${MODEL_NAME}/userspace/UserspaceVars.cmake Does not exist... Maybe the model has not been built?")
endif()

if(NOT EXISTS ${RCG_BUILDD}/models/${MODEL_NAME}/include/${MODEL_NAME}.h)
    message(FATAL_ERROR "${RCG_BUILDD}/src/include/${MODEL_NAME}.h Does not exist... Maybe the model has not been built?")
endif()

#Make sure a static or shared lib exists for the model
if(NOT EXISTS ${RCG_BUILDD}/models/${MODEL_NAME}/userspace/build/lib${MODEL_NAME}.a
   AND NOT EXISTS ${RCG_BUILDD}/models/${MODEL_NAME}/userspace/build/lib${MODEL_NAME}.so )
    message(FATAL_ERROR "The model library has not been built, please build the model for userspace. Ex. make <model_name>")
endif()


option(LIBRTS_NO_SHMEM "Don't used any shared memory (POSIX shmem) and just use local buffers" OFF)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Debug)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)


add_subdirectory(src) #Build librts

# Examples
if(NOT LIBRTS_NO_EXAMPLES)
    add_subdirectory(examples/load-run-model)
    add_subdirectory(examples/filter-module-control)
    add_subdirectory(examples/trend-model-var)
    add_subdirectory(examples/load-coeffs)
    add_subdirectory(examples/load-filter-from-file)
    add_subdirectory(examples/load-snap-file)
    add_subdirectory(examples/legacy-awg-generator)
    add_subdirectory(examples/statespace)
endif()

# Pybind Build
add_subdirectory(python/pybind)

# awg
add_subdirectory(awgstandalone)

# Unit Tests
if(NOT LIBRTS_NO_UNIT_TESTS)

# Check to see if Catch2 is installed
find_package(Catch2 QUIET)
if (NOT Catch2_FOUND)
    message(STATUS "Did not find Catch2 unit testing framework, defaulting to old catch header only interface")
    set( USE_OLD_CATCH_HEADER_ONLY YES)
endif()


enable_testing()
add_subdirectory(tests/general-test)
add_subdirectory(tests/filter-control-test)
add_subdirectory(tests/trend-model-var-test)
add_subdirectory(tests/set-get-var-test)
add_subdirectory(tests/fcn-test)
add_subdirectory(tests/logical-operator-test)
add_subdirectory(tests/math-function-test)
add_subdirectory(tests/product-divide-sum-test)
add_subdirectory(tests/matrix-test)
add_subdirectory(tests/osc-test)
add_subdirectory(tests/statespace-test)
add_subdirectory(tests/python)
endif()
