#-----------------------------------------------------------------
# CMake file for the MRPT C++ library
#  Run with "cmake ." at the root directory
#
#  October 2007, Jose Luis Blanco <jlblanco@ctima.uma.es>
#-----------------------------------------------------------------

INCLUDE(../../cmakemodules/AssureCMakeRootFile.cmake) # Avoid user mistake in CMake source directory

PROJECT(mrpt-core)

#MESSAGE(STATUS "Makefile for: mrpt-core C++ LIBRARY ")

# ---------------------------------------------
# TARGET: The MRPT-CORE C++ library
# ---------------------------------------------
# List of C++ files:
SET(mrpt_lib_srcs "")
SET(mrpt_lib_hdrs "")

# -----------------------------------------------------------
# A macro to add files to the project and at the same time
#  divide them into IDE's solution folders, if applicable.
# -----------------------------------------------------------
MACRO(ADD_MRPT_CORE_SOURCES dir)
	# The .cpp files:
	FILE(GLOB aux_srcs1 ${dir}/*.cpp)
	FILE(GLOB aux_srcs2 ${dir}/*.c)
	FILE(GLOB aux_srcs3 ${dir}/*.h)
	SET(aux_srcs ${aux_srcs1} ${aux_srcs2} ${aux_srcs3})
#	AUX_SOURCE_DIRECTORY(${dir} aux_srcs)
	SET(mrpt_lib_srcs ${mrpt_lib_srcs} ${aux_srcs})
	SOURCE_GROUP("${dir}" FILES ${aux_srcs})

	# The corresponding .h files:
	FILE(GLOB aux_hdrs ${CMAKE_SOURCE_DIR}/include/mrpt/${dir}/*.h)
	SOURCE_GROUP("${dir}" FILES ${aux_hdrs})
	SET(mrpt_lib_hdrs ${mrpt_lib_hdrs} ${aux_hdrs})
ENDMACRO(ADD_MRPT_CORE_SOURCES)

# Global files:
AUX_SOURCE_DIRECTORY(. mrpt_lib_srcs)
FILE(GLOB aux_hdrs_1 ${CMAKE_SOURCE_DIR}/include/mrpt/*.h)
SET(mrpt_lib_srcs ${mrpt_lib_srcs} ${aux_hdrs_1})

ADD_MRPT_CORE_SOURCES(bayes)
ADD_MRPT_CORE_SOURCES(compress)
ADD_MRPT_CORE_SOURCES(gui)
ADD_MRPT_CORE_SOURCES(math)
ADD_MRPT_CORE_SOURCES(opengl)
ADD_MRPT_CORE_SOURCES(poses)
ADD_MRPT_CORE_SOURCES(random)
ADD_MRPT_CORE_SOURCES(scan_matching)
ADD_MRPT_CORE_SOURCES(slam)
ADD_MRPT_CORE_SOURCES(synch)
ADD_MRPT_CORE_SOURCES(system)
ADD_MRPT_CORE_SOURCES(utils)
ADD_MRPT_CORE_SOURCES(utils/simpleini)
ADD_MRPT_CORE_SOURCES(utils/xmlparser)
ADD_MRPT_CORE_SOURCES(vision)
ADD_MRPT_CORE_SOURCES(topography)

# Embedded version of libjpeg for (mostly) Windows:
IF (CMAKE_MRPT_HAS_JPEG AND NOT CMAKE_MRPT_HAS_JPEG_SYSTEM)
	ADD_MRPT_CORE_SOURCES(utils/jpeglib)
ENDIF (CMAKE_MRPT_HAS_JPEG AND NOT CMAKE_MRPT_HAS_JPEG_SYSTEM)

# Embedded version of freeglut-2.4.0 for Windows:
IF (WIN32)
	ADD_MRPT_CORE_SOURCES(opengl/freeglut-2.4.0)
ENDIF (WIN32)

# Embedded version of lib3ds: We have it embedded because the API
#  changes between versions and it'd be a mess between Lin/Win builds...
#  even worse since lib3ds does NOT have any VERSION #define...
ADD_MRPT_CORE_SOURCES(opengl/lib3ds)
INCLUDE_DIRECTORIES("${MRPT_SOURCE_DIR}/src/core/opengl/")

# Embedded version of the ANN library:
ADD_MRPT_CORE_SOURCES(slam/ann)
INCLUDE_DIRECTORIES("${MRPT_SOURCE_DIR}/include/mrpt/otherlibs/")


# Embedded version of Glew:
IF(WIN32)
	INCLUDE_DIRECTORIES("${MRPT_SOURCE_DIR}/src/core/opengl/glew/include")
	ADD_MRPT_CORE_SOURCES(opengl/glew/src)
ENDIF(WIN32)
	
# List of .H files:
FILE(GLOB_RECURSE mrpt_lib_hdrs1 ${CMAKE_SOURCE_DIR}/include/doc_files/*.h)
SET(mrpt_lib_hdrs ${mrpt_lib_hdrs} ${mrpt_lib_hdrs1})

# Only if compiling with WXWIDGETS, also add the wxMathPlot library:
# ----------------------------------------------------------------------------------
IF(CMAKE_MRPT_HAS_WXWIDGETS)
	SET(mrpt_lib_srcs ${mrpt_lib_srcs} "${CMAKE_SOURCE_DIR}/otherlibs/mathplot/mathplot.cpp")
	SET(mrpt_lib_hdrs ${mrpt_lib_hdrs} "${CMAKE_SOURCE_DIR}/include/mrpt/otherlibs/mathplot/mathplot.h")
ENDIF(CMAKE_MRPT_HAS_WXWIDGETS)

# Only if compiling with WXWIDGETS, also embed the wxFreeChart library:
# -----------------------------------------------------------------------------
if (0) # JL: not yet... this needs quite a bit of work to be implemented.
IF(CMAKE_MRPT_HAS_WXWIDGETS)
	# The .cpp files:
	FILE(GLOB_RECURSE wxfreechart_srcs ${CMAKE_SOURCE_DIR}/otherlibs/wxFreeChart/*.cpp)
	SET(mrpt_lib_srcs ${mrpt_lib_srcs} ${wxfreechart_srcs})

	# The corresponding .h files:
	FILE(GLOB_RECURSE wxfreechart_hdrs ${CMAKE_SOURCE_DIR}/otherlibs/wxFreeChart/*.h)
	SET(mrpt_lib_hdrs ${mrpt_lib_hdrs} ${wxfreechart_hdrs})
	
	SOURCE_GROUP("wxFreeChart" FILES ${wxfreechart_srcs} ${wxfreechart_hdrs})

	INCLUDE_DIRECTORIES("${MRPT_SOURCE_DIR}/otherlibs/wxFreeChart/include/")	
ENDIF(CMAKE_MRPT_HAS_WXWIDGETS)
endif(0)


# Remove _LIN files when compiling under Windows, and _WIN files when compiling under Linux.
# ----------------------------------------------------------------------------------
SET(aux_var "")
FOREACH(FIL ${mrpt_lib_srcs})
	SET(THIS_COUNTS 1)
	IF(${FIL} MATCHES ".*_LIN.cpp")
		IF(WIN32)
			SET(THIS_COUNTS 0)
		ENDIF(WIN32)
	ENDIF(${FIL} MATCHES ".*_LIN.cpp")

	IF(${FIL} MATCHES ".*_WIN.cpp")
		IF(UNIX)
			SET(THIS_COUNTS 0)
		ENDIF(UNIX)
	ENDIF(${FIL} MATCHES ".*_WIN.cpp")

	IF(THIS_COUNTS)
		SET(aux_var "${aux_var}" "${FIL}")
	ENDIF(THIS_COUNTS)
ENDFOREACH(FIL)
SET(mrpt_lib_srcs ${aux_var})

# ----------------------------------------------------------------------------------
# 				Define the MRPT C++ library target:
# ----------------------------------------------------------------------------------
ADD_LIBRARY(mrpt-core ${mrpt_lib_srcs} ${mrpt_lib_hdrs})

SET_TARGET_PROPERTIES(mrpt-core PROPERTIES PROJECT_LABEL lib-mrpt-core)

# The target file will have "-dbg" for DEBUG:
# ----------------------------------------------------------------------------------
SET(the_trg mrpt-core)

# For dynamic link numbering convenions in Linux:
SET_TARGET_PROPERTIES(${the_trg} PROPERTIES 
	OUTPUT_NAME ${MRPT_LIB_PREFIX}${the_trg}${MRPT_DLL_VERSION_POSTFIX}
	ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/"
	RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/"
	VERSION "${CMAKE_MRPT_VERSION_NUMBER_MAJOR}.${CMAKE_MRPT_VERSION_NUMBER_MINOR}.${CMAKE_MRPT_VERSION_NUMBER_PATCH}"
	SOVERSION ${CMAKE_MRPT_VERSION_NUMBER_MAJOR}.${CMAKE_MRPT_VERSION_NUMBER_MINOR}
	)

# Add the required libraries for linking:
TARGET_LINK_LIBRARIES(mrpt-core ${MRPTLIB_LINKER_LIBS})

# Link against wx libs (for dlls only):
IF(BUILD_SHARED_LIBS)
		TARGET_LINK_LIBRARIES(mrpt-core ${wxWidgets_LIBRARIES})
ENDIF(BUILD_SHARED_LIBS)

# Dependencies of the MRPT library:
#ADD_DEPENDENCIES(mrpt-core mrpt-ann)
ADD_DEPENDENCIES(mrpt-core mrpt-sift-hess)

IF (NOT CMAKE_MRPT_HAS_WXWIDGETS AND CMAKE_MRPT_HAS_ZLIB AND NOT CMAKE_MRPT_HAS_ZLIB_SYSTEM)
	ADD_DEPENDENCIES(mrpt-core mrpt-zlib)
ENDIF (NOT CMAKE_MRPT_HAS_WXWIDGETS AND CMAKE_MRPT_HAS_ZLIB AND NOT CMAKE_MRPT_HAS_ZLIB_SYSTEM)

#IF (CMAKE_MRPT_HAS_JPEG AND NOT CMAKE_MRPT_HAS_JPEG_SYSTEM)
#	ADD_DEPENDENCIES(mrpt-core mrpt-jpeg)
#ENDIF (CMAKE_MRPT_HAS_JPEG AND NOT CMAKE_MRPT_HAS_JPEG_SYSTEM)

# This indicates we are compiling the libmrpt-core project (for internal use of precomp. headers)
ADD_DEFINITIONS(-DMRPT_IS_BUILDING_LIBMRPT_CORE)

# --------------------------------------------
#     Add precompiled headers options
# --------------------------------------------
IF(MRPT_ENABLE_PRECOMPILED_HDRS)
	# --------------------------------------
	# Precompiled hdrs for MSVC:
	# --------------------------------------
	IF (MSVC)
		# Symbol used in precomp_core.h
		ADD_DEFINITIONS(-DMRPT_CORE_USE_PRECOMPILED_HEADER)

		# The "use precomp.headr" for all the files...
		set_target_properties(mrpt-core
			PROPERTIES
			COMPILE_FLAGS "/Yumrpt/precomp_core.h")

		# But for the file "registerAllClasses.cpp", used to build the precomp. header:
		set_source_files_properties("${CMAKE_SOURCE_DIR}/src/core/registerAllClasses.cpp"
			PROPERTIES
			COMPILE_FLAGS "/Ycmrpt/precomp_core.h")

		# ignore precompiled headers in non-mrpt sources:
		FILE(GLOB phdrs_ignore "${MRPT_SOURCE_DIR}/src/core/utils/jpeglib/*.cpp")
		set_source_files_properties(${phdrs_ignore} PROPERTIES COMPILE_FLAGS "/Y- /wd4267")

		FILE(GLOB phdrs_ignore "${MRPT_SOURCE_DIR}/src/core/utils/simpleini/*.c*")
		set_source_files_properties(${phdrs_ignore} PROPERTIES COMPILE_FLAGS "/Y-")

		FILE(GLOB phdrs_ignore "${MRPT_SOURCE_DIR}/src/core/utils/xmlparser/*.cpp")
		set_source_files_properties(${phdrs_ignore} PROPERTIES COMPILE_FLAGS "/Y-")

		FILE(GLOB phdrs_ignore "${MRPT_SOURCE_DIR}/src/core/opengl/lib3ds/*.c")
		set_source_files_properties(${phdrs_ignore} PROPERTIES COMPILE_FLAGS "/Y-") # /wd4244 /wd4267 /wd4311")

		FILE(GLOB phdrs_ignore "${MRPT_SOURCE_DIR}/src/core/slam/ann/*.c*")
		set_source_files_properties(${phdrs_ignore} PROPERTIES COMPILE_FLAGS "/Y-") # /wd4244 /wd4267 /wd4311")
		
		FILE(GLOB phdrs_ignore "${MRPT_SOURCE_DIR}/src/core/opengl/freeglut-2.4.0/*.c")
		set_source_files_properties(${phdrs_ignore} PROPERTIES COMPILE_FLAGS "/Y- /DFREEGLUT_STATIC /D_LIB /wd4244 /wd4267 /wd4311")
		
		FILE(GLOB phdrs_ignore "${MRPT_SOURCE_DIR}/src/core/opengl/glew/src/*.c")
		set_source_files_properties(${phdrs_ignore} PROPERTIES COMPILE_FLAGS "/Y- /DGLEW_STATIC") # /DWIN32_LEAN_AND_MEAN /DVC_EXTRALEAN /DGLEW_MX")

		IF(CMAKE_MRPT_HAS_WXWIDGETS)
			set_source_files_properties("${CMAKE_SOURCE_DIR}/otherlibs/mathplot/mathplot.cpp" PROPERTIES COMPILE_FLAGS "/Y-")
		
			# Y-      : Do not use MRPT precomp. headers
			# _LOCAL_ : Linking locally to mrpt-core (ie. don't building/importing a wx DLL).
			set_source_files_properties(${wxfreechart_srcs} PROPERTIES COMPILE_FLAGS "/Y- /D_LOCAL_")  
		ENDIF(CMAKE_MRPT_HAS_WXWIDGETS)

	ENDIF (MSVC)

ENDIF(MRPT_ENABLE_PRECOMPILED_HDRS)

INSTALL(TARGETS mrpt-core
	RUNTIME DESTINATION ${libmrpt_INSTALL_PREFIX}bin
	LIBRARY DESTINATION ${libmrpt_INSTALL_PREFIX}lib${LIB_SUFFIX}
	ARCHIVE DESTINATION ${libmrpt_INSTALL_PREFIX}lib${LIB_SUFFIX}
    )

