dyld-655.1.1.tar.gz
[apple/dyld.git] / bin / expand.rb
1 #!/usr/bin/env ruby
2
3 require 'yaml'
4
5 $availCmd = ENV["SDKROOT"] + "/usr/local/libexec/availability.pl";
6
7 def versionString(vers)
8 uvers = ""
9 if vers =~ /^(\d+)$/
10 uvers = "#{$1}_0"
11 elsif vers =~ /^(\d+).(\d+)$/
12 uvers = "#{$1}_#{$2}"
13 elsif vers =~ /^(\d+).(\d+).(\d+)$/
14 if $3 == 0
15 uvers = "#{$1}_#{$2}"
16 else
17 uvers = "#{$1}_#{$2}_#{$3}"
18 end
19 end
20 uvers
21 end
22
23 def versionHex(vers)
24 major = 0;
25 minor = 0;
26 revision = 0;
27
28 if vers =~ /^(\d+)$/
29 major = $1.to_i;
30 elsif vers =~ /^(\d+).(\d+)$/
31 major = $1.to_i;
32 minor = $2.to_i;
33 elsif vers =~ /^(\d+).(\d+).(\d+)$/
34 major = $1.to_i;
35 minor = $2.to_i;
36 revision = $3.to_i;
37 end
38 "0x00#{major.to_s(16).rjust(2, '0')}#{minor.to_s(16).rjust(2, '0')}#{revision.to_s(16).rjust(2, '0')}"
39 end
40
41 def expandVersions(prefix, arg)
42 versionList = `#{$availCmd} #{arg}`.gsub(/\s+/m, ' ').strip.split(" ")
43 versionList.each { |version|
44 puts "#define #{prefix}#{versionString(version)}".ljust(48, ' ') + versionHex(version)
45 }
46 end
47
48 def expandPlatformVersions(prefix, platform, arg)
49 versionList = `#{$availCmd} #{arg}`.gsub(/\s+/m, ' ').strip.split(" ")
50 versionList.each { |version|
51 puts "static dyld_build_version_t dyld_platform_version_#{prefix}_#{versionString(version)}".ljust(72, ' ') + "= { .platform = #{platform}, .version = #{versionHex(version)} };"
52 }
53 end
54
55 def versionSetsForOSes(versionSets, key, platform, target)
56 puts "#if #{target}"
57 versionSets.each { |k,v|
58 puts "#define dyld_#{k}_os_versions dyld_platform_version_#{platform}_#{versionString(v[key].to_s)}"
59 }
60 puts "#endif /* #{target} */"
61 end
62
63 def expandVersionSets()
64 versionSets = YAML.load(`#{$availCmd} --sets`)
65 versionSetsForOSes(versionSets, "macos", "macOS", "TARGET_OS_OSX")
66 versionSetsForOSes(versionSets, "ios", "iOS", "TARGET_OS_IOS")
67 versionSetsForOSes(versionSets, "tvos", "tvOS", "TARGET_OS_TV")
68 versionSetsForOSes(versionSets, "watchos", "watchOS", "TARGET_OS_WATCH")
69 versionSetsForOSes(versionSets, "bridgeos", "bridgeOS", "TARGET_OS_BRIDGE")
70 end
71
72 ARGF.each do |line|
73 if line =~ /^\/\/\@MAC_VERSION_DEFS\@$/
74 expandVersions("DYLD_MACOSX_VERSION_", "--macosx")
75 elsif line =~ /^\/\/\@IOS_VERSION_DEFS\@$/
76 expandVersions("DYLD_IOS_VERSION_", "--ios")
77 elsif line =~ /^\/\/\@WATCHOS_VERSION_DEFS\@$/
78 expandVersions("DYLD_WATCHOS_VERSION_", "--watchos")
79 elsif line =~ /^\/\/\@MACOS_PLATFORM_VERSION_DEFS\@$/
80 expandPlatformVersions("macOS", "PLATFORM_MACOS", "--macosx")
81 elsif line =~ /^\/\/\@IOS_PLATFORM_VERSION_DEFS\@$/
82 expandPlatformVersions("iOS", "PLATFORM_IOS", "--ios")
83 elsif line =~ /^\/\/\@WATCHOS_PLATFORM_VERSION_DEFS\@$/
84 expandPlatformVersions("watchOS", "PLATFORM_WATCHOS", "--watchos")
85 elsif line =~ /^\/\/\@TVOS_PLATFORM_VERSION_DEFS\@$/
86 expandPlatformVersions("tvOS", "PLATFORM_TVOS", "--appletvos")
87 elsif line =~ /^\/\/\@BRIDGEOS_PLATFORM_VERSION_DEFS\@$/
88 expandPlatformVersions("bridgeOS", "PLATFORM_BRIDGEOS", "--bridgeos")
89 elsif line =~ /^\/\/\@VERSION_SET_DEFS\@$/
90 expandVersionSets()
91 else
92 puts line
93 end
94 end