cpp-utilities/cmake/modules/AppTarget.cmake

143 lines
5.2 KiB
CMake

# before including this module, BasicConfig must be included
# check whether project type is set correctly
if(NOT "${META_PROJECT_TYPE}" STREQUAL "application")
message(FATAL_ERROR "The AppTarget CMake module is intended to be used for building application projects only (and not for libraries).")
endif()
# set the windows extension to "exe", this is required by the Windows specific WindowsResources module
if(WIN32)
set(WINDOWS_EXT "exe")
endif(WIN32)
# use correct linker flags and compile definitions (depend on linkage)
if(STATIC_LINKAGE)
set(ACTUAL_ADDITIONAL_LINK_FLAGS ${ADDITIONAL_STATIC_LINK_FLAGS})
set(ACTUAL_ADDITIONAL_COMPILE_DEFINITIONS ${ADDITIONAL_STATIC_COMPILE_DEFINITIONS})
else()
set(ACTUAL_ADDITIONAL_LINK_FLAGS ${ADDITIONAL_LINK_FLAGS})
set(ACTUAL_ADDITIONAL_COMPILE_DEFINITIONS ${ADDITIONAL_COMPILE_DEFINITIONS})
endif()
# add target for building the application
add_executable(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX} ${GUI_TYPE} ${HEADER_FILES} ${SRC_FILES} ${WIDGETS_FILES} ${QML_FILES} ${RES_FILES} ${QM_FILES} ${WINDOWS_ICON_PATH})
target_link_libraries(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX} ${ACTUAL_ADDITIONAL_LINK_FLAGS} ${LIBRARIES})
set_target_properties(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX} PROPERTIES
CXX_STANDARD 11
COMPILE_DEFINITIONS "${ACTUAL_ADDITIONAL_COMPILE_DEFINITIONS}"
LINK_SEARCH_START_STATIC ${STATIC_LINKAGE}
LINK_SEARCH_END_STATIC ${STATIC_LINKAGE}
)
if(MAKE_APP_TARGET_LINKABLE OR LINK_TESTS_AGAINST_APP_TARGET)
set_target_properties(${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX} PROPERTIES
ENABLE_EXPORTS ON
)
endif()
# add install target for binary
install(TARGETS ${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}
RUNTIME DESTINATION bin
COMPONENT binary
)
if(NOT TARGET install-binary)
add_custom_target(install-binary
DEPENDS ${META_PROJECT_NAME}
COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=binary -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
)
endif()
# add install target for localization
if(NOT TARGET install-mingw-w64)
add_custom_target(install-mingw-w64
DEPENDS install-binary ${LOCALIZATION_TARGET}
)
endif()
# add install target for desktop entries and icons
foreach(DESKTOP_FILE ${DESKTOP_FILES})
install(
FILES "${DESKTOP_FILE}"
DESTINATION "share/applications"
COMPONENT desktop
)
endforeach()
foreach(ICON_FILE ${ICON_FILES})
install(
FILES "${ICON_FILE}"
DESTINATION "share/icons/hicolor/scalable/apps"
COMPONENT desktop
)
endforeach()
if(NOT TARGET install-desktop)
add_custom_target(install-desktop
DEPENDS ${META_PROJECT_NAME}
COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=desktop -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
)
endif()
# add install target for stripped binaries
if(NOT TARGET install-binary-strip)
add_custom_target(install-binary-strip
DEPENDS ${META_PROJECT_NAME}
COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_DO_STRIP=1 -DCMAKE_INSTALL_COMPONENT=binary -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
)
endif()
# add mingw-w64 specific install target
if(NOT TARGET install-mingw-w64-strip)
add_custom_target(install-mingw-w64-strip
DEPENDS install-binary-strip ${LOCALIZATION_TARGET}
)
endif()
# add target for launching application with wine ensuring the WINEPATH is set correctly so wine is able to find all required *.dll files
# requires script from c++utilities, hence the sources of c++utilities must be present
if(MINGW AND CMAKE_CROSSCOMPILING AND CPP_UTILITIES_SOURCE_DIR)
if(NOT TARGET ${META_PROJECT_NAME}_run)
if(CMAKE_FIND_ROOT_PATH)
list(APPEND RUNTIME_LIBRARY_PATH "${CMAKE_FIND_ROOT_PATH}/bin")
endif()
add_custom_target(${META_PROJECT_NAME}_run COMMAND "${CPP_UTILITIES_SOURCE_DIR}/scripts/wine.sh" "${CMAKE_CURRENT_BINARY_DIR}/${META_PROJECT_NAME}.${WINDOWS_EXT}" ${RUNTIME_LIBRARY_PATH})
add_dependencies(${META_PROJECT_NAME}_run ${META_PROJECT_NAME})
endif()
endif()
# find template for *.desktop files
include(TemplateFinder)
find_template_file("desktop" CPP_UTILITIES APP_DESKTOP_TEMPLATE_FILE)
# function to add *.desktop files
function(add_custom_desktop_file
FILE_NAME
DESKTOP_FILE_APP_NAME
DESKTOP_FILE_DESCRIPTION
DESKTOP_FILE_CATEGORIES
DESKTOP_FILE_CMD
DESKTOP_FILE_ICON)
# create desktop file from template
configure_file(
"${APP_DESKTOP_TEMPLATE_FILE}"
"${CMAKE_CURRENT_BINARY_DIR}/resources/${FILE_NAME}.desktop"
)
# add install for the desktop file
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/resources/${FILE_NAME}.desktop"
DESTINATION "share/applications"
COMPONENT desktop
)
endfunction()
# convenience function to add *.desktop file from project meta data
function(add_desktop_file)
add_custom_desktop_file(
"${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}"
"${META_APP_NAME}"
"${META_APP_DESCRIPTION}"
"${META_APP_CATEGORIES}"
"${TARGET_PREFIX}${META_PROJECT_NAME}${TARGET_SUFFIX}"
"${META_PROJECT_NAME}"
)
endfunction()