#
# author: 
# Feb 27 2014
#
# CMakeLists used to generate the executable in charge of automatically
# generating the new versions of 'ControllersStruct.h/.c' and 'user_sf_IO.h/.c'
# when 'control_variables.txt' or 'simu_variables.txt' are modified
#

# CMake minimum version
cmake_minimum_required(VERSION 2.8.7)

# project name
project (GEN_MDS_USER)

# message to display the project name
message(STATUS "Processing ${PROJECT_NAME}")

# use Release version and not the Debug one (faster)
set(CMAKE_BUILD_TYPE Debug)

# for Unix: display all the warnings, except the ones related to /* -- */
if (UNIX)
  set(CMAKE_C_FLAGS "-g -Wall -Wno-comment -Wno-unused-variable")
endif ( )

set(ROBOTRAN_DIR_COMMON "${PROJECT_SOURCE_DIR}/../../")
set(ROBOTRAN_SOURCE_DIR ${ROBOTRAN_DIR_COMMON}) 

# define the location of all the 'Find<module>.cmake' files
set(CMAKE_MODULE_PATH ${ROBOTRAN_SOURCE_DIR}/conf)

# add additional CMakeLists.txt files
set(CMAKE_AUX ${ROBOTRAN_SOURCE_DIR}/cmake_aux)
set(CMAKE_AUX_BIN ${PROJECT_BINARY_DIR}/cmake_aux)
add_subdirectory (${CMAKE_AUX}/libraries/ ${CMAKE_AUX_BIN}/libraries/)



if (WIN32)

## --- WIN32 or WIN64 DETECTION --- ##
	if( CMAKE_SIZEOF_VOID_P EQUAL 8 )   # 64bit Windows
		SET(WIN_LIB_DIRECTORY win64_include_lib)
	else( )  # 32bit Windows
		SET(WIN_LIB_DIRECTORY win32_include_lib)
	endif( )

## ---- WINDOWS DLL FILES ---- ##

# copy all the dll (except 'jvm.dll') used for Windows
# these dll files are copied in the Executable directory (Debug or Release)
    file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/ DESTINATION ${CMAKE_BINARY_DIR}/Debug)
    file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/ DESTINATION ${CMAKE_BINARY_DIR}/Release)
	
endif ( )


# Libxml2
xml_lib(gen_user)

# function to list all source files in the src/ directory, 
# recursing into sub-directories
function(list_gen_source_files)
    file(GLOB_RECURSE SOURCE_FILES_TMP 
		"${PROJECT_SOURCE_DIR}/src/*.c" 
		"${ROBOTRAN_SOURCE_DIR}/mbs_common/mbs_load_xml/mbs_xml_reader.c"
		"${ROBOTRAN_SOURCE_DIR}/mbs_common/mbs_utilities/useful_functions.c")
    set(SOURCE_FILES ${SOURCE_FILES_TMP} PARENT_SCOPE)
endfunction()

# function to list all paths to header files in the src/ directory, 
# recursing into sub-directories (includes)
function(list_gen_include_directories)
    file(GLOB_RECURSE FULL_INCLUDE_DIRECTORIES_TMP RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} 
		"${PROJECT_SOURCE_DIR}/src/*.h"
		"${ROBOTRAN_SOURCE_DIR}/mbs_common/mbs_load_xml/mbs_xml_reader.h"
		"${ROBOTRAN_SOURCE_DIR}/mbs_common/mbs_utilities/useful_functions.h"
                "${ROBOTRAN_SOURCE_DIR}/mbs_common/mbs_struct/mbs_data.h"
		)
    set(DIR_LIST "")
    foreach(FILE_PATH ${FULL_INCLUDE_DIRECTORIES_TMP})
        get_filename_component(DIR_PATH ${FILE_PATH} PATH)
        set(DIR_LIST ${DIR_LIST} ${DIR_PATH})
    endforeach(FILE_PATH)
    list(REMOVE_DUPLICATES DIR_LIST) 
    set(FULL_INCLUDE_DIRECTORIES ${DIR_LIST} PARENT_SCOPE)
endfunction()
 
# list source files to compile
list_gen_source_files(SOURCE_FILES)

# list include directories (to find headers)
list_gen_include_directories(FULL_INCLUDE_DIRECTORIES)

# include these directories
include_directories (${FULL_INCLUDE_DIRECTORIES})

# name of the executable: 'Gen_mds_user'
set (Executable Gen_mds_user)

# generate the executable
add_executable (${Executable} ${SOURCE_FILES})

target_link_libraries (${Executable} ${LIBXML2_LIBRARIES})


# math external library (for Unix): linkage
if (UNIX)
	target_link_libraries (${Executable} m)
endif ( )
