set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES /usr/include)
set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES /usr/include)


# Whatever the platform, this always works.
find_package(Qt6 COMPONENTS Core Svg Xml Network REQUIRED)


message("\n${BoldGreen}Now configuring library libXpertMass${ColourReset}\n")


########################################################
# Files
set(XpertMass_SRCS
  #
  # Most general features
  globals.cpp
  Prop.cpp
  PropListHolder.cpp
  #
  # The basic chemical features
  Isotope.cpp
  IsotopicData.cpp
  IsotopicDataBaseHandler.cpp
  IsotopicDataLibraryHandler.cpp
  IsotopicDataUserConfigHandler.cpp
  IsotopicDataManualConfigHandler.cpp
  IsotopicClusterGenerator.cpp
  IsotopicClusterShaper.cpp
  PeakCentroid.cpp
  Ponderable.cpp
  ChemicalGroup.cpp
  ChemicalGroupRule.cpp
  Modif.cpp
  ModifSpec.cpp
  Monomer.cpp
  MonomerDictionary.cpp
  MonomerSpec.cpp
  Oligomer.cpp
  Polymer.cpp
  CleaveMotif.cpp
  CleaveRule.cpp
  CleaveSpec.cpp
  Coordinates.cpp
  Formula.cpp
  FragRule.cpp
  FragSpec.cpp
  Ionizable.cpp
  IonizeRule.cpp
  CrossLink.cpp
  CrossLinker.cpp
  CrossLinkerSpec.cpp
  PolChemDef.cpp
  PolChemDefEntity.cpp
  PolChemDefSpec.cpp
  PkaPhPi.cpp
  PkaPhPiDataParser.cpp
  Sequence.cpp
  #
  # The calculation features
  CalcOptions.cpp
  MassPeakShaper.cpp
  MassPeakShaperConfig.cpp
  #
  # Envemind implementation
  Envemind.cpp
  #
  # The Averagine implementation
  Averagine.cpp
  #
  # The network functionality
  MassDataCborBaseHandler.cpp
  MassDataCborMassSpectrumHandler.cpp
  MassDataServer.cpp
  MassDataServerThread.cpp
  MassDataClient.cpp
)

# Because the header files are in their own directory and not along the source
# files, we need to have them listed explicitely for automoc to work properly
# below.

# Create a variable to hold the 'includes' directory *relative* to the
# current CMakeLists.txt file.

set(INCLUDES_DIR "${CMAKE_CURRENT_LIST_DIR}/includes/libXpertMass")

file(GLOB XpertMass_HEADERS ${INCLUDES_DIR}/*.hpp)
list(APPEND XpertMass_HEADERS ${INCLUDES_DIR}/exportimportconfig.h)
message(STATUS "Included the header files from ${INCLUDES_DIR}: \n\
  ${XpertMass_HEADERS}")

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

add_library(compiler_flags INTERFACE)
target_compile_features(compiler_flags INTERFACE cxx_std_11)

###############################################################
# Configuration of the binary to be built
###############################################################

# Only now can we add the library, because we have defined the sources.

#############################################################
# Build the static lib
add_library(Core_static STATIC
  ${XpertMass_HEADERS}
  ${XpertMass_SRCS}
  ${PLATFORM_SPECIFIC_SOURCES})

set_target_properties(Core_static
  PROPERTIES OUTPUT_NAME XpertMass
  LINK_FLAGS "-Wl,--whole-archive")

target_link_libraries(Core_static

  PappsoMSpp::Core

  IsoSpec++::IsoSpec++

  Qt6::Core
  Qt6::Xml
  Qt6::Network)

# The install interface that is ${prefix}/include
# These include directories are for users of this lib.
target_include_directories(Core_static
PUBLIC
  $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

# These include directories are for building of this lib.
target_include_directories(Core_static
PUBLIC
  $<BUILD_INTERFACE:${INCLUDES_DIR}>)

get_target_property(Core_static_INCLUDES Core_static INCLUDE_DIRECTORIES)

message(STATUS "Core_static_INCLUDES: \
${Core_static_INCLUDES}")

install(TARGETS Core_static
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})


#############################################################
# Build the shared lib
add_library(Core SHARED
  ${XpertMass_HEADERS}
  ${XpertMass_SRCS}
  ${PLATFORM_SPECIFIC_SOURCES})

set_target_properties(Core
  PROPERTIES OUTPUT_NAME XpertMass
  VERSION ${PROJECT_VERSION}
  SOVERSION ${PROJECT_VERSION_MAJOR}
  LINK_FLAGS "-Wl,--no-as-needed"
  POSITION_INDEPENDENT_CODE ON)

target_link_libraries(Core

  PappsoMSpp::Core

  IsoSpec++::IsoSpec++

  Qt6::Core
  Qt6::Xml
  Qt6::Network
)

# The install interface that is ${prefix}/include
# These include directories are for users of this lib.
target_include_directories(Core
PUBLIC
  $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

# These include directories are for building of this lib.
target_include_directories(Core
PUBLIC
  $<BUILD_INTERFACE:${INCLUDES_DIR}>)

get_target_property(Core_INCLUDES Core INCLUDE_DIRECTORIES)

message(STATUS "Core_INCLUDES: \
${Core_INCLUDES}")

target_compile_definitions(Core PRIVATE "EXPORT_LIB_SYMBOLS")

# This is to avoid the "include_next(math.h) file not found" error.
if(WIN32)
  set_target_properties(Core_static
  PROPERTIES NO_SYSTEM_FROM_IMPORTED ON)
  set_target_properties(Core
  PROPERTIES NO_SYSTEM_FROM_IMPORTED ON)
endif()


#############################################################
# Common installation configuration

install(FILES ${XpertMass_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libXpertMass")

message("")
message(STATUS "${BoldGreen}Finished configuration of libXpertMass.${ColourReset}")
message("")
