cmake_minimum_required(VERSION 2.8)
project(wfr-unittests C CXX)

# Compiler flags
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g3 -DNDEBUG -Wall")
set(CMAKE_C_FLAGS_DEBUG "-O0 -g3 -DNDEBUG -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall")
set(CMAKE_C_FLAGS_RELEASE "-O3 -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_DEBUG}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS_DEBUG}")

# Find WFR-libraries
if (NOT WFR_FOUND)
	if (NOT WFR_DIR)
		set(WFR_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../WFR-lib/dist")
	endif()
	find_package(WFR REQUIRED)
endif()

# Find IWIR2WFR compiler
if (NOT IWIR2WFR_FOUND)
	if (NOT IWIR2WFR_DIR)
		set(IWIR2WFR_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../IWIR2WFRcompiler/dist")
	endif()
	find_package(IWIR2WFR REQUIRED)
endif()

# Where to find IWIR file and activity implementation repository
set(IWIR_DIR "${CMAKE_CURRENT_SOURCE_DIR}/iwir")
set(REPO_DIR "${CMAKE_CURRENT_SOURCE_DIR}/iwir/coderepo")

get_directory_property(hasParent PARENT_DIRECTORY)
# Where to install the output
if (NOT hasParent)
	if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
		set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/dist")
	endif()
endif()

# Setup include dir
include_directories(${WFR_INCLUDE_DIR})

# Wavefront needs OpenMP
FIND_PACKAGE(OpenMP)
if(OPENMP_FOUND)
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

# Enable C++11 support and ignore the Sparselu BOTS main
ADD_DEFINITIONS( -std=c++11 -DSPARSELU_SKIP_BOTS_MAIN )

#
# Convenience function for setting up individual unit-tests
#
function(add_wfr_utest utest_name add_inc)
	file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${utest_name})
	add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${utest_name}/${utest_name}.cpp ${CMAKE_CURRENT_BINARY_DIR}/${utest_name}/${utest_name}.h
		COMMAND ${IWIR2WFR_EXE} -i ${IWIR_DIR}/${utest_name}.iwir -o ${CMAKE_CURRENT_BINARY_DIR}/${utest_name}/${utest_name} -printir -repoconfig ${REPO_DIR}/${utest_name}.properties
		DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${utest_name}
	)
	add_executable(${utest_name}.bin ${CMAKE_CURRENT_BINARY_DIR}/${utest_name}/${utest_name}.cpp ${ARGN})
	target_include_directories(${utest_name}.bin PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${utest_name} ${add_inc})
	target_link_libraries(${utest_name}.bin ${WFR_LIBRARIES})
	add_dependencies(${utest_name}.bin wfr_lib) # Otherwise we would have race conditions when compiling the parent project with -j
	install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${utest_name}/${utest_name}.cpp ${CMAKE_CURRENT_BINARY_DIR}/${utest_name}/${utest_name}.h DESTINATION ${CMAKE_INSTALL_PREFIX}/utests/${utest_name})
	install(TARGETS ${utest_name}.bin DESTINATION ${CMAKE_INSTALL_PREFIX}/utests/${utest_name})
endfunction()


#
# Setup individual unit-tests
#
add_wfr_utest(for1 "")
add_wfr_utest(foreach1 "")
add_wfr_utest(if1 "")
#add_wfr_utest(if01 "")
add_wfr_utest(if2 "")
add_wfr_utest(pfor1 "")
add_wfr_utest(pforeach1 "")
add_wfr_utest(sparselu-wfr ${CMAKE_CURRENT_SOURCE_DIR}/src/sparselu ${CMAKE_CURRENT_SOURCE_DIR}/src/sparselu/sparselu_funcs.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/sparselu/bots_main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/sparselu/bots_common.cpp)
if(OPENMP_FOUND)
	add_wfr_utest(wavefront "")
endif()
add_wfr_utest(while1 "")

install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/bin/unittests.sh PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE DESTINATION ${CMAKE_INSTALL_PREFIX}/utests)

