1#-------------------------------------------------------------------------------
2# Copyright (c) 2019, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8#FindSphinx
9#-----------
10#Sphinx is a document generation tool written in Python.
11#See http://www.sphinx-doc.org/en/master/
12#
13#This module checks availability of the Sphinx document generator
14#(sphinx-build) and it's dependences (Python).
15#Sphinx is distributed as pip package or on Linux as a distribution specific
16#package (i.e. python-sphinx for Ubuntu). Independent of the distribution
17#method this module expects sphix-build to be either available on the PATH,
18#or to be located in a host OS specific standard location.
19#
20#This modules has the following parameters:
21#   SPHINX_PATH   = variable specifying where sphinx-build can be found.
22#                         If it is not defined the environment variable with
23#                         the same name is used. If that is also undefined,
24#                         then OS specific standard locations will be
25#                         searched.
26#
27# This modules defines the following variables:
28#   SPHINX_VERSION   = The version reported by "sphinx-build --version"
29#   SPHINX_FOUND     = True is sphinx-build was found and executed fine
30#
31
32Include(CMakeParseArguments)
33
34#Sphinx needs Python.
35find_package(PythonInterp 3)
36if (NOT PYTHONINTERP_FOUND)
37	message(STATUS "Can not find Python3.x interpreter. Pyhton3 must be installed and available on the PATH.")
38	message(STATUS "Sphinx documentation targets will not be created.")
39	return()
40endif()
41
42if (NOT DEFINED SPHINX_PATH)
43	if (DEFINED $ENV{SPHINX_PATH})
44	set(SPHINX_PATH $ENV{SPHINX_PATH})
45	endif()
46endif()
47
48
49if (DEFINED SPHINX_PATH)
50	#Find the Sphinx executable. Search only at SPHINX_PATH.
51	find_program(SPHINX_EXECUTABLE
52		NAMES sphinx-build
53		DOC "Sphinx Documentation Builder (sphinx-doc.org)"
54		PATH ${SPHINX_PATH}
55		NO_DEFAULT_PATH
56		NO_CMAKE_ENVIRONMENT_PATH
57		NO_CMAKE_PATH
58		NO_SYSTEM_ENVIRONMENT_PATH
59		NO_CMAKE_SYSTEM_PATH
60		NO_CMAKE_FIND_ROOT_PATH
61	)
62	if (SPHINX_EXECUTABLE-NOTFOUND)
63		message(STATUS "Failed to find sphinx-build at ${SPHINX_PATH}.")
64		message(STATUS "Sphinx documentation targets will not be created.")
65		return()
66	endif()
67else()
68	#Find the Sphinx executable. Search OS specific default locations.
69	find_program(SPHINX_EXECUTABLE
70	  NAMES sphinx-build
71	  DOC "Sphinx Documentation Builder (sphinx-doc.org)"
72	)
73
74	if (SPHINX_EXECUTABLE-NOTFOUND)
75		message(STATUS "Failed to find sphinx-build at OS specific default locations.")
76		message(STATUS "Sphinx documentation targets will not be created.")
77		return()
78	endif()
79endif()
80
81#Get Sphinx version
82execute_process(COMMAND "${SPHINX_EXECUTABLE}" "--version" OUTPUT_VARIABLE _SPHINX_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
83#Parse output
84if(_SPHINX_VERSION)
85	if(_SPHINX_VERSION MATCHES ".*sphinx-build[^0-9.]*([0-9.]+).*")
86		string(REGEX REPLACE ".*sphinx-build ([0-9.]+).*" "\\1" SPHINX_VERSION "${_SPHINX_VERSION}")
87	endif()
88endif()
89
90#Set "standard" find module return values
91include(FindPackageHandleStandardArgs)
92find_package_handle_standard_args(Sphinx REQUIRED_VARS SPHINX_EXECUTABLE SPHINX_VERSION VERSION_VAR SPHINX_VERSION)
93