cmake_minimum_required(VERSION 3.10)
project(cdd2mgs)

# control where the static and shared libraries are built so that on windows
# we don't need to tinker with the path to run the executable
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

#find_package(OpenMP)
#if(OpenMP_CXX_FOUND)
#    target_link_libraries(cdd2mgs PUBLIC OpenMP::OpenMP_CXX)
#endif()

option(BUILD_SHARED_LIBS "Build using shared libraries" ON)

# the version number.
set(cdd2mgs_VERSION_MAJOR 3)
set(cdd2mgs_VERSION_MINOR 2)

if(APPLE)
  set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
elseif(UNIX)
  set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
endif()

# configure a header file to pass the version number only
#configure_file(
#  "${PROJECT_SOURCE_DIR}/cdd2mgsConfig.h.in"
#  "${PROJECT_BINARY_DIR}/cdd2mgsConfig.h"
#  )

# Note the use of the variable EXTRA_LIBS to collect up any optional libraries
# to later be linked into the executable. The variable EXTRA_INCLUDES is used
# similarly for optional header files. 
get_filename_component(PARENT_DIR ../ ABSOLUTE)
# Mgs Afn Gsq Blst Tax Alex Aln Tools Hat Gsmo
list(APPEND EXTRA_LIBS Afn Gsq Aln Hat Mgs Blst Tax Alex Tools Chn Hpt )
list(APPEND EXTRA_INCLUDES 
        "${PARENT_DIR}/lib/libafn"
        "${PARENT_DIR}/lib/libgsq"
        "${PARENT_DIR}/lib/libaln"
        "${PARENT_DIR}/lib/libblst"
        "${PARENT_DIR}/lib/libtax"
        "${PARENT_DIR}/lib/libalex"
        "${PARENT_DIR}/lib/libtools"
        "${PARENT_DIR}/lib/libhpt"
        "${PARENT_DIR}/lib/libchn"
        "${PARENT_DIR}/lib/libhat"
        "${PARENT_DIR}/lib/libmgs"
        "${PARENT_DIR}/lib/include"
        )

#=========== flex and bison (yacc) instructions =================

find_package(BISON REQUIRED)
find_package(FLEX REQUIRED)

BISON_TARGET(cdd2cma_parser
             cdd2cma.yy
             ${CMAKE_CURRENT_BINARY_DIR}/cdd2cma_yy.tab.cc
            COMPILE_FLAGS " -vd -p cdd2cma_yy "
        )

FLEX_TARGET(cdd2cma_lexer
            cdd2cma.ll
            ${CMAKE_CURRENT_BINARY_DIR}/cdd2cma_yy.lex.cc
            COMPILE_FLAGS " -Pcdd2cma_yy "
        )
ADD_FLEX_BISON_DEPENDENCY(cdd2cma_lexer cdd2cma_parser)

# add the executable
add_executable(cdd2mgs run_cdd2mgs.cxx ftc_typ.cc tbn_typ.cc 
	cdd2cma_yy.tab.cc cdd2cma_yy.lex.cc
        ${FLEX_cdd2cma_lexer_OUTPUTS} ${BISON_cdd2cma_parser_OUTPUTS})
target_link_libraries(cdd2mgs ${EXTRA_LIBS} ${FLEX_LIBRARIES})
include_directories(${PARENT_DIR}/lib/include ${EXTRA_INCLUDES})

target_include_directories(cdd2mgs PRIVATE ${PROJECT_BINARY_DIR}
	${CMAKE_CURRENT_SOURCE_DIR} ${EXTRA_INCLUDES}
        ${FLEX_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})

# add the binary tree to the search path for include files
# so that we will find cdd2mgsConfig.h
#target_include_directories(cdd2mgs PRIVATE "${PROJECT_BINARY_DIR}" ${EXTRA_INCLUDES} )
target_include_directories(cdd2mgs PUBLIC "${PROJECT_BINARY_DIR}" ${EXTRA_INCLUDES} )

# add the install targets
#install(TARGETS cdd2mgs DESTINATION bin)
#install(FILES "${PROJECT_BINARY_DIR}/cdd2mgsConfig.h"
#  DESTINATION include
#  )


