-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
76 lines (58 loc) · 2.76 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
# cmake最低版本需求
cmake_minimum_required(VERSION 3.10)
# 預設產生src執行檔
option(UNIT_TEST "Generate unit test?" OFF)
# 預設產生debug類型執行檔
option(DEBUG_BUILD_TYPE "Debug build type?" ON)
# 基於DEBUG_BUILD_TYPE選項創建不同執行檔
if(DEBUG_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
# 基於UNIT_TEST選項創建不同執行檔
if(UNIT_TEST)
message(STATUS "Generate unit test project...")
# 定義專案類型 //project(<project-name> [LANGUAGES <language-names>...] [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]])
project(MyTestProject C CXX)
# 設定path相關 //set(<variable> <value>... [CACHE <type> <docstring> [FORCE]])
set(COMPILER_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/build)
set(PREPROCESS_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${COMPILER_OUTPUT_PATH}/bin/test)
set(SRC_DIR ${CMAKE_SOURCE_DIR}/src)
set(TEST_CASE_DIR ${CMAKE_SOURCE_DIR}/test)
set(THIRD_PARTY_DIR ${CMAKE_SOURCE_DIR}/third_party)
# 指定header搜尋路徑(default: include往前放,將優先被查找) //include_directories([AFTER|BEFORE] [SYSTEM] <dir1> [<dir2> ...])
include_directories(${SRC_DIR})
include_directories(${THIRD_PARTY_DIR}/gtest/include)
include_directories(${THIRD_PARTY_DIR}/fff)
# 設定gtest相關
set(GTEST_INCLUDE_DIR ${THIRD_PARTY_DIR}/gtest/googletest/include)
set(GTEST_LIBRARY ${THIRD_PARTY_DIR}/gtest/build/lib/libgtest.a)
set(GTEST_MAIN_LIBRARY ${THIRD_PARTY_DIR}/gtest/build/lib/libgtest_main.a)
# find_package(GTest REQUIRED) //find_package(<PackageName> [version] [REQUIRED] [QUIET] [MODULE] [EXACT] [NO_MODULE] [CONFIG] [NAMES name1 [name2 ...]] [COMPONENTS [components ...]] [OPTIONAL_COMPONENTS [components ...]] [NO_POLICY_SCOPE])
# include_directories(${GTEST_INCLUDE_DIR})
# 使用子模組的CMakeLists.txt加入gtest lib //add_subdirectory(<source_dir> [binary_dir] [EXCLUDE_FROM_ALL])
add_subdirectory(${THIRD_PARTY_DIR}/gtest)
# 使用子模組的CMakeLists.txt加入fff lib
add_subdirectory(${THIRD_PARTY_DIR}/fff)
# 啟動測試功能 //enable_testing()
enable_testing()
# 使用子模組的CMakeLists.txt加入test suite
add_subdirectory(${TEST_CASE_DIR})
else()
message(STATUS "Generate main project...")
# 定義專案類型
project(MyProject C)
# 設定path相關
set(COMPILER_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/build)
set(PREPROCESS_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${COMPILER_OUTPUT_PATH}/bin)
set(SRC_DIR ${CMAKE_SOURCE_DIR}/src)
# 指定header搜尋路徑
include_directories(${SRC_DIR})
# 查找該目錄底下.c檔
#file(GLOB SRC_FILES "${SRC_DIR}/*.c")
# 查找該目錄與其所有子目錄.c檔
file(GLOB_RECURSE SRC_FILES "${SRC_DIR}/*.c")
# 使用子模組的CMakeLists.txt加入main code
add_subdirectory(${SRC_DIR})
endif()