forked from 4a256b6b3e7t3e8b7t9q7t/libhydrogen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
185 lines (144 loc) · 6.12 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
cmake_minimum_required(VERSION 3.12)
project(hydrogen LANGUAGES C CXX)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
string(TOUPPER "${PROJECT_NAME}" setting_prefix)
function(get_setting setting_name setting_type setting_description)
string(TOUPPER "${setting_prefix}_${setting_name}" setting_external_name)
set("${setting_external_name}" "" CACHE "${setting_type}" "${setting_description}")
set("${setting_name}" "${${setting_external_name}}" PARENT_SCOPE)
endfunction()
# Project files
set(source_files
"${PROJECT_NAME}.c"
"pw.cpp"
"impl/common.h"
"impl/core.h"
"impl/gimli-core.h"
"impl/gimli-core/portable.h"
"impl/gimli-core/sse2.h"
"impl/hash.h"
"impl/${PROJECT_NAME}_p.h"
"impl/kdf.h"
"impl/kx.h"
"impl/pwhash.h"
"impl/random.h"
"impl/secretbox.h"
"impl/sign.h"
"impl/x25519.h")
set(header_files
"${PROJECT_NAME}.h")
set(test_files
"tests/tests.c")
set(arduino_files
"library.properties")
# Compile options
get_setting(target_arch STRING "Target system architecture (fed to the compiler's -march=...).")
if(NOT target_arch AND NOT CMAKE_CROSSCOMPILING)
set(target_arch native)
endif()
set(compile_options
# GNU, Clang
$<$<OR:$<C_COMPILER_ID:AppleClang>,$<C_COMPILER_ID:Clang>,$<C_COMPILER_ID:GNU>>:
# Optimizations
-Os $<$<BOOL:${target_arch}>:-march=${target_arch}> -fno-exceptions
# Warnings
-Wall -Wextra -Wmissing-prototypes -Wdiv-by-zero -Wbad-function-cast -Wcast-align
-Wcast-qual -Wfloat-equal -Wmissing-declarations -Wnested-externs -Wno-unknown-pragmas
-Wpointer-arith -Wredundant-decls -Wstrict-prototypes -Wswitch-enum -Wno-type-limits>
# MSVC
$<$<C_COMPILER_ID:MSVC>:
# Optimizations
/Os /EHsc
# Warnings
/WX /W4
/wd4197 # suppress warning "top-level volatile in cast is ignored"
/wd4146 # suppress warning "unary minus operator applied to unsigned type, result still unsigned"
/wd4310 # suppress warning "cast truncates constant value"
>)
# Prefix project files with the project root
function(prefix_project_paths list_name)
list(TRANSFORM "${list_name}"
PREPEND "${PROJECT_SOURCE_DIR}/"
OUTPUT_VARIABLE prefixed_list)
set("project_${list_name}" ${prefixed_list} PARENT_SCOPE)
endfunction()
prefix_project_paths(source_files)
prefix_project_paths(header_files)
prefix_project_paths(test_files)
prefix_project_paths(arduino_files)
# Main library
add_library("${PROJECT_NAME}")
add_library("${PROJECT_NAME}::${PROJECT_NAME}" ALIAS "${PROJECT_NAME}")
target_sources("${PROJECT_NAME}" PRIVATE ${project_source_files})
target_include_directories("${PROJECT_NAME}" PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_compile_options("${PROJECT_NAME}" PRIVATE ${compile_options})
# Installation
set(targets_export_name "${PROJECT_NAME}-targets")
install(TARGETS "${PROJECT_NAME}"
EXPORT "${targets_export_name}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")
install(FILES ${project_header_files}
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
# CMake find_package() support
set(install_config_dir "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}")
set(targets_export_file_name "${targets_export_name}.cmake")
set(targets_export_file "${PROJECT_BINARY_DIR}/${targets_export_file_name}")
install(EXPORT "${targets_export_name}"
FILE "${targets_export_file_name}"
NAMESPACE "${PROJECT_NAME}::"
DESTINATION "${install_config_dir}")
set(config_file_name "${PROJECT_NAME}-config.cmake")
set(config_template_file "${PROJECT_SOURCE_DIR}/cmake/${config_file_name}.in")
set(config_file "${PROJECT_BINARY_DIR}/${config_file_name}")
CONFIGURE_PACKAGE_CONFIG_FILE("${config_template_file}" "${config_file}"
INSTALL_DESTINATION "${install_config_dir}")
install(FILES "${config_file}"
DESTINATION "${install_config_dir}")
export(EXPORT "${targets_export_name}"
FILE "${targets_export_file}"
NAMESPACE "${PROJECT_NAME}::")
export(PACKAGE "${PROJECT_NAME}")
# Tests
set(tests_executable "${PROJECT_NAME}-tests")
set(tests_run_target "${PROJECT_NAME}-run-tests")
set(tests_run_file "${PROJECT_BINARY_DIR}/${tests_run_target}.done")
enable_testing()
add_executable("${tests_executable}" ${project_test_files})
target_compile_options("${tests_executable}" PRIVATE ${compile_options})
target_link_libraries("${tests_executable}" "${PROJECT_NAME}")
add_test(NAME "${tests_executable}" COMMAND "${tests_executable}")
if(CMAKE_CROSSCOMPILING)
# Disable tests executable by default when cross-compiling (as it will fail
# to build when, e.g., cross-compiling for Arduino/AVR).
set_target_properties("${tests_executable}"
PROPERTIES
EXCLUDE_FROM_ALL 1
EXCLUDE_FROM_DEFAULT_BUILD 1)
else()
# Otherwise, auto-run the tests on build.
add_custom_command(OUTPUT "${tests_run_file}"
DEPENDS "${tests_executable}"
COMMAND cmake
ARGS -E remove "${tests_run_file}"
COMMAND ctest
ARGS -C $<CONFIGURATION> --output-on-failure
COMMAND cmake
ARGS -E touch "${tests_run_file}"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}")
add_custom_target("${tests_run_target}" ALL DEPENDS "${tests_run_file}")
endif()
# Generate Arduino package
set(arduino_package_file "${PROJECT_BINARY_DIR}/hydrogen-crypto.zip")
# Use the relative versions of the file path lists or else the full paths will
# end up in the generated archive.
add_custom_command(OUTPUT "${arduino_package_file}"
COMMAND cmake
ARGS -E tar cf "${arduino_package_file}" --format=zip
-- ${source_files} ${header_files} ${arduino_files}
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
add_custom_target("${PROJECT_NAME}-arduino-package"
DEPENDS "${arduino_package_file}")