Ticket #61868: fixincludes_usb.h.sh

File fixincludes_usb.h.sh, 4.9 KB (added by michaelld (Michael Dickens), 4 years ago)
Line 
1#!/bin/sh -f
2# USB.h contains #pragma's that are not compatible with GCC on MacOS through 10.14
3# patch this file if found to provide the correct #pragma for the correct compiler
4
5DIFF=${DIFF:-/usr/bin/diff}
6ECHO=${ECHO:-/bin/echo}
7GREP=${GREP:-/usr/bin/grep}
8HEAD=${HEAD:-/usr/bin/head}
9MKDIR=${MKDIR:-/bin/mkdir}
10RM=${RM:-/bin/rm}
11SED=${SED:-/usr/bin/sed}
12WC=${WC:-/usr/bin/wc}
13
14HEADER_FILE="USB.h"
15FRAMEWORK_HEADER_DIR="IOKit/usb"
16DESTDIR=${DESTDIR:-"include"}
17
18# remove trailing '/' from DESTDIR, if it exists
19DESTDIR=$(${ECHO} "${DESTDIR}" | ${SED} -e "s@/\$@@") 
20
21# create the destination directory and new file with path to it
22FILE_DIR="${DESTDIR}/${FRAMEWORK_HEADER_DIR}"
23NEW_USB_H_FILE="${FILE_DIR}/${HEADER_FILE}"
24
25# just in case SED needs these ...
26LANG=C
27LC_CTYPE=C
28LC_ALL=C
29
30# if "xcode-select" exists, then use it to find the current developer path
31XCODE_SELECT=$(which xcode-select)
32if [ -z "${XCODE_SELECT}" ]; then
33    # does not exist, which happens in Xcode for 10.4; assume default install
34    DEV_DIR=/Developer
35else
36    DEV_DIR=$(${XCODE_SELECT} -print-path)
37fi
38
39PLATFORM=$(uname -r | sed -ne 's/\([0-9][0-9]*\)\..*/\1/p')
40MACOS_MINOR=$(expr ${PLATFORM} - 4)
41echo "MACOS_MINOR is '${MACOS_MINOR}'"
42MACOS_MAJOR=10
43if [ "${MACOS_MINOR}" == "16" ]; then
44    MACOS_MAJOR=11
45fi
46echo "MACOS_MAJOR is '${MACOS_MAJOR}"
47MACOS_MAJOR_MINOR=${MACOS_MAJOR}.${MACOS_MINOR}
48echo "MACOS_MAJOR_MINOR 1 is '${MACOS_MAJOR_MINOR}"
49# special for 10.4 ... use "10.4u"
50if [ "${MACOS_MINOR}" == "4" ]; then
51    MACOS_MAJOR_MINOR=${MACOS_MAJOR_MINOR}u
52fi
53echo "MACOS_MAJOR_MINOR 2 is '${MACOS_MAJOR_MINOR}"
54
55# find the SDK directory
56# typically either $(DEV_DIR)/SDKs or $(DEV_DIR)/Platforms/MacOSX.platform/Developer/SDKs
57SDK_PARENT_DIR="${DEV_DIR}/SDKs"
58if [ ! -d "${SDK_PARENT_DIR}" ]; then
59    SDK_PARENT_DIR="${DEV_DIR}/Platforms/MacOSX.platform/Developer/SDKs"
60    if [ ! -d "${SDK_PARENT_DIR}" ]; then
61        echo "Unable to find parent SDK directory"
62        exit -1
63    fi
64fi
65echo "Found SDK parent directory '${SDK_PARENT_DIR}'"
66
67# find the correct SDK within DEV_DIR_2
68# typically either $(SDK_PARENT_DIR)/MacOS$(MACOS_MAJOR_MINOR).sdk
69# or $(SDK_PARENT_DIR)/MacOS.sdk for more recent MacOS
70SDK_DIR="${SDK_PARENT_DIR}/MacOSX.sdk"
71if [ ! -d "${SDK_DIR}" ]; then
72    SDK_DIR="${SDK_PARENT_DIR}/MacOSX${MACOS_MAJOR_MINOR}.sdk"
73    if [ ! -d "${SDK_DIR}" ]; then
74        echo "Unable to find MacOSX SDK directory"
75        exit -2
76    fi
77fi
78echo "Found SDK directory '${SDK_DIR}'"
79
80# make sure the header exists; it should -always- be
81# $(SDK_DIR)/System/Library/Frameworks/IOKit.framework/Headers/usb/USB.h
82USB_H_FILE="${SDK_DIR}/System/Library/Frameworks/IOKit.framework/Headers/usb/USB.h"
83if [ ! -f "${USB_H_FILE}" ]; then
84    echo "Unable to find header '${HEADER_FILE}'"
85    exit -3
86fi
87echo "Found USB header file as '${USB_H_FILE}'"
88
89# we have the file; check to see if it needs patching
90HAS_PRAGMAS=$(${GREP} -E "^#pragma options align=reset" "${USB_H_FILE}")
91if [ -z "${HAS_PRAGMAS}" ]; then
92    echo "IOKit '${HEADER_FILE}' header seems to not contain the faulty #pragma directives"
93    exit 0
94fi
95
96# yes: needs patching
97
98# if the file exists, delete it
99if [ -f "${NEW_USB_H_FILE}" ]; then
100    echo "File '${NEW_USB_H_FILE}' exists; deleting it."
101    ${RM} "${NEW_USB_H_FILE}"
102else
103    # make the destination directory, if it doesn't exist
104    if [ ! -d "${FILE_DIR}" ]; then
105        echo "Making patched USB header directory '${FILE_DIR}'"
106        $(${MKDIR} -p "${FILE_DIR}")
107    fi
108fi
109
110# create the patched version of the header
111echo "Creating patched '${HEADER_FILE}' header file"
112$(${SED} -e "s@^#pragma options align=reset@#if defined(__clang__) || defined(__llvm__)\\
113    #pragma options align=reset\\
114#else\\
115    #pragma pack()\\
116#endif@g" < "${USB_H_FILE}" > "${NEW_USB_H_FILE}")
117
118# verify patched version as best we can
119# (1) verify file exists
120if [ ! -f "${NEW_USB_H_FILE}" ]; then
121    echo "File '${NEW_USB_H_FILE}' not created!"
122    exit -4
123fi
124echo "Found created file '${NEW_USB_H_FILE}'; verifying changes."
125
126# (2) looking at the "diff", make sure the ratio of lines starting with '-' to those starting with '+' is 1:5
127WC_MINUS_DIFF=$(${DIFF} -u "${USB_H_FILE}" "${NEW_USB_H_FILE}" | ${GREP} -E "^-" | ${GREP} -vE "^---" | ${WC} -l | ${SED} -e "s/^[ \t]*//")
128WC_PLUS_DIFF=$(${DIFF} -u "${USB_H_FILE}" "${NEW_USB_H_FILE}" | ${GREP} -E "^\\+" | ${GREP} -vE "^\\+\\+\\+" | ${WC} -l | ${SED} -e "s/^[ \t]*//")
129WC_MINUS_DIFF_TIMES_5=$(expr ${WC_MINUS_DIFF} * 5)
130if [ "${WC_MINUS_DIFF_TIMES_5}" != "${WC_PLUS_DIFF}" ]; then
131    echo "Number of '#pragma' changes isn't 1:5 from original; new header might not work as desired."
132    exit -5
133fi
134echo "Found correct ratio of '#pragma' changes; verifying first."
135# compare just the first entry
136GREP_OPTIONS_1=$(${GREP} "\#pragma options" "${NEW_USB_H_FILE}" | ${HEAD} -1)
137if [ "${GREP_OPTIONS_1}" != "    #pragma options align=reset" ]; then
138    echo "Pragma changes do not seem correct; expecting '    #pragma options align=reset' but got '${GREP_OPTIONS_1}'"
139    exit -6
140fi
141
142# success
143echo "Success!"
144exit 0