CMake: Try to avoid Strawberry Perl's xsltproc.
[metze/wireshark/wip.git] / cmake / modules / FindXSLTPROC.cmake
1 #
2 # - Find unix commands from cygwin
3 # This module looks for some usual Unix commands.
4 #
5
6 include(FindCygwin)
7
8 # Strawberry Perl ships with xsltproc but no DocBook XML files, which
9 # is detrimental to our interests. Search for the Chocolatey and Cygwin
10 # versions first.
11 find_program(XSLTPROC_EXECUTABLE
12   NAMES
13     xsltproc
14   HINTS
15     ${ChocolateyInstall}/bin
16     ${CYGWIN_INSTALL_PATH}/bin
17   PATHS
18     /usr/local/bin
19     /sbin
20 )
21
22 # Handle the QUIETLY and REQUIRED arguments and set XSLTPROC_FOUND to TRUE if
23 # all listed variables are TRUE
24 INCLUDE(FindPackageHandleStandardArgs)
25 FIND_PACKAGE_HANDLE_STANDARD_ARGS(XSLTPROC DEFAULT_MSG XSLTPROC_EXECUTABLE)
26
27 MARK_AS_ADVANCED(XSLTPROC_EXECUTABLE)
28
29 set (_common_xsltproc_args
30     --stringparam use.id.as.filename 1
31     --stringparam admon.graphics 1
32     --stringparam admon.graphics.extension .svg
33     --stringparam section.autolabel 1
34     --stringparam section.label.includes.component.label 1
35     --stringparam html.stylesheet ws.css
36     )
37
38 if (WIN32 AND NOT "${CYGWIN_INSTALL_PATH}" STREQUAL "" AND ${XSLTPROC_EXECUTABLE} MATCHES "${CYGWIN_INSTALL_PATH}")
39     FIND_PROGRAM(CYGPATH_EXECUTABLE
40         NAMES cygpath
41         PATHS ${CYGWIN_INSTALL_PATH}/bin
42     )
43     MACRO( TO_XSLTPROC_COMPATIBLE_PATH _cmake_path _result )
44         execute_process(
45             COMMAND ${CYGPATH_EXECUTABLE} -u ${_cmake_path}
46             OUTPUT_VARIABLE _cygwin_path
47         )
48         # cygpath adds a linefeed.
49         string(STRIP "${_cygwin_path}" _cygwin_path)
50
51         set( ${_result} ${_cygwin_path} )
52     ENDMACRO()
53
54     TO_XSLTPROC_COMPATIBLE_PATH( ${CMAKE_CURRENT_SOURCE_DIR} _xsltproc_current_source_dir )
55     TO_XSLTPROC_COMPATIBLE_PATH( ${CMAKE_CURRENT_BINARY_DIR} _xsltproc_current_binary_dir )
56
57     set ( _xsltproc_path "${_xsltproc_current_source_dir}:${_xsltproc_current_binary_dir}:${_xsltproc_current_binary_dir}/wsluarm_src")
58 else()
59     set ( _xsltproc_path "${CMAKE_CURRENT_SOURCE_DIR}:${CMAKE_CURRENT_BINARY_DIR}:${CMAKE_CURRENT_BINARY_DIR}/wsluarm_src")
60 endif()
61
62 # Workaround for parallel build issue with msbuild.
63 # https://gitlab.kitware.com/cmake/cmake/issues/16767
64 if(CMAKE_GENERATOR MATCHES "Visual Studio")
65   # msbuild (as used by the Visual Studio generators) must not depend on the XML
66   # file (otherwise the XML file will be generated multiple times, possibly in
67   # parallel, breaking the build). Workaround: add one dependency to generate
68   # the XML file when outdated, depend on the -stamp file to ensure that the
69   # target is rebuilt when the XML file is regenerated.
70   function(get_docbook_xml_depends varname _dbk_source)
71     set(${varname}
72       "generate_${_dbk_source}"
73       "${CMAKE_CURRENT_BINARY_DIR}/${_dbk_source}-stamp"
74       PARENT_SCOPE
75     )
76   endfunction()
77 else()
78   # Unix Makefiles, Ninja, etc: first dependency enforces that the XML file is
79   # rebuilt when outdated, the second dependency ensures that the target is
80   # rebuilt when the XML file has changed.
81   function(get_docbook_xml_depends varname _dbk_source)
82     set(${varname}
83       "generate_${_dbk_source}"
84       "${_dbk_source}"
85       PARENT_SCOPE
86     )
87   endfunction()
88 endif()
89
90 # Translate XML to HTML
91 #XML2HTML(
92 #        wsug or wsdg
93 #        single-page or chunked
94 #        WSUG_FILES
95 #        WSUG_GRAPHICS
96 #)
97 MACRO(XML2HTML _target_dep _dir_pfx _mode _dbk_source _gfx_sources)
98     # We depend on the docbook target to avoid parallel builds.
99     SET(_dbk_dep ${_target_dep}_docbook)
100
101     # We can pass chunker.xxx parameters to customize the chunked output.
102     # We have to use a custom layer to customize the single-page output.
103     # Set the output encoding for both to UTF-8. Indent the single-page
104     # output because we sometimes need to copy and paste the release
105     # note contents.
106     IF(${_mode} STREQUAL "chunked")
107         SET(_basedir ${_dir_pfx}_html_chunked)
108         SET(_stylesheet "http://docbook.sourceforge.net/release/xsl/current/html/chunk.xsl")
109         SET(_modeparams --stringparam chunker.output.encoding UTF-8)
110     ELSE() # single-page
111         SET(_basedir ${_dir_pfx}_html)
112         SET(_stylesheet custom_layer_single_html.xsl)
113         SET(_modeparams --output ${_basedir}/index.html)
114     ENDIF()
115
116     SET(_out_dir ${CMAKE_CURRENT_BINARY_DIR}/${_basedir})
117     SET(_output ${_basedir}/index.html)
118     get_docbook_xml_depends(_dbk_xml_deps "${_dbk_source}")
119
120     FOREACH(_tmpgfx ${${_gfx_sources}})
121         set(_gfx_deps ${CMAKE_CURRENT_SOURCE_DIR}/${_tmpgfx})
122     ENDFOREACH()
123
124     SET(_gfx_dir ${_dir_pfx}_graphics)
125     ADD_CUSTOM_COMMAND(
126         OUTPUT
127             ${_output}
128         COMMAND ${CMAKE_COMMAND}
129             -E make_directory ${_out_dir}
130         COMMAND ${CMAKE_COMMAND}
131            -E make_directory ${_out_dir}/${_gfx_dir}/toolbar
132         COMMAND ${CMAKE_COMMAND}
133            -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/${_gfx_dir} ${_out_dir}/${_gfx_dir}
134         COMMAND ${CMAKE_COMMAND}
135            -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/common_graphics ${_out_dir}/${_gfx_dir}
136         COMMAND ${CMAKE_COMMAND}
137            -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/${_gfx_dir}/toolbar ${_out_dir}/${_gfx_dir}/toolbar
138         COMMAND ${CMAKE_COMMAND}
139             -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/ws.css ${_out_dir}
140         COMMAND ${XSLTPROC_EXECUTABLE}
141             --path "${_xsltproc_path}"
142             --stringparam base.dir ${_basedir}/
143             ${_common_xsltproc_args}
144             --stringparam admon.graphics.path ${_gfx_dir}/
145             ${_modeparams}
146             --noout ${_stylesheet}
147             ${_dbk_source}
148         DEPENDS
149             ${_dbk_xml_deps}
150             ${_dbk_dep}
151             ${_gfx_deps}
152             custom_layer_single_html.xsl
153     )
154     IF(NOT WIN32)
155         ADD_CUSTOM_COMMAND(
156             OUTPUT
157                 ${_output}
158             COMMAND chmod
159                 -R og+rX ${_out_dir}
160             APPEND
161         )
162     ENDIF()
163 ENDMACRO(XML2HTML)
164
165 # Translate XML to HHP
166 #XML2HHP(
167 #       wsug or wsdg
168 #       user-guide.xml or developer-guide.xml
169 #)
170 MACRO(XML2HHP _target_dep _guide _dbk_source)
171     # We depend on the docbook target to avoid parallel builds.
172     SET(_dbk_dep ${_target_dep}_docbook)
173     GET_FILENAME_COMPONENT( _source_base_name ${_dbk_source} NAME_WE )
174     set( _output_chm ${_source_base_name}.chm )
175     set( _output_hhp ${_source_base_name}.hhp )
176     set( _output_toc_hhc ${_source_base_name}-toc.hhc )
177     set( _docbook_plain_title ${_source_base_name}-plain-title.xml )
178     get_docbook_xml_depends(_dbk_xml_deps "${_dbk_source}")
179
180     SET(_gfxdir ${_guide}_graphics)
181     SET(_basedir ${_guide}_chm)
182     ADD_CUSTOM_COMMAND(
183         OUTPUT
184             ${_output_hhp}
185             ${_output_toc_hhc}
186         COMMAND ${CMAKE_COMMAND} -E make_directory ${_basedir}
187         COMMAND ${CMAKE_COMMAND} -E make_directory ${_basedir}/${_gfxdir}
188         COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/${_gfxdir} ${_basedir}/${_gfxdir}
189         COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/common_graphics ${_basedir}/${_gfxdir}
190         # HTML Help doesn't render decimal character entities in the title.
191         COMMAND ${PERL_EXECUTABLE} -p
192             -e "s|er&#8217;s Guide</title>|er's Guide</title>|"
193             < ${_dbk_source}
194             > ${_docbook_plain_title}
195         COMMAND ${XSLTPROC_EXECUTABLE}
196             --path "${_xsltproc_path}"
197             --stringparam base.dir ${_basedir}/
198             --stringparam htmlhelp.chm ${_output_chm}
199             --stringparam htmlhelp.hhp ${_output_hhp}
200             --stringparam htmlhelp.hhc ${_output_toc_hhc}
201             ${_common_xsltproc_args}
202             --stringparam admon.graphics.path ${_gfxdir}/
203             --nonet custom_layer_chm.xsl
204             ${_docbook_plain_title}
205         DEPENDS
206             ${_dbk_xml_deps}
207             ${_dbk_dep}
208             # AsciiDoc uses UTF-8 by default, which is unsupported by HTML
209             # Help. We may want to render an ISO-8859-1 version, or get rid
210             # of HTML Help.
211             custom_layer_chm.xsl
212     )
213 ENDMACRO(XML2HHP)