174 lines
5.5 KiB
CMake
174 lines
5.5 KiB
CMake
cmake_minimum_required(VERSION 3.0)
|
|
get_filename_component(project_name ${CMAKE_CURRENT_SOURCE_DIR} NAME)
|
|
project(${project_name})
|
|
|
|
set(EXTENSION "cpp")
|
|
|
|
set(CMAKE_C_STANDARD 99)
|
|
set(CMAKE_CXX_STANDARD 14)
|
|
|
|
set(GEN_BINARY ON)
|
|
set(GEN_LIBRARY ON)
|
|
set(LIB_TYPE STATIC) # NONE, STATIC, SHARED, MODULE
|
|
set(LIBS_TYPE STATIC)
|
|
|
|
set(FLAGS_ANY "-Wall -Wextra -Winline -Wfatal-errors")
|
|
set(FLAGS_DEBUG "-DDEBUG -O0 -pg")
|
|
set(FLAGS_RELEASE "-DNDEBUG -O2")
|
|
|
|
set(SRCDIRS src)
|
|
set(LIBSDIRS )
|
|
set(LIBDIRS src/tsp)
|
|
set(TESTSDIRS celero)
|
|
set(EXAMPLESDIRS bench examples)
|
|
|
|
set(INCLUDE_DIRS "inc")
|
|
set(LIBRARIES "-lpthread -ltbb")
|
|
|
|
set(celero_FLAGS "-fopenmp")
|
|
set(celero_INCLUDE_DIRS "celero")
|
|
set(celero_LIBRARIES "-lpthread -fopenmp")
|
|
|
|
set(USER_LIBRARIES "")
|
|
|
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAGS_ANY}")
|
|
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${FLAGS_ANY} ${FLAGS_DEBUG}")
|
|
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${FLAGS_ANY} ${FLAGS_RELEASE}")
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS_ANY}")
|
|
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${FLAGS_ANY} ${FLAGS_DEBUG}")
|
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${FLAGS_ANY} ${FLAGS_RELEASE}")
|
|
|
|
## Libraries
|
|
if(NOT ${LIB_TYPE} MATCHES "^NONE$")
|
|
# Project library
|
|
if(GEN_LIBRARY)
|
|
set(lib_src "")
|
|
foreach(srcdir ${SRCDIRS})
|
|
set(srcpath ${CMAKE_CURRENT_SOURCE_DIR}/${srcdir})
|
|
file(GLOB_RECURSE tmpsrc ${srcpath}/*.${EXTENSION})
|
|
list(APPEND lib_src ${tmpsrc})
|
|
endforeach()
|
|
|
|
set(lib ${PROJECT_NAME})
|
|
if(lib_src)
|
|
message(STATUS "+ Library: ${lib}")
|
|
add_library(${lib} ${LIB_TYPE} ${lib_src})
|
|
target_include_directories(${lib} PUBLIC ${INCLUDE_DIRS})
|
|
target_link_libraries(${lib} ${LIBRARIES})
|
|
list(APPEND USER_LIBRARIES ${lib})
|
|
else()
|
|
message(WARNING "! Library ${lib}: no sources")
|
|
endif()
|
|
endif()
|
|
endif()
|
|
|
|
## Other libraries
|
|
if(NOT ${LIBS_TYPE} MATCHES "^NONE$")
|
|
foreach(libsdir ${LIBSDIRS})
|
|
set(libspath ${CMAKE_CURRENT_SOURCE_DIR}/${libsdir})
|
|
file(GLOB libs RELATIVE ${libspath} ${libspath}/*)
|
|
if(libs)
|
|
foreach(child ${libs})
|
|
set(lib "")
|
|
if(IS_DIRECTORY ${libspath}/${child})
|
|
set(lib ${child})
|
|
list(APPEND LIBDIRS ${libsdir}/${child})
|
|
else()
|
|
message(WARNING "! Ignoring file: ${libsdir}/${child}")
|
|
endif()
|
|
endforeach()
|
|
endif()
|
|
endforeach()
|
|
|
|
foreach(libdir ${LIBDIRS})
|
|
set(libpath ${CMAKE_CURRENT_SOURCE_DIR}/${libdir})
|
|
file(GLOB_RECURSE lib_src ${libpath}/*.${EXTENSION})
|
|
if(lib_src)
|
|
get_filename_component(lib ${libpath} NAME)
|
|
message(STATUS "+ Library: ${lib}")
|
|
add_library(${lib} ${LIBS_TYPE} ${lib_src})
|
|
target_include_directories(${lib} PUBLIC ${INCLUDE_DIRS})
|
|
target_link_libraries(${lib} ${LIBRARIES})
|
|
list(APPEND USER_LIBRARIES ${lib})
|
|
else()
|
|
message(WARNING "! Library ${lib}: no sources")
|
|
endif()
|
|
endforeach()
|
|
endif()
|
|
|
|
## Binary
|
|
if(GEN_BINARY)
|
|
set(src "")
|
|
foreach(srcdir ${SRCDIRS})
|
|
set(srcpath ${CMAKE_CURRENT_SOURCE_DIR}/${srcdir})
|
|
file(GLOB_RECURSE tmpsrc ${srcpath}/*.${EXTENSION})
|
|
list(APPEND src ${tmpsrc})
|
|
endforeach()
|
|
set(bin ${PROJECT_NAME})
|
|
if(src)
|
|
if(GEN_LIBRARY)
|
|
set(bin ${bin}.bin)
|
|
endif()
|
|
message(STATUS "+ Binary: ${bin}")
|
|
add_executable(${bin} ${src})
|
|
target_include_directories(${bin} PUBLIC ${LIBSDIRS} ${INCLUDE_DIRS})
|
|
target_link_libraries(${bin} ${LIBRARIES} ${USER_LIBRARIES})
|
|
else()
|
|
message(WARNING "! Binary ${bin}: no sources")
|
|
endif()
|
|
endif()
|
|
|
|
## Tests
|
|
foreach(testsdir ${TESTSDIRS})
|
|
set(testspath ${CMAKE_CURRENT_SOURCE_DIR}/${testsdir})
|
|
file(GLOB_RECURSE tests_src ${testspath}/*.${EXTENSION})
|
|
if(tests_src)
|
|
set(tests ${testsdir}_${PROJECT_NAME})
|
|
message(STATUS "+ Tests: ${tests}")
|
|
add_executable(${tests} ${tests_src})
|
|
target_compile_options(${tests} PUBLIC ${${testsdir}_FLAGS})
|
|
target_include_directories(${tests} PUBLIC ${SRCDIRS} ${LIBSDIRS} ${INCLUDE_DIRS} ${${testsdir}_INCLUDE_DIRS})
|
|
target_link_libraries(${tests} ${LIBRARIES} ${USER_LIBRARIES} ${${testsdir}_LIBRARIES})
|
|
endif()
|
|
endforeach()
|
|
|
|
## Examples
|
|
foreach(examplesdir ${EXAMPLESDIRS})
|
|
set(examplespath ${CMAKE_CURRENT_SOURCE_DIR}/${examplesdir})
|
|
file(GLOB examples RELATIVE ${examplespath} ${examplespath}/*)
|
|
if(examples)
|
|
foreach(child ${examples})
|
|
set(example_bin_filename "")
|
|
set(example "")
|
|
if(IS_DIRECTORY ${examplespath}/${child})
|
|
set(example_bin_filename ${child})
|
|
set(example ${examplesdir}_${example_bin_filename})
|
|
file(GLOB_RECURSE example_src ${examplespath}/${child}/*.${EXTENSION})
|
|
else()
|
|
get_filename_component(extension ${child} EXT)
|
|
if(${extension} MATCHES "^.${EXTENSION}$")
|
|
get_filename_component(example_name ${child} NAME_WE)
|
|
set(example_bin_filename ${example_name})
|
|
set(example ${examplesdir}_${example_bin_filename})
|
|
set(example_src ${examplespath}/${child})
|
|
endif()
|
|
endif()
|
|
if(example)
|
|
if(example_src)
|
|
message(STATUS "+ Example: ${examplesdir}/${example}")
|
|
add_executable(${example} ${example_src})
|
|
target_include_directories(${example} PUBLIC ${SRCDIRS} ${LIBSDIRS} ${INCLUDE_DIRS})
|
|
target_link_libraries(${example} ${LIBRARIES} ${USER_LIBRARIES})
|
|
set_target_properties(${example} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${examplesdir})
|
|
set_target_properties(${example} PROPERTIES OUTPUT_NAME ${example_bin_filename})
|
|
else()
|
|
message(WARNING "! Example ${example}: no sources")
|
|
endif()
|
|
endif()
|
|
endforeach()
|
|
endif()
|
|
endforeach()
|