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