1# SPDX-License-Identifier: LGPL-2.1-or-later
2
3# This is lame, I know, but meson has no other include mechanism
4subdir('rules')
5
6want_man = get_option('man')
7want_html = get_option('html')
8xsltproc = find_program('xsltproc',
9                        required : want_man == 'true' or want_html == 'true')
10want_man = want_man != 'false' and xsltproc.found()
11want_html = want_html != 'false' and xsltproc.found()
12
13xsltproc_flags = [
14        '--nonet',
15        '--xinclude',
16        '--maxdepth', '9000',
17        '--stringparam', 'man.output.quietly', '1',
18        '--stringparam', 'funcsynopsis.style', 'ansi',
19        '--stringparam', 'man.authors.section.enabled', '0',
20        '--stringparam', 'man.copyright.section.enabled', '0',
21        '--stringparam', 'systemd.version', '@0@'.format(meson.project_version()),
22        '--path',
23        '@0@:@1@'.format(meson.current_build_dir(), meson.current_source_dir())]
24
25custom_man_xsl = files('custom-man.xsl')
26custom_html_xsl = files('custom-html.xsl')
27xslt_cmd = [xsltproc, '-o', '@OUTPUT0@'] + xsltproc_flags
28
29custom_entities_ent = custom_target(
30        'custom-entities.ent',
31        input : 'custom-entities.ent.in',
32        output : 'custom-entities.ent',
33        command : [jinja2_cmdline, '@INPUT@', '@OUTPUT@'])
34
35man_pages = []
36html_pages = []
37source_xml_files = []
38dbus_docs = []
39foreach tuple : manpages
40        stem = tuple[0]
41        section = tuple[1]
42        aliases = tuple[2]
43        condition = tuple[3]
44
45        xml = stem + '.xml'
46        html = stem + '.html'
47        man = stem + '.' + section
48
49        manaliases = []
50        htmlaliases = []
51        foreach alias : aliases
52                manaliases += alias + '.' + section
53                htmlaliases += alias + '.html'
54        endforeach
55
56        mandirn = get_option('mandir') / ('man' + section)
57
58        if condition == '' or conf.get(condition) == 1
59                file = files(tuple[0] + '.xml')
60                source_xml_files += file
61                if tuple[0].startswith('org.freedesktop.')
62                        dbus_docs += file
63                endif
64
65                if xsltproc.found()
66                        p1 = custom_target(
67                                man,
68                                input : xml,
69                                output : [man] + manaliases,
70                                command : xslt_cmd + [custom_man_xsl, '@INPUT@'],
71                                depends : custom_entities_ent,
72                                install : want_man,
73                                install_dir : mandirn)
74                        man_pages += p1
75
76                        p2 = []
77                        foreach htmlalias : htmlaliases
78                                link = custom_target(
79                                        htmlalias,
80                                        output : htmlalias,
81                                        command : [ln, '-fs', html, '@OUTPUT@'])
82                                if want_html
83                                        dst = docdir / 'html' / htmlalias
84                                        cmd = 'ln -fs @0@ $DESTDIR@1@'.format(html, dst)
85                                        meson.add_install_script('sh', '-c', cmd)
86                                        p2 += link
87                                endif
88                                html_pages += link
89                        endforeach
90
91                        p3 = custom_target(
92                                html,
93                                input : xml,
94                                output : html,
95                                command : xslt_cmd + [custom_html_xsl, '@INPUT@'],
96                                depends : [custom_entities_ent, p2],
97                                install : want_html,
98                                install_dir : docdir / 'html')
99                        html_pages += p3
100                endif
101        else
102                message('Skipping @0@.@1@ because @2@ is false'.format(stem, section, condition))
103        endif
104endforeach
105
106############################################################
107
108have_lxml = run_command(xml_helper_py, check: false).returncode() == 0
109if not have_lxml
110        message('python-lxml not available, not making man page indices')
111endif
112
113systemd_directives_xml = custom_target(
114        'systemd.directives.xml',
115        input : ['directives-template.xml', source_xml_files],
116        output : 'systemd.directives.xml',
117        depends : custom_entities_ent,
118        command : [make_directive_index_py, '@OUTPUT@', '@INPUT@'])
119
120nonindex_xml_files = source_xml_files + [systemd_directives_xml]
121systemd_index_xml = custom_target(
122        'systemd.index.xml',
123        input : nonindex_xml_files,
124        output : 'systemd.index.xml',
125        command : [make_man_index_py, '@OUTPUT@'] + nonindex_xml_files)
126
127foreach tuple : xsltproc.found() ? [['systemd.directives', '7', systemd_directives_xml],
128                                    ['systemd.index',      '7', systemd_index_xml]] : []
129        stem = tuple[0]
130        section = tuple[1]
131        xml = tuple[2]
132
133        html = stem + '.html'
134        man = stem + '.' + section
135
136        mandirn = get_option('mandir') / ('man' + section)
137
138        p1 = custom_target(
139                man,
140                input : xml,
141                output : man,
142                command : xslt_cmd + [custom_man_xsl, '@INPUT@'],
143                install : want_man and have_lxml,
144                install_dir : mandirn)
145        man_pages += p1
146
147        p2 = []
148        if html == 'systemd.index.html'
149                htmlalias = 'index.html'
150                link = custom_target(
151                        htmlalias,
152                        input : p2,
153                        output : htmlalias,
154                        command : [ln, '-fs', html, '@OUTPUT@'])
155                if want_html
156                        dst = docdir / 'html' / htmlalias
157                        cmd = 'ln -fs @0@ $DESTDIR@1@'.format(html, dst)
158                        meson.add_install_script('sh', '-c', cmd)
159                        p2 += link
160                endif
161                html_pages += link
162        endif
163
164        p3 = custom_target(
165                html,
166                input : xml,
167                output : html,
168                command : xslt_cmd + [custom_html_xsl, '@INPUT@'],
169                depends : [custom_entities_ent, p2],
170                install : want_html and have_lxml,
171                install_dir : docdir / 'html')
172        html_pages += p3
173endforeach
174
175# Cannot use run_target because those targets are used in depends
176# Also see https://github.com/mesonbuild/meson/issues/368.
177man = custom_target(
178        'man',
179        output : 'man',
180        depends : man_pages,
181        command : [echo])
182
183html = custom_target(
184        'html',
185        output : 'html',
186        depends : html_pages,
187        command : [echo])
188
189if rsync.found()
190        run_target(
191                'doc-sync',
192                depends : man_pages + html_pages,
193                command : [rsync, '-rlv',
194                           '--delete-excluded',
195                           '--include=man',
196                           '--include=*.html',
197                           '--exclude=*',
198                           '--omit-dir-times',
199                           meson.current_build_dir(),
200                           get_option('www-target')])
201endif
202
203############################################################
204
205buildroot_substs = configuration_data()
206buildroot_substs.set_quoted('BUILD_ROOT', project_build_root)
207
208configure_file(
209        input : 'man.in',
210        output : 'man',
211        configuration : buildroot_substs)
212
213configure_file(
214        input : 'html.in',
215        output : 'html',
216        configuration : buildroot_substs)
217
218############################################################
219
220update_dbus_docs = custom_target(
221        'update-dbus-docs',
222        output : 'update-dbus-docs',
223        command : [update_dbus_docs_py, '--build-dir', project_build_root, '@INPUT@'],
224        input : dbus_docs)
225
226if conf.get('BUILD_MODE_DEVELOPER') == 1
227        test('dbus-docs-fresh',
228             update_dbus_docs_py,
229             suite : 'dist-check',
230             args : ['--build-dir', project_build_root, '--test', dbus_docs],
231             depends : dbus_programs)
232endif
233
234update_man_rules = custom_target(
235        'update-man-rules',
236        output : 'update-man-rules',
237        command : [update_man_rules_py,
238                   '@0@/man/*.xml'.format(project_source_root),
239                   '@0@/rules/meson.build'.format(meson.current_source_dir())],
240        depends : custom_entities_ent)
241