| 1 |
# Copyright (c) 2020, The Monero Project |
| 2 |
# |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without modification, are |
| 6 |
# permitted provided that the following conditions are met: |
| 7 |
# |
| 8 |
# 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
# conditions and the following disclaimer. |
| 10 |
# |
| 11 |
# 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 |
# of conditions and the following disclaimer in the documentation and/or other |
| 13 |
# materials provided with the distribution. |
| 14 |
# |
| 15 |
# 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 |
# used to endorse or promote products derived from this software without specific |
| 17 |
# prior written permission. |
| 18 |
# |
| 19 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 |
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 |
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 |
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 |
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 |
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
set(MONERO_CRYPTO_DIR ${CMAKE_CURRENT_LIST_DIR}) |
| 30 |
add_subdirectory("${MONERO_CRYPTO_DIR}/src" ${CMAKE_CURRENT_BINARY_DIR}/monero_crypto_src) |
| 31 |
|
| 32 |
# Set `OUT` to a list of all valid library names. |
| 33 |
function(monero_crypto_libraries OUT) |
| 34 |
set(${OUT} "amd64-64-24k" "amd64-51-30k" PARENT_SCOPE) |
| 35 |
endfunction (monero_crypto_libraries) |
| 36 |
|
| 37 |
# `OUT` is set to 1 iff `CRYPTO_LIBRARY` is a valid library name. |
| 38 |
function(monero_crypto_valid CRYPTO_LIBRARY OUT) |
| 39 |
monero_crypto_libraries(ALL_LIBRARIES) |
| 40 |
list(FIND ALL_LIBRARIES ${CRYPTO_LIBRARY} FOUND) |
| 41 |
if (FOUND LESS 0) |
| 42 |
set(${OUT} 0 PARENT_SCOPE) |
| 43 |
else () |
| 44 |
set(${OUT} 1 PARENT_SCOPE) |
| 45 |
endif () |
| 46 |
endfunction (monero_crypto_valid) |
| 47 |
|
| 48 |
# Set `OUT` to a valid C/C++ namespace name based on `CRYPTO_LIBRARY`. |
| 49 |
function(monero_crypto_get_namespace CRYPTO_LIBRARY OUT) |
| 50 |
string(REPLACE "-" "_" TEMP "${CRYPTO_LIBRARY}") |
| 51 |
set(${OUT} ${TEMP} PARENT_SCOPE) |
| 52 |
endfunction (monero_crypto_get_namespace) |
| 53 |
|
| 54 |
# Set `OUT` to a valid target dependency name based on `CRYPTO_LIBRARY`. |
| 55 |
function(monero_crypto_get_target CRYPTO_LIBRARY OUT) |
| 56 |
set(${OUT} "monero-crypto-${CRYPTO_LIBRARY}" PARENT_SCOPE) |
| 57 |
endfunction (monero_crypto_get_target) |
| 58 |
|
| 59 |
# Sets `BEST` to the preferred crypto library for the target platform, and |
| 60 |
# sets `AVAILABLE` to all valid crypto libraries for the target platform. |
| 61 |
function(monero_crypto_autodetect AVAILABLE BEST) |
| 62 |
include(CheckLanguage) |
| 63 |
check_language(ASM-ATT) |
| 64 |
if (CMAKE_ASM-ATT_COMPILER AND UNIX AND (CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|AMD64.*|x86_64.*")) |
| 65 |
set(${AVAILABLE} "amd64-64-24k" "amd64-51-30k" PARENT_SCOPE) |
| 66 |
set(${BEST} "amd64-64-24k" PARENT_SCOPE) |
| 67 |
else () |
| 68 |
message("Monero crypto autodetect failed to find any libraries for target platform") |
| 69 |
unset(${AVAILABLE} PARENT_SCOPE) |
| 70 |
unset(${BEST} PARENT_SCOPE) |
| 71 |
endif () |
| 72 |
endfunction (monero_crypto_autodetect) |
| 73 |
|
| 74 |
# Writes a C header to `HEADER_FILE` that contains these C functions: |
| 75 |
# - `monero_crypto_generate_key_derivation` |
| 76 |
# - `monero_crypto_derive_subaddress_public_key` |
| 77 |
# that map to the selected `CRYPTO_LIBRARY` implementation using macro |
| 78 |
# replacement. See README.md for function explanation. |
| 79 |
# |
| 80 |
# A fatal error is generated if `CRYPTO_LIBRARY` is not valid - linking will |
| 81 |
# always fails in this situation. |
| 82 |
function(monero_crypto_generate_header MONERO_CRYPTO_LIBRARY HEADER_FILE) |
| 83 |
monero_crypto_valid(${MONERO_CRYPTO_LIBRARY} VALID) |
| 84 |
if (NOT VALID) |
| 85 |
monero_crypto_libraries(ALL_LIBRARIES) |
| 86 |
string(REPLACE ";" " " ALL_LIBRARIES "${ALL_LIBRARIES}") |
| 87 |
message(FATAL_ERROR "Library ${MONERO_CRYPTO_LIBRARY} is not valid. Must be one of: ${ALL_LIBRARIES}") |
| 88 |
endif () |
| 89 |
monero_crypto_get_namespace(${MONERO_CRYPTO_LIBRARY} MONERO_CRYPTO_NAMESPACE) |
| 90 |
configure_file("${MONERO_CRYPTO_DIR}/src/crypto.h.in" ${HEADER_FILE}) |
| 91 |
endfunction (monero_crypto_generate_header) |
| 92 |
|