-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMakeLists.txt
151 lines (122 loc) · 4.43 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
144
145
146
147
148
149
150
151
cmake_minimum_required(VERSION 3.10)
project(mtfmt)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
# debug版的后缀
set(CMAKE_DEBUG_POSTFIX "_d")
# 构建输出
set(BUILD_OUTPUT_DIR ${CMAKE_BINARY_DIR})
# 选项
option(MTFMT_RT_USE_DIV "Use hardware divide instruction" OFF)
option(MTFMT_RT_USE_MALLOC "Use malloc instead build-in heap manager" OFF)
option(MTFMT_RT_USE_STDOUT "Enable stdio support in the library" OFF)
option(MTFMT_RT_USE_ASSERT "Enable assert" OFF)
option(MTFMT_RT_USE_UTF8 "Enable UTF-8 string support" ON)
option(MTFMT_RT_USE_FP32 "Enable 32bits floating number formatting support" OFF)
option(MTFMT_RT_USE_FP64 "Enable 64bits floating number formatting support" OFF)
option(MTFMT_BUILD_SHARED "Build dymatic library" OFF)
# 库文件输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${BUILD_OUTPUT_DIR}/target/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${BUILD_OUTPUT_DIR}/target/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${BUILD_OUTPUT_DIR}/target/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${BUILD_OUTPUT_DIR}/target/bin)
# 源文件
aux_source_directory("${CMAKE_CURRENT_SOURCE_DIR}/src" LIB_SRCS)
# 资源文件
if(MSVC AND WIN32)
set(LIB_WIN_RC "${CMAKE_CURRENT_SOURCE_DIR}/src/win_resource.rc")
else()
set(LIB_WIN_RC "")
endif()
# 目标名
set(TARGET_NAME mtfmt)
# 构建目标
if(MTFMT_BUILD_SHARED)
add_library(${TARGET_NAME} SHARED ${LIB_SRCS} ${LIB_WIN_RC})
else()
add_library(${TARGET_NAME} STATIC ${LIB_SRCS})
endif()
# including path
target_include_directories(${TARGET_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc")
# debug?
if(NOT MSVC)
if(NOT CMAKE_BUILD_TYPE)
message("NO build type was set, the default value is release")
set(CMAKE_BUILD_TYPE Release)
endif()
else()
# 默认release ...
set(CMAKE_BUILD_TYPE Release)
endif()
# 宏开关
# default: auto, following the compiler pre-defined macros
if(MTFMT_RT_USE_DIV)
target_compile_definitions(${TARGET_NAME} PRIVATE _MSTR_USE_HARDWARE_DIV)
endif()
# default: 0
if(MTFMT_RT_USE_MALLOC)
target_compile_definitions(${TARGET_NAME} PRIVATE _MSTR_USE_MALLOC)
endif()
# default: 0
if(MTFMT_RT_USE_STDOUT)
target_compile_definitions(${TARGET_NAME} PRIVATE _MSTR_USE_STD_IO)
endif()
# default: 0
if(MTFMT_RT_USE_ASSERT)
target_compile_definitions(${TARGET_NAME} PRIVATE _MSTR_RUNTIME_ASSERT)
target_compile_definitions(${TARGET_NAME} PRIVATE _MSTR_RUNTIME_CTRLFLOW_MARKER)
endif()
# default: 1
if(MTFMT_RT_USE_UTF8)
target_compile_definitions(${TARGET_NAME} PRIVATE _MSTR_USE_UTF_8=1)
else()
target_compile_definitions(${TARGET_NAME} PRIVATE _MSTR_USE_UTF_8=0)
endif()
# default: 0
if(MTFMT_RT_USE_FP32)
target_compile_definitions(${TARGET_NAME} PRIVATE _MSTR_USE_FP_FLOAT32)
endif()
# default: auto, following the compiler pre-defined macros
if(MTFMT_RT_USE_FP64)
target_compile_definitions(${TARGET_NAME} PRIVATE _MSTR_USE_FP_FLOAT64)
endif()
if(MSVC)
# 大多数东东没有second api
# 因此mtfmt开发过程也没有用它们, disable掉
target_compile_definitions(${TARGET_NAME} PRIVATE _CRT_SECURE_NO_WARNINGS)
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(${TARGET_NAME} PRIVATE _DEBUG)
endif()
# dll export
if(MTFMT_BUILD_SHARED)
target_compile_definitions(${TARGET_NAME} PRIVATE _MSTR_BUILD_DLL)
endif()
install(TARGETS mtfmt
EXPORT mtfmt-targets
RUNTIME DESTINATION mtfmt/bin # dll
LIBRARY DESTINATION mtfmt/lib # 动态库安装路径
ARCHIVE DESTINATION mtfmt/lib/static # 静态库安装路径
)
# 安装头文件
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/inc/ DESTINATION mtfmt/include)
install(EXPORT mtfmt-targets
FILE mtfmt-config.cmake
NAMESPACE mtfmt::
DESTINATION mtfmt/lib/cmake/mtfmt)
# 测试
# enable_testing()
# file(GLOB_RECURSE TEST_SOURCES tests/*.c thirds/unity/src/*.c)
# foreach(FILE_PATH ${TEST_SOURCES})
# get_filename_component(FILE_NAME ${FILE_PATH} NAME_WE)
# set(TEST_TARGET_NAME ${FILE_NAME}_test)
# add_executable(${TEST_TARGET_NAME} ${FILE_PATH})
# target_include_directories(${TEST_TARGET_NAME} PRIVATE inc thirds/unity/src)
# target_link_libraries(${TEST_TARGET_NAME} PRIVATE ${LIB_TARGET_NAME})
# if (MTFMT_BUILD_USE_LTO)
# set_target_properties(${TEST_TARGET_NAME} PROPERTIES LINK_FLAGS -flto)
# endif()
# add_test(NAME ${FILE_NAME} COMMAND ${TEST_TARGET_NAME})
# endforeach()
# 自定义目标 alltests
# add_custom_target(alltests COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure)