1find_package(Doxygen QUIET)
2if (PICO_SDK_TOP_LEVEL_PROJECT AND ${DOXYGEN_FOUND})
3    set(PICO_BUILD_DOCS_DEFAULT 1)
4endif()
5option(PICO_BUILD_DOCS "Build HTML Doxygen docs" ${PICO_BUILD_DOCS_DEFAULT})
6
7if (DEFINED ENV{PICO_EXAMPLES_PATH} AND NOT PICO_EXAMPLES_PATH)
8    set(PICO_EXAMPLES_PATH $ENV{PICO_EXAMPLES_PATH})
9    message("Using PICO_EXAMPLES_PATH from environment ('${PICO_EXAMPLES_PATH}')")
10endif()
11
12if(PICO_BUILD_DOCS)
13    if(NOT DOXYGEN_FOUND)
14        message(FATAL_ERROR "Doxygen is needed to build the documentation.")
15    endif()
16
17    include(ExternalProject)
18
19    if(PICO_EXAMPLES_PATH)
20        get_filename_component(PICO_EXAMPLES_PATH "${PICO_EXAMPLES_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
21        if (EXISTS ${PICO_EXAMPLES_PATH})
22            message("Documentation example code will come from ${PICO_EXAMPLES_PATH}")
23        else()
24            message(WARNING "Documentation example code configured to come from ${PICO_EXAMPLES_PATH}, but that path does not exist")
25        endif()
26        add_custom_target(doc-pico-examples)
27    else()
28        ExternalProject_Add(doc-pico-examples
29                GIT_REPOSITORY    https://github.com/raspberrypi/pico-examples
30                GIT_TAG           master
31                CONFIGURE_COMMAND ""
32                BUILD_COMMAND ""
33                INSTALL_COMMAND ""
34                )
35        ExternalProject_Get_property(doc-pico-examples SOURCE_DIR)
36        ExternalProject_Get_property(doc-pico-examples GIT_REPOSITORY)
37        ExternalProject_Get_property(doc-pico-examples GIT_TAG)
38        set(PICO_EXAMPLES_PATH ${SOURCE_DIR})
39        message("Documentation example code will come from git repo ${GIT_REPOSITORY}, branch ${GIT_TAG}")
40    endif()
41
42    set(DOXY_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/doxygen")
43    set(DOXY_INPUT_DIRS "${PICO_DOXYGEN_PATHS}")
44    set(DOXY_EXCLUDE_DIRS "${PICO_DOXYGEN_EXCLUDE_PATHS}")
45    set(DOXY_EXAMPLE_DIR "${PICO_EXAMPLES_PATH}")
46
47    set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
48    set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
49
50    configure_file(${doxyfile_in} ${doxyfile} @ONLY)
51
52    add_custom_target(docs
53            COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
54            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
55            COMMENT "Generating API documentation with Doxygen"
56            VERBATIM)
57
58    add_dependencies(docs doc-pico-examples)
59endif()
60