-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
116 lines (106 loc) · 3.61 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
cmake_minimum_required(VERSION 3.27)
#工程名字
project(AES VERSION 1.0 LANGUAGES CXX C)
#使用C++ 17编译 , GCC对C++23支持不是很好
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD_REQUIRED ON)
#设置静态库编译文件
set(TARGET_DIR ${CMAKE_CURRENT_LIST_DIR}/../target/)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${TARGET_DIR}/target/)
#设置动态库文件目录
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${TARGET_DIR}/target/)
#设置可执行库文件目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${TARGET_DIR}/target/)
#设置头文件目录
include_directories(
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/aes/inc
${CMAKE_CURRENT_LIST_DIR}/aes/inc/aesni
${CMAKE_CURRENT_LIST_DIR}/base64/inc
)
#设置编译类型
# Debug Release RelWithDebInfo MinSizeRel
set(Build_Type Release)
#获取编译器型号,MSVC不开启硬件加速,代码支持GCC编译时,开启硬件加速
#使用MSVC时,代码文件使用utf-8,而不是utf-8 with BOM
#两种编译器,分别设置编译类型
set (Compiler ${CMAKE_CXX_COMPILER_ID})
if(Compiler MATCHES "MSVC")
set(CMAKE_CONFIGURATION_TYPES ${Build_Type}) #MSVC编译器使用
# msvc要使用utf-8 with BOM,使用下面的代码改变为utf-8
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
set(AES_ENABLE_AESNI OFF)
else()
set(CMAKE_BUILD_TYPE ${Build_Type}) #Mingw编译器使用
set(AES_ENABLE_AESNI ON)
endif()
if(AES_ENABLE_AESNI)
message("Enable AES-NI")
add_definitions(-DUSE_INTEL_AES_IF_AVAILABLE)
set(AESNI
${CMAKE_CURRENT_LIST_DIR}/aes/inc/aesni/aesni-enc-cbc.h
${CMAKE_CURRENT_LIST_DIR}/aes/inc/aesni/aesni-enc-ecb.h
${CMAKE_CURRENT_LIST_DIR}/aes/inc/aesni/aesni-key-exp.h
${CMAKE_CURRENT_LIST_DIR}/aes/inc/aesni/aesni-key-init.h
)
else()
message("Disable AES-NI")
set(AESNI "")
endif()
#设置AES_头文件
set(AES_Headers
${CMAKE_CURRENT_LIST_DIR}/aes/inc/aes.hpp
${CMAKE_CURRENT_LIST_DIR}/base64/inc/base64.hpp
${CMAKE_CURRENT_LIST_DIR}/aes/inc/exaes.hpp
)
#设置AES_源文件
set(AES_Sources
${CMAKE_CURRENT_LIST_DIR}/aes/src/aes.cpp
${CMAKE_CURRENT_LIST_DIR}/base64/src/base64.cpp
${CMAKE_CURRENT_LIST_DIR}/aes/src/exaes.cpp
)
#合并工程文件
set(AES_Project
${AESNI}
${AES_Headers}
${AES_Sources}
)
#生成库文件
set(LibName ${CMAKE_PROJECT_NAME})
add_library(${LibName} SHARED
${AES_Project}
)
#生成可执行文件
add_executable(${CMAKE_PROJECT_NAME}Test
${CMAKE_CURRENT_LIST_DIR}/main.cpp
)
target_link_libraries(${CMAKE_PROJECT_NAME}Test PRIVATE
${LibName}
)
if(AES_ENABLE_AESNI)
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-maes CXX_COMPILER_HAS_FLAG_MAES)
check_cxx_compiler_flag("/arch:AVX" CXX_COMPILER_HAS_ARCH_AVX)
IF(CXX_COMPILER_HAS_ARCH_AVX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX")
ENDIF()
target_compile_options(${LibName}
PRIVATE
$<$<BOOL:${CXX_COMPILER_HAS_FLAG_MAES}>:-maes>
)
endif()
#设置需要导出的头文件
set(IncludeFile
${CMAKE_CURRENT_LIST_DIR}/aes/inc/exaes.hpp
${CMAKE_CURRENT_LIST_DIR}/aes/inc/aes.hpp
)
#设置编译输出目录
set(CMAKE_INSTALL_PREFIX ${TARGET_DIR}/install)
install(TARGETS ${LibName}
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
)
install(FILES ${IncludeFile} DESTINATION ${CMAKE_INSTALL_PREFIX}/include)