cmake_minimum_required(VERSION 3.16)

project({{{packageName}}} VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS Core Network{{#enableQmlCode}} Qml{{/enableQmlCode}})
qt_standard_project_setup()

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

qt_add_library(${PROJECT_NAME}
{{#models}}
{{#model}}
    {{#lambda.lowercase}}{{{classname}}}{{/lambda.lowercase}}.h
{{/model}}
{{/models}}
{{#apiInfo}}
{{#apis}}
{{#operations}}
    {{#lambda.lowercase}}{{{classname}}}{{/lambda.lowercase}}.h
{{/operations}}
{{/apis}}
{{/apiInfo}}
{{#models}}
{{#model}}
    {{#lambda.lowercase}}{{{classname}}}{{/lambda.lowercase}}.cpp
{{/model}}
{{/models}}
{{#apiInfo}}
{{#apis}}
{{#operations}}
    {{#lambda.lowercase}}{{{classname}}}{{/lambda.lowercase}}.cpp
{{/operations}}
{{/apis}}
{{/apiInfo}}
){{#enableQmlCode}}

qt_policy(SET QTP0001 NEW)
qt_policy(SET QTP0004 NEW)
qt_add_library(${PROJECT_NAME}_module)
qt_add_qml_module(${PROJECT_NAME}_module
    URI client
    VERSION 1.0
)
target_include_directories(${PROJECT_NAME}_module
    PRIVATE
        ${CMAKE_CURRENT_BINARY_DIR} # find autogenerated header
)
target_link_libraries(${PROJECT_NAME}_module
    PRIVATE
        ${PROJECT_NAME}
)
qt_generate_foreign_qml_types(${PROJECT_NAME} ${PROJECT_NAME}_module)
# Make sure the backing library is found on Windows next to the executable
set_target_properties(
    ${PROJECT_NAME}_module
    PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/..
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/..
        ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/..
){{/enableQmlCode}}

target_include_directories(${PROJECT_NAME}
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
{{#enableCommonLibGeneration}}
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../common>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/../common>
{{/enableCommonLibGeneration}}
)

get_target_property(target_type ${PROJECT_NAME} TYPE)
if(target_type STREQUAL "SHARED_LIBRARY" OR target_type STREQUAL "MODULE_LIBRARY")
    target_compile_definitions(${PROJECT_NAME} PRIVATE {{prefix}}_LIB_SHARED)
elseif(target_type STREQUAL "STATIC_LIBRARY")
    target_compile_definitions(${PROJECT_NAME} PRIVATE {{prefix}}_LIB_STATIC)
endif()
target_compile_definitions(${PROJECT_NAME} PRIVATE {{prefix}}_BUILD_LIB)

target_link_libraries(${PROJECT_NAME}
    PUBLIC
        Qt::Core
        Qt::Network
{{#enableQmlCode}}
        Qt::QmlIntegration
{{/enableQmlCode}}
{{#enableCommonLibGeneration}}
    PRIVATE
        {{{commonLibraryName}}}
{{/enableCommonLibGeneration}}
)

configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/config.cmake.in
    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
    INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
)

install(
    TARGETS ${PROJECT_NAME}
    EXPORT ${PROJECT_NAME}Targets
    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
    INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}"
)

install(
    FILES ${HEADER_FILES}
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
)

install(
    FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)

install(
    EXPORT ${PROJECT_NAME}Targets
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
