Open Quantum Safe Library Documentation: CMakeLists

# SPDX-License-Identifier: MIT

option(OQS_ENABLE_TEST_CONSTANT_TIME "Build test suite with support for Valgrind-based detection of non-constant time behaviour." OFF)

if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR
   CMAKE_C_COMPILER_ID MATCHES "Clang")
    add_compile_options(-Wbad-function-cast)
endif()
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
    add_compile_options(-Wcast-qual)
    add_compile_options(-Wnarrowing)
    add_compile_options(-Wconversion)
endif()
if (MINGW OR MSYS OR CYGWIN)
    add_definitions(-D__USE_MINGW_ANSI_STDIO=1)
    add_compile_options(-Wno-unknown-pragmas)
    add_compile_options(-Wno-unused-parameter)
endif()
if(MSVC)
    # ignore warning for test apps
    add_compile_options(/wd4996 /wd4244)
endif()

if(OQS_USE_OPENSSL)
    include_directories(${OPENSSL_INCLUDE_DIR})
endif()

if(NOT WIN32)
    set(LIBM m)
endif()

# List oqs-internal after oqs so that oqs has linking precedence.
if(${OQS_USE_OPENSSL})
    find_package(OpenSSL 1.1.1 REQUIRED)
    set(TEST_DEPS oqs oqs-internal ${LIBM} OpenSSL::Crypto)
else()
    set(TEST_DEPS oqs oqs-internal ${LIBM})
endif()

if(OQS_USE_PTHREADS)
    set(TEST_DEPS ${TEST_DEPS} Threads::Threads)
endif()

if(NOT WIN32)
    execute_process(COMMAND ${PROJECT_SOURCE_DIR}/scripts/git_commit.sh OUTPUT_VARIABLE GIT_COMMIT)
    add_definitions(-DOQS_COMPILE_GIT_COMMIT="${GIT_COMMIT}")

    add_executable(test_aes test_aes.c)
    target_link_libraries(test_aes PRIVATE ${TEST_DEPS})

    add_executable(test_hash test_hash.c)
    target_link_libraries(test_hash PRIVATE ${TEST_DEPS})

    add_executable(test_sha3 test_sha3.c)
    target_link_libraries(test_sha3 PRIVATE ${TEST_DEPS})

    add_executable(speed_common speed_common.c)
    target_link_libraries(speed_common PRIVATE ${TEST_DEPS})

    set(UNIX_TESTS test_aes test_hash test_sha3 speed_common)

    set(PYTHON3_EXEC python3)
else()
    set(PYTHON3_EXEC python)
endif()

# KEM API tests
add_executable(example_kem example_kem.c)
target_link_libraries(example_kem PRIVATE ${TEST_DEPS})

add_executable(kat_kem kat_kem.c test_helpers.c)
target_link_libraries(kat_kem PRIVATE ${TEST_DEPS})
if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND BUILD_SHARED_LIBS)
    # workaround for Windows .dll
    if(MINGW OR MSYS OR CYGWIN OR CMAKE_CROSSCOMPILING)
        target_link_options(kat_kem PRIVATE -Wl,--allow-multiple-definition)
    else()
        target_link_options(kat_kem PRIVATE "/FORCE:MULTIPLE")
    endif()
endif()

add_executable(test_kem test_kem.c test_helpers.c)
target_link_libraries(test_kem PRIVATE ${TEST_DEPS})

if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND BUILD_SHARED_LIBS)
    # workaround for Windows .dll
    if(MINGW OR MSYS OR CYGWIN OR CMAKE_CROSSCOMPILING)
        target_link_options(test_kem PRIVATE -Wl,--allow-multiple-definition)
    else()
        target_link_options(test_kem PRIVATE "/FORCE:MULTIPLE")
    endif()
endif()

add_executable(test_kem_mem test_kem_mem.c test_helpers.c)
target_link_libraries(test_kem_mem PRIVATE ${TEST_DEPS})
if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND BUILD_SHARED_LIBS)
    # workaround for Windows .dll
    if(MINGW OR MSYS OR CYGWIN OR CMAKE_CROSSCOMPILING)
        target_link_options(test_kem_mem PRIVATE -Wl,--allow-multiple-definition)
    else()
        target_link_options(test_kem_mem PRIVATE "/FORCE:MULTIPLE")
    endif()
endif()

add_executable(speed_kem speed_kem.c)
target_link_libraries(speed_kem PRIVATE ${TEST_DEPS})

set(KEM_TESTS example_kem kat_kem test_kem test_kem_mem speed_kem vectors_kem)

# SIG API tests
add_executable(example_sig example_sig.c)
target_link_libraries(example_sig PRIVATE ${TEST_DEPS})

if(OQS_BUILD_FUZZ_TESTS AND CMAKE_C_COMPILER_ID MATCHES "Clang")
    add_executable(fuzz_test_sig fuzz_test_sig.c)
    target_link_libraries(fuzz_test_sig PRIVATE ${TEST_DEPS})
    set_target_properties(fuzz_test_sig PROPERTIES
        COMPILE_FLAGS "${FUZZING_COMPILE_FLAGS}"
        LINK_FLAGS "${FUZZING_LINK_FLAGS}"
    )
    add_executable(fuzz_test_kem fuzz_test_kem.c)
    target_link_libraries(fuzz_test_kem PRIVATE ${TEST_DEPS})
    set_target_properties(fuzz_test_kem PROPERTIES
        COMPILE_FLAGS "${FUZZING_COMPILE_FLAGS}"
        LINK_FLAGS "${FUZZING_LINK_FLAGS}"
    )
    add_executable(fuzz_test_sig_stfl_xmss fuzz_test_sig_stfl_xmss.c)
    target_link_libraries(fuzz_test_sig_stfl_xmss PRIVATE ${TEST_DEPS})
    set_target_properties(fuzz_test_sig_stfl_xmss PROPERTIES
        COMPILE_FLAGS "${FUZZING_COMPILE_FLAGS}"
        LINK_FLAGS "${FUZZING_LINK_FLAGS}"
    )
    add_executable(fuzz_test_sig_stfl_lms fuzz_test_sig_stfl_lms.c)
    target_link_libraries(fuzz_test_sig_stfl_lms PRIVATE ${TEST_DEPS})
    set_target_properties(fuzz_test_sig_stfl_lms PROPERTIES
        COMPILE_FLAGS "${FUZZING_COMPILE_FLAGS}"
        LINK_FLAGS "${FUZZING_LINK_FLAGS}"
    )
endif()

# Stateful SIG API tests
add_executable(example_sig_stfl example_sig_stfl.c)
target_link_libraries(example_sig_stfl PRIVATE ${TEST_DEPS})

add_executable(kat_sig kat_sig.c test_helpers.c)
target_link_libraries(kat_sig PRIVATE ${TEST_DEPS})
if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND BUILD_SHARED_LIBS)
    # workaround for Windows .dll
    if(MINGW OR MSYS OR CYGWIN OR CMAKE_CROSSCOMPILING)
        target_link_options(kat_sig PRIVATE -Wl,--allow-multiple-definition)
    else()
        target_link_options(kat_sig PRIVATE "/FORCE:MULTIPLE")
    endif()
endif()

add_executable(kat_sig_stfl kat_sig_stfl.c test_helpers.c)
target_link_libraries(kat_sig_stfl PRIVATE ${TEST_DEPS})
if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND BUILD_SHARED_LIBS)
    # workaround for Windows .dll
    if(CMAKE_CROSSCOMPILING)
        target_link_options(kat_sig_stfl PRIVATE -Wl,--allow-multiple-definition)
    else()
        target_link_options(kat_sig_stfl PRIVATE "/FORCE:MULTIPLE")
    endif()
endif()

add_executable(test_sig test_sig.c test_helpers.c)
target_link_libraries(test_sig PRIVATE ${TEST_DEPS})
if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND BUILD_SHARED_LIBS)
    # workaround for Windows .dll
    if(CMAKE_CROSSCOMPILING)
        target_link_options(test_sig PRIVATE -Wl,--allow-multiple-definition)
    else()
        target_link_options(test_sig PRIVATE "/FORCE:MULTIPLE")
    endif()
endif()

add_executable(test_sig_mem test_sig_mem.c)
target_link_libraries(test_sig_mem PRIVATE ${TEST_DEPS})

add_executable(speed_sig speed_sig.c)
target_link_libraries(speed_sig PRIVATE ${TEST_DEPS})


set(SIG_TESTS example_sig kat_sig test_sig test_sig_mem speed_sig vectors_sig)

# SIG_STFL API tests
add_executable(test_sig_stfl test_sig_stfl.c test_helpers.c)
if((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_C_COMPILER_ID STREQUAL "GNU"))
    target_link_libraries(test_sig_stfl PRIVATE ${TEST_DEPS} Threads::Threads)
else ()
    target_link_libraries(test_sig_stfl PRIVATE ${TEST_DEPS})
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND BUILD_SHARED_LIBS)
    # workaround for Windows .dll
    if(CMAKE_CROSSCOMPILING)
        target_link_options(test_sig_stfl PRIVATE -Wl,--allow-multiple-def