PothosUtil.cmake 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. if(DEFINED INCLUDED_POTHOS_UTIL_CMAKE)
  2. return()
  3. endif()
  4. set(INCLUDED_POTHOS_UTIL_CMAKE TRUE)
  5. ########################################################################
  6. ## POTHOS_MODULE_UTIL - build and install modules for Pothos
  7. ##
  8. ## This utility can handle the build and installation operations
  9. ## for most Pothos user projects. Otherwise, please copy and modify.
  10. ##
  11. ## Arguments:
  12. ##
  13. ## TARGET - the name of the module to build
  14. ## The target name should be unique per project.
  15. ## If this target shares a destination with other modules,
  16. ## Then the target should also be unique among those modules.
  17. ##
  18. ## SOURCES - a list of c++ source files
  19. ## This list will primarily consist of c++ source files and not headers.
  20. ## However, if header files contain documentation markup for the GUI,
  21. ## then these header files should be included for markup parsing.
  22. ## See ENABLE_DOCS which is required to enable SOURCES for scanning.
  23. ##
  24. ## LIBRARIES - a list of libraries to link the module to
  25. ## The module will automatically link to Pothos and Poco libraries.
  26. ## This argument should be used to specify additional libraries.
  27. ##
  28. ## DESTINATION - relative destination path
  29. ## The destination path is relative to the module directory.
  30. ## Try to use destination paths that reflect the plugin registry path.
  31. ## Example: Suppose you are building a module for /blocks/foo/bar,
  32. ## Then the destination should be blocks/foo and the TARGET bar.
  33. ## However, this is simply a recomendation.
  34. ##
  35. ## DOC_SOURCES - an alternative list of sources to scan for docs
  36. ## This allows the user to explicitly specity the list of sources
  37. ## that are used exclusively for documentation markup for the GUI.
  38. ##
  39. ## ENABLE_DOCS - enable scanning of SOURCES for documentation markup.
  40. ## Pass this flag to the util function to enable scanning of SOURCES.
  41. ## This is required to scan SOURCES but not when DOC_SOURCES are used.
  42. ##
  43. ## PREFIX - override the default install prefix when specified
  44. ## The prefix modifies the destination with an absolute path
  45. ## to replace the typical CMAKE_INSTALL_PREFIX install rules.
  46. ##
  47. ## VERSION - specify a version string to build into this module
  48. ## When not specified, the util will fall-back to PROJECT_VERSION,
  49. ## and scanning the in-tree Changelog.txt file (if available).
  50. ## Packagers can pass PROJECT_VERSION_EXTRA for additional version info.
  51. ##
  52. ########################################################################
  53. function(POTHOS_MODULE_UTIL)
  54. include(CMakeParseArguments)
  55. CMAKE_PARSE_ARGUMENTS(POTHOS_MODULE_UTIL "ENABLE_DOCS" "TARGET;DESTINATION;PREFIX;VERSION" "SOURCES;LIBRARIES;DOC_SOURCES" ${ARGN})
  56. #version not specified, try to use project version
  57. if (NOT POTHOS_MODULE_UTIL_VERSION AND PROJECT_VERSION)
  58. set(POTHOS_MODULE_UTIL_VERSION "${PROJECT_VERSION}")
  59. endif()
  60. #version not specified, try to use changelog entry
  61. if (NOT POTHOS_MODULE_UTIL_VERSION AND EXISTS "${PROJECT_SOURCE_DIR}/Changelog.txt")
  62. file(READ "${PROJECT_SOURCE_DIR}/Changelog.txt" changelog_txt)
  63. string(REGEX MATCH "Release ([-\\._0-9a-zA-Z]*) \\(" CHANGELOG_MATCH "${changelog_txt}")
  64. if(CHANGELOG_MATCH)
  65. set(POTHOS_MODULE_UTIL_VERSION "${CMAKE_MATCH_1}")
  66. endif(CHANGELOG_MATCH)
  67. endif()
  68. #additional version information when specified
  69. if (PROJECT_VERSION_EXTRA)
  70. if (POTHOS_MODULE_UTIL_VERSION)
  71. set(POTHOS_MODULE_UTIL_VERSION "${POTHOS_MODULE_UTIL_VERSION}-${PROJECT_VERSION_EXTRA}")
  72. else()
  73. set(POTHOS_MODULE_UTIL_VERSION "${PROJECT_VERSION_EXTRA}")
  74. endif()
  75. endif()
  76. #add git hash when possible
  77. if (EXISTS "${PROJECT_SOURCE_DIR}/.git")
  78. find_package(Git)
  79. if(GIT_FOUND)
  80. execute_process(
  81. COMMAND ${GIT_EXECUTABLE} -C "${PROJECT_SOURCE_DIR}" rev-parse --short HEAD
  82. OUTPUT_STRIP_TRAILING_WHITESPACE
  83. OUTPUT_VARIABLE GIT_HASH)
  84. if (GIT_HASH)
  85. if (POTHOS_MODULE_UTIL_VERSION)
  86. set(POTHOS_MODULE_UTIL_VERSION "${POTHOS_MODULE_UTIL_VERSION}-${GIT_HASH}")
  87. else()
  88. set(POTHOS_MODULE_UTIL_VERSION "${GIT_HASH}")
  89. endif()
  90. endif()
  91. endif(GIT_FOUND)
  92. endif()
  93. #setup module build and install rules
  94. add_library(${POTHOS_MODULE_UTIL_TARGET} MODULE ${POTHOS_MODULE_UTIL_SOURCES})
  95. target_link_libraries(${POTHOS_MODULE_UTIL_TARGET} PRIVATE Pothos ${POTHOS_MODULE_UTIL_LIBRARIES})
  96. set_target_properties(${POTHOS_MODULE_UTIL_TARGET} PROPERTIES DEBUG_POSTFIX "") #same name in debug mode
  97. #symbols are only exported from the module explicitly
  98. set_property(TARGET ${POTHOS_MODULE_UTIL_TARGET} PROPERTY C_VISIBILITY_PRESET hidden)
  99. set_property(TARGET ${POTHOS_MODULE_UTIL_TARGET} PROPERTY CXX_VISIBILITY_PRESET hidden)
  100. set_property(TARGET ${POTHOS_MODULE_UTIL_TARGET} PROPERTY VISIBILITY_INLINES_HIDDEN ON)
  101. #version specified, build into source file
  102. if (POTHOS_MODULE_UTIL_VERSION)
  103. message(STATUS "Module ${POTHOS_MODULE_UTIL_TARGET} configured with version: ${POTHOS_MODULE_UTIL_VERSION}")
  104. set(version_file "${CMAKE_CURRENT_BINARY_DIR}/Version.cpp")
  105. file(WRITE "${version_file}" "#include <Pothos/Plugin/Module.hpp>
  106. static const Pothos::ModuleVersion register${MODULE_TARGET}Version(\"${POTHOS_MODULE_UTIL_VERSION}\");
  107. ")
  108. target_sources(${POTHOS_MODULE_UTIL_TARGET} PRIVATE "${version_file}")
  109. endif()
  110. #always enable docs if user specifies doc sources
  111. if (POTHOS_MODULE_UTIL_DOC_SOURCES)
  112. set(POTHOS_MODULE_UTIL_ENABLE_DOCS TRUE)
  113. #otherwise doc sources come from the regular sources
  114. else()
  115. set(POTHOS_MODULE_UTIL_DOC_SOURCES ${POTHOS_MODULE_UTIL_SOURCES})
  116. endif()
  117. #setup json doc file generation and install
  118. if (POTHOS_MODULE_UTIL_ENABLE_DOCS)
  119. #turn sources into an absolute path
  120. unset(__POTHOS_SOURCES)
  121. foreach(source ${POTHOS_MODULE_UTIL_DOC_SOURCES})
  122. if (EXISTS ${source})
  123. list(APPEND __POTHOS_SOURCES ${source})
  124. else()
  125. list(APPEND __POTHOS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/${source})
  126. endif()
  127. endforeach(source)
  128. set(POTHOS_MODULE_UTIL_DOC_SOURCES ${__POTHOS_SOURCES})
  129. set(cpp_doc_file ${CMAKE_CURRENT_BINARY_DIR}/${POTHOS_MODULE_UTIL_TARGET}Docs.cpp)
  130. add_custom_command(
  131. OUTPUT ${cpp_doc_file}
  132. COMMAND ${POTHOS_UTIL_EXE}
  133. --doc-parse ${POTHOS_MODULE_UTIL_DOC_SOURCES}
  134. --output ${cpp_doc_file}
  135. DEPENDS ${POTHOS_MODULE_UTIL_DOC_SOURCES}
  136. DEPENDS ${__POTHOS_UTIL_TARGET_NAME}
  137. )
  138. target_sources(${POTHOS_MODULE_UTIL_TARGET} PRIVATE ${cpp_doc_file})
  139. set_property(SOURCE ${cpp_doc_file} PROPERTY SKIP_AUTOMOC ON)
  140. endif()
  141. set(MODULE_DESTINATION ${CMAKE_INSTALL_LIBDIR}/Pothos/modules${POTHOS_ABI_VERSION}/${POTHOS_MODULE_UTIL_DESTINATION})
  142. #determine user-specified or automatic install prefix
  143. if (POTHOS_MODULE_UTIL_PREFIX)
  144. set(MODULE_DESTINATION ${POTHOS_MODULE_UTIL_PREFIX}/${MODULE_DESTINATION})
  145. endif()
  146. if(CMAKE_COMPILER_IS_GNUCXX)
  147. #force a compile-time error when symbols are missing
  148. #otherwise modules will cause a runtime error on load
  149. target_link_libraries(${POTHOS_MODULE_UTIL_TARGET} PRIVATE "-Wl,--no-undefined")
  150. endif()
  151. install(
  152. TARGETS ${POTHOS_MODULE_UTIL_TARGET}
  153. DESTINATION ${MODULE_DESTINATION}
  154. )
  155. endfunction(POTHOS_MODULE_UTIL)