1+ # SPDX-FileCopyrightText: 2026 Intel Corporation
12# SPDX-FileCopyrightText: 2021 Open Networking Foundation <info@opennetworking.org>
23# SPDX-License-Identifier: Apache-2.0
34
@@ -15,7 +16,16 @@ BUILDDIR ?= _build
1516# name of python virtualenv that is used to run commands
1617VENV_NAME := venv-docs
1718
18- .PHONY : help Makefile test doc8 dict-check sort-dict license clean clean-all
19+ # Git repository information for versioning
20+ GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
21+ GIT_TAG := $(shell git describe --tags --exact-match 2>/dev/null || echo "")
22+ VERSION_TAGS := $(shell git tag --sort=-version:refname | head -3)
23+
24+ # Temporary directory for git worktrees
25+ WORKTREE_DIR := $(BUILDDIR ) /worktrees
26+
27+ .PHONY : help Makefile test doc8 dict-check sort-dict license clean clean-all \
28+ multiversion add-nojekyll create-version-index version-info clean-worktrees
1929
2030# Put it first so that "make" without argument is like "make help".
2131help : $(VENV_NAME )
@@ -58,11 +68,106 @@ clean:
5868clean-all : clean
5969 rm -rf $(VENV_NAME )
6070
61- # build multiple versions
62- multiversion : $(VENV_NAME ) Makefile
71+ # Clean up git worktrees
72+ clean-worktrees :
73+ @echo " Cleaning up git worktrees..."
74+ @if [ -d " $( WORKTREE_DIR) " ]; then \
75+ for worktree in $( WORKTREE_DIR) /* ; do \
76+ if [ -d " $$ worktree" ]; then \
77+ echo " Removing worktree: $$ worktree" ; \
78+ git worktree remove " $$ worktree" 2> /dev/null || rm -rf " $$ worktree" ; \
79+ fi ; \
80+ done ; \
81+ rmdir " $( WORKTREE_DIR) " 2> /dev/null || true ; \
82+ fi
83+
84+ # Ensure .nojekyll file exists for GitHub Pages
85+ add-nojekyll :
86+ @echo " Adding .nojekyll file for GitHub Pages..."
87+ @if [ -d " $( BUILDDIR) /html" ]; then \
88+ touch " $( BUILDDIR) /html/.nojekyll" ; \
89+ echo " Created .nojekyll in $( BUILDDIR) /html/" ; \
90+ fi
91+ @if [ -d " $( BUILDDIR) /multiversion" ]; then \
92+ touch " $( BUILDDIR) /multiversion/.nojekyll" ; \
93+ echo " Created .nojekyll in $( BUILDDIR) /multiversion/" ; \
94+ fi
95+
96+ # Build single version (current branch/tag)
97+ build : $(VENV_NAME ) Makefile
98+ source $< /bin/activate ; set -u ; \
99+ $(SPHINXBUILD ) -b html " $( SOURCEDIR) " " $( BUILDDIR) /html" $(SPHINXOPTS )
100+ $(MAKE ) add-nojekyll
101+ @echo " Documentation built successfully!"
102+ @echo " Open $( BUILDDIR) /html/index.html in your browser"
103+
104+ # Build multiple versions
105+ multiversion : $(VENV_NAME ) Makefile clean-worktrees
106+ @echo " Building multi-version documentation..."
107+ @echo " Current branch: $( GIT_BRANCH) "
108+ @echo " Available tags: $( VERSION_TAGS) "
109+ rm -rf " $( BUILDDIR) /multiversion"
110+ mkdir -p " $( BUILDDIR) /multiversion"
111+ mkdir -p " $( WORKTREE_DIR) "
112+ @echo " Building latest version from current working directory..."
63113 source $< /bin/activate ; set -u ; \
64- sphinx-multiversion " $( SOURCEDIR) " " $( BUILDDIR) /multiversion" $(SPHINXOPTS )
65- cp " $( SOURCEDIR) /_templates/meta_refresh.html" " $( BUILDDIR) /multiversion/index.html"
114+ $(SPHINXBUILD ) -b html " $( SOURCEDIR) " " $( BUILDDIR) /multiversion/latest" $(SPHINXOPTS )
115+ @if [ -n " $( VERSION_TAGS) " ]; then \
116+ for tag in $( VERSION_TAGS) ; do \
117+ echo " Building documentation for tag: $$ tag using git worktree..." ; \
118+ worktree_path=" $( WORKTREE_DIR) /$$ tag" ; \
119+ if git worktree add " $$ worktree_path" " $$ tag" 2> /dev/null; then \
120+ echo " Created worktree for $$ tag at $$ worktree_path" ; \
121+ source $< /bin/activate ; set -u ; \
122+ $(SPHINXBUILD ) -b html " $$ worktree_path" " $( BUILDDIR) /multiversion/$$ tag" $(SPHINXOPTS ) || echo " Warning: Failed to build $$ tag" ; \
123+ git worktree remove " $$ worktree_path" 2> /dev/null || echo " Warning: Failed to remove worktree $$ worktree_path" ; \
124+ else \
125+ echo " Warning: Could not create worktree for tag $$ tag" ; \
126+ fi ; \
127+ done ; \
128+ else \
129+ echo " No version tags found, building only latest version" ; \
130+ fi
131+ $(MAKE ) create-version-index
132+ $(MAKE ) add-nojekyll
133+ $(MAKE ) clean-worktrees
134+ @echo " Multi-version documentation built successfully!"
135+ @echo " Open $( BUILDDIR) /multiversion/index.html in your browser"
136+
137+ # Create version selection index page
138+ create-version-index :
139+ @echo " Creating version index page..."
140+ @mkdir -p " $( BUILDDIR) /multiversion"
141+ @if [ ! -f " _templates/version-index.html" ]; then \
142+ echo " Error: Template file _templates/version-index.html not found" ; \
143+ exit 1; \
144+ fi
145+ @cp " _templates/version-index.html" " $( BUILDDIR) /multiversion/index.html"
146+ @if [ -n " $( VERSION_TAGS) " ]; then \
147+ version_cards=" " ; \
148+ for tag in $( VERSION_TAGS) ; do \
149+ version_cards=" $$ version_cards <a href=\" $$ tag/\" class=\" version-card\" >" $$ ' \n' ; \
150+ version_cards=" $$ version_cards <div class=\" version-title\" >$$ tag</div>" $$ ' \n' ; \
151+ version_cards=" $$ version_cards <div class=\" version-desc\" >Release $$ tag documentation</div>" $$ ' \n' ; \
152+ version_cards=" $$ version_cards </a>" $$ ' \n' ; \
153+ done ; \
154+ sed -i.bak " s|{{VERSION_CARDS}}|$$ version_cards|g" " $( BUILDDIR) /multiversion/index.html" ; \
155+ rm -f " $( BUILDDIR) /multiversion/index.html.bak" ; \
156+ else \
157+ sed -i.bak " s|{{VERSION_CARDS}}||g" " $( BUILDDIR) /multiversion/index.html" ; \
158+ rm -f " $( BUILDDIR) /multiversion/index.html.bak" ; \
159+ fi
160+
161+ # Show current version information
162+ version-info :
163+ @echo " === Version Information ==="
164+ @echo " Git Branch: $( GIT_BRANCH) "
165+ @echo " Git Tag: $( GIT_TAG) "
166+ @echo " Recent Tags: $( VERSION_TAGS) "
167+ @if [ -f " VERSION" ]; then echo " Version File: $$ (cat VERSION)" ; fi
168+ @echo " Build Directory: $( BUILDDIR) "
169+ @echo " Worktree Directory: $( WORKTREE_DIR) "
170+ @echo " =========================="
66171
67172# Catch-all target: route all unknown targets to Sphinx using the new
68173# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
0 commit comments