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 | |
---|
5 | DIFF=${DIFF:-/usr/bin/diff} |
---|
6 | ECHO=${ECHO:-/bin/echo} |
---|
7 | GREP=${GREP:-/usr/bin/grep} |
---|
8 | HEAD=${HEAD:-/usr/bin/head} |
---|
9 | MKDIR=${MKDIR:-/bin/mkdir} |
---|
10 | RM=${RM:-/bin/rm} |
---|
11 | SED=${SED:-/usr/bin/sed} |
---|
12 | WC=${WC:-/usr/bin/wc} |
---|
13 | |
---|
14 | HEADER_FILE="USB.h" |
---|
15 | FRAMEWORK_HEADER_DIR="IOKit/usb" |
---|
16 | DESTDIR=${DESTDIR:-"include"} |
---|
17 | |
---|
18 | # remove trailing '/' from DESTDIR, if it exists |
---|
19 | DESTDIR=$(${ECHO} "${DESTDIR}" | ${SED} -e "s@/\$@@") |
---|
20 | |
---|
21 | # create the destination directory and new file with path to it |
---|
22 | FILE_DIR="${DESTDIR}/${FRAMEWORK_HEADER_DIR}" |
---|
23 | NEW_USB_H_FILE="${FILE_DIR}/${HEADER_FILE}" |
---|
24 | |
---|
25 | # just in case SED needs these ... |
---|
26 | LANG=C |
---|
27 | LC_CTYPE=C |
---|
28 | LC_ALL=C |
---|
29 | |
---|
30 | # if "xcode-select" exists, then use it to find the current developer path |
---|
31 | XCODE_SELECT=$(which xcode-select) |
---|
32 | if [ -z "${XCODE_SELECT}" ]; then |
---|
33 | # does not exist, which happens in Xcode for 10.4; assume default install |
---|
34 | DEV_DIR=/Developer |
---|
35 | else |
---|
36 | DEV_DIR=$(${XCODE_SELECT} -print-path) |
---|
37 | fi |
---|
38 | |
---|
39 | PLATFORM=$(uname -r | sed -ne 's/\([0-9][0-9]*\)\..*/\1/p') |
---|
40 | MACOS_MINOR=$(expr ${PLATFORM} - 4) |
---|
41 | echo "MACOS_MINOR is '${MACOS_MINOR}'" |
---|
42 | MACOS_MAJOR=10 |
---|
43 | if [ "${MACOS_MINOR}" == "16" ]; then |
---|
44 | MACOS_MAJOR=11 |
---|
45 | fi |
---|
46 | echo "MACOS_MAJOR is '${MACOS_MAJOR}" |
---|
47 | MACOS_MAJOR_MINOR=${MACOS_MAJOR}.${MACOS_MINOR} |
---|
48 | echo "MACOS_MAJOR_MINOR 1 is '${MACOS_MAJOR_MINOR}" |
---|
49 | # special for 10.4 ... use "10.4u" |
---|
50 | if [ "${MACOS_MINOR}" == "4" ]; then |
---|
51 | MACOS_MAJOR_MINOR=${MACOS_MAJOR_MINOR}u |
---|
52 | fi |
---|
53 | echo "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 |
---|
57 | SDK_PARENT_DIR="${DEV_DIR}/SDKs" |
---|
58 | if [ ! -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 |
---|
64 | fi |
---|
65 | echo "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 |
---|
70 | SDK_DIR="${SDK_PARENT_DIR}/MacOSX.sdk" |
---|
71 | if [ ! -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 |
---|
77 | fi |
---|
78 | echo "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 |
---|
82 | USB_H_FILE="${SDK_DIR}/System/Library/Frameworks/IOKit.framework/Headers/usb/USB.h" |
---|
83 | if [ ! -f "${USB_H_FILE}" ]; then |
---|
84 | echo "Unable to find header '${HEADER_FILE}'" |
---|
85 | exit -3 |
---|
86 | fi |
---|
87 | echo "Found USB header file as '${USB_H_FILE}'" |
---|
88 | |
---|
89 | # we have the file; check to see if it needs patching |
---|
90 | HAS_PRAGMAS=$(${GREP} -E "^#pragma options align=reset" "${USB_H_FILE}") |
---|
91 | if [ -z "${HAS_PRAGMAS}" ]; then |
---|
92 | echo "IOKit '${HEADER_FILE}' header seems to not contain the faulty #pragma directives" |
---|
93 | exit 0 |
---|
94 | fi |
---|
95 | |
---|
96 | # yes: needs patching |
---|
97 | |
---|
98 | # if the file exists, delete it |
---|
99 | if [ -f "${NEW_USB_H_FILE}" ]; then |
---|
100 | echo "File '${NEW_USB_H_FILE}' exists; deleting it." |
---|
101 | ${RM} "${NEW_USB_H_FILE}" |
---|
102 | else |
---|
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 |
---|
108 | fi |
---|
109 | |
---|
110 | # create the patched version of the header |
---|
111 | echo "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 |
---|
120 | if [ ! -f "${NEW_USB_H_FILE}" ]; then |
---|
121 | echo "File '${NEW_USB_H_FILE}' not created!" |
---|
122 | exit -4 |
---|
123 | fi |
---|
124 | echo "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 |
---|
127 | WC_MINUS_DIFF=$(${DIFF} -u "${USB_H_FILE}" "${NEW_USB_H_FILE}" | ${GREP} -E "^-" | ${GREP} -vE "^---" | ${WC} -l | ${SED} -e "s/^[ \t]*//") |
---|
128 | WC_PLUS_DIFF=$(${DIFF} -u "${USB_H_FILE}" "${NEW_USB_H_FILE}" | ${GREP} -E "^\\+" | ${GREP} -vE "^\\+\\+\\+" | ${WC} -l | ${SED} -e "s/^[ \t]*//") |
---|
129 | WC_MINUS_DIFF_TIMES_5=$(expr ${WC_MINUS_DIFF} * 5) |
---|
130 | if [ "${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 |
---|
133 | fi |
---|
134 | echo "Found correct ratio of '#pragma' changes; verifying first." |
---|
135 | # compare just the first entry |
---|
136 | GREP_OPTIONS_1=$(${GREP} "\#pragma options" "${NEW_USB_H_FILE}" | ${HEAD} -1) |
---|
137 | if [ "${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 |
---|
140 | fi |
---|
141 | |
---|
142 | # success |
---|
143 | echo "Success!" |
---|
144 | exit 0 |
---|