1set(libs
2    ${mbedtls_target}
3)
4
5set(executables_libs
6    selftest
7    udp_proxy
8)
9
10set(executables_mbedcrypto
11    benchmark
12    query_compile_time_config
13    zeroize
14)
15
16if(TEST_CPP)
17    set(cpp_dummy_build_cpp "${CMAKE_CURRENT_BINARY_DIR}/cpp_dummy_build.cpp")
18    set(generate_cpp_dummy_build "${CMAKE_CURRENT_SOURCE_DIR}/generate_cpp_dummy_build.sh")
19    add_custom_command(
20        OUTPUT "${cpp_dummy_build_cpp}"
21        COMMAND "${generate_cpp_dummy_build}" "${cpp_dummy_build_cpp}"
22        DEPENDS "${generate_cpp_dummy_build}"
23        WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
24    )
25    add_executable(cpp_dummy_build "${cpp_dummy_build_cpp}")
26    target_include_directories(cpp_dummy_build PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../include)
27    target_link_libraries(cpp_dummy_build ${mbedcrypto_target})
28endif()
29
30if(GEN_FILES)
31    find_package(Perl REQUIRED)
32
33    add_custom_command(
34        OUTPUT
35            ${CMAKE_CURRENT_BINARY_DIR}/query_config.c
36        COMMAND
37            ${PERL}
38                ${CMAKE_CURRENT_SOURCE_DIR}/../../scripts/generate_query_config.pl
39                ${CMAKE_CURRENT_SOURCE_DIR}/../../include/mbedtls/mbedtls_config.h
40                ${CMAKE_CURRENT_SOURCE_DIR}/../../scripts/data_files/query_config.fmt
41                ${CMAKE_CURRENT_BINARY_DIR}/query_config.c
42        DEPENDS
43            ${CMAKE_CURRENT_SOURCE_DIR}/../../scripts/generate_query_config.pl
44            ${CMAKE_CURRENT_SOURCE_DIR}/../../include/mbedtls/mbedtls_config.h
45            ${CMAKE_CURRENT_SOURCE_DIR}/../../scripts/data_files/query_config.fmt
46    )
47    # this file will also be used in another directory, so create a target, see
48    # https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#how-can-i-add-a-dependency-to-a-source-file-which-is-generated-in-a-subdirectory
49    add_custom_target(generate_query_config_c
50        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/query_config.c)
51else()
52    link_to_source(query_config.c)
53endif()
54
55foreach(exe IN LISTS executables_libs executables_mbedcrypto)
56    set(extra_sources "")
57    if(exe STREQUAL "query_compile_time_config")
58        list(APPEND extra_sources
59            ${CMAKE_CURRENT_SOURCE_DIR}/query_config.h
60            ${CMAKE_CURRENT_BINARY_DIR}/query_config.c)
61    endif()
62    add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>
63        ${extra_sources})
64    target_include_directories(${exe} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../tests/include)
65    if(exe STREQUAL "query_compile_time_config")
66        target_include_directories(${exe} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
67    endif()
68
69    # This emulates "if ( ... IN_LIST ... )" which becomes available in CMake 3.3
70    list(FIND executables_libs ${exe} exe_index)
71    if (${exe_index} GREATER -1)
72        target_link_libraries(${exe} ${libs})
73    else()
74        target_link_libraries(${exe} ${mbedcrypto_target})
75    endif()
76endforeach()
77
78install(TARGETS ${executables_libs} ${executables_mbedcrypto}
79        DESTINATION "bin"
80        PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
81