forked from vkoskiv/c-ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
117 lines (99 loc) · 4.19 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
cmake_minimum_required (VERSION 3.9)
project (c-ray C)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin/)
if (DEFINED ENV{CRAY_EXPORT_CMDS})
message(STATUS "Exporting compile commands")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
endif()
message(STATUS "The C compiler is: ${CMAKE_C_COMPILER}")
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
if (MSVC)
add_definitions(-DWINDOWS)
set(CMAKE_CONFIGURATION_TYPES Release Debug CACHE STRING "" FORCE)
# Silence "deprecation" warnings
add_definitions(/D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_DEPRECATE /D_SCL_SECURE_NO_WARNINGS)
# Avoid non-compliant standard library extensions
add_definitions(/D_CRT_DECLARE_NONSTDC_NAMES)
# Avoid windows.h junk
add_definitions(/DNOMINMAX)
# Avoid windows.h from including some usually unused libs like winsocks.h, since this might cause some redefinition errors.
add_definitions(/DWIN32_LEAN_AND_MEAN)
set(CMAKE_C_FLAGS "/W3 /MP /Zi /Zo /permissive-" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} /EHsc /std:c++latest /Zc:throwingNew,inline" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_DEBUG "/Od /MDd" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELEASE "/O2 /GS- /MD" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG /MANIFEST:NO" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/DEBUG /MANIFEST:NO /INCREMENTAL:NO /OPT:REF,ICF" CACHE STRING "" FORCE)
else()
# set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wconversion -std=gnu99")
set(CMAKE_C_FLAGS "-Wall -Wextra -Wno-missing-field-initializers -std=gnu99 -march=native -mtune=native -D_GNU_SOURCE")
set(CMAKE_C_FLAGS_RELEASE "-O2 -ftree-vectorize")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g")
if (ASAN)
unset(UBSAN CACHE)
unset(ASAN CACHE)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address")
message(STATUS "Address sanitizer enabled")
elseif(UBSAN)
unset(ASAN CACHE)
unset(UBSAN CACHE)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=undefined")
message(STATUS "Undefined behaviour sanitizer enabled")
else()
message(STATUS "No sanitizations")
endif()
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DCRAY_DEBUG_ENABLED")
endif()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${c-ray_SOURCE_DIR}/cmake")
file(GLOB_RECURSE SOURCES src/*.c)
include(GetGitRevisionDescription)
get_git_head_revision(GIT_REFSPEC GIT_SHA1)
configure_file("${c-ray_SOURCE_DIR}/src/utils/gitsha1.c.in" "${c-ray_BINARY_DIR}/generated/gitsha1.c" @ONLY)
list(APPEND SOURCES "${c-ray_BINARY_DIR}/generated/gitsha1.c")
add_executable(c-ray ${SOURCES})
target_include_directories(c-ray PRIVATE ${c-ray_SOURCE_DIR}/src)
target_include_directories(c-ray PRIVATE ${c-ray_SOURCE_DIR}/tests)
if (NOT MSVC)
target_link_libraries(c-ray PRIVATE -lpthread -lm)
endif ()
include(CheckIPOSupported)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
check_ipo_supported(RESULT ipo_supported OUTPUT ipo_errors)
if (ipo_supported)
message(STATUS "Enabling IPO")
set_property(TARGET c-ray PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(WARNING "IPO is not supported by this compiler. Everything will work, just a bit slower for now!")
endif()
endif()
if (NO_IPO)
message(STATUS "Explicitly disabling IPO")
set_property(TARGET c-ray PROPERTY INTERPROCEDURAL_OPTIMIZATION FALSE)
unset(NO_IPO CACHE)
endif()
if (NO_LOGO)
target_compile_definitions(c-ray PRIVATE -DNO_LOGO)
endif()
if (NOT NO_SDL2)
find_package(SDL2 QUIET)
if (SDL2_FOUND)
message(STATUS "SDL2 found! Enabling UI components.")
target_link_libraries(c-ray PRIVATE ${SDL2_LIBRARY})
target_include_directories(c-ray PRIVATE ${SDL2_INCLUDE_DIR})
target_compile_definitions(c-ray PRIVATE -DCRAY_SDL_ENABLED)
else()
message(STATUS "SDL2 not found! You should install it, it'll be awesome! See README for instructions.")
endif()
else()
message(STATUS "SDL2 explicitly disabled, disabling UI components")
unset(NO_SDL2 CACHE)
endif()
if (TESTING)
message(STATUS "Enabling test suite.")
target_compile_definitions(c-ray PRIVATE -DCRAY_TESTING)
endif()