ffce3c13a85d7979bceaffb47ebec63f31a5f604
[vpp.git] / doxygen / Makefile
1 # Copyright (c) 2016 Comcast Cable Communications Management, LLC.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 #
16 # Build the documentation
17 #
18
19 # Default target
20 .PHONY: all
21 all: doxygen
22
23 # These should be passed in by the root Makefile
24 WS_ROOT ?= $(CURDIR)/..
25 BR ?= $(WS_ROOT)/build-root
26
27 # We support MacOS for docs generation
28 ifeq ($(shell uname),Darwin)
29 OS_ID = darwin
30 endif
31
32 # Work out the OS if we haven't already
33 OS_ID ?= $(shell grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g')
34
35 # Package dependencies
36 DOC_DEB_DEPENDS = doxygen graphviz python-pyparsing python-jinja2
37 DOC_RPM_DEPENDS = doxygen graphviz pyparsing python-jinja2
38 DOC_MAC_BIN_DEPENDS = doxygen dot git
39 DOC_MAC_PY_DEPENDS = pyparsing jinja2
40
41 # Doxygen configuration and our utility scripts
42 DOXY_DIR ?= $(WS_ROOT)/doxygen
43
44 # Primary source directories
45 DOXY_SRC ?= src
46 DOXY_SRC_DIRECTORIES = \
47         $(DOXY_SRC)/vppinfra \
48         $(DOXY_SRC)/svm \
49         $(DOXY_SRC)/vlib \
50         $(DOXY_SRC)/vlibapi \
51         $(DOXY_SRC)/vlibmemory \
52         $(DOXY_SRC)/vlibsocket \
53         $(DOXY_SRC)/vnet \
54         $(DOXY_SRC)/vpp \
55         $(DOXY_SRC)/vpp-api \
56         vpp-api
57
58 # Input directories and files
59 DOXY_INPUT ?= \
60         $(wildcard $(WS_ROOT)/*.md) \
61         $(wildcard $(DOXY_DIR)/*.md) \
62         $(DOXY_SRC_DIRECTORIES) \
63         $(DOXY_SRC)/plugins \
64         plugins
65
66 # Strip leading workspace path from input names
67 DOXY_INPUT := $(subst $(WS_ROOT)/,,$(DOXY_INPUT))
68
69 # Files to exclude, from pre-Doxygen steps, eg because they're
70 # selectively compiled.
71 # Examples would be to exclude non-DPDK related sources when
72 # there's a DPDK equivalent that conflicts.
73 # These must be left-anchored paths for the regexp below to work.
74 DOXY_EXCLUDE ?= \
75         $(DOXY_SRC)/vlib/vlib/buffer.c \
76         $(DOXY_SRC)/vlib/example \
77         plugins/sample-plugin \
78         vpp-api/lua
79
80 # Generate a regexp for filenames to exclude
81 DOXY_EXCLUDE_REGEXP = ($(subst .,\.,$(shell echo '$(strip $(DOXY_EXCLUDE))' | sed -e 's/ /|/g')))
82
83 # Include all the normal source directories in the include file path
84 DOXY_INCLUDE_PATH = $(DOXY_SRC_DIRECTORIES)
85
86 # Also include any plugin directories that exist
87 DOXY_INCLUDE_PATH += \
88         $(shell find $(WS_ROOT)/plugins -maxdepth 1 -type d | sed -e 's@^$(WS_ROOT)/*@@')
89
90 # Find API header directories and include them in the header path.
91 # This is only useful if VPP and plugins are already built; nothing
92 # here depends on those targets. We don't build documentation for these
93 # header files, they're just added to the INCLUDE search path for Doxygen.
94 _vpp_br = $(shell find "$(BR)" -maxdepth 1 -type d \
95         '(' -name build-vpp_debug-native -o -name build-vpp-native ')' -print \
96         | sed -e 's@^$(WS_ROOT)/*@@' -e 1q)
97 ifneq ($(strip $(_vpp_br)),)
98 DOXY_INCLUDE_PATH += \
99         $(_vpp_br)/vlib-api \
100         $(_vpp_br)/vpp
101 # Also include any plugin directories that exist
102 DOXY_INCLUDE_PATH += \
103         $(shell find $(WS_ROOT)/$(_vpp_br)/plugins -maxdepth 1 -type d | sed -e 's@^$(WS_ROOT)/*@@')
104 endif
105
106 # Discover if we have CPP available
107 _cpp = $(shell which cpp)
108 ifneq ($(strip $(_cpp)),)
109 # Add whatever directories CPP normally includes to the header path
110 DOXY_INCLUDE_PATH += $(shell set -e; $(_cpp) -v </dev/null 2>&1 | awk 'f&&/^ /{print $$1} /^\#include/{f=1}')
111 endif
112
113 # Target directory for doxygen output
114 DOXY_OUTPUT ?= $(BR)/docs
115
116 # Siphoned fragments end up in here
117 SIPHON_INPUT ?= $(DOXY_OUTPUT)/siphon_fragments
118
119 # Siphoned fragements are processed into here
120 SIPHON_OUTPUT ?= $(DOXY_OUTPUT)/siphon_docs
121
122 # Extra document inputs that are processed in addition to DOXY_INPUT
123 EXTRA_DOXY_INPUT += $(SIPHON_OUTPUT)
124
125 # All the siphon types we know about
126 SIPHONS ?= clicmd syscfg
127
128 SIPHON_FILES = $(addprefix $(SIPHON_INPUT)/,$(addsuffix .siphon,$(SIPHONS)))
129 SIPHON_DOCS = $(addprefix $(SIPHON_OUTPUT)/,$(addsuffix .md,$(SIPHONS)))
130 SIPHON_ITEMLIST = $(addprefix $(SIPHON_OUTPUT)/,$(addsuffix .itemlist,$(filter clicmd,$(SIPHONS))))
131
132 $(BR)/.doxygen-bootstrap.ok: Makefile
133         @echo "Checking whether dependencies for Doxygen are installed..."
134 ifeq ($(OS_ID),ubuntu)
135         @set -e; inst=; \
136                 for i in $(DOC_DEB_DEPENDS); do \
137                         dpkg-query --show $$i >/dev/null 2>&1 || inst="$$inst $$i"; \
138                 done; \
139                 if [ "$$inst" ]; then \
140                         sudo apt-get update; \
141                         sudo apt-get $(CONFIRM) $(FORCE) install $$inst; \
142                 fi
143         @if [ ! -s /usr/lib/graphviz/config6a ]; then \
144                 echo "Rebuidlding system Graphviz configuration."; \
145                 sudo dot -c; \
146         fi
147 else ifneq ("$(wildcard /etc/redhat-release)","")
148         @sudo yum install $(CONFIRM) $(DOC_RPM_DEPENDS)
149 else ifeq ($(OS_ID),darwin)
150         @set -e; \
151         for bin in $(DOC_MAC_BIN_DEPENDS); do \
152                 which -s $${bin} || (\
153                         echo "Program '$${bin}' not found, please install it."; \
154                         false; \
155                 ); \
156         done
157         @set -e; \
158         for py in $(DOC_MAC_PY_DEPENDS); do \
159                 python -c "import $${py}" >/dev/null 2>&1 || (\
160                         echo "Python package '$${py}' not found, please install it."; \
161                         false; \
162                 ); \
163         done
164 else
165         $(error "Building documentation currently works only on Ubuntu, CentOS or MacOS systems.")
166 endif
167         @touch $@
168
169 .PHONY: bootstrap-doxygen
170 bootstrap-doxygen: $(BR)/.doxygen-bootstrap.ok
171
172 .DELETE_ON_ERROR: $(BR)/.doxygen-siphon.dep
173 $(BR)/.doxygen-siphon.dep: Makefile \
174                 $(addprefix,$(WSROOT),$(DOXY_INPUT))
175         @echo "Building siphon dependencies..."
176         @rm -f "$@"; for input in $(DOXY_INPUT); do \
177                 [ -e "$(WS_ROOT)/$$input" ] && \
178                 find "$(WS_ROOT)/$$input" -type f \
179                         \( -name '*.[ch]' -or -name '*.dox' \) -print \
180                         | grep -v -E '^$(WS_ROOT)/$(DOXY_EXCLUDE_REGEXP)' \
181                         | sed -e "s/^/\$$(SIPHON_FILES): /" \
182                         >> $@; \
183         done
184
185 # Include the source -> siphon dependencies
186 -include $(BR)/.doxygen-siphon.dep
187
188 # Generate .siphon files that contain fragments of source file that
189 # relate to the siphons we support.
190 .NOTPARALLEL: $(SIPHON_FILES)
191 $(SIPHON_FILES): $(BR)/.doxygen-bootstrap.ok \
192                 $(DOXY_DIR)/siphon-generate \
193                 $(addprefix,$(WSROOT),$(DOXY_INPUT)) \
194                 $(wildcard $(DOXY_DIR)/siphon/*.py)
195         @echo "Validating source tree..."
196         @set -e; for input in $(DOXY_INPUT); do \
197                 if [ ! -e "$(WS_ROOT)/$$input" ]; then \
198                         echo "ERROR: Input path '$$input' does not exist." >&2; \
199                         exit 1; \
200                 fi; \
201         done
202         @rm -rf "$(SIPHON_INPUT)" "$(SIPHON_OUTPUT)"
203         @mkdir -p "$(SIPHON_INPUT)" "$(SIPHON_OUTPUT)"
204         @touch $(SIPHON_INPUT)/files
205         @echo "Collating source file list for siphoning..."
206         @for input in $(DOXY_INPUT); do \
207                 cd "$(WS_ROOT)"; \
208                 find "$$input" -type f \
209                         \( -name '*.[ch]' -or -name '*.dox' \) -print \
210                         | grep -v -E '^$(DOXY_EXCLUDE_REGEXP)' \
211                         >> $(SIPHON_INPUT)/files; \
212         done
213         @echo "Generating siphons..."
214         @set -e; \
215         cd "$(WS_ROOT)"; \
216         $(DOXY_DIR)/siphon-generate \
217                 --output="$(SIPHON_INPUT)" \
218                 "@$(SIPHON_INPUT)/files"
219
220 # Evaluate this to build a siphon doc output target for each desired
221 # output type:
222 # $(eval $(call siphon-process,file_extension,output_type_name))
223 define siphon-process
224 $(SIPHON_OUTPUT)/%.$(1): $(SIPHON_INPUT)/%.siphon \
225                 $(DOXY_DIR)/siphon-process \
226                 $(wildcard $(DOXY_DIR)/siphon/*.py) \
227                 $(wildcard $(DOXY_DIR)/siphon_templates/$(2)/*/*.$(1))
228         @echo "Processing siphon for $(2) from $$(notdir $$<)..."
229         @set -e; \
230         cd "$(WS_ROOT)"; \
231         $(DOXY_DIR)/siphon-process \
232                 --type=$$(basename $$(notdir $$<)) \
233                 --format=$(2) \
234                 --output="$$@" \
235                 "$$<"
236 endef
237
238 # Process the .siphon source fragments and render them into doxygen flavored
239 # markdown documentation
240 .DELETE_ON_ERROR: $(SIPHON_DOCS)
241 $(eval $(call siphon-process,md,markdown))
242
243 # Process the .siphon source fragments and render them into a list of cli
244 # commands.
245 .DELETE_ON_ERROR: $(SIPHON_ITEMLIST)
246 $(eval $(call siphon-process,itemlist,itemlist))
247
248 # This target can be used just to generate the siphoned things
249 .PHONY: doxygen-siphon
250 doxygen-siphon: $(SIPHON_DOCS) $(SIPHON_ITEMLIST)
251
252 # Generate the doxygen docs
253 .PHONY: doxygen
254 doxygen: $(SIPHON_DOCS)
255         @mkdir -p "$(DOXY_OUTPUT)"
256         @echo "Running Doxygen..."
257         set -e; cd "$(WS_ROOT)"; \
258             ROOT="$(WS_ROOT)" \
259             BUILD_ROOT="$(BR)" \
260             INPUT="$(addprefix $(WS_ROOT)/,$(DOXY_INPUT)) $(EXTRA_DOXY_INPUT)" \
261             INCLUDE_PATH="$(DOXY_INCLUDE_PATH)" \
262             EXCLUDE="$(DOXY_EXCLUDE)" \
263             HTML=YES \
264             VERSION="`git describe --tags --dirty`" \
265             doxygen $(DOXY_DIR)/doxygen.cfg
266
267 .PHONY: wipe-doxygen
268 wipe-doxygen:
269         rm -rf "$(BR)/docs" "$(BR)/.doxygen-siphon.d"
270
271 .PHONY: clean
272 clean: wipe-doxygen