set(CMAKE_COLOR_MAKEFILE ON)
set(CMAKE_VERBOSE_MAKEFILE ON)

# Get the name of the current directory but with no path:
get_filename_component(TARGET ${CMAKE_CURRENT_BINARY_DIR} NAME)

message("")
message(STATUS "${BoldGreen}Start processing ${TARGET} dir.${ColourReset}")
message("")

########################################################
# Files

file(GLOB mass_SRCS *.cpp)
file(GLOB glob_SRCS ../globals/*.cpp)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

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

if(SYSTEM_UNAME_S MATCHES "^MINGW.*")
	set(CMAKE_BUILD_TYPE "release")
else()
	set(CMAKE_BUILD_TYPE "debug")
endif()

find_package(Qt5 COMPONENTS Core Xml Svg Sql Script)

add_library(mass STATIC
	${mass_SRCS}
	${glob_SRCS})

qt5_use_modules(mass Xml)
qt5_use_modules(mass Svg)
qt5_use_modules(mass Sql)
qt5_use_modules(mass Script)

include_directories(
	# For the config file that is in the top binary dir.
	${CMAKE_BINARY_DIR}

	# For all the source subdirectories
	${CMAKE_SOURCE_DIR}

	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_BINARY_DIR}

	${Qt5Xml_INCLUDE_DIRS}
	${Qt5Svg_INCLUDE_DIRS}
	${Qt5Sql_INCLUDE_DIRS}
	${Qt5Script_INCLUDE_DIRS})


# On WIN64 we provide a number of libraries by our own, that we
# put in /usr/local/lib under /mingw64/.
if(WIN64)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -LC:/msys64/mingw64/usr/local/lib")
endif()


##############################################################
# libpwiz for reading mass spec data files,
message(STATUS "${BoldYellow}Link against the libpwiz library.${ColourReset}")
if(WIN64)
	include_directories("C:/msys64/mingw64/usr/local/include/pwiz")
elseif(APPLE)
	include_directories(/opt/local/include/pwiz)
	link_directories(/opt/local/lib/)
else()
	include_directories(/usr/include/proteowizard)
endif()

set(required_target_link_libraries
	${required_target_link_libraries}
	pwiz)


##############################################################
# libboost_* on mingw
# On MINGW.* and mac we are our own provider of the boost libraries.
if(APPLE OR WIN64)

	message(STATUS "${BoldYellow}Link against the libboost libraries.${ColourReset}")

	if(WIN64)
	include_directories("${CMAKE_SOURCE_DIR}/../../boost/boost_1_56_0")
	elseif(APPLE)
		include_directories(/opt/local/include)
		include_directories(/opt/local/include/pwiz/libraries/boost_aux)
	endif()

	set(required_target_link_libraries
		${required_target_link_libraries}
		boost_chrono
		boost_filesystem
		boost_iostreams
		boost_program_options
		boost_serialization
		boost_system
		boost_thread
		z)
endif()


# Use the Widgets module from Qt 5. We craft a variable for this so as to
# transmit it directly to the tests subdirectory that will need it.
# Also, take the opportunity to ask for linking of the libgomp or lomp OPENMP
# library, depending on the compiler.

message(STATUS "${BoldYellow}Using parallelizing library ${OPENMP_LIBRARY}${ColourReset}")

set(required_target_link_libraries
	${required_target_link_libraries}
	Qt5::Xml
	Qt5::Svg
	Qt5::Sql
	Qt5::Script
	${OPENMP_LIBRARY})

# on MINGW.*, we'll need to add the -lstdc++ flag. Don't ask why.
if(SYSTEM_UNAME_S MATCHES "^MINGW.*")
	set(required_target_link_libraries
		${required_target_link_libraries}
		"stdc++")
endif()


#### Debugging stuff
message(STATUS required_target_link_libraries: ${required_target_link_libraries})
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
message(STATUS "included dir='${dir}'")
endforeach()

# Finally actually set the liking dependencies to the library.
target_link_libraries(mass
	${required_target_link_libraries})



message(STATUS "${TARGET} CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}")
message(STATUS "Qt5Xml: " ${Qt5Xml_INCLUDE_DIRS})
message(STATUS "Qt5Svg: " ${Qt5Svg_INCLUDE_DIRS})
message(STATUS "Qt5Sql: " ${Qt5Sql_INCLUDE_DIRS})
message(STATUS "Using parallelizing library ${OPENMP_LIBRARY}")


message("")
message(STATUS "${BoldGreen}Finished processing ${TARGET} dir.${ColourReset}")
message("")

