cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

set (quickfix_PROJECT_NAME quickfix)

add_definitions(-DNOMINMAX)

# Large file support
if(UNIX)
  add_definitions(-D_FILE_OFFSET_BITS=64)
endif()

project(${quickfix_PROJECT_NAME} VERSION 1.16.0 LANGUAGES CXX C)
message("-- Project name ${CMAKE_PROJECT_NAME}")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if( MSVC )
  add_compile_options(/MP)
endif()

# Allow overriding the output directory for libraries and executables.
# Default: <project-root>/lib
set(QUICKFIX_LIB_OUTPUT_DIR "${CMAKE_SOURCE_DIR}/lib" CACHE PATH "Top-level output directory for quickfix libraries and runtime files")

# Single-config generators
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${QUICKFIX_LIB_OUTPUT_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${QUICKFIX_LIB_OUTPUT_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${QUICKFIX_LIB_OUTPUT_DIR}")

# Multi-config generators (Visual Studio, Xcode)� set per-config variables
foreach(cfg IN ITEMS Debug Release RelWithDebInfo MinSizeRel)
  string(TOUPPER "${cfg}" cfg_up)
  set("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${cfg_up}" "${QUICKFIX_LIB_OUTPUT_DIR}")
  set("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${cfg_up}" "${QUICKFIX_LIB_OUTPUT_DIR}")
  set("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${cfg_up}" "${QUICKFIX_LIB_OUTPUT_DIR}")
endforeach()

if( MSVC AND STATIC_RUNTIME )
  set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
  set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
  set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
endif()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")

include(QuickfixPlatformSettings)
include(QuickfixPrebuildSetup)

# Call cmake with-DHAVE_SSL=ON to compile with SSL. Similarly for others.
option(HAVE_SSL "Build with SSL")
option(HAVE_MYSQL "Build with MySQL")
option(HAVE_POSTGRESQL "Build with PostgreSQL")
option(HAVE_ODBC "Build with ODBC")
option(HAVE_PYTHON3 "Build with default Python3 version")

# add option to build shared or static
option(QUICKFIX_SHARED_LIBS "Build using shared libraries" ON)
# add option to build other targets
option(QUICKFIX_EXAMPLES "Build quickfix examples" ON)
option(QUICKFIX_TESTS "Build quickfix tests" ON)

# Registers the suites with CTest. Must come before add_subdirectory(), or the
# add_test() calls in the subdirectories register nothing. Plain enable_testing()
# rather than include(CTest): the latter adds a BUILD_TESTING option that would
# compete with QUICKFIX_TESTS above, plus CDash targets this project never uses.
enable_testing()

# Base port for the tests that bind a socket, so two configured trees of the same
# checkout (CI builds Debug and Release of it) never contend for a port.
#   +0      acceptance (at)
#   +2, +3  pt network benchmark -- --port N binds N and N+1
#   +10..13 Python session tests, one port per transport pair
set(QUICKFIX_TEST_PORT_BASE 6660 CACHE STRING "Base TCP port for tests that bind a socket")

#Make sure that a previous config.h has not undefined HAVE_SSL
if(HAVE_SSL)
# Can set location explicitly, example, cmake -DOPENSSL_ROOT_DIR=/usr/local/ssl -DOPENSSL_LIBRARIES=/usr/local/ssl/lib

find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})

message("-- OPENSSL_INCLUDE_DIR: ${OPENSSL_INCLUDE_DIR}")
message("-- OPENSSL_LIBRARIES: ${OPENSSL_LIBRARIES}")
message("-- OPENSSL_ROOT_DIR: ${OPENSSL_ROOT_DIR}")

message("-- Building with SSL")
endif()

if(HAVE_MYSQL)
find_package(MySQL REQUIRED)
include_directories(${MYSQL_INCLUDE_DIR})

message("-- Building with MySQL")
endif()

if(HAVE_POSTGRESQL)
find_package(PostgreSQL REQUIRED)
include_directories(${PostgreSQL_INCLUDE_DIRS})

message("-- Building with POSTGRESQL")
endif()

if(HAVE_ODBC)
find_package(ODBC REQUIRED)
add_definitions(-DHAVE_ODBC)
include_directories(SYSTEM ${ODBC_INCLUDE_DIRS})
set(ODBC_LIBRARIES "ODBC::ODBC")

message("-- Building with ODBC")
endif()

if (HAVE_PYTHON3)
  if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.12")
# Interpreter is required, not optional: without it FindPython3 cannot run the
# interpreter to determine its ABI, so on a free-threaded build it looks for
# python3XX.lib instead of python3XXt.lib and Development is reported missing.
# Development.Module (not the full Development, which also demands
# Development.Embed) is all an extension module needs; manylinux images don't
# ship the embeddable libpythonX.Y.so that Development.Embed requires, so
# asking for it fails builds that never embed a Python interpreter.
   find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
   set(PYTHON_INCLUDE_DIRS "${Python3_INCLUDE_DIRS}")
   if (TARGET Python3::Module)
     set(PYTHON_LIBRARY Python3::Module)
   elseif (TARGET Python3::Python)
     set(PYTHON_LIBRARY Python3::Python)
   else()
     set(PYTHON_LIBRARY "${Python3_LIBRARIES}")
   endif()
  else()
   find_package(PythonLibs 3 REQUIRED)
  endif()
  include_directories(${PYTHON_INCLUDE_DIRS})
endif ()

add_subdirectory(src)

if (QUICKFIX_EXAMPLES)
  add_subdirectory(examples)
endif()

if (QUICKFIX_TESTS)
  # Added on every platform now that it carries the acceptance/perf add_test()
  # calls; test/CMakeLists.txt still self-gates its Windows-only atrun target.
  add_subdirectory(test)
endif()

configure_file(cmake_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/src/C++/config.h @ONLY)
configure_file(cmake_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/include/quickfix/config.h @ONLY)
file(REMOVE config.h)


configure_file(cmake_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/include/quickfix/config.h @ONLY)

# pkg-config file, previously generated by configure. The template's old
# `Requires: libxml-2.0` is gone with it: the engine vendors pugixml and links
# no libxml2.
include(GNUInstallDirs)
configure_file(quickfix.pc.in ${CMAKE_CURRENT_BINARY_DIR}/quickfix.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/quickfix.pc
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/spec/ DESTINATION share/quickfix FILES_MATCHING PATTERN "FIX*.xml")
