summaryrefslogtreecommitdiff
path: root/cmake/SphinxCheckInternalLinks.cmake
blob: 32c2566e79103308f0ded983a7fc628debe3dfa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Check "local" sphinx links, using files in the build tree and in the
# install tree (other components)

message(STATUS "Checking internal links")

set(failed OFF)

file(STRINGS "${OUTPUT}" output)
file(REMOVE "${OUTPUT}.new")
foreach(line IN LISTS output)
  # Parse the filename, link type, link and anchor from each line
  string(REGEX MATCH "^.*: \\[.*\\] .*" line_match "${line}")
  string(REGEX MATCH "^.*: \\[.*\\] .*#.*" anchor_match "${line}")
  if(line_match)
    string(REGEX REPLACE "^(.*): \\[(.*)\\] (.*)" "\\1" filename "${line}")
    string(REGEX REPLACE "^(.*): \\[(.*)\\] (.*)" "\\2" type "${line}")
    if(anchor_match)
      string(REGEX REPLACE "^(.*): \\[(.*)\\] (.*)#(.*)" "\\3" link "${line}")
      string(REGEX REPLACE "^(.*): \\[(.*)\\] (.*)#(.*)" "\\4" link "${anchor}")
    else()
      string(REGEX REPLACE "^(.*): \\[(.*)\\] (.*)" "\\3" link "${line}")
      unset(anchor)
    endif()

    # Only check local links
    if(type STREQUAL "local")
      # Compute path relative to our install path
      file(RELATIVE_PATH relpath "/" "/${SPHINX_INSTALL_PATH}/${link}")

      # Check sphinx link in the install tree
      unset(linkfile)
      if(EXISTS "${EXTERNAL_REFERENCE}/${relpath}")
        set(linkfile "${EXTERNAL_REFERENCE}/${relpath}")
      endif()
      # Check sphinx link in the build tree
      if(EXISTS "${INTERNAL_REFERENCE}/${link}")
        set(linkfile "${INTERNAL_REFERENCE}/${link}")
      endif()
      # Check doxygen link in the install tree
      if(EXISTS "${DOXYGEN_REFERENCE}/${link}")
        set(linkfile "${DOXYGEN_REFERENCE}/${link}")
      endif()
      # Check doxygen link in the build tree
      if(DOXYGEN_REFERENCE AND DOXYGEN_INSTALL_PATH)
        string(REGEX REPLACE "^${DOXYGEN_INSTALL_PATH}/(.*)" "\\1" doxygen_path "${relpath}")
        if(EXISTS "${DOXYGEN_REFERENCE}/${doxygen_path}")
          set(linkfile "${DOXYGEN_REFERENCE}/${doxygen_path}")
        endif()
      endif()
      # Check link and any anchor.  If the link does not exist, or the
      # anchor does not exist, set the link type to broken when
      # rewriting the output file with our test results.
      if(linkfile)
        if(anchor)
          file(STRINGS "${linkfile}" anchor_matches REGEX "<a +class=\"anchor\" +id=\"${anchor}\"")
          if(anchor_matches)
            file(APPEND "${OUTPUT}.new" "${filename}: [${type}] ${link}#${anchor}\n")
          else()
            set(failed ON)
            file(APPEND "${OUTPUT}.new" "${filename}: [broken] ${link}#${anchor}\n")
            message(STATUS "Broken link: ${filename}: ${link}#${anchor}")
          endif()
        else()
          file(APPEND "${OUTPUT}.new" "${filename}: [${type}] ${link}\n")
        endif()
      else()
        set(failed ON)
        file(APPEND "${OUTPUT}.new" "${filename}: [broken] ${link}\n")
        message(STATUS "Broken link: ${filename}: ${link}")
      endif()
    else()
      if(type STREQUAL "broken")
        set(failed ON)
        message(STATUS "Broken link: ${filename}: ${link}")
      endif()
      file(APPEND "${OUTPUT}.new" "${filename}: [${type}] ${link}\n")
    endif()
  else()
    file(APPEND "${OUTPUT}.new" "${line}\n")
  endif()
endforeach()

file(RENAME "${OUTPUT}.new" "${OUTPUT}")

if(failed)
  message(FATAL_ERROR "Broken links detected")
endif()