# Example project: links the SaQura library the way your program will -
# once statically, once dynamically, both times only through saqura.h.
#
#   cmake -S example -B example/build -DSAQURA_LIB_DIR=<folder with libsaqura.* / saqura.dll>
#   cmake --build example/build --config Release
#   ./example/build/example-static    [license.lic]
#   ./example/build/example-dynamic   [license.lic]
#
# 3.15 is the floor on purpose: from there on policy CMP0091 is NEW and
# CMAKE_MSVC_RUNTIME_LIBRARY takes effect - nothing else is needed, and older
# build environments (Debian 11 ships 3.18) stay usable.
cmake_minimum_required(VERSION 3.15)

# Same minimum system as the library. Without it a program inherits the macOS
# of the build machine - the library would run on macOS 12, the program not.
# Must come before project().
if(APPLE AND NOT DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
    set(CMAKE_OSX_DEPLOYMENT_TARGET "12.0" CACHE STRING "minimum macOS, like the library")
endif()

project(saqura_example C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Windows: the same C runtime as the library, the static one. Mixed runtimes
# make the linker warn (LNK4098) and put two heaps into one process. A program
# that links the SaQura library must set this too - that is why it is here and
# not only in the manual.
if(MSVC)
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

if(NOT DEFINED SAQURA_LIB_DIR)
    message(FATAL_ERROR
        "SAQURA_LIB_DIR is missing - the folder with libsaqura.a and libsaqura.dylib/.so or saqura.dll.")
endif()

set(SAQURA_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../include"
    CACHE PATH "folder with saqura.h")

# --- static ---------------------------------------------------------------
# The static library already contains liboqs and OpenSSL; only the system
# libraries the Rust runtime needs are added.
add_library(saqura_static STATIC IMPORTED)
set_target_properties(saqura_static PROPERTIES
    IMPORTED_LOCATION "${SAQURA_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}saqura${CMAKE_STATIC_LIBRARY_SUFFIX}")

add_executable(example-static example.c)
target_include_directories(example-static PRIVATE "${SAQURA_INCLUDE_DIR}")
target_link_libraries(example-static PRIVATE saqura_static)
if(APPLE)
    target_link_libraries(example-static PRIVATE m)
elseif(UNIX)
    target_link_libraries(example-static PRIVATE pthread dl m)
elseif(WIN32)
    target_link_libraries(example-static PRIVATE
        ws2_32 advapi32 crypt32 user32 bcrypt userenv ntdll)
endif()

# --- dynamic --------------------------------------------------------------
add_library(saqura_dynamic SHARED IMPORTED)
set_target_properties(saqura_dynamic PROPERTIES
    IMPORTED_LOCATION "${SAQURA_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}saqura${CMAKE_SHARED_LIBRARY_SUFFIX}")
if(WIN32)
    set_target_properties(saqura_dynamic PROPERTIES
        IMPORTED_IMPLIB "${SAQURA_LIB_DIR}/saqura.dll.lib")
endif()

add_executable(example-dynamic example.c)
target_include_directories(example-dynamic PRIVATE "${SAQURA_INCLUDE_DIR}")
target_link_libraries(example-dynamic PRIVATE saqura_dynamic)
# Windows: SAQURA_DLL switches saqura.h to __declspec(dllimport).
if(WIN32)
    target_compile_definitions(example-dynamic PRIVATE SAQURA_DLL)
endif()

# The dynamic library identifies itself as "@rpath/libsaqura.dylib" or
# "libsaqura.so" - where it lives is up to the program that links it. Here it
# is the folder you passed in; in a shipped program usually the program's own
# folder (@executable_path or $ORIGIN). Pass -DSAQURA_RPATH=... to change it.
if(NOT DEFINED SAQURA_RPATH)
    set(SAQURA_RPATH "${SAQURA_LIB_DIR}")
endif()
if(APPLE OR UNIX)
    set_target_properties(example-dynamic PROPERTIES
        INSTALL_RPATH "${SAQURA_RPATH}"
        BUILD_WITH_INSTALL_RPATH ON)
elseif(WIN32)
    # Windows has no rpath: the DLL must sit next to the program - which is
    # also how you ship it.
    add_custom_command(TARGET example-dynamic POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${SAQURA_LIB_DIR}/saqura.dll" "$<TARGET_FILE_DIR:example-dynamic>")
endif()
