-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
140 lines (119 loc) · 5.16 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
cmake_minimum_required(VERSION 3.6)
# Fix behavior of CMAKE_CXX_STANDARD when targeting macOS.
if(POLICY CMP0025)
cmake_policy(SET CMP0025 NEW)
endif()
project(Theodore LANGUAGES CXX VERSION 0.1.0)
message(STATUS "CMake version: ${CMAKE_VERSION}")
# for old cmake version compatible
macro(use_cxx11)
if(CMAKE_VERSION VERSION_LESS "3.1")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
endif()
else()
set (CMAKE_CXX_STANDARD 11)
endif()
endmacro(use_cxx11)
use_cxx11()
set(CMAKE_WARN_DEPRECATED 0)
set(GLEW_INCLUDE_DIRS "third_party/include/")
set(SOURCE_PREFIX "src")
set(THEODORE_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/${SOURCE_PREFIX}")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(GLEW REQUIRED)
# MacOS lib settings
if(APPLE)
message(STATUS "Platform detected: Macos")
find_library(COCOA_LIBRARY Cocoa REQUIRED)
find_library(OPENGL_LIBRARY OpenGL REQUIRED)
set(EXTRA_LIBS ${COCOA_LIBRARY} ${OPENGL_LIBRARY} GLEW::GLEW)
set(GLEW_LIBRARIES_DIRS "third_party/lib/Mac/")
endif(APPLE)
# Linux lib settings
if(LINUX)
message(STATUS "Platform detected: Linux")
# include_directories("/usr/X11R6/include/")
# link_directories("/usr/X11R6/lib")
find_library(X11_LIBRARY X11 REQUIRED)
find_library(OPENGL_LIBRARY GL REQUIRED)
set(EXTRA_LIBS ${OPENGL_LIBRARY} ${X11_LIBRARY} GLEW::GLEW)
set(GLEW_LIBRARIES_DIRS "third_party/lib/Linux/")
endif(LINUX)
# Windows lib settings
if(WIN32)
message(STATUS "Platform detected: Windows")
find_package(OpenGL REQUIRED)
find_package(WindowsSDK REQUIRED)
set(EXTRA_LIBS opengl32)
# detect cpu architecture
math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}")
message(STATUS "Detected cpu archetecture: ${BITS}bits")
set(GLEW_LIBRARIES_DIRS "third_party/lib/Win${BITS}/")
link_libraries(glew32.lib)
endif(WIN32)
message(STATUS "Library linking: GLEW linking")
include_directories(${GLEW_INCLUDE_DIRS})
link_directories(${GLEW_LIBRARIES_DIRS})
# release build path
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_SOURCE_DIR}/release")
# resource path
set(THEODORE_RESOURCE_DIR "res")
set(THEODORE_SHADER_DIR "shaders")
configure_file("${CMAKE_SOURCE_DIR}/cmake/RootDir.h.in" "${THEODORE_INCLUDE_DIR}/RootDir.h")
file(GLOB_RECURSE SOURCE_FILES ${SOURCE_PREFIX}/*.h ${SOURCE_PREFIX}/*.cpp ${SOURCE_PREFIX}/*.hpp ${SOURCE_PREFIX}/*.mm)
add_library(${CMAKE_PROJECT_NAME} STATIC ${SOURCE_FILES})
target_link_libraries(${CMAKE_PROJECT_NAME} ${EXTRA_LIBS})
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${THEODORE_INCLUDE_DIR})
# Create virtual folders to make it look nicer in VS
if(MSVC_IDE)
# Macro to preserve source files hierarchy in the IDE
macro(GroupSources curdir)
file(GLOB children RELATIVE ${PROJECT_SOURCE_DIR}/${curdir} ${PROJECT_SOURCE_DIR}/${curdir}/*)
foreach(child ${children})
if(IS_DIRECTORY ${PROJECT_SOURCE_DIR}/${curdir}/${child})
GroupSources(${curdir}/${child})
else()
string(REPLACE "/" "\\" groupname ${curdir})
string(REPLACE "src" "Sources" groupname ${groupname})
source_group(${groupname} FILES ${PROJECT_SOURCE_DIR}/${curdir}/${child})
endif()
endforeach()
endmacro()
# Run macro
GroupSources(src)
# disable warning
add_definitions(/wd4267) #remove size_t to int warning
add_definitions(/wd4244) #remove double to float conversion warning
add_definitions(/wd4305) #remove double to float truncation warning
endif()
# make sample
function(add_sample TARGET ENTRYPOINT)
source_group("" FILES ${ENTRYPOINT})
add_executable(${TARGET} ${ENTRYPOINT})
target_link_libraries(${TARGET} ${CMAKE_PROJECT_NAME})
endfunction()
set(PROJECT_SAMPLE_DIR "${CMAKE_SOURCE_DIR}/sample")
add_sample(01-hello_world "${PROJECT_SAMPLE_DIR}/01-hello_world/main.cpp")
add_sample(02-triangle "${PROJECT_SAMPLE_DIR}/02-triangle/main.cpp")
add_sample(03-camera "${PROJECT_SAMPLE_DIR}/03-camera/main.cpp")
add_sample(04-sprite "${PROJECT_SAMPLE_DIR}/04-sprite/main.cpp")
add_sample(05-font_render "${PROJECT_SAMPLE_DIR}/05-font_render/main.cpp")
add_sample(06-cubemap "${PROJECT_SAMPLE_DIR}/06-cubemap/main.cpp")
add_sample(07-mesh_render "${PROJECT_SAMPLE_DIR}/07-mesh_render/main.cpp")
add_sample(08-transform "${PROJECT_SAMPLE_DIR}/08-transform/main.cpp")
add_sample(09-light "${PROJECT_SAMPLE_DIR}/09-light/main.cpp")
add_sample(10-collider "${PROJECT_SAMPLE_DIR}/10-collider/main.cpp")
add_sample(11-ray_casting "${PROJECT_SAMPLE_DIR}/11-ray_casting/main.cpp")
add_sample(12-tween "${PROJECT_SAMPLE_DIR}/12-tween/main.cpp")
# unit test target
find_package(Catch2 REQUIRED)
set(PROJECT_UNIT_TEST_NAME "${CMAKE_PROJECT_NAME}_unit_test")
set(PROJECT_TEST_DIR "${CMAKE_SOURCE_DIR}/test")
file(GLOB_RECURSE TEST_SOURCE_FILES "${PROJECT_TEST_DIR}/*.cpp")
add_executable(${PROJECT_UNIT_TEST_NAME} ${TEST_SOURCE_FILES})
target_link_libraries(${PROJECT_UNIT_TEST_NAME} Catch2::Catch2 ${CMAKE_PROJECT_NAME})
include(CTest)
include(Catch)
catch_discover_tests(${PROJECT_UNIT_TEST_NAME})