Refactor pre-Doxy siphon scripts; VPP-396
[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 OS_ID ?= $(shell grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g')
27
28 # Package dependencies
29 DOC_DEB_DEPENDS = doxygen graphviz python-pyparsing python-jinja2
30 DOC_RPM_DEPENDS = doxygen graphviz pyparsing python-jinja2
31
32 # Doxygen configuration and our utility scripts
33 DOXY_DIR ?= $(WS_ROOT)/doxygen
34
35 # Primary source directories
36 DOXY_SRC_DIRECTORIES = \
37         vppinfra \
38         svm \
39         vlib \
40         vlib-api \
41         vnet \
42         vpp \
43         vpp-api
44
45 # Input directories and files
46 DOXY_INPUT ?= \
47         $(wildcard $(WS_ROOT)/*.md) \
48         $(wildcard $(DOXY_DIR)/*.md) \
49         $(DOXY_SRC_DIRECTORIES) \
50         plugins
51
52 # Strip leading workspace path from input names
53 DOXY_INPUT := $(subst $(WS_ROOT)/,,$(DOXY_INPUT))
54
55 # Files to exclude, from pre-Doxygen steps, eg because they're
56 # selectively compiled.
57 # Examples would be to exclude non-DPDK related sources when
58 # there's a DPDK equivalent that conflicts.
59 # These must be left-anchored paths for the regexp below to work.
60 DOXY_EXCLUDE ?= \
61         vlib/vlib/buffer.c \
62         vlib/example \
63         plugins/sample-plugin
64
65 # Generate a regexp for filenames to exclude
66 DOXY_EXCLUDE_REGEXP = ($(subst .,\.,$(shell echo '$(strip $(DOXY_EXCLUDE))' | sed -e 's/ /|/g')))
67
68 # Include all the normal source directories in the include file path
69 DOXY_INCLUDE_PATH = $(DOXY_SRC_DIRECTORIES)
70
71 # Also include any plugin directories that exist
72 DOXY_INCLUDE_PATH += \
73         $(shell find $(WS_ROOT)/plugins -maxdepth 1 -type d | sed -e 's@^$(WS_ROOT)/*@@')
74
75 # Find API header directories and include them in the header path.
76 # This is only useful if VPP and plugins are already built; nothing
77 # here depends on those targets. We don't build documentation for these
78 # header files, they're just added to the INCLUDE search path for Doxygen.
79 _vpp_br = $(shell find "$(BR)" -maxdepth 1 -type d \
80         '(' -name build-vpp_debug-native -o -name build-vpp-native ')' -print \
81         | sed -e 's@^$(WS_ROOT)/*@@' -e 1q)
82 ifneq ($(strip $(_vpp_br)),)
83 DOXY_INCLUDE_PATH += \
84         $(_vpp_br)/vlib-api \
85         $(_vpp_br)/vpp
86 # Also include any plugin directories that exist
87 DOXY_INCLUDE_PATH += \
88         $(shell find $(WS_ROOT)/$(_vpp_br)/plugins -maxdepth 1 -type d | sed -e 's@^$(WS_ROOT)/*@@')
89 endif
90
91 # Discover if we have CPP available
92 _cpp = $(shell which cpp)
93 ifneq ($(strip $(_cpp)),)
94 # Add whatever directories CPP normally includes to the header path
95 DOXY_INCLUDE_PATH += $(shell set -e; $(_cpp) -v </dev/null 2>&1 | awk 'f&&/^ /{print $$1} /^\#include/{f=1}')
96 endif
97
98 # Target directory for doxygen output
99 DOXY_OUTPUT ?= $(BR)/docs
100
101 # Siphoned fragments end up in here
102 SIPHON_INPUT ?= $(DOXY_OUTPUT)/siphon_fragments
103
104 # Siphoned fragements are processed into here
105 SIPHON_OUTPUT ?= $(DOXY_OUTPUT)/siphon_docs
106
107 # Extra document inputs that are processed in addition to DOXY_INPUT
108 EXTRA_DOXY_INPUT += $(SIPHON_OUTPUT)
109
110 # All the siphon types we know about
111 SIPHONS ?= clicmd syscfg
112
113 SIPHON_FILES = $(addprefix $(SIPHON_INPUT)/,$(addsuffix .siphon,$(SIPHONS)))
114 SIPHON_DOCS = $(addprefix $(SIPHON_OUTPUT)/,$(addsuffix .md,$(SIPHONS)))
115
116 $(BR)/.doxygen-bootstrap.ok: Makefile
117         @echo "Checking whether dependencies for Doxygen are installed..."
118 ifeq ($(OS_ID),ubuntu)
119         @set -e; inst=; \
120                 for i in $(DOC_DEB_DEPENDS); do \
121                         dpkg-query --show $$i >/dev/null 2>&1 || inst="$$inst $$i"; \
122                 done; \
123                 if [ "$$inst" ]; then \
124                         sudo apt-get update; \
125                         sudo apt-get $(CONFIRM) $(FORCE) install $$inst; \
126                 fi
127         @if [ ! -s /usr/lib/graphviz/config6a ]; then \
128                 echo "Rebuidlding system Graphviz configuration."; \
129                 sudo dot -c; \
130         fi
131 else ifneq ("$(wildcard /etc/redhat-release)","")
132         @sudo yum install $(CONFIRM) $(DOC_RPM_DEPENDS)
133 else
134         $(error "This option currently works only on Ubuntu or Centos systems")
135 endif
136         @touch $@
137
138 .PHONY: bootstrap-doxygen
139 bootstrap-doxygen: $(BR)/.doxygen-bootstrap.ok
140
141 .DELETE_ON_ERROR: $(BR)/.doxygen-siphon.dep
142 $(BR)/.doxygen-siphon.dep: Makefile
143         @echo "Building siphon dependencies..."
144         @set -e; rm -f "$@"; for input in $(DOXY_INPUT); do \
145                 find "$(WS_ROOT)/$$input" -type f \
146                         \( -name '*.[ch]' -or -name '*.dox' \) -print \
147                         | grep -v -E '^$(WS_ROOT)/$(DOXY_EXCLUDE_REGEXP)' \
148                         | sed -e "s/^/\$$(SIPHON_FILES): /" \
149                         >> $@; \
150         done
151
152 # Include the source -> siphon dependencies
153 -include $(BR)/.doxygen-siphon.dep
154
155 # Generate .siphon files that contain fragments of source file that
156 # relate to the siphons we support.
157 .NOTPARALLEL: $(SIPHON_FILES)
158 $(SIPHON_FILES): $(BR)/.doxygen-bootstrap.ok \
159                 $(DOXY_DIR)/siphon-generate \
160                 $(wildcard $(DOXY_DIR)/siphon/*.py)
161         @rm -rf "$(SIPHON_INPUT)" "$(SIPHON_OUTPUT)"
162         @mkdir -p "$(SIPHON_INPUT)" "$(SIPHON_OUTPUT)"
163         @touch $(SIPHON_INPUT)/files
164         @echo "Collating source file list for siphoning..."
165         @for input in $(DOXY_INPUT); do \
166                 cd "$(WS_ROOT)"; \
167                 find "$$input" -type f \
168                         \( -name '*.[ch]' -or -name '*.dox' \) -print \
169                         | grep -v -E '^$(DOXY_EXCLUDE_REGEXP)' \
170                         >> $(SIPHON_INPUT)/files; \
171         done
172         @echo "Generating siphons..."
173         @set -e; \
174         cd "$(WS_ROOT)"; \
175         $(DOXY_DIR)/siphon-generate \
176                 --output="$(SIPHON_INPUT)" \
177                 "@$(SIPHON_INPUT)/files"
178
179 # Process the .siphon source fragments and render them into doxygen flavored
180 # markdown documentation
181 .DELETE_ON_ERROR: $(SIPHON_DOCS)
182 $(SIPHON_OUTPUT)/%.md: $(SIPHON_INPUT)/%.siphon \
183                 $(DOXY_DIR)/siphon-process \
184                 $(wildcard $(DOXY_DIR)/siphon/*.py) \
185                 $(wildcard $(DOXY_DIR)/siphon_templates/*/*.md)
186         @echo "Processing siphon from $(notdir $<)..."
187         @set -e; \
188         cd "$(WS_ROOT)"; \
189         $(DOXY_DIR)/siphon-process \
190                 --type=$(basename $(notdir $<)) \
191                 --output="$@" \
192                 "$<"
193
194 # This target can be used just to generate the siphoned docs
195 .PHONY: doxygen-siphon
196 doxygen-siphon: $(SIPHON_DOCS)
197
198 # Generate the doxygen docs
199 .PHONY: doxygen
200 doxygen: $(SIPHON_DOCS)
201         @mkdir -p "$(DOXY_OUTPUT)"
202         @echo "Running Doxygen..."
203         set -e; cd "$(WS_ROOT)"; \
204             ROOT="$(WS_ROOT)" \
205             BUILD_ROOT="$(BR)" \
206             INPUT="$(addprefix $(WS_ROOT)/,$(DOXY_INPUT)) $(EXTRA_DOXY_INPUT)" \
207             INCLUDE_PATH="$(DOXY_INCLUDE_PATH)" \
208             EXCLUDE="$(DOXY_EXCLUDE)" \
209             HTML=YES \
210             VERSION="`git describe --tags --dirty`" \
211             doxygen $(DOXY_DIR)/doxygen.cfg
212
213 .PHONY: wipe-doxygen
214 wipe-doxygen:
215         rm -rf "$(BR)/docs" "$(BR)/.doxygen-siphon.d"
216
217 .PHONY: clean
218 clean: wipe-doxygen