Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/cmake/SphinxCheckInternalLinks.cmake

raw
1 # Check "local" sphinx links, using files in the build tree and in the
2 # install tree (other components)
3
4 message(STATUS "Checking internal links")
5
6 set(failed OFF)
7
8 file(STRINGS "${OUTPUT}" output)
9 file(REMOVE "${OUTPUT}.new")
10 foreach(line IN LISTS output)
11 # Parse the filename, link type, link and anchor from each line
12 string(REGEX MATCH "^.*: \\[.*\\] .*" line_match "${line}")
13 string(REGEX MATCH "^.*: \\[.*\\] .*#.*" anchor_match "${line}")
14 if(line_match)
15 string(REGEX REPLACE "^(.*): \\[(.*)\\] (.*)" "\\1" filename "${line}")
16 string(REGEX REPLACE "^(.*): \\[(.*)\\] (.*)" "\\2" type "${line}")
17 if(anchor_match)
18 string(REGEX REPLACE "^(.*): \\[(.*)\\] (.*)#(.*)" "\\3" link "${line}")
19 string(REGEX REPLACE "^(.*): \\[(.*)\\] (.*)#(.*)" "\\4" link "${anchor}")
20 else()
21 string(REGEX REPLACE "^(.*): \\[(.*)\\] (.*)" "\\3" link "${line}")
22 unset(anchor)
23 endif()
24
25 # Only check local links
26 if(type STREQUAL "local")
27 # Compute path relative to our install path
28 file(RELATIVE_PATH relpath "/" "/${SPHINX_INSTALL_PATH}/${link}")
29
30 # Check sphinx link in the install tree
31 unset(linkfile)
32 if(EXISTS "${EXTERNAL_REFERENCE}/${relpath}")
33 set(linkfile "${EXTERNAL_REFERENCE}/${relpath}")
34 endif()
35 # Check sphinx link in the build tree
36 if(EXISTS "${INTERNAL_REFERENCE}/${link}")
37 set(linkfile "${INTERNAL_REFERENCE}/${link}")
38 endif()
39 # Check doxygen link in the install tree
40 if(EXISTS "${DOXYGEN_REFERENCE}/${link}")
41 set(linkfile "${DOXYGEN_REFERENCE}/${link}")
42 endif()
43 # Check doxygen link in the build tree
44 if(DOXYGEN_REFERENCE AND DOXYGEN_INSTALL_PATH)
45 string(REGEX REPLACE "^${DOXYGEN_INSTALL_PATH}/(.*)" "\\1" doxygen_path "${relpath}")
46 if(EXISTS "${DOXYGEN_REFERENCE}/${doxygen_path}")
47 set(linkfile "${DOXYGEN_REFERENCE}/${doxygen_path}")
48 endif()
49 endif()
50 # Check link and any anchor. If the link does not exist, or the
51 # anchor does not exist, set the link type to broken when
52 # rewriting the output file with our test results.
53 if(linkfile)
54 if(anchor)
55 file(STRINGS "${linkfile}" anchor_matches REGEX "<a +class=\"anchor\" +id=\"${anchor}\"")
56 if(anchor_matches)
57 file(APPEND "${OUTPUT}.new" "${filename}: [${type}] ${link}#${anchor}\n")
58 else()
59 set(failed ON)
60 file(APPEND "${OUTPUT}.new" "${filename}: [broken] ${link}#${anchor}\n")
61 message(STATUS "Broken link: ${filename}: ${link}#${anchor}")
62 endif()
63 else()
64 file(APPEND "${OUTPUT}.new" "${filename}: [${type}] ${link}\n")
65 endif()
66 else()
67 set(failed ON)
68 file(APPEND "${OUTPUT}.new" "${filename}: [broken] ${link}\n")
69 message(STATUS "Broken link: ${filename}: ${link}")
70 endif()
71 else()
72 if(type STREQUAL "broken")
73 set(failed ON)
74 message(STATUS "Broken link: ${filename}: ${link}")
75 endif()
76 file(APPEND "${OUTPUT}.new" "${filename}: [${type}] ${link}\n")
77 endif()
78 else()
79 file(APPEND "${OUTPUT}.new" "${line}\n")
80 endif()
81 endforeach()
82
83 file(RENAME "${OUTPUT}.new" "${OUTPUT}")
84
85 if(failed)
86 message(FATAL_ERROR "Broken links detected")
87 endif()