SDL: cmake: allow man page installation by SDL satellite libraries

From 937f5d0059664c37d0dffcaf94422afd310407ea Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Tue, 27 Jun 2023 02:49:05 +0200
Subject: [PATCH] cmake: allow man page installation by SDL satellite libraries

This is done by installing the wikiheaders.pl script, and add a wrapping cmake function.
---
 CMakeLists.txt            | 30 +++++--------------
 cmake/SDL3Config.cmake.in |  5 ++++
 cmake/sdlmanpages.cmake   | 61 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 22 deletions(-)
 create mode 100644 cmake/sdlmanpages.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ec1de6d0f8a0..17d99c467df2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -59,6 +59,7 @@ find_package(PkgConfig)
 
 list(APPEND CMAKE_MODULE_PATH "${SDL3_SOURCE_DIR}/cmake")
 include(${SDL3_SOURCE_DIR}/cmake/macros.cmake)
+include(${SDL3_SOURCE_DIR}/cmake/sdlmanpages.cmake)
 include(${SDL3_SOURCE_DIR}/cmake/sdlchecks.cmake)
 include(${SDL3_SOURCE_DIR}/cmake/sdlfind.cmake)
 include(${SDL3_SOURCE_DIR}/cmake/sdlplatform.cmake)
@@ -3536,6 +3537,7 @@ else()
 endif()
 
 configure_file("cmake/sdlfind.cmake" "sdlfind.cmake" COPYONLY)
+configure_file("cmake/sdlmanpages.cmake" "sdlmanpages.cmake" COPYONLY)
 
 include(CMakePackageConfigHelpers)
 configure_package_config_file(cmake/SDL3Config.cmake.in SDL3Config.cmake
@@ -3654,6 +3656,8 @@ if(NOT SDL_DISABLE_INSTALL)
       ${CMAKE_CURRENT_BINARY_DIR}/SDL3Config.cmake
       ${CMAKE_CURRENT_BINARY_DIR}/SDL3ConfigVersion.cmake
       ${SDL3_SOURCE_DIR}/cmake/sdlfind.cmake
+      ${SDL3_SOURCE_DIR}/build-scripts/wikiheaders.pl
+      ${SDL3_SOURCE_DIR}/cmake/sdlmanpages.cmake
     DESTINATION "${SDL_SDL_INSTALL_REAL_CMAKEDIR}"
   )
 
@@ -3686,29 +3690,11 @@ if(NOT SDL_DISABLE_INSTALL)
     include(CPack)
   endif()
 
+  set(WIKIHEADERS_PL_PATH "${CMAKE_CURRENT_SOURCE_DIR}/build-scripts/wikiheaders.pl")
   if(NOT SDL_DISABLE_INSTALL_MAN)
-    find_package(Perl)
-    if(PERL_FOUND)
-      file(GLOB SDL3_MAN_INCLUDE_FILES "${SDL3_SOURCE_DIR}/include/SDL3/*.h")
-      set(SDL3_BINARY_DOCDIR "${SDL3_BINARY_DIR}/docs")
-      set(SDL3_BINARY_DOC_WIKIDIR "${SDL3_BINARY_DOCDIR}/wiki")
-      set(SDL3_BINARY_DOC_MANDIR "${SDL3_BINARY_DOCDIR}/man")
-      execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory "${SDL3_BINARY_DOC_WIKIDIR}")
-      add_custom_command(
-        OUTPUT "${SDL3_BINARY_DOC_WIKIDIR}/SDL_Init.md"
-        COMMAND "${PERL_EXECUTABLE}" "${SDL3_SOURCE_DIR}/build-scripts/wikiheaders.pl" "${SDL3_SOURCE_DIR}" "${SDL3_BINARY_DOC_WIKIDIR}" "--options=${SDL3_SOURCE_DIR}/.wikiheaders-options" --copy-to-wiki
-        DEPENDS ${SDL3_MAN_INCLUDE_FILES} "${SDL3_SOURCE_DIR}/build-scripts/wikiheaders.pl" "${SDL3_SOURCE_DIR}/.wikiheaders-options"
-        COMMENT "Generating SDL3 wiki markdown files"
-      )
-      add_custom_command(
-        OUTPUT "${SDL3_BINARY_DOC_MANDIR}/man3/SDL_Init.3"
-        COMMAND "${PERL_EXECUTABLE}" "${SDL3_SOURCE_DIR}/build-scripts/wikiheaders.pl" "${SDL3_SOURCE_DIR}" "${SDL3_BINARY_DOC_WIKIDIR}" "--options=${SDL3_SOURCE_DIR}/.wikiheaders-options" --"manpath=${SDL3_BINARY_DOC_MANDIR}" --copy-to-manpages
-        DEPENDS "${SDL3_BINARY_DOC_WIKIDIR}/SDL_Init.md" "${SDL3_SOURCE_DIR}/build-scripts/wikiheaders.pl" "${SDL3_SOURCE_DIR}/.wikiheaders-options"
-        COMMENT "Generating SDL3 man pages"
-      )
-      add_custom_target(SDL3_docs ALL DEPENDS "${SDL3_BINARY_DOC_MANDIR}/man3/SDL_Init.3")
-      install(DIRECTORY "${SDL3_BINARY_DOCDIR}/man/" DESTINATION "${CMAKE_INSTALL_MANDIR}")
-    endif()
+    SDL_generate_manpages(
+      SYMBOL "SDL_Init"
+    )
   endif()
 endif()
 
diff --git a/cmake/SDL3Config.cmake.in b/cmake/SDL3Config.cmake.in
index 18bce2706cb1..3b4033167639 100644
--- a/cmake/SDL3Config.cmake.in
+++ b/cmake/SDL3Config.cmake.in
@@ -93,3 +93,8 @@ set(SDL3_STATIC_LIBRARIES SDL3::SDL3-static)
 set(SDL3_STATIC_PRIVATE_LIBS)
 
 set(SDL3TEST_LIBRARY SDL3::SDL3_test)
+
+if(SDL3_FOUND)
+  set(WIKIHEADERS_PL_PATH "${CMAKE_CURRENT_LIST_DIR}/wikiheaders.pl")
+  include("${CMAKE_CURRENT_LIST_DIR}/sdlmanpages.cmake")
+endif()
diff --git a/cmake/sdlmanpages.cmake b/cmake/sdlmanpages.cmake
new file mode 100644
index 000000000000..12593d29377f
--- /dev/null
+++ b/cmake/sdlmanpages.cmake
@@ -0,0 +1,61 @@
+include(CMakeParseArguments)
+include(GNUInstallDirs)
+
+function(SDL_generate_manpages)
+  cmake_parse_arguments(ARG "" "RESULT_VARIABLE;NAME;BUILD_DOCDIR;HEADERS_DIR;SOURCE_DIR;SYMBOL;OPTION_FILE" "" ${ARGN})
+
+  if(NOT ARG_NAME)
+    set(ARG_NAME "${PROJECT_NAME}")
+  endif()
+
+  if(NOT ARG_SOURCE_DIR)
+    set(ARG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
+  endif()
+
+  if(NOT ARG_OPTION_FILE)
+    set(ARG_OPTION_FILE "${PROJECT_SOURCE_DIR}/.wikiheaders-options")
+  endif()
+
+  if(NOT ARG_HEADERS_DIR)
+    set(ARG_HEADERS_DIR "${PROJECT_SOURCE_DIR}/include/SDL3")
+  endif()
+
+  # FIXME: get rid of SYMBOL and let the perl script figure out the dependencies
+  if(NOT ARG_SYMBOL)
+    message(FATAL_ERROR "Missing required SYMBOL argument")
+  endif()
+
+  if(NOT ARG_BUILD_DOCDIR)
+    set(ARG_BUILD_DOCDIR "${CMAKE_CURRENT_BINARY_DIR}/docs")
+  endif()
+  set(BUILD_WIKIDIR "${ARG_BUILD_DOCDIR}/wiki")
+  set(BUILD_MANDIR "${ARG_BUILD_DOCDIR}/man")
+
+  find_package(Perl)
+  file(GLOB HEADER_FILES "${HEADERS_DIR}/*.h")
+
+  set(result FALSE)
+
+  if(PERL_FOUND AND EXISTS "${WIKIHEADERS_PL_PATH}")
+    add_custom_command(
+      OUTPUT "${BUILD_WIKIDIR}/${ARG_SYMBOL}.md"
+      COMMAND "${PERL_EXECUTABLE}" "${WIKIHEADERS_PL_PATH}" "${ARG_SOURCE_DIR}" "${BUILD_WIKIDIR}" "--options=${ARG_OPTION_FILE}" --copy-to-wiki
+      DEPENDS ${HEADER_FILES} "${WIKIHEADERS_PL_PATH}" "${ARG_OPTION_FILE}"
+      COMMENT "Generating ${ARG_NAME} wiki markdown files"
+    )
+    add_custom_command(
+      OUTPUT "${BUILD_MANDIR}/man3/${ARG_SYMBOL}.3"
+      COMMAND "${PERL_EXECUTABLE}" "${WIKIHEADERS_PL_PATH}" "${ARG_SOURCE_DIR}" "${BUILD_WIKIDIR}" "--options=${ARG_OPTION_FILE}" "--manpath=${BUILD_MANDIR}" --copy-to-manpages
+      DEPENDS  "${BUILD_WIKIDIR}/${ARG_SYMBOL}.md" "${WIKIHEADERS_PL_PATH}" "${ARG_OPTION_FILE}"
+      COMMENT "Generating ${ARG_NAME} man pages"
+    )
+    add_custom_target(${ARG_NAME}-docs ALL DEPENDS "${BUILD_MANDIR}/man3/${ARG_SYMBOL}.3")
+
+    install(DIRECTORY "${BUILD_MANDIR}/" DESTINATION "${CMAKE_INSTALL_MANDIR}")
+    set(result TRUE)
+  endif()
+
+  if(ARG_RESULT_VARIABLE)
+    set(${ARG_RESULT_VARIABLE} ${result} PARENT_SCOPE)
+  endif()
+endfunction()