forked from BurningEnlightenment/base64-cmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
210 lines (176 loc) · 6.85 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Written in 2016-2017 by Henrik Steffen Gaßmann [email protected]
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
#
# http://creativecommons.org/publicdomain/zero/1.0/
#
########################################################################
cmake_minimum_required(VERSION 3.1)
project(base64 LANGUAGES C VERSION 0.3.0)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
#######################################################################
# platform detection
include(TargetArch)
detect_target_architecture(_TARGET_ARCH)
########################################################################
# Compilation options
option(BASE64_INSTALL_TARGET "add an install target" ON)
option(BASE64_BUILD_TESTS "add test projects" ON)
option(BASE64_WITH_OPENMP "use openmp" OFF)
if (_TARGET_ARCH STREQUAL "x86" OR _TARGET_ARCH STREQUAL "x64")
option(BASE64_WITH_FAST_UNALIGNED_ACCESS "vectorization: unaligned access enabled" ON)
option(BASE64_WITH_SSSE3 "add SSSE 3 codepath" ON)
option(BASE64_WITH_SSE41 "add SSE 4.1 codepath" ON)
option(BASE64_WITH_SSE42 "add SSE 4.2 codepath" ON)
option(BASE64_WITH_AVX "add AVX codepath" ON)
option(BASE64_WITH_AVX2 "add AVX 2 codepath" ON)
elseif (_TARGET_ARCH STREQUAL "arm")
option(BASE64_WITH_NEON32 "add NEON32 codepath" OFF)
elseif (_TARGET_ARCH STREQUAL "arm64")
option(BASE64_WITH_NEON64 "add NEON64 codepath" ON)
endif()
###################################################################
# OpenMP
if(BASE64_WITH_OPENMP)
find_package(OpenMP)
if (OPENMP_FOUND)
option(BASE64_USE_OpenMP "Utilize OpenMP to parallelize encoding and decoding." ON)
endif()
endif()
########################################################################
# library project
add_library(base64 STATIC
# library files
lib/lib.c
lib/codec_choose.c
include/libbase64.h
# codec implementations
lib/arch/generic/codec.c
lib/arch/ssse3/codec.c
lib/arch/sse41/codec.c
lib/arch/sse42/codec.c
lib/arch/avx/codec.c
lib/arch/avx2/codec.c
lib/arch/neon32/codec.c
lib/arch/neon64/codec.c
)
if(NOT BASE64_STANDALONE_PROJECT)
# export inlucde path if base64 added as subdirectory
target_include_directories(base64 INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
endif()
####################################################################
# platform/compiler specific configuration
set_target_properties(base64 PROPERTIES
C_STANDARD 99
C_STANDARD_REQUIRED YES
C_EXTENSIONS OFF
)
target_compile_options(base64 PRIVATE
$<$<C_COMPILER_ID:MSVC>:
/W4
/WX # All warnings as error
/we4013 # Error warning C4013: 'function' undefined; assuming extern returning int
/we4700 # Error warning C4700: uninitialized local variable
/we4715 # not all control paths return a value
/we4003 # not enough actual parameters for macro
>
$<$<NOT:$<C_COMPILER_ID:MSVC>>:
-Wall
-Wextra
-Wpedantic
>
)
target_compile_definitions(base64 PRIVATE
$<$<C_COMPILER_ID:MSVC>:
# remove unnecessary warnings about unchecked iterators
_SCL_SECURE_NO_WARNINGS
>
)
########################################################################
# SIMD settings
include(TargetSIMDInstructionSet)
define_SIMD_compile_flags()
if (_TARGET_ARCH STREQUAL "x86" OR _TARGET_ARCH STREQUAL "x64")
macro(configure_codec _TYPE)
if (BASE64_WITH_${_TYPE})
message(STATUS "Add codec: lib/arch/${_DIR}/codec.c")
string(TOLOWER "${_TYPE}" _DIR)
set_source_files_properties("lib/arch/${_DIR}/codec.c" PROPERTIES
COMPILE_FLAGS "${COMPILE_FLAGS_${_TYPE}}"
)
if (${ARGC} GREATER 1 AND MSVC)
set_source_files_properties("lib/arch/${_DIR}/codec.c" PROPERTIES
COMPILE_DEFINITIONS ${ARGV1}
)
endif()
endif()
endmacro()
configure_codec(SSSE3 __SSSE3__)
configure_codec(SSE41 __SSSE4_1__)
configure_codec(SSE42 __SSSE4_2__)
configure_codec(AVX)
configure_codec(AVX2)
elseif (_TARGET_ARCH STREQUAL "arm")
set(BASE64_NEON32_CFLAGS "${COMPILE_FLAGS_NEON32}" CACHE STRING "the NEON32 compile flags (for 'lib/arch/neon32/codec.c')")
mark_as_advanced(BASE64_NEON32_CFLAGS)
if (BASE64_WITH_NEON32)
set_source_files_properties("lib/arch/neon32/codec.c" PROPERTIES
COMPILE_FLAGS "${BASE64_NEON32_CFLAGS} "
)
endif()
#elseif (_TARGET_ARCH STREQUAL "arm64" AND BASE64_WITH_NEON64)
endif()
configure_file(config.h.in ${CMAKE_CURRENT_BINARY_DIR}/libbase64_config.h @ONLY)
target_compile_definitions(base64
PRIVATE
BASE64_DIRECT_CONFIG=$<BOOL:${ON}>
HAVE_FAST_UNALIGNED_ACCESS=$<BOOL:${BASE64_WITH_FAST_UNALIGNED_ACCESS}>
HAVE_SSSE3=$<BOOL:${BASE64_WITH_SSSE3}>
HAVE_SSE41=$<BOOL:${BASE64_WITH_SSE41}>
HAVE_SSE42=$<BOOL:${BASE64_WITH_SSE42}>
HAVE_AVX=$<BOOL:${BASE64_WITH_AVX}>
HAVE_AVX2=$<BOOL:${BASE64_WITH_AVX2}>
HAVE_NEON32=$<BOOL:${BASE64_WITH_NEON32}>
HAVE_NEON64=$<BOOL:${BASE64_WITH_NEON64}>
)
########################################################################
# OpenMP Settings
if (BASE64_USE_OpenMP)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
endif()
########################################################################
if (BASE64_BUILD_TESTS)
enable_testing()
add_subdirectory(test)
endif()
########################################################################
# install target
if(BASE64_INSTALL_TARGET)
install(TARGETS base64 EXPORT base64-targets
RUNTIME DESTINATION bin/$<CONFIG>
LIBRARY DESTINATION lib/$<CONFIG>
ARCHIVE DESTINATION lib/$<CONFIG>
INCLUDES DESTINATION include
)
install(FILES include/libbase64.h DESTINATION include)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/base64-config-version.cmake"
VERSION ${BASE64_VERSION}
COMPATIBILITY ExactVersion
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/base64-config-version.cmake" DESTINATION cmake)
configure_file(cmake/base64-config.cmake
"${CMAKE_CURRENT_BINARY_DIR}/base64-config.cmake"
COPYONLY
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/base64-config.cmake" DESTINATION cmake)
install(EXPORT base64-targets DESTINATION cmake)
endif()