summaryrefslogtreecommitdiff
path: root/tools/reloc_spec_check.sh
blob: b0f18922b30af7193a4a8b1966650766c1593562 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash

# This script checks for overlay's relocs to be correctly used on the spec file depending on the overlay's NON_MATCHING/NON_EQUIVALENT
# and minimize possible broken NON_MATCHING builds.
# The script performs the following checks:
# - The extracted reloc isn't being used on the spec if the overlay has no other GLOBAL_ASM left (it has been matched).
# - Warn if an overlay is using both NON_MATCHING and NON_EQUIVALENT.
# - Warn if an overlay has NON_MATCHING ifdefs but some functions are still unattempted.
# - Both relocs (generated and extracted) are used on the spec on NON_MATCHING overlays.
# - The generated reloc isn't being used on NON_EQUIVALENT overlays.
# If any of those checks fails for any overlay, the script will print a warning and exit with a non-zero exit code.

BOLD="$(tput bold)"
RED="$(tput setaf 1)"
PURPLE="$(tput setaf 5)"
WHITE="$(tput setaf 7)"
BLINK="$(tput blink)"
RST="$(tput sgr0)"

# Run the script from the root of the project
DIR="$(dirname "$(readlink -f "$0")")"
cd "$DIR/.."

EXIT_CODE=0

OVERLAYS_SUBFOLDERS="$(find src/overlays/*/*  -type d)"

while read OVL_FOLDER; do
    # C files which are part of the current overlay
    FILES_OF_OVERLAY="$(find "$OVL_FOLDER" -name '*.c')"
    # List of files (of the current overlay) which doesn't contain GLOBAL_ASM
    FILES_WITHOUT_PRAGMA="$(grep -L '#pragma GLOBAL_ASM' ${FILES_OF_OVERLAY})"

    if [ "$FILES_OF_OVERLAY" == "$FILES_WITHOUT_PRAGMA" ]; then
        # This overlay is fully matched, so it should not be using the extracted reloc (use the generated one instead)

        OVL_NAME="$(cut -d '/' -f 4 <<< ${OVL_FOLDER})"

        # Parse the spec, searching for the extracted reloc of this overlay
        OVL_WITH_EXTRACTED_RELOC="$(grep -F "$OVL_NAME".reloc spec)"

        if [ "$OVL_WITH_EXTRACTED_RELOC" != "" ]; then
            echo "${BOLD} Matching overlay using extracted reloc on spec found: ${RED} ${OVL_FOLDER} ${RST}"
            echo "    Don't forget to change the extracted reloc to the generated one on the spec, otherwise the matched overlay won't be shiftable"
            EXIT_CODE=1
        fi
    else
        # Overlay not fully matched

        # Search for files with NON_MATCHING and NON_EQUIVALENT
        FILES_WITH_NON_MATCHING="$(grep -Fl '#ifdef NON_MATCHING' ${FILES_OF_OVERLAY})"
        FILES_WITH_NON_EQUIVALENT="$(grep -Fl '#ifdef NON_EQUIVALENT' ${FILES_OF_OVERLAY})"

        if [ "$FILES_WITH_NON_MATCHING" != "" ] && [ "$FILES_WITH_NON_EQUIVALENT" != "" ]; then
            # An overlay can't have both NON_MATCHINGs and NON_EQUIVALENTs, because relocs wouldn't be generated correctly

            echo "${BOLD} Overlay with both NON_MATCHINGs and NON_EQUIVALENTs found: ${RED} ${OVL_FOLDER} ${RST}"
            echo "    Overlays can't have both NON_MATCHING and NON_EQUIVALENT because it breaks NON_MATCHING builds"
            echo "    As an alternative you could mark NON_MATCHING functions as NON_EQUIVALENT and add a comment mentioning it is actually equivalent"
            EXIT_CODE=1

        elif [ "$FILES_WITH_NON_MATCHING" != "" ]; then
            # Overlay only contains NON_MATCHINGs

            # Get the count of NON_MATCHING and GLOBAL_ASM of the current overlay
            NON_MATCHINGS_COUNT_OF_OVERLAY="$(grep -F '#ifdef NON_MATCHING' ${FILES_WITH_NON_MATCHING} | wc -l)"
            PRAGMAS_COUNT_OF_OVERLAY="$(grep -F '#pragma GLOBAL_ASM' ${FILES_WITH_NON_MATCHING} | wc -l)"

            if [ "$NON_MATCHINGS_COUNT_OF_OVERLAY" != "$PRAGMAS_COUNT_OF_OVERLAY" ]; then
                # If an overlay uses NON_MATCHING, then every function should be either matching or marked as NON_MATCHING

                echo "${BOLD} Found an overlay with a differing number of NON_MATCHINGs and #pragma GLOBAL_ASM: ${RED} ${OVL_FOLDER} ${RST}"
                echo "    To use a NON_MATCHING check on an overlay, every function that does not match should verified to be equivalent"
                echo "    But the amount of NON_MATCHING ifdefs and GLOBAL_ASM does no match, which may suggest not every function of this overlay may have been attempted"
                EXIT_CODE=1
            fi

            OVL_NAME="$(cut -d '/' -f 4 <<< ${OVL_FOLDER})"

            # Parse the spec, searching for the extracted reloc of this overlay
            OVL_WITH_EXTRACTED_RELOC="$(grep -F "$OVL_NAME".reloc spec)"

            # Parse the spec, searching for the generated reloc of this overlay
            OVL_WITH_GENERATED_RELOC="$(grep -F "$OVL_NAME"_reloc spec)"

            if [ "$OVL_WITH_EXTRACTED_RELOC" == "" ] || [ "$OVL_WITH_GENERATED_RELOC" == "" ]; then
                # Overlays with NON_MATCHINGs should have both relocs on the spec

                echo "${BOLD} NON_MATCHING overlay missing either extracted or generated reloc on spec found: ${RED} ${OVL_FOLDER} ${RST}"
                echo "    Overlays with NON_MATCHING ifdefs must also add a NON_MATCHING ifdef on the spec to allow using the generated reloc on NON_MATCHING builds"
                EXIT_CODE=1
            fi
        elif [ "$FILES_WITH_NON_EQUIVALENT" != "" ]; then
            # Overlay only contains NON_EQUIVALENTs

            OVL_NAME="$(cut -d '/' -f 4 <<< ${OVL_FOLDER})"

            # Parse the spec, searching for the generated reloc of this overlay
            OVL_WITH_GENERATED_RELOC="$(grep -F "$OVL_NAME"_reloc spec)"

            if [ "$OVL_WITH_GENERATED_RELOC" != "" ]; then
                # Overlays with NON_EQUIVALENTs should not use a generated reloc

                echo "${BOLD} NON_EQUIVALENT overlay with generated reloc on spec found: ${RED} ${OVL_FOLDER} ${RST}"
                echo "    Overlays with NON_EQUIVALENT ifdefs can't use generated relocs"
                EXIT_CODE=1
            fi
        fi
    fi
done <<< ${OVERLAYS_SUBFOLDERS}

exit $EXIT_CODE