Browse Source

Replace qtprotobuf_link_archive with qtprotobuf_link_target

- New function does OBJECT library linking and adds dependency to
  generated code
Alexey Edelev 4 years ago
parent
commit
2927496dcb
3 changed files with 8 additions and 22 deletions
  1. 4 17
      cmake/QtProtobufGen.cmake
  2. 3 3
      src/generator/generator.h
  3. 1 2
      tests/test_qml/CMakeLists.txt

+ 4 - 17
cmake/QtProtobufGen.cmake

@@ -1,18 +1,6 @@
-function(qtprotobuf_link_archive TARGET GENERATED_TARGET)
-    set_target_properties(${qtprotobuf_generate_TARGET} PROPERTIES LINKER_LANGUAGE CXX)
-    set_target_properties(${TARGET} PROPERTIES LINK_DIRECTORIES "$<TARGET_FILE_DIR:${GENERATED_TARGET}>")
-    get_target_property(EXISTING_FLAGS ${TARGET} LINK_FLAGS)
-    if(WIN32)
-        if("${EXISTING_FLAGS}" STREQUAL "EXISTING_FLAGS-NOTFOUND")
-            set_target_properties(${TARGET} PROPERTIES LINK_FLAGS "/WHOLEARCHIVE:${GENERATED_TARGET}")
-        else()
-            set_target_properties(${TARGET} PROPERTIES LINK_FLAGS "${EXISTING_FLAGS} /WHOLEARCHIVE:${GENERATED_TARGET}")
-        endif()
-    else()
-        target_link_libraries(${TARGET} PRIVATE
-        -Wl,--whole-archive $<TARGET_FILE:${GENERATED_TARGET}> -Wl,--no-whole-archive)
-    endif()
-    target_include_directories(${TARGET} PRIVATE $<TARGET_PROPERTY:${GENERATED_TARGET},INCLUDE_DIRECTORIES>)
+function(qtprotobuf_link_target TARGET GENERATED_TARGET)
+    target_sources(${TARGET} PRIVATE $<TARGET_OBJECTS:${GENERATED_TARGET}>)
+    target_include_directories(${TARGET} PRIVATE $<TARGET_PROPERTY:${GENERATED_TARGET},INTERFACE_INCLUDE_DIRECTORIES>)
     add_dependencies(${TARGET} ${GENERATED_TARGET})
 endfunction()
 
@@ -165,7 +153,6 @@ function(qtprotobuf_generate)
 
     #Automatically link whole static library to specified in parameters target
     if(DEFINED qtprotobuf_generate_TARGET)
-        target_sources(${qtprotobuf_generate_TARGET} PRIVATE $<TARGET_OBJECTS:${GENERATED_TARGET_NAME}>)
-        target_include_directories(${qtprotobuf_generate_TARGET} PRIVATE $<TARGET_PROPERTY:${GENERATED_TARGET_NAME},INTERFACE_INCLUDE_DIRECTORIES>)
+        qtprotobuf_link_target(${qtprotobuf_generate_TARGET} ${GENERATED_TARGET_NAME})
     endif()
 endfunction()

+ 3 - 3
src/generator/generator.h

@@ -80,7 +80,7 @@ namespace generator {
  * For CMake based project QtProtobuf has macroses those should be used to generate code and in link it to your project:
  *
  * - \ref cmake_qtprotobuf_generate
- * - \ref cmake_qtprotobuf_link_archive
+ * - \ref cmake_qtprotobuf_link_target
  *
  *
  * \subsection cmake_qtprotobuf_generate qtprotobuf_generate
@@ -99,9 +99,9 @@ namespace generator {
  * \param COMMENTS Enables comments copying from .proto files. If provided in parameter list message and field related comments will be copied to generated header files.
  * \param FOLDER Enables folder based generation. If provided in parameter list generator will place generated artifacts to folder structure according to package of corresponding .proto file
  *
- * \subsection cmake_qtprotobuf_link_archive qtprotobuf_link_archive
+ * \subsection cmake_qtprotobuf_link_target qtprotobuf_link_target
  *
- * \brief qtprotobuf_link_archive is cmake helper function that links whole archive to your library or executable target.
+ * \brief qtprotobuf_link_target is cmake helper function that links generated protobuf target to your binary
  *
  * \details It's useful when you try to link generated target to shared library or/and to executable that doesn't utilize all protobuf generated classes directly from C++ code, but requires them from QML.
  *

+ 1 - 2
tests/test_qml/CMakeLists.txt

@@ -15,8 +15,7 @@ if(TARGET QtProtobufProject::QtProtobufWellKnownTypes)
     target_link_libraries(${TARGET} PRIVATE QtProtobufProject::QtProtobufWellKnownTypes)
 endif()
 
-target_sources(${TARGET} PRIVATE $<TARGET_OBJECTS:qtprotobuf_test_qtprotobuf_gen>)
-target_include_directories(${TARGET} PRIVATE $<TARGET_PROPERTY:qtprotobuf_test_qtprotobuf_gen,INTERFACE_INCLUDE_DIRECTORIES>)
+qtprotobuf_link_target(${TARGET} qtprotobuf_test_qtprotobuf_gen)
 
 add_target_qml(TARGET ${TARGET} QML_FILES ${QML_FILES})
 add_target_windeployqt(TARGET ${TARGET} QML_DIR ${CMAKE_CURRENT_SOURCE_DIR}/qml)