-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMakeLists.txt
300 lines (254 loc) · 10.5 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#
# Public Domain.
#
project(nacl C)
cmake_minimum_required(VERSION 2.4)
# Generic hook to allow arbitrary cmake code to be run for preconfiguration.
if(CNACL_CONFIG_SCRIPT)
include(${CNACL_CONFIG_SCRIPT})
endif()
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include")
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include_internal")
add_subdirectory(randombytes)
if(CMAKE_VERSION VERSION_LESS 2.8.4)
message("Parallel building (-j) will not be available.")
message("To build in parallel, upgrade to cmake 2.8.4 or newer.")
message("see: http://www.cmake.org/Bug/print_bug_page.php?bug_id=10395")
endif()
#################################
enable_language(ASM)
set(CMAKE_ASM_COMPILER_ARG1 "${MY_CMAKE_ASM_FLAGS} -c")
macro(print appendTo content)
list(APPEND ${appendTo} "${content}\n")
endmacro()
##
# Create a header file for an operation/primitive such as crypto_stream/salsa20.
# NOTE: Not all primitives are required to behave the same for a given operation.
# crypto_stream/salsa20 will obviously give you a different output
# than crypto_stream/aes128ctr.
#
# @param operation the operation name, a generic name for the job which is done.
# @param primitive the name of the algorithm which is used to do the operation.
# @param output the name of a variable which will be set to a string containing the header file.
##
function(writeOperationHeader operation primitive output)
set(op "${operation}_${primitive}")
set(out "")
print(out "#ifndef ${operation}_H")
print(out "#define ${operation}_H")
print(out "")
print(out "#include \"${op}.h\"")
print(out "")
foreach(macro ${MACROS})
if("${macro}" MATCHES "${operation}$|${operation}\\(|${operation}_")
string(REGEX REPLACE "${operation}" "${op}" regexout "${macro}")
print( out "#define ${macro} ${regexout}")
endif()
endforeach()
print(out "#define ${operation}_PRIMITIVE \"${primitive}\"")
print(out "#define ${operation}_IMPLEMENTATION ${op}_IMPLEMENTATION")
print(out "#define ${operation}_VERSION ${op}_VERSION")
print(out "")
print(out "#endif")
string(REGEX REPLACE "\n;" "\n" tmpoutStr "${out}")
set(${output} ${tmpoutStr} PARENT_SCOPE)
endfunction()
##
# Create a header file for an operation/primitive/implementation
# such as crypto_stream/salsa20/amd64_xmm.
# This is a (potentially processor specific) implementation of an operation/primitive.
# All implementations must return the same output for a given input.
#
# @param operation the operation name, a generic name for the job which is done.
# @param primitive the name of the algorithm which is used to do the operation.
# @param implementation the name of the implementation of the operation/primitive.
# @param output the name of a variable which will be set to a string containing the header file.
##
function(writeOperationPrimitiveHeader operation primitive implementation output)
set(op "${operation}_${primitive}")
set(path "${operation}/${primitive}/${implementation}")
string(REGEX REPLACE "[\\.-/]" "_" opi "${path}")
set(tmpout "")
print(tmpout "#ifndef ${op}_H")
print(tmpout "#define ${op}_H")
print(tmpout "")
file(STRINGS "${CMAKE_SOURCE_DIR}/${path}/api.h" api)
foreach(line ${api})
string(REGEX REPLACE "[ \\t]CRYPTO_" " ${opi}_" out "${line}")
print(tmpout "${out}")
endforeach()
# C++
print(tmpout "#ifdef __cplusplus")
print(tmpout "#include <string>")
foreach(prototype ${PROTOTYPES_CPP})
if("${prototype}" MATCHES "${operation}$|${operation}\\(|${operation}_")
string(REGEX REPLACE "${operation}" "${opi}" out "${prototype}")
print(tmpout "${out}")
endif()
endforeach()
print(tmpout "extern \"C\" {")
print(tmpout "#endif")
# C
foreach(prototype ${PROTOTYPES})
if("${prototype}" MATCHES "${operation}$|${operation}\\(|${operation}_")
string(REGEX REPLACE "${operation}" "${opi}" out "${prototype}")
print(tmpout "${out}")
endif()
endforeach()
print(tmpout "#ifdef __cplusplus")
print(tmpout "}")
print(tmpout "#endif")
foreach(macro ${MACROS})
if("${macro}" MATCHES "${operation}$|${operation}\\(|${operation}_")
string(REGEX REPLACE "${operation}" "${opi}" mopi "${macro}")
string(REGEX REPLACE "${operation}" "${op}" mop "${macro}")
print(tmpout "#define ${mop} ${mopi}")
endif()
endforeach()
print(tmpout "#define ${op}_IMPLEMENTATION \"${path}\"")
print(tmpout "#ifndef ${opi}_VERSION")
print(tmpout "#define ${opi}_VERSION \"-\"")
print(tmpout "#endif")
print(tmpout "#define ${op}_VERSION ${opi}_VERSION")
print(tmpout "")
print(tmpout "#endif")
string(REGEX REPLACE "\n;" "\n" tmpoutStr "${tmpout}")
set(${output} ${tmpoutStr} PARENT_SCOPE)
endfunction()
function(doOperationPrimitiveImplementation
operation
primitive
implementation
allOpPrimitives
runTest
)
#message("${operation} --- ${primitive} --- ${implementation}")
#message("${allOpPrimitives}")
set(opiPath "${operation}/${primitive}/${implementation}")
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/${opiPath}")
writeOperationPrimitiveHeader("${operation}" "${primitive}" "${implementation}" output)
file(WRITE "${CMAKE_BINARY_DIR}/include/${operation}_${primitive}.h" "${output}")
file(GLOB files "${CMAKE_SOURCE_DIR}/${opiPath}/*.[csS]")
set(lib "${operation}_${primitive}_${implementation}")
print(tmpout "set(CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})")
print(tmpout "include_directories(
\"${CMAKE_BINARY_DIR}/${operation}/${primitive}\"
\"${CMAKE_BINARY_DIR}/${opiPath}\"
\"${CMAKE_BINARY_DIR}/include\"
\"${CMAKE_BINARY_DIR}/include_internal\"
)")
print(tmpout "add_library(${lib} ${files})")
print(tmpout "set_target_properties(${lib} PROPERTIES LINKER_LANGUAGE C)")
string(REGEX REPLACE "\n;" "\n" tmpoutStr "${tmpout}")
file(WRITE "${CMAKE_BINARY_DIR}/${opiPath}/CMakeLists.txt" "${tmpoutStr}")
endfunction()
function(executePlan implementations)
set(opPrimitives "")
foreach(line ${implementations})
string(REGEX REPLACE "/[^/]*$" "" opSlashPrim ${line})
string(REPLACE "/" "_" opPrim ${opSlashPrim})
list(APPEND opPrimitives ${opPrim})
endforeach()
set(operations "")
foreach(line ${implementations})
string(REPLACE "/" ";" newLine ${line})
list(GET newLine 0 op)
list(GET newLine 1 prim)
list(GET newLine 2 impl)
doOperationPrimitiveImplementation(${op} ${prim} ${impl} "${opPrimitives}" FALSE)
set("${op}_primitives"
"${${op}_primitives};${prim}"
CACHE INTERNAL "primitives by operation" FORCE
)
set("${op}_${prim}_implementations"
"${${op}_${prim}_implementations};${impl}"
CACHE INTERNAL "implementations by primitivie" FORCE
)
list(APPEND operations "${op}")
endforeach()
set(libraries "")
cmake_policy(SET CMP0007 NEW)
list(REMOVE_DUPLICATES operations)
foreach(op ${operations})
list(REMOVE_DUPLICATES ${op}_primitives)
foreach(prim ${${op}_primitives})
list(REMOVE_DUPLICATES ${op}_${prim}_implementations)
#message("${op} ---- ${prim}")
foreach(impl ${${op}_${prim}_implementations})
#message("${op} === ${prim} === ${impl}")
print(tmpout "add_subdirectory(${impl})")
list(APPEND libraries "${op}_${prim}_${impl}")
endforeach()
string(REGEX REPLACE "\n;" "\n" tmpoutStr "${tmpout}")
file(WRITE "${CMAKE_BINARY_DIR}/${op}/${prim}/CMakeLists.txt" "${tmpoutStr}")
set(tmpout "")
print(opOut "add_subdirectory(${prim})")
writeOperationHeader("${op}" "${prim}" output)
file(WRITE "${CMAKE_BINARY_DIR}/${op}/${prim}/${op}.h" "${output}")
endforeach()
string(REGEX REPLACE "\n;" "\n" tmpoutStr "${opOut}")
file(WRITE "${CMAKE_BINARY_DIR}/${op}/CMakeLists.txt" "${tmpoutStr}")
set(opOut "")
add_subdirectory("${CMAKE_BINARY_DIR}/${op}")
endforeach()
include("${CMAKE_SOURCE_DIR}/cmake/libutils.cmake")
merge_static_libs(nacl nacl "${libraries}")
#add_dependencies(nacl randombytes)
endFunction()
function(writeTypesHeaders types)
foreach(intfile
"crypto_uint8.h"
"crypto_int8.h"
"crypto_uint16.h"
"crypto_int26.h"
"crypto_uint32.h"
"crypto_int32.h"
"crypto_uint64.h"
"crypto_int64.h"
)
file(WRITE "${CMAKE_BINARY_DIR}/include_internal/${intfile}" "#include <crypto_types.h>")
endforeach()
set(out)
print(out "#ifndef crypto_types_h")
print(out "#define crypto_types_h")
foreach(type ${types})
print(out "${type};")
endforeach()
print(out "#endif")
string(REGEX REPLACE "\n;" "\n" tmpoutStr "${out}")
file(WRITE "${CMAKE_BINARY_DIR}/include/crypto_types.h" "${tmpoutStr}")
endfunction()
include("${CMAKE_SOURCE_DIR}/cmake/AbiName.cmake")
AbiName_get(abi)
message("Detected ABI as ${abi}")
set(planPath "${CMAKE_SOURCE_DIR}/cmake/plans/${abi}_plan.cmake")
## On apple systems, the assembler is different and it doesn't accept
## a number of the asm files in NaCl
if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Darwin")
set(planPath "${CMAKE_SOURCE_DIR}/cmake/plans/apple_${abi}_plan.cmake")
endif()
message("Using a [${CMAKE_HOST_SYSTEM_NAME}] toolchain.")
if (NOT EXISTS "${planPath}")
message("Could not find compile plan for this ABI, please wait while one is generated...")
execute_process(COMMAND "${CMAKE_SOURCE_DIR}/do" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
include("${CMAKE_SOURCE_DIR}/cmake/MakePlan.cmake")
makePlan("${abi}" "${planPath}")
if (NOT EXISTS "${planPath}")
message("Can't find plan for the target ABI [${abi}].")
message(FATAL_ERROR "Cross compiling is not supported without a premade plan.")
endif()
message("Created new plan for ${abi} at ${planPath}")
file(READ "${planPath}" plan)
message("${plan}")
else()
message("Using preexisting build plan ${planPath}")
endif()
include("${planPath}")
writeTypesHeaders("${PLAN_TYPES}")
file(STRINGS "${CMAKE_SOURCE_DIR}/MACROS" MACROS)
file(STRINGS "${CMAKE_SOURCE_DIR}/PROTOTYPES.c" PROTOTYPES)
file(STRINGS "${CMAKE_SOURCE_DIR}/PROTOTYPES.cpp" PROTOTYPES_CPP)
file(STRINGS "${CMAKE_SOURCE_DIR}/OPERATIONS" OPERATIONS)
executePlan("${PLAN_IMPLEMENTATIONS}")
enable_testing()
add_subdirectory(tests)