forked from azawawi/libsodium-ios
-
Notifications
You must be signed in to change notification settings - Fork 1
/
libsodium.rb
executable file
·295 lines (267 loc) · 11.4 KB
/
libsodium.rb
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env ruby
#
# A Ruby script to download and build libsodium for iOS, macOS, tvOS and watchOS
# Adapted from https://github.com/drewcrawford/libsodium-ios/blob/master/libsodium.sh
#
require 'fileutils'
if /darwin/ !~ RUBY_PLATFORM
puts "This script needs macOS to run"
exit 1
end
#
# libsodium release version
# Please visit https://github.com/jedisct1/libsodium/releases
#
# Download and extract the latest stable release indicated by PKG_VER variable
def download_and_extract_libsodium()
puts "Downloading latest stable release of 'libsodium'"
pkg_name = "libsodium-#{PKG_VER}"
pkg = "#{pkg_name}.tar.gz"
url = "https://github.com/jedisct1/libsodium/releases/download/#{PKG_VER}/#{pkg}"
exit 1 unless system("cd #{BUILDDIR} && curl -O -L #{url}")
exit 1 unless system("cd #{BUILDDIR} && tar xzf #{pkg}")
FileUtils.mv "#{BUILDDIR}/#{pkg_name}", "build/libsodium"
FileUtils.rm "#{BUILDDIR}/#{pkg}"
end
def find_sdks
sdks=`xcodebuild -showsdks`.chomp
sdk_versions = {}
for line in sdks.lines do
if line =~ /-sdk iphoneos(\S+)/
sdk_versions["iOS"] = $1
elsif line =~ /-sdk macosx(\S+)/
sdk_versions["macOS"] = $1
elsif line =~ /-sdk appletvos(\S+)/
sdk_versions["tvOS"] = $1
elsif line =~ /-sdk watchos(\S+)/
sdk_versions["watchOS"] = $1
end
end
return sdk_versions
end
# libsodium release version
PKG_VER = "1.0.14"
# Minimum platform versions
IOS_VERSION_MIN = 9.0
MACOS_VERSION_MIN = 10.11
TVOS_VERSION_MIN = 9.0
WATCHOS_VERSION_MIN = 2.0
LIBNAME = "libsodium.a"
VALID_ARHS_PER_PLATFORM = {
"iOS" => ["armv7", "armv7s", "arm64", "i386", "x86_64"],
"macOS" => ["x86_64"],
"tvOS" => ["arm64", "x86_64"],
"watchOS" => ["armv7k", "i386"],
}
DEVELOPER = `xcode-select -print-path`.chomp
LIPO = `xcrun -sdk iphoneos -find lipo`.chomp
# Script's directory
SCRIPTDIR = File.absolute_path(File.dirname(__FILE__))
# libsodium root directory
LIBDIR = File.join(SCRIPTDIR, "build/libsodium")
# Destination directory for build and install
DSTDIR = SCRIPTDIR
BUILDDIR = "#{DSTDIR}/build"
DISTDIR = "#{DSTDIR}/dist"
DISTLIBDIR = "#{DISTDIR}/lib"
sdk_versions = find_sdks()
IOS_SDK_VERSION = sdk_versions["iOS"]
MACOS_SDK_VERSION = sdk_versions["macOS"]
TVOS_SDK_VERSION = sdk_versions["tvOS"]
WATCHOS_SDK_VERSION = sdk_versions["watchOS"]
puts "iOS SDK version = #{IOS_SDK_VERSION}"
puts "macOS SDK version = #{MACOS_SDK_VERSION}"
puts "watchOS SDK version = #{WATCHOS_SDK_VERSION}"
puts "tvOS SDK version = #{TVOS_SDK_VERSION}"
OTHER_CFLAGS = "-Os -Qunused-arguments -fembed-bitcode"
# Cleanup
if File.directory? BUILDDIR
FileUtils.rm_rf BUILDDIR
end
if File.directory? DISTDIR
FileUtils.rm_rf DISTDIR
end
FileUtils.mkdir_p BUILDDIR
FileUtils.mkdir_p DISTDIR
# Download/extract libsodium into build folder
download_and_extract_libsodium()
PLATFORMS = sdk_versions.keys
libs_per_platform = {}
# Compile libsodium for each Apple device platform
for platform in PLATFORMS
# Compile libsodium for each valid Apple device architecture
archs = VALID_ARHS_PER_PLATFORM[platform]
for arch in archs
puts "Building #{platform}/#{arch}..."
build_arch_dir=File.absolute_path("#{BUILDDIR}/#{platform}-#{arch}")
FileUtils.mkdir_p(build_arch_dir)
build_type = "#{platform}-#{arch}"
case build_type
when "iOS-armv7"
# iOS 32-bit ARM (till iPhone 4s)
platform_name = "iPhoneOS"
host = "#{arch}-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{IOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mios-version-min=#{IOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-mthumb -arch #{arch} -isysroot #{isdk_root}"
when "iOS-armv7s"
# iOS 32-bit ARM (iPhone 5 till iPhone 5c)
platform_name = "iPhoneOS"
host = "#{arch}-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{IOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mios-version-min=#{IOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-mthumb -arch #{arch} -isysroot #{isdk_root}"
when "watchOS-armv7k"
# watchOS 32-bit ARM
platform_name = "WatchOS"
host = "#{arch}-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{WATCHOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mwatchos-version-min=#{WATCHOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-mthumb -arch #{arch} -isysroot #{isdk_root}"
when "iOS-arm64"
# iOS 64-bit ARM (iPhone 5s and later)
platform_name = "iPhoneOS"
host = "arm-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{IOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mios-version-min=#{IOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-mthumb -arch #{arch} -isysroot #{isdk_root}"
when "tvOS-arm64"
# tvOS 64-bit ARM (Apple TV 4)
platform_name = "AppleTVOS"
host = "arm-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{TVOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mtvos-version-min=#{TVOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-mthumb -arch #{arch} -isysroot #{isdk_root}"
# tvsos-version-min?
when "iOS-i386"
# iOS 32-bit simulator (iOS 6.1 and below)
platform_name = "iPhoneSimulator"
host = "#{arch}-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{IOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mios-version-min=#{IOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-m32 -arch #{arch}"
when "macOS-i386"
# macOS 32-bit
platform_name = "MacOSX"
host = "#{arch}-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{MACOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mmacosx-version-min=#{MACOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-m32 -arch #{arch}"
when "watchOS-i386"
# watchOS 32-bit simulator
platform_name = "WatchSimulator"
host = "#{arch}-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{WATCHOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mwatchos-version-min=#{WATCHOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-m32 -arch #{arch}"
when "iOS-x86_64"
# iOS 64-bit simulator (iOS 7+)
platform_name = "iPhoneSimulator"
host = "#{arch}-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{IOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mios-version-min=#{IOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-arch #{arch}"
when "macOS-x86_64"
# macOS 64-bit
platform_name = "MacOSX"
host = "#{arch}-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{MACOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mmacosx-version-min=#{MACOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-arch #{arch}"
when "tvOS-x86_64"
# tvOS 64-bit simulator
platform_name = "AppleTVSimulator"
host = "#{arch}-apple-darwin"
base_dir = "#{DEVELOPER}/Platforms/#{platform_name}.platform/Developer"
ENV["BASEDIR"] = base_dir
isdk_root = "#{base_dir}/SDKs/#{platform_name}#{TVOS_SDK_VERSION}.sdk"
ENV["ISDKROOT"] = isdk_root
ENV["CFLAGS"] = "-arch #{arch} -isysroot #{isdk_root} -mtvos-version-min=#{TVOS_VERSION_MIN} #{OTHER_CFLAGS}"
ENV["LDFLAGS"] = "-arch #{arch}"
else
warn "Unsupported platform/architecture #{build_type}"
next
#exit 1
end
# Modify path to include Xcode toolchain path
ENV["PATH"] = "#{DEVELOPER}/Toolchains/XcodeDefault.xctoolchain/usr/bin:" +
"#{DEVELOPER}/Toolchains/XcodeDefault.xctoolchain/usr/sbin:#{ENV["PATH"]}"
puts "Configuring for #{arch}..."
FileUtils.cd(LIBDIR)
configure_cmd = [
"./configure",
"--prefix=#{build_arch_dir}",
"--disable-shared",
"--enable-static",
"--host=#{host}",
]
exit 1 unless system(configure_cmd.join(" "))
puts "Building #{LIBNAME} for #{arch}..."
exit 1 unless system("make clean")
exit 1 unless system("make -j8 V=0")
exit 1 unless system("make install")
# Add to the architecture-dependent library list for the current platform
libs = libs_per_platform[platform]
if libs == nil
libs_per_platform[platform] = libs = []
end
libs.push "#{build_arch_dir}/lib/#{LIBNAME}"
end
end
# Build a single universal (fat) library file for each platform
# And copy headers
for platform in PLATFORMS
dist_platform_folder = "#{DISTDIR}/#{platform.downcase}"
dist_platform_lib = "#{dist_platform_folder}/lib"
FileUtils.mkdir_p dist_platform_lib
# Find libraries for platform
libs = libs_per_platform[platform]
# Make sure library list is not empty
if libs == nil || libs.length == 0
warn "Nothing to do for #{LIBNAME}"
next
end
# Build universal library file (aka fat binary)
lipo_cmd = "#{LIPO} -create #{libs.join(" ")} -output #{dist_platform_lib}/#{LIBNAME}"
puts "Combining #{libs.length} libraries into #{LIBNAME} for #{platform}..."
exit 1 unless system(lipo_cmd)
# Copy headers for architecture
for arch in VALID_ARHS_PER_PLATFORM["iOS"]
include_dir = "#{BUILDDIR}/#{platform}-#{arch}/include"
if File.directory? include_dir
FileUtils.cp_r(include_dir, dist_platform_folder)
end
end
end
# Cleanup
FileUtils.rm_rf BUILDDIR