cmake_minimum_required(VERSION 3.10)
project(marte_dev)

if(NOT DEFINED ENV{MARTe2_DIR})
    message(FATAL_ERROR "MARTe2_DIR not set. Please source env.sh")
endif()

set(MARTe2_DIR $ENV{MARTe2_DIR})
set(MARTe2_Components_DIR $ENV{MARTe2_Components_DIR})
set(TARGET $ENV{TARGET})

# Define Architecture macros
add_definitions(-DARCHITECTURE=x86_gcc)
add_definitions(-DENVIRONMENT=Linux)
add_definitions(-DMARTe2_TEST_ENVIRONMENT=GTest) # Optional
add_definitions(-DUSE_PTHREAD)

# Add -pthread flag
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

include_directories(
    ${MARTe2_DIR}/Source/Core/BareMetal/L0Types
    ${MARTe2_DIR}/Source/Core/BareMetal/L1Portability
    ${MARTe2_DIR}/Source/Core/BareMetal/L2Objects
    ${MARTe2_DIR}/Source/Core/BareMetal/L3Streams
    ${MARTe2_DIR}/Source/Core/BareMetal/L4Configuration
    ${MARTe2_DIR}/Source/Core/BareMetal/L4Events
    ${MARTe2_DIR}/Source/Core/BareMetal/L4Logger
    ${MARTe2_DIR}/Source/Core/BareMetal/L4Messages
    ${MARTe2_DIR}/Source/Core/BareMetal/L5FILES
    ${MARTe2_DIR}/Source/Core/BareMetal/L5GAMs
    ${MARTe2_DIR}/Source/Core/BareMetal/L6App
    ${MARTe2_DIR}/Source/Core/Scheduler/L1Portability
    ${MARTe2_DIR}/Source/Core/Scheduler/L3Services
    ${MARTe2_DIR}/Source/Core/Scheduler/L4LoggerService
    ${MARTe2_DIR}/Source/Core/FileSystem/L1Portability
    ${MARTe2_DIR}/Source/Core/FileSystem/L3Streams
    ${MARTe2_Components_DIR}/Source/Components/DataSources/EpicsDataSource
    ${MARTe2_Components_DIR}/Source/Components/DataSources/FileDataSource
    ${MARTe2_Components_DIR}/Source/Components/GAMs/IOGAM
    Source
    Headers
)

file(GLOB_RECURSE SOURCES "Source/*.cpp")

add_library(${PROJECT_NAME} SHARED ${SOURCES})

# Target MARTe2 library
set(MARTe2_LIB ${MARTe2_DIR}/Build/${TARGET}/Core/libMARTe2.so)
set(IOGAM_LIB ${MARTe2_Components_DIR}/Build/${TARGET}/Components/GAMs/IOGAM/libIOGAM.so)
set(LinuxTimer_LIB ${MARTe2_Components_DIR}/Build/${TARGET}/Components/DataSources/LinuxTimer/libLinuxTimer.so)

target_link_libraries(${PROJECT_NAME}
    ${MARTe2_LIB}
)

add_subdirectory(Test/UnitTests)
add_subdirectory(Test/Integration)
