###############################################################################
# CMake file for the CHASE tool managing Logics-based specifications.
# @author Michele Lora
###############################################################################

cmake_minimum_required (VERSION 3.18)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
endif()
set(CMAKE_C_FLAGS_DEBUG "-O0 -Wall -Wextra -pedantic -ggdb -fPIC")
set(CMAKE_C_FLAGS_RELEASE "-O3 -fPIC")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -Wall -Wextra -pedantic -ggdb -fPIC")
set(CMAKE_CXX_FLAGS_RELEASE "-O3  -fPIC")

if(DEFINED ENV{CHASE_INSTALL_DIR})
    set(CMAKE_INSTALL_PREFIX $ENV{CHASE_INSTALL_DIR} CACHE PATH "Install top-level directory" FORCE)
else()
    set(CMAKE_INSTALL_PREFIX ../../install_dir CACHE PATH "Install top-level directory" FORCE)
endif()

project(LOGICS_TOOL VERSION 1.0)

# The headers of the core library need C++17.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Use one Python for the headers and the module suffix (FindPython).
find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
set(PYBIND11_FINDPYTHON ON)
find_package(pybind11 REQUIRED)


set(CHASEPATH ${CMAKE_INSTALL_PREFIX} CACHE PATH "The path of the Chase installation")

include_directories(
    ${CHASEPATH}/third_party/antlr4/include
    ${CHASEPATH}/chase/include
    ${CHASEPATH}/chase/include/utilities
    ${CHASEPATH}/chase/include/representation
    ${CHASEPATH}/chase/include/backends
    ${PROJECT_SOURCE_DIR}/include
    ${PROJECT_SOURCE_DIR}/include/LogicsParser
)

set(Logics_PARSER_SRC_DIR ${PROJECT_SOURCE_DIR}/src/LogicsParser)

set(Logics_PARSER_SRCS
        ${Logics_PARSER_SRC_DIR}/LogicsContractsBaseVisitor.cpp
        ${Logics_PARSER_SRC_DIR}/LogicsContractsLexer.cpp
        ${Logics_PARSER_SRC_DIR}/LogicsContractsVisitor.cpp
        ${Logics_PARSER_SRC_DIR}/LogicsContractsParser.cpp)

set(Logics_SRC_DIR ${PROJECT_SOURCE_DIR}/src)

set(Logics_Library_SRCS
        ${Logics_SRC_DIR}/Console.cc
        ${Logics_SRC_DIR}/Console_commands.cc
        ${Logics_PARSER_SRCS}
        ${Logics_SRC_DIR}/LogicsSpecsBuilder.cc
)

set(Logics_tool_SRCS
    ${Logics_Library_SRCS}
    ${Logics_SRC_DIR}/main.cc)


if(COMMAND cmake_policy)
    cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

add_library(chase_logicsLang
    ${Logics_Library_SRCS}
    )

install(TARGETS chase_logicsLang
    DESTINATION chase/lib)

install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/
    DESTINATION chase/include/frontends
    )

add_executable(logics_tool 
    ${Logics_tool_SRCS})

target_link_libraries(logics_tool 
    chase
    chase-backends
    antlr4-runtime)

target_link_directories(
    logics_tool
    PRIVATE ${CHASEPATH}/chase/lib
    PRIVATE ${CHASEPATH}/third_party/antlr4/lib)

# Set rpath for macOS
if(APPLE)
    set_target_properties(logics_tool PROPERTIES
        INSTALL_RPATH "@executable_path/../lib;@executable_path/../../third_party/antlr4/lib"
    )
elseif(UNIX)
	set_target_properties(logics_tool PROPERTIES
		INSTALL_RPATH "\$ORIGIN/../lib;\$ORIGIN/../../third_party/antlr4/lib")
endif()

install(TARGETS logics_tool DESTINATION chase/bin)

pybind11_add_module(
    pychase_logicsLang MODULE
    src/bindings.cc
    ${Logics_Library_SRCS}
    )

target_link_libraries(pychase_logicsLang PRIVATE
    chase-backends
    antlr4-runtime
    chase)

target_link_directories(
    pychase_logicsLang
    PRIVATE ${CHASEPATH}/chase/lib
    PRIVATE ${CHASEPATH}/third_party/antlr4/lib
    )

# Set rpath for the Python module (installed in chase/lib)
if(APPLE)
    set_target_properties(pychase_logicsLang PROPERTIES
        INSTALL_RPATH "@loader_path;@loader_path/../../third_party/antlr4/lib"
    )
elseif(UNIX)
    set_target_properties(pychase_logicsLang PROPERTIES
        INSTALL_RPATH "\$ORIGIN;\$ORIGIN/../../third_party/antlr4/lib")
endif()

install(TARGETS pychase_logicsLang
    EXPORT pychaseTargets
    DESTINATION chase/lib)

add_custom_target(install_logics_tool
        "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target install
        DEPENDS ${logics_tool}
        DEPENDS ${pychase_logicsLang}
        DEPENDS ${chase_logicsLang}
        COMMENT "Installing Logics Tool")
