-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake-gcc-without-xcode
executable file
·251 lines (211 loc) · 6.88 KB
/
make-gcc-without-xcode
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/zsh
# ------------------------------------------------------------------------------
# FILE: make-gcc-without-xcode
# DESCRIPTION: Makes a GCC without Xcode archive.
# AUTHOR: Sorin Ionescu <[email protected]>
# ------------------------------------------------------------------------------
# Amend path for PlistBuddy.
path+=( /usr/libexec/ )
script_name="${0:t}"
script_version='1.0.4'
xcode_installer='/Applications/Install Xcode.app'
xcode_installer_plist="${xcode_installer}/Contents/Info.plist"
xcode_installer_packages="${xcode_installer}/Contents/Resources/Packages"
# Writes to standard error.
function print-error() {
print "${script_name}: ${@}" >&2
}
# Writes to standard output.
function print-info() {
print "${@}" >&1
}
# Writes version information to standard errror.
function version() {
print "${script_name} ${script_version}
Copyright (c) 2011 Sorin Ionescu
This program is free software. You may modify or distribute it
under the terms of the MIT License." >&2
}
# Writes help to standard error.
function help() {
print "Usage: ${script_name} [‐option ...]
Options:
-v, --version Display version and copyright
-h, --help Display this help
Report bugs to <[email protected]>." >&2
}
# Parse switches.
while [[ "${1}" == -* ]]; do
case "${1}" in
( -v | --version )
version
exit 0
;;
( -h | --help )
help
exit 0
;;
( -- )
shift
break
;;
( -* )
print-error "invalid option: ${1}"
help
exit 1
;;
esac
done
# Root is required to preserve permissions.
if (( ${EUID} != 0 )); then
print-info "Root privileges are required; running sudo..."
exec sudo "${0}" "${@}"
exit "${?}"
fi
# Make sure the xcode_installer is not missing before continuing.
if [[ ! -d "${xcode_installer}" ]]; then
print-error "'${xcode_installer}' not found."
exit 1
fi
xcode_version="$( PlistBuddy -c 'Print :CFBundleShortVersionString' "${xcode_installer_plist}" )"
mac_osx_version="$( PlistBuddy -c 'Print :LSMinimumSystemVersion' "${xcode_installer_plist}" )"
archive_prefix="${PWD}/${script_name#make-}-${xcode_version}-mac-os-x-${mac_osx_version}"
packages=(
'BluetoothSDK.pkg'
'CoreAudioSDK.pkg'
'DevSDK.pkg'
'DistributedBuildsSupport.pkg'
'DeveloperToolsCLI.pkg'
'DeveloperToolsSystemSupport.pkg'
'FireWireSDK.pkg'
'JavaSDK.pkg'
'OpenGLSDK.pkg'
'QuickTimeSDK.pkg'
'WebKitSDK.pkg'
'X11Documentation.pkg'
'X11SDK.pkg'
'clang.pkg'
'llvm-gcc4.2.pkg'
)
# Make sure packages are not missing before continuing.
for package in "${packages[@]}"; do
if [[ ! -e "${xcode_installer_packages}/${package}" ]]; then
print-error "Package '${package}' is missing. Xcode Installer is corrupt."
exit 1
fi
done
# Not every package has to be fully intalled.
typeset -A package_root package_includes package_excludes
package_root[MacOSX10.7.pkg]='/SDKs/MacOSX10.7.sdk'
# Excludes
package_excludes[DeveloperToolsCLI.pkg]='
Library
'
# Includes
package_includes[DeveloperToolsSystemSupport.pkg]='
private/etc/gdb.conf
usr/bin/xcode-select
usr/share/man/man1/xcode-select.1
usr/share/xcode-select
'
payload_file_dir="$( mktemp -d -t "${script_name}" )"
payload_data_dir="$( mktemp -d -t "${script_name}" )"
package_merge_dir="$( mktemp -d -t "${script_name}" )"
print-info "Extracting:"
for package in "${packages[@]}"; do
print-info " ${package}"
# Set full path to package.
package_path="${xcode_installer_packages}/${package}"
# Extract Payload file from package.
(
[[ -n "${payload_file_dir}" ]] || continue
cd "${payload_file_dir}" || continue
xar -x -f "${package_path}" Payload
)
# Zsh does not support array of arrays; fake it.
included_files=( ${(ps:\n:)package_includes[${package}]} )
excluded_files=( ${(ps:\n:)package_excludes[${package}]} )
# Prepend the -f cpio switch to each item.
index=1
length=$(( ${#excluded_files} * 2 ))
while (( ${index} <= ${length} )); do
excluded_files[${index}]=( '-f' "${excluded_files[${index}]}" )
(( index += 2 ))
done
# Extract Payload file.
(
[[ -n "${payload_data_dir}" ]] || continue
cd "${payload_data_dir}" || continue
gzcat < "${payload_file_dir}/Payload" \
| cpio -idm ${excluded_files} ${included_files} 2> /dev/null
)
# Merge package files.
ditto "${payload_data_dir}${package_root[${package}]}" "${package_merge_dir}"
# Clean directories for next package.
for dirty_dir in "${payload_file_dir}" "${payload_data_dir}"; do
(
[[ -n "${dirty_dir}" ]] || continue
cd "${dirty_dir}" || continue
find . -path './*' -maxdepth 1 -exec rm -rf '{}' \;
)
done
done
# Homebrew needs to know the Xcode version.
print-info 'Generating:'
print-info ' Dummy Xcode Files'
xcodebuild_file="${package_merge_dir}/usr/bin/xcodebuild"
mkdir -p "${xcodebuild_file:h}"
print '#!/bin/sh' >> "${xcodebuild_file}"
print "echo 'Xcode ${xcode_version}'" >> "${xcodebuild_file}"
chmod 755 "${xcodebuild_file}"
# Do not overwrite existing files.
print-info 'Purging:'
print-info ' Existing Files'
(
[[ -n "${package_merge_dir}" ]] || exit 1
cd "${package_merge_dir}" || exist 1
# Find all files and replace './' with '/'.
while IFS=$'\n' read existing_file; do
if [[ -e "${existing_file}" ]] || [[ -L "${existing_file}" ]]; then
# Delete all existing files (replace '/' with './').
rm -f ".${existing_file}"
fi
done < <(find . ! -type d | sed -e 's:^\.::g')
)
print-info 'Configuring:'
print-info ' Xcode Directory Path'
# Xcode root is now /, not /Developer (can be set with xcode-select).
if [[ -f "${package_merge_dir}/usr/share/xcode-select/xcode_dir_path" ]]; then
print '/' > "${package_merge_dir}/usr/share/xcode-select/xcode_dir_path"
fi
# Use high compression.
if (( ${+commands[xz]} )); then
archive="${archive_prefix}.cpio.xz"
compressor='xz'
else
archive="${archive_prefix}.cpio.gz"
compressor='gzip'
fi
print-info "Archiving:"
print-info " ${archive}"
# Do not archive the directory, only the contents,
# or risk having tar change / permissions,
# and crash the system until fixed.
(
[[ -n "${package_merge_dir}" ]] || exit 1
cd "${package_merge_dir}" || exit 1
ls -1A \
| xargs -I% find '%' -depth -print0 \
| cpio -0o 2> /dev/null \
| "${compressor}" -9 > "${archive}"
)
print-info 'Purging:'
print-info ' Temporary Files'
rm -rf "${payload_file_dir}" "${payload_data_dir}" "${package_merge_dir}"
if [[ "${archive}" == *.xz ]]; then
decompressor='xzcat'
else
decompressor='gzcat'
fi
print-info 'Install:'
print-info " cd / && "${decompressor}" < '${archive}' | sudo cpio -idmv"