diff --git a/MyxCMakeConfig.cmake b/MyxCMakeConfig.cmake index 842c9f7..8cfb635 100644 --- a/MyxCMakeConfig.cmake +++ b/MyxCMakeConfig.cmake @@ -1,6 +1,8 @@ cmake_policy(PUSH) cmake_policy(SET CMP0057 NEW) # IN_LIST operator +list(INSERT CMAKE_MODULE_PATH 0 "${PROJECT_SOURCE_DIR}/cmake/find") + get_filename_component(MYX_CMAKE_SOURCE_DIR "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY) set(MYX_CMAKE_BACKPORTS_DIR "${MYX_CMAKE_SOURCE_DIR}/backports") diff --git a/MyxCMakeConfigVersion.cmake b/MyxCMakeConfigVersion.cmake index 18f147a..3715903 100644 --- a/MyxCMakeConfigVersion.cmake +++ b/MyxCMakeConfigVersion.cmake @@ -1,4 +1,4 @@ -set(MYX_CMAKE_PACKAGE_VERSION "1.99.11") +set(MYX_CMAKE_PACKAGE_VERSION "1.99.12") if(MYX_CMAKE_PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) set(PACKAGE_VERSION_COMPATIBLE FALSE) else() diff --git a/README.md b/README.md index 266390b..c94c818 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ ```cmake if(MYX_CMAKE_USE_SYSTEM) - find_package(MyxCMake 1.99.11 REQUIRED) + find_package(MyxCMake 1.99.12 REQUIRED) else() include(${PROJECT_SOURCE_DIR}/cmake/myx/MyxCMakeConfig.cmake) endif() diff --git a/lib/FetchContentAdd.cmake b/lib/FetchContentAdd.cmake index e531de4..431fa3d 100644 --- a/lib/FetchContentAdd.cmake +++ b/lib/FetchContentAdd.cmake @@ -5,17 +5,17 @@ function(FetchContent_Add NAME) set(oneValueArgs GIT_REPOSITORY GIT_REMOTE GIT_PATH) set(multiValueArgs "") - cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) find_package(Git) - if(GIT_FOUND AND arg_GIT_REMOTE AND arg_GIT_PATH) - execute_process(COMMAND ${GIT_EXECUTABLE} config --get remote.${arg_GIT_REMOTE}.url OUTPUT_VARIABLE REMOTE_URL ERROR_QUIET) + if(GIT_FOUND AND ARG_GIT_REMOTE AND ARG_GIT_PATH) + execute_process(COMMAND ${GIT_EXECUTABLE} config --get remote.${ARG_GIT_REMOTE}.url OUTPUT_VARIABLE REMOTE_URL ERROR_QUIET) if(REMOTE_URL) string(REGEX REPLACE ":.*" "" SERVER ${REMOTE_URL}) string(FIND ${SERVER} "http" POS) if(NOT POS EQUAL 0) if(NOT SERVER STREQUAL REMOTE_URL) - set(arg_GIT_REPOSITORY "${SERVER}:${arg_GIT_PATH}") + set(ARG_GIT_REPOSITORY "${SERVER}:${ARG_GIT_PATH}") endif() endif() endif() @@ -23,8 +23,8 @@ function(FetchContent_Add NAME) FetchContent_Declare( ${NAME} - ${arg_UNPARSED_ARGUMENTS} - GIT_REPOSITORY ${arg_GIT_REPOSITORY} + ${ARG_UNPARSED_ARGUMENTS} + GIT_REPOSITORY ${ARG_GIT_REPOSITORY} ) if(NOT ${NAME}_POPULATED) diff --git a/lib/Qt5TargetSetup.cmake b/lib/Qt5TargetSetup.cmake index f225130..9cc53b6 100644 --- a/lib/Qt5TargetSetup.cmake +++ b/lib/Qt5TargetSetup.cmake @@ -4,28 +4,40 @@ function(myx_qt5_target_setup NAME) set(options) set(oneValueArgs) set(multiValueArgs COMPONENTS PRIVATE SOURCES MOC UI QRC LANGS) + cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) - if(arg_COMPONENTS) - find_package(Qt5 COMPONENTS ${arg_COMPONENTS} REQUIRED) - foreach(iter ${arg_COMPONENTS}) + get_target_property(type ${NAME} TYPE) + + if(ARG_COMPONENTS) + find_package(Qt5 COMPONENTS ${ARG_COMPONENTS} REQUIRED) + foreach(iter ${ARG_COMPONENTS}) target_include_directories(${NAME} PRIVATE ${Qt5${iter}_INCLUDE_DIRS}) + if(type STREQUAL "EXECUTABLE") + target_link_libraries(${NAME} PRIVATE "Qt5::${iter}") + endif() endforeach() endif() - if(arg_PRIVATE) - foreach(iter ${arg_PRIVATE}) - find_package(Qt5${iter} COMPONENTS Private REQUIRED) + if(ARG_PRIVATE) + foreach(iter ${ARG_PRIVATE}) + find_package("Qt5${iter}" COMPONENTS Private REQUIRED) endforeach() endif() - qt5_wrap_cpp(moc_cpp ${arg_MOC}) - qt5_wrap_ui(ui_h ${arg_UI}) - qt5_add_resources(qrc_cpp ${arg_QRC}) + if(type STREQUAL "EXECUTABLE") + target_compile_options(${NAME} PRIVATE ${Qt5Core_EXECUTABLE_COMPILE_FLAGS}) + endif() - if("LinguistTools" IN_LIST arg_COMPONENTS) + qt5_wrap_cpp(moc_cpp ${ARG_MOC}) + qt5_add_resources(qrc_cpp ${ARG_QRC}) + if("Widgets" IN_LIST ARG_COMPONENTS) + qt5_wrap_ui(ui_h ${ARG_UI}) + endif() + + if("LinguistTools" IN_LIST ARG_COMPONENTS) set(ts) - foreach(iter ${arg_LANGS}) + foreach(iter ${ARG_LANGS}) list(APPEND ts ${PROJECT_SOURCE_DIR}/l10n/${NAME}_${iter}.ts) endforeach() @@ -36,7 +48,6 @@ function(myx_qt5_target_setup NAME) ) endif() - target_sources(${NAME} - PRIVATE - ${arg_SOURCES} ${moc_cpp} ${ui_h} ${qrc_cpp} ${qm}) + target_sources(${NAME} PRIVATE ${ARG_SOURCES} ${ARG_MOC} ${moc_cpp} ${ui_h} ${qrc_cpp} ${qm}) + endfunction() diff --git a/lib/TargetSetup.cmake b/lib/TargetSetup.cmake index 9501472..5d9cb5e 100644 --- a/lib/TargetSetup.cmake +++ b/lib/TargetSetup.cmake @@ -4,25 +4,53 @@ function(myx_target_setup NAME) set(options) set(oneValueArgs PCH) set(multiValueArgs COMPILE_DEFINITIONS FIND_PACKAGES LINK_LIBRARIES SOURCES) - cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) - foreach(iter ${arg_FIND_PACKAGES}) - find_package(${iter} CONFIG REQUIRED) + cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(NOT TARGET ${NAME}) + myx_message_fatal_error("Target ${NAME} does not exists.") + endif() + + get_target_property(type ${NAME} TYPE) + + foreach(iter ${ARG_FIND_PACKAGES}) + find_package(${iter} REQUIRED) target_include_directories(${NAME} PRIVATE ${${iter}_INCLUDE_DIRS}) target_compile_definitions(${NAME} PRIVATE ${${iter}_COMPILE_DEFINITIONS}) endforeach() - target_compile_definitions(${NAME} PRIVATE ${arg_COMPILE_DEFINITIONS}) + target_compile_definitions(${NAME} PRIVATE ${ARG_COMPILE_DEFINITIONS}) - if(arg_PCH) + if(ARG_PCH) if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.16) - target_precompile_headers(${NAME} PRIVATE ${PROJECT_SOURCE_DIR}/${arg_PCH}) + target_precompile_headers(${NAME} PRIVATE "${PROJECT_SOURCE_DIR}/${ARG_PCH}") else() - target_compile_options(${NAME} PRIVATE -include ${PROJECT_SOURCE_DIR}/${arg_PCH}) + target_compile_options(${NAME} PRIVATE -include "${PROJECT_SOURCE_DIR}/${ARG_PCH}") endif() - target_sources(${NAME} PRIVATE ${PROJECT_SOURCE_DIR}/${arg_PCH}) + target_sources(${NAME} PRIVATE "${PROJECT_SOURCE_DIR}/${ARG_PCH}") endif() - target_sources(${NAME} PRIVATE ${arg_SOURCES}) - target_link_libraries(${NAME} PRIVATE ${arg_LINK_LIBRARIES}) - target_compile_definitions(${NAME} PRIVATE ${arg_COMPILE_DEFINITIONS}) + if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}/src") + target_include_directories(${PROJECT_NAME} PRIVATE + $) + endif() + + if(type STREQUAL EXECUTABLE) + if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}/include") + target_include_directories(${PROJECT_NAME} PRIVATE + $) + endif() + + set_target_properties(${NAME} PROPERTIES + POSITION_INDEPENDENT_CODE ON + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR} + ) + if(CMAKE_CXX_COMPILE_OPTIONS_PIE) + target_compile_options(${NAME} PUBLIC ${CMAKE_CXX_COMPILE_OPTIONS_PIE}) + endif() + install(TARGETS ${NAME} COMPONENT main RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + endif() + + target_sources(${NAME} PRIVATE ${ARG_SOURCES}) + target_link_libraries(${NAME} PRIVATE ${ARG_LINK_LIBRARIES}) + target_compile_definitions(${NAME} PRIVATE ${ARG_COMPILE_DEFINITIONS}) endfunction()