comparison scripts/gen.rb @ 3:d0162d8ca0f2

Added support for function pointers in dstepgen
author Jacob Carlborg <doob@me.com>
date Sun, 05 Jul 2009 20:54:42 +0200
parents
children 6255d355d752
comparison
equal deleted inserted replaced
2:9fd439a28ce3 3:d0162d8ca0f2
1 #!/usr/bin/env ruby
2
3 ##
4 # Copyright:: Copyright (c) 2009 Jacob Carlborg.
5 # Author:: Jacob Carlborg
6 # Version:: Initial created: 2009
7 # License:: [Boost Software License 1.0]http://www.boost.org/LICENSE_1_0.txt
8 #
9
10 require "rubygems"
11 gem "xml-simple"
12 require "xmlsimple"
13 require "optparse"
14 require "date"
15
16 # Extensions to String
17 class String
18
19 # Returns the index of the given character or length of the string if not found
20 # char:: the character to look for
21 # start:: the index where to start the search
22 #
23 # === Example:
24 # "012345789".index_of("1") #=> 1
25 # "0123 0123".index_of("3", 6) #=> 8
26 #
27 def index_of (char, start = 0)
28 return self.length if start >= self.length
29
30 i = 0
31
32 if start == 0
33 self.each_char do |c|
34 return i if char == c
35 i += 1
36 end
37 else
38 self[start + 1 .. -1].each_char do |c|
39 return i + start + 1 if char == c
40 i += 1
41 end
42 end
43
44 return self.length
45 end
46 end
47
48 class Generator
49 VERSION = 1.0
50
51 attr_accessor :dependencies_switch, :out_dir, :private
52
53 def initialize
54 @frameworks = []
55 @do_64bit = false
56 @dependencies_switch = false
57 @dependencies = {}
58 @private = false
59 end
60
61 def framework_path (val)
62 return val if File.exist?(val)
63
64 val += ".framework" unless /\.framework$/.match(val)
65 paths = ["/System/Library/Frameworks", "/Library/Frameworks", "#{ENV['HOME']}/Library/Frameworks"]
66 paths << "/System/Library/PrivateFrameworks" if @private
67
68 paths.each do |dir|
69 path = File.join(dir, val)
70 return path if File.exist?(path)
71 end
72
73 return nil
74 end
75
76 def add_framework (framework)
77 @frameworks << framework_path(framework)
78 end
79
80 def collect_dependencies (framework)
81 result = `./dstepgen.rb -f #{framework} -d`
82 @dependencies[framework] = {}
83
84 result.each_line do |path|
85 path = path[0 .. -2] # remove the newline
86 @dependencies[framework][path] = true
87 collect_dependencies(path) unless @dependencies.key?(path)
88 end
89 end
90
91 def write_dependencies
92 @frameworks.each do |framework|
93 collect_dependencies(framework)
94 end
95
96 file = STDOUT
97
98 @dependencies.each do |key, value|
99 file << "#{key}\n"
100 end
101
102 exit
103 end
104
105 def generate
106 dstep_file = "Frameworks.dstep"
107
108 @frameworks.each do |framework|
109 collect_dependencies(framework)
110 end
111
112 str = StringIO.new
113
114 frameworks = []
115
116 @dependencies.each do |framework, value|
117 frameworks << get_umbrella_framework(framework)
118 end
119
120 frameworks.uniq!
121
122 frameworks.each do |framework|
123 str << " -f "
124 str << framework
125 end
126
127 `./dstepgen.rb -o #{@out_dir}/#{dstep_file} #{str.string}`
128 `./dgen.rb -o #{@out_dir} #{@out_dir}/#{dstep_file}`
129 end
130
131 def get_framework_name (framework)
132 i = framework.rindex(".framework")
133 return framework if i.nil?
134 x = framework.rindex("/", i)
135 framework[x + 1 ... i]
136 end
137
138 def get_umbrella_framework (framework)
139 str = ".framework"
140 i = framework.index(str)
141 return framework[0 ... i + str.length] unless i.nil?
142 framework
143 end
144 end
145
146 if __FILE__ == $0
147 gen = Generator.new
148
149 OptionParser.new do |opts|
150 opts.banner = "Usage: #{File.basename(__FILE__)} [options] <frameworks...>"
151 opts.separator ""
152 opts.separator "Options:"
153
154 opts.on("-u", "--umbrella FRAMEWORK", "Link againts the given umbrella framework.") do |opt|
155 gen.umbrella_framework(opt)
156 end
157
158 opts.on(nil, "--64-bit", "Write 64-bit annotations.") do
159 gen.do_64bit = true
160 end
161
162 opts.on("-p", "--private", "Include private frameworks.") do |opt|
163 gen.private = true
164 end
165
166 opts.on("-o", "--output DIRECTORY", "Place the output file(s) in this directory.") do |opt|
167 die "The specified directory \"#{opt}\" does not exists" if File.exist?(opt) == false
168 die "Output directory cannot be specified more than once" if gen.out_dir
169 gen.out_dir = opt
170 end
171
172 opts.on("-c", "--code CODE", "Inject CODE in the type file.") do |opt|
173 gen.code_to_inject = opt
174 end
175
176 opts.on("-d", "--dependencies", "Write dependencies to stdout and exit.") do |opt|
177 gen.dependencies_switch = true
178 end
179
180 help_msg = "Use the `-h' flag or for help."
181
182 opts.on("-h", "--help", "Show this message and exit.") do
183 puts opts, help_msg
184 exit
185 end
186
187 opts.on('-v', '--version', 'Show version and exit.') do
188 puts Generator::VERSION
189 exit
190 end
191
192 opts.separator ""
193
194 if ARGV.empty?
195 die opts.banner
196 else
197 #begin
198 opts.parse!(ARGV)
199
200 ARGV.each do |framework|
201 gen.add_framework(framework)
202 end
203
204 gen.write_dependencies if gen.dependencies_switch
205
206 die "No output directory given" if gen.out_dir.nil?
207
208 gen.generate
209 # rescue => e
210 # msg = e.message
211 # msg = "Internal error" if msg.empty?
212 #
213 # die msg, opts.banner, help_msg
214 # end
215 end
216 end
217 end