Skip to content

Commit

Permalink
build: factor out manpage generation from toplevel CMakeLists.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Aug 9, 2024
1 parent ced2b0f commit b7024df
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 45 deletions.
47 changes: 2 additions & 45 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,41 +228,9 @@ if(STATIC_BUILD_DO_NOT_USE)
endif()

if(WITH_MAN_PAGES)
if(DWARFS_GIT_BUILD)
find_program(RONN_EXE ronn DOC "ronn man page generator" REQUIRED)
endif()

include("${CMAKE_SOURCE_DIR}/cmake/manpage.cmake")
foreach(man dwarfs.1 mkdwarfs.1 dwarfsck.1 dwarfsextract.1 dwarfs-format.5)
string(REGEX MATCH "^[^.]*" docname "${man}")
string(REGEX MATCH "[^.]*$" section "${man}")

if(DWARFS_GIT_BUILD)
set(man_dir "${CMAKE_CURRENT_BINARY_DIR}/man${section}")
set(man_input "${CMAKE_CURRENT_SOURCE_DIR}/doc/${docname}.md")
set(man_output "${man_dir}/${man}")

execute_process(
COMMAND ${RONN_EXE}
INPUT_FILE "${man_input}"
RESULT_VARIABLE ronn_result
OUTPUT_VARIABLE ronn_output
ERROR_VARIABLE ronn_error)

if(${ronn_result} EQUAL 0)
add_custom_command(
OUTPUT "${man_output}"
COMMAND mkdir -p "${man_dir}"
COMMAND ${RONN_EXE} <"${man_input}" >"${man_output}"
DEPENDS "${man_input}")
list(APPEND MAN_PAGES "${man_output}")
list(APPEND MAN_DIRS "${man_dir}")
else()
message(WARNING "${RONN_EXE} failed to process ${man_input} -> ${man}")
message(WARNING "error: ${ronn_error}")
endif()
else()
list(APPEND MAN_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/doc/man${section}")
endif()
add_manpage(${man})
endforeach()
endif()

Expand Down Expand Up @@ -851,13 +819,6 @@ if(WITH_FUZZ)
list(APPEND BINARY_TARGETS fuzz_mkdwarfs)
endif()

if(WITH_MAN_PAGES)
list(REMOVE_DUPLICATES MAN_DIRS)
if(DWARFS_GIT_BUILD)
add_custom_target(manpages ALL DEPENDS ${MAN_PAGES})
endif()
endif()

target_link_libraries(dwarfs_common PRIVATE dwarfs_thrift_lite)

add_cpp2_thrift_library(fbthrift/thrift/lib/thrift/frozen.thrift
Expand Down Expand Up @@ -1184,10 +1145,6 @@ add_custom_target(
format
COMMAND clang-format -i ${ALL_SOURCES})

foreach(man_dir ${MAN_DIRS})
install(DIRECTORY "${man_dir}" DESTINATION share/man)
endforeach()

if(NOT STATIC_BUILD_DO_NOT_USE)
foreach(tgt dwarfs_common dwarfs_reader dwarfs_writer dwarfs_extractor dwarfs_rewrite)
set_target_properties(${tgt} PROPERTIES VERSION ${PRJ_VERSION_MAJOR}.${PRJ_VERSION_MINOR}.${PRJ_VERSION_PATCH})
Expand Down
73 changes: 73 additions & 0 deletions cmake/manpage.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#
# Copyright (c) Marcus Holland-Moritz
#
# This file is part of dwarfs.
#
# dwarfs is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# dwarfs is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# dwarfs. If not, see <https://www.gnu.org/licenses/>.
#

cmake_minimum_required(VERSION 3.28.0)

function(add_manpage MANPAGE)
if(DWARFS_GIT_BUILD)
find_program(RONN_EXE ronn DOC "ronn man page generator" REQUIRED)
endif()

if(NOT TARGET manpages)
add_custom_target(manpages ALL)
set_target_properties(manpages PROPERTIES MANPAGE_DIRECTORIES "")
endif()

string(REGEX MATCH "^[^.]*" _docname "${MANPAGE}")
string(REGEX MATCH "[^.]*$" _section "${MANPAGE}")

set(_man_dir "")

if(DWARFS_GIT_BUILD)
set(_man_input "${CMAKE_CURRENT_SOURCE_DIR}/doc/${_docname}.md")

execute_process(
COMMAND ${RONN_EXE}
INPUT_FILE "${_man_input}"
RESULT_VARIABLE _ronn_result
OUTPUT_VARIABLE _ronn_output
ERROR_VARIABLE _ronn_error)

if(${_ronn_result} EQUAL 0)
set(_man_dir "${CMAKE_CURRENT_BINARY_DIR}/man${_section}")
set(_man_output "${_man_dir}/${MANPAGE}")
add_custom_command(
OUTPUT "${_man_output}"
COMMAND ${CMAKE_COMMAND} -E make_directory "${_man_dir}"
COMMAND ${RONN_EXE} <"${_man_input}" >"${_man_output}"
DEPENDS "${_man_input}")
add_custom_target("_manpage_${_docname}_${_section}" DEPENDS "${_man_output}")
add_dependencies(manpages "_manpage_${_docname}_${_section}")
else()
message(WARNING "${RONN_EXE} failed to process ${_man_input} -> ${MANPAGE}")
message(WARNING "error: ${_ronn_error}")
endif()
else()
set(_man_dir "${CMAKE_CURRENT_SOURCE_DIR}/doc/man${_section}")
endif()

if(_man_dir)
get_target_property(_man_dirs manpages MANPAGE_DIRECTORIES)
list(FIND _man_dirs "${_man_dir}" _index)
if(${_index} EQUAL -1)
list(APPEND _man_dirs "${_man_dir}")
install(DIRECTORY "${_man_dir}" DESTINATION share/man)
set_target_properties(manpages PROPERTIES MANPAGE_DIRECTORIES "${_man_dirs}")
endif()
endif()
endfunction()

0 comments on commit b7024df

Please sign in to comment.