-
Notifications
You must be signed in to change notification settings - Fork 149
/
CMakeLists.txt
143 lines (119 loc) · 4.18 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
include(./.env.cmake OPTIONAL RESULT_VARIABLE LOCAL_ENV)
message(STATUS "Local .env.cmake: ${LOCAL_ENV}")
cmake_minimum_required(VERSION 3.11.0)
set(NAME LveEngine)
message(STATUS "using ${CMAKE_GENERATOR}")
if (CMAKE_GENERATOR STREQUAL "MinGW Makefiles")
if (NOT MINGW_PATH)
message(FATAL_ERROR "MINGW_PATH not set in .env.cmake")
endif()
set(USE_MINGW "True")
set(CMAKE_C_COMPILER ${MINGW_PATH}/bin/gcc.exe)
set(CMAKE_CXX_COMPILER ${MINGW_PATH}/bin/g++.exe)
endif()
project(${NAME} VERSION 0.23.0)
# 1. Set VULKAN_SDK_PATH in .env.cmake to target specific vulkan version
if (DEFINED VULKAN_SDK_PATH)
set(Vulkan_INCLUDE_DIRS "${VULKAN_SDK_PATH}/Include") # 1.1 Make sure this include path is correct
set(Vulkan_LIBRARIES "${VULKAN_SDK_PATH}/Lib") # 1.2 Make sure lib path is correct
set(Vulkan_FOUND "True")
else()
find_package(Vulkan REQUIRED) # throws error if could not find Vulkan
message(STATUS "Found Vulkan: $ENV{VULKAN_SDK}")
endif()
if (NOT Vulkan_FOUND)
message(FATAL_ERROR "Could not find Vulkan library!")
else()
message(STATUS "Using vulkan lib at: ${Vulkan_LIBRARIES}")
endif()
# 2. Set GLFW_PATH in .env.cmake to target specific glfw
if (DEFINED GLFW_PATH)
message(STATUS "Using GLFW path specified in .env")
set(GLFW_INCLUDE_DIRS "${GLFW_PATH}/include")
if (MSVC)
set(GLFW_LIB "${GLFW_PATH}/lib-vc2019") # 2.1 Update lib-vc2019 to use same version as your visual studio
elseif (CMAKE_GENERATOR STREQUAL "MinGW Makefiles")
message(STATUS "USING MINGW")
set(GLFW_LIB "${GLFW_PATH}/lib-mingw-w64") # 2.1 make sure matches glfw mingw subdirectory
endif()
else()
find_package(glfw3 3.3 REQUIRED)
set(GLFW_LIB glfw)
message(STATUS "Found GLFW")
endif()
if (NOT GLFW_LIB)
message(FATAL_ERROR "Could not find glfw library!")
else()
message(STATUS "Using glfw lib at: ${GLFW_LIB}")
endif()
include_directories(external)
# If TINYOBJ_PATH not specified in .env.cmake, try fetching from git repo
if (NOT TINYOBJ_PATH)
message(STATUS "TINYOBJ_PATH not specified in .env.cmake, using external/tinyobjloader")
set(TINYOBJ_PATH external/tinyobjloader)
endif()
file(GLOB_RECURSE SOURCES ${PROJECT_SOURCE_DIR}/src/*.cpp)
add_executable(${PROJECT_NAME} ${SOURCES})
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/build")
if (WIN32)
message(STATUS "CREATING BUILD FOR WINDOWS")
if (USE_MINGW)
target_include_directories(${PROJECT_NAME} PUBLIC
${MINGW_PATH}/include
)
target_link_directories(${PROJECT_NAME} PUBLIC
${MINGW_PATH}/lib
)
endif()
target_include_directories(${PROJECT_NAME} PUBLIC
${PROJECT_SOURCE_DIR}/src
${Vulkan_INCLUDE_DIRS}
${TINYOBJ_PATH}
${GLFW_INCLUDE_DIRS}
${GLM_PATH}
)
target_link_directories(${PROJECT_NAME} PUBLIC
${Vulkan_LIBRARIES}
${GLFW_LIB}
)
target_link_libraries(${PROJECT_NAME} glfw3 vulkan-1)
elseif (UNIX)
message(STATUS "CREATING BUILD FOR UNIX")
target_include_directories(${PROJECT_NAME} PUBLIC
${PROJECT_SOURCE_DIR}/src
${TINYOBJ_PATH}
)
target_link_libraries(${PROJECT_NAME} glfw ${Vulkan_LIBRARIES})
endif()
############## Build SHADERS #######################
# Find all vertex and fragment sources within shaders directory
# taken from VBlancos vulkan tutorial
# https://github.com/vblanco20-1/vulkan-guide/blob/all-chapters/CMakeLists.txt
find_program(GLSL_VALIDATOR glslangValidator HINTS
${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}
/usr/bin
/usr/local/bin
${VULKAN_SDK_PATH}/Bin
${VULKAN_SDK_PATH}/Bin32
$ENV{VULKAN_SDK}/Bin/
$ENV{VULKAN_SDK}/Bin32/
)
# get all .vert and .frag files in shaders directory
file(GLOB_RECURSE GLSL_SOURCE_FILES
"${PROJECT_SOURCE_DIR}/shaders/*.frag"
"${PROJECT_SOURCE_DIR}/shaders/*.vert"
)
foreach(GLSL ${GLSL_SOURCE_FILES})
get_filename_component(FILE_NAME ${GLSL} NAME)
set(SPIRV "${PROJECT_SOURCE_DIR}/shaders/${FILE_NAME}.spv")
add_custom_command(
OUTPUT ${SPIRV}
COMMAND ${GLSL_VALIDATOR} -V ${GLSL} -o ${SPIRV}
DEPENDS ${GLSL})
list(APPEND SPIRV_BINARY_FILES ${SPIRV})
endforeach(GLSL)
add_custom_target(
Shaders
DEPENDS ${SPIRV_BINARY_FILES}
)