Files
cs3460-cpp/Hw3/CMakeLists.txt
2019-09-27 23:21:56 -07:00

96 lines
3.1 KiB
CMake

cmake_minimum_required(VERSION 3.14)
project(Hw3)
set(CMAKE_CXX_STANDARD 17)
set(HEADER_FILES
distributions.hpp)
set(SOURCE_FILES
distributions.cpp)
set(UNIT_TEST_FILES
TestDistributions.cpp)
#
# This is the main target
#
add_executable(RandomDistributions ${HEADER_FILES} ${SOURCE_FILES} main.cpp)
add_executable(UnitTestRunner ${HEADER_FILES} ${SOURCE_FILES} ${UNIT_TEST_FILES})
#
# We want the C++ 17 standard for our project
#
set_property(TARGET RandomDistributions PROPERTY CXX_STANDARD 17)
set_property(TARGET UnitTestRunner PROPERTY CXX_STANDARD 17)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
target_compile_options(GoogleTestIntro PRIVATE /W4 /permissive-)
target_compile_options(UnitTestRunner PRIVATE /W4 /permissive-)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(RandomDistributions PRIVATE -Wall -Wextra -pedantic)
target_compile_options(UnitTestRunner PRIVATE -Wall -Wextra -pedantic)
endif()
#
# Prepare a pre-build step to run clang-format over all the [ch]pp source files.
# Start by finding the location of the clang-format executable.
#
find_program(CLANG_FORMAT "clang-format")
if (CLANG_FORMAT)
#
# Need to take the simple source file locations used for the project and get their full
# file system locations for use in putting together the clang-format command line
#
unset(SOURCE_FILES_PATHS)
foreach(SOURCE_FILE ${HEADER_FILES} ${SOURCE_FILES} ${UNIT_TEST_FILES} main.cpp)
get_source_file_property(WHERE ${SOURCE_FILE} LOCATION)
set(SOURCE_FILES_PATHS ${SOURCE_FILES_PATHS} ${WHERE})
endforeach()
#
# This creates the clang-format target/command
#
add_custom_target(
ClangFormat
COMMAND ${CLANG_FORMAT}
-i
-style=file
${SOURCE_FILES_PATHS}
)
#
# This makes the clang-format target a dependency of the main GoogleTestIntro project
#
add_dependencies(RandomDistributions ClangFormat)
else()
message("Unable to find clang-format")
endif()
#
# Add GoogleTest
#
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# Now simply link against gtest or gtest_main as needed. Eg
target_link_libraries(UnitTestRunner gtest_main)