message("\nUNIX non APPLE environment\n")
message("\nIf building as the ${CMAKE_PROJECT_NAME} standalone library,
run the build like this:\n")
message("mkdir build; pushd build; cmake -G \"Unix Makefiles\" -DCMAKE_BUILD_TYPE=Debug ../ ; make -j10 ; popd\n")
message("")
message("To build the tests, configure CMake with -DBUILD_TESTS=1")
message("")

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

# This is used throughout all the build system files
set(TARGET ${LOWCASE_PROJECT_NAME})


message("")
if(STANDALONE_BUILD_MODE)
  message(STATUS "${BoldGreen}Building this library in STANDALONE mode${ColourReset}\n")
else()
  message(STATUS "${BoldGreen}Building this library in EMBEDDED mode${ColourReset}\n")
endif(STANDALONE_BUILD_MODE)
message("")


set(LIBNAME mass)


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

########################################################
# Files
set(${LIBNAME}_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
  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. We need to use ${CMAKE_CURRENT_LIST_DIR}/../
# relative path # instead of ${CMAKE_SOURCE_DIR}/ because that variable
# is going to be modified when the libmass standalone build is replaced by a
# build where libmass is embedded in another source tree.

set(INCLUDE_DIR_RELATIVE_PATH "${CMAKE_CURRENT_LIST_DIR}/../includes")

file(GLOB ${LIBNAME}_HEADERS ${INCLUDE_DIR_RELATIVE_PATH}/lib${LIBNAME}/*.hpp)
message(STATUS "Included the header files from ${INCLUDE_DIR_RELATIVE_PATH}/lib${LIBNAME}")
message(STATUS "The header files: ${${LIBNAME}_HEADERS}")

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

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

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

add_library(${LIBNAME} STATIC
  ${${LIBNAME}_HEADERS}
  ${${LIBNAME}_SRCS}
  ${PLATFORM_SPECIFIC_SOURCES}
)

target_include_directories(${LIBNAME} PUBLIC
${INCLUDE_DIR_RELATIVE_PATH}
${INCLUDE_DIR_RELATIVE_PATH}/lib${LIBNAME})

#message(STATUS "CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}")

set_target_properties(${LIBNAME}
  PROPERTIES OUTPUT_NAME ${LIBNAME}
)

add_custom_command(TARGET ${LIBNAME}
  POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy
  ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_NAME:${LIBNAME}>
  ${CMAKE_CURRENT_LIST_DIR}/..
)

# Finally actually set the linking dependencies to the executable.
target_link_libraries(${LIBNAME}

  -Wl,--no-as-needed
  PappsoMSpp::Core
  -Wl,--as-needed

  IsoSpec++::IsoSpec++

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


#############################################################
#############################################################
# Platform-specific CMake configuration that makes the library
# available to consumers (standalone or embedded alike).


# There are two ways this library might be built for use:

# 1. As a standalone library (call cmake directly to the main source directory)
# 2. As an embedded library, for example inside the massXpert2 or mineXpert2 top
# source directories.

# In both cases we might need to know where the built lib is (along with
# the header files), because it might be needed (as standalone, it is typically
# needed for the test binaries).

# When including lib headers from the embedding project the includes must
# be in the form #include <libmass/header.hpp>

set_property(GLOBAL PROPERTY glob_prop_lib${LIBNAME}_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/../includes/")
set_property(GLOBAL PROPERTY glob_prop_lib${LIBNAME}_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/../lib${LIBNAME}.a")

get_property(lib${LIBNAME}_INCLUDE_DIRS GLOBAL PROPERTY glob_prop_lib${LIBNAME}_INCLUDE_DIRS)
get_property(lib${LIBNAME}_LIBRARIES GLOBAL PROPERTY glob_prop_lib${LIBNAME}_LIBRARIES)

message("")
message(STATUS "Making lib${LIBNAME} headers available at: ${lib${LIBNAME}_INCLUDE_DIRS}.")
message(STATUS "Making lib${LIBNAME} available at ${lib${LIBNAME}_LIBRARIES}.\n")


message("")
message(STATUS "${BoldGreen}Finished configuring of lib${LIBNAME}.${ColourReset}")
message("")

