-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
68 lines (55 loc) · 2.64 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
cmake_minimum_required(VERSION 3.15)
set(OPENGL_EXAMPLE_PROJECT_NAME opengl_example)
project(${OPENGL_EXAMPLE_PROJECT_NAME})
#set(CMAKE_CXX_COMPILER /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++)
#set(CMAKE_CXX_COMPILER c++)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
set(OPENGL_EXAMPLE_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
set(OPENGL_EXAMPLE_LIBRARIES_DIR ${PROJECT_SOURCE_DIR}/libs)
set(OPENGL_EXAMPLE_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(OPENGL_EXAMPLE_EXCERCISE_DIR ${PROJECT_SOURCE_DIR}/test)
set(OPENGL_EXAMPLE_EXAMPLES_DIR ${PROJECT_SOURCE_DIR}/examples)
# 设置源码编译依赖库
set(OPENGL_EXAMPLE_LIBRARIES glfw assimp soil)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(INJECT_DEBUG_POSTFIX OFF CACHE BOOL "" FORCE)
set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(ASSIMP_INSTALL OFF CACHE BOOL "" FORCE)
option(STB_IMAGE_IMPLEMENTATION ON)
# 从源码编译依赖库
foreach(LIBRARY ${OPENGL_EXAMPLE_LIBRARIES})
add_subdirectory(${OPENGL_EXAMPLE_LIBRARIES_DIR}/${LIBRARY})
endforeach(LIBRARY)
# 从系统查找依赖库
find_package(OpenGL REQUIRED) # xcode
# 从src编译依赖库
add_library(GLAD ${OPENGL_EXAMPLE_SOURCE_DIR}/glad/glad.c)
# 指定头文件路径
include_directories(${OPENGL_EXAMPLE_INCLUDE_DIR})
# 编译src
file(GLOB SOURCES ${OPENGL_EXAMPLE_SOURCE_DIR}/game/*.cpp)
add_executable(${OPENGL_EXAMPLE_PROJECT_NAME} ${OPENGL_EXAMPLE_SOURCE_DIR}/main.cpp ${SOURCES})
target_link_libraries(${OPENGL_EXAMPLE_PROJECT_NAME} OpenGL::GL glfw soil GLAD)
# 编译examples和test
FILE(GLOB SRC_CPP_LIST ${OPENGL_EXAMPLE_EXAMPLES_DIR}/*/*.cpp ${OPENGL_EXAMPLE_EXCERCISE_DIR}/*/*.cpp)
foreach(src_cpp ${SRC_CPP_LIST})
# 获取cpp文件前缀
get_filename_component(exe_name ${src_cpp} NAME_WE)
# 获取cpp文件路径以及上一级目录
get_filename_component(exe_dir ${src_cpp} DIRECTORY)
get_filename_component(par_dir ${exe_dir} NAME)
# 设置可执行文件全名
set(full_exe_name ${par_dir}_${exe_name})
# 生成可执行文件
add_executable(${full_exe_name} ${src_cpp})
# 链接相关依赖库
target_link_libraries(${full_exe_name} OpenGL::GL ${OPENGL_EXAMPLE_LIBRARIES} GLAD)
endforeach()
add_custom_command(TARGET ${OPENGL_EXAMPLE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/resources $<TARGET_FILE_DIR:${OPENGL_EXAMPLE_PROJECT_NAME}>/resources)