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