-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
145 lines (117 loc) · 5.31 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
cmake_minimum_required(VERSION 3.10)
# Assignment Information (these are the *only* things you need to change here between assignments)
set(assignment_name "wikipedia-graph") # Name of the assignment
set(assignment_version 1.0) # Version, where minor=semester_year, patch=semester_end_month, tweak=revision
set(assignment_entrypoints "main") # Entrypoints to run the program
set(assignment_clean_rm "") # Generated files that should be removed with "make clean"
set(assignment_container "fa22") # Container we are targetting
# Add color support to our messages.
# See: https://stackoverflow.com/questions/18968979/how-to-get-colorized-output-with-cmake
if(NOT WIN32)
string(ASCII 27 Esc)
set(ColorReset "${Esc}[m")
set(ColorBold "${Esc}[1m")
set(ColorRed "${Esc}[31m")
set(ColorGreen "${Esc}[32m")
set(ColorYellow "${Esc}[33m")
set(ColorBlue "${Esc}[34m")
set(ColorMagenta "${Esc}[35m")
set(ColorCyan "${Esc}[36m")
set(ColorWhite "${Esc}[37m")
endif()
site_name(SITE_NAME)
if(SITE_NAME MATCHES "ews.illinois.edu$")
set(EWS TRUE)
endif()
# Handle unsupported container warnings.
# Default to remind students to use our container, unless they are using our targeted container.
set(REMIND_CONTAINER TRUE)
if(DEFINED ENV{CS225_CONTAINER})
if($ENV{CS225_CONTAINER} STREQUAL ${assignment_container})
# This is the container we are targetting, so don't print the reminder.
unset(REMIND_CONTAINER)
elseif($ENV{CS225_CONTAINER} MATCHES ".+-arm")
message(STATUS "${ColorYellow}Looks like you are using an ARM/M1 container.${ColorReset}")
else()
message(STATUS "${ColorYellow}Looks like you are using an unrecognized container.${ColorReset}")
endif()
elseif(DEFINED EWS)
message(STATUS "${ColorYellow}Looks like you are using EWS.${ColorReset}")
message(STATUS "${ColorRed}EWS is outdated and unsupported. Use at your own risk.${ColorReset}")
else()
# Warn students who are not using our container at all.
message(STATUS "${ColorYellow}Looks like you are not using the provided container.${ColorReset}")
endif()
# Remind students to use our container.
if(DEFINED REMIND_CONTAINER)
message(WARNING "${ColorYellow}Make sure you test on the CS225 container before the deadline.${ColorReset}")
endif()
# Disable in-source builds (running "cmake ." in the project directory)
# This is bad practice as it pollutes the project directory.
# See: https://dpiepgrass.medium.com/cmake-fights-against-disabling-in-source-builds-ab1d71c1d26f
if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
message(FATAL_ERROR "${ColorRed}In-source builds are disabled.\n"
"Create a subdirectory `build/` and use `cmake ..` inside it.\n"
"${ColorBold}Delete `CMakeCache.txt` and `CMakeFiles/` before you continue.${ColorReset}")
endif()
# Specify C++ compiler and linker.
if(NOT DEFINED EWS)
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_LINKER "/usr/bin/clang++")
else()
set(CMAKE_C_COMPILER "/software/llvm-6.0.1/bin/clang")
set(CMAKE_CXX_COMPILER "/software/llvm-6.0.1/bin/clang++")
set(CMAKE_LINKER "/software/llvm-6.0.1/bin/clang++")
# Add other required flags for EWS.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
endif()
# Specify C++ Standard.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# Define project.
project(${assignment_name} VERSION ${assignment_version})
# Specify Debug build.
set(CMAKE_BUILD_TYPE Debug)
# Specify warnings for all builds.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Werror -Wfatal-errors -Wextra -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function")
# Specify debug symbols and no optimizations for Debug builds.
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG -O0 -gdwarf-4")
# Specify additional clean files.
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES "${assignment_clean_rm}")
# Add Catch2.
# Note: Ubuntu 20.04 LTS does not have Catch2 on apt
# See: https://github.com/catchorg/Catch2/issues/1383
if(NOT DEFINED EWS)
find_package(Catch2 REQUIRED)
else()
include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.0.0-preview3
)
FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
endif()
# Path definitions.
set(lib_dir ${CMAKE_SOURCE_DIR}/lib)
set(src_dir ${CMAKE_SOURCE_DIR}/src)
set(tests_dir ${CMAKE_SOURCE_DIR}/tests)
set(entry_dir ${CMAKE_SOURCE_DIR}/entry)
# Run CMakeLists in lib_dir to build our required libraries.
add_subdirectory(${lib_dir})
# Run CMakeLists in src_dir to build the student's code.
add_subdirectory(${src_dir})
# Add the Catch2 entrypoint using Catch2, our libs and src code.
file(GLOB_RECURSE tests_src CONFIGURE_DEPENDS ${tests_dir}/*.cpp)
include(Catch)
add_executable(test ${tests_src})
target_link_libraries(test PRIVATE Catch2::Catch2WithMain libs src)
catch_discover_tests(test)
# Add the assignment entrypoints using our libs and src code.
foreach(entrypoint IN LISTS assignment_entrypoints)
add_executable(${entrypoint} ${entry_dir}/${entrypoint}.cpp)
target_link_libraries(${entrypoint} PRIVATE libs src)
endforeach()