Debian Codegraph @

Rakefile

raw
1 $VERBOSE = true
2 $verbose = Rake.application.options.trace
3
4 require 'rubygems'
5 require 'ffi'
6 require 'fileutils'
7
8 ICONSTANT = "jnr.constants.Constant"
9 PLATFORM_PREFIX = "jnr.constants.platform"
10 PLATFORM_DIR = "src/main/java/jnr/constants/platform"
11 OS = FFI::Platform::OS
12 ARCH = FFI::Platform::ARCH
13 IS_WINDOWS = OS =~ /^win/
14
15 if (IS_WINDOWS)
16 CONSTANTS = ['LastError', 'Errno', 'OpenFlags', 'Signal', 'AddressFamily', 'ProtocolFamily', 'IPProto', 'SocketMessage', 'SocketLevel', 'Sock', 'SocketOption', 'InterfaceInfo', 'ErrnoAddressInfo', 'AddressInfo', 'INet', 'INet6', 'IP', 'TCP', 'IPv6']
17 else
18 CONSTANTS = Dir["gen/const/*.rb"].map {|c| File.basename(c, ".rb") }
19 CONSTANTS.delete('LastError') # this is Windows-specific
20 end
21
22 def gen_platform_constants(name, pkg, file_name, options = {})
23 meth = "gen_#{name.downcase}_java".to_sym
24 FileUtils.makedirs(File.dirname(file_name))
25 File.open(file_name, "w") do |f|
26 constants = send(meth, options).constants
27 raise ArgumentError, "No constants generated for #{name}" if constants.empty?
28
29 f.puts "// WARNING: This file is autogenerated. DO NOT EDIT!"
30 f.puts "// Generated #{Time.now}"
31 f.puts "package #{pkg};"
32 f.puts "public enum #{name} implements #{ICONSTANT} {";
33 sep = nil
34 comments = []
35
36 values = constants.values
37
38 values.each_with_index do |c, i|
39 if c.value.nil?
40 comments << "// #{c.name} not defined"
41 else
42 if sep
43 f.puts sep
44 comments.each {|comm| f.puts "#{comm}" }
45 comments.clear
46 else
47 sep = ","
48 end
49 f.print "#{c.name}(#{c.value}L)"
50 end
51 end
52 f.puts ";"
53
54 # Generate the string description table
55 filtered = values.reject {|cv| cv.value.nil? }
56 sorted = filtered.sort
57
58 if sorted.empty?
59 min_value = max_value = 0
60 else
61 min_value, max_value = sorted.first.value, sorted.last.value
62 end
63
64 comments.each {|comm| f.puts "#{comm}" }
65 f.puts "private final long value;"
66 f.puts "private #{name}(long value) { this.value = value; }"
67 f.puts "public static final long MIN_VALUE = #{min_value}L;"
68 f.puts "public static final long MAX_VALUE = #{max_value}L;"
69 f.puts ""
70
71 unless filtered.empty?
72 f.puts "static final class StringTable {"
73 f.puts " public static final java.util.Map<#{name}, String> descriptions = generateTable();"
74 f.puts " public static final java.util.Map<#{name}, String> generateTable() {"
75 f.puts " java.util.Map<#{name}, String> map = new java.util.EnumMap<#{name}, String>(#{name}.class);"
76 filtered.each do |c|
77 f.puts " map.put(#{c.name}, \"#{c.description.nil? ? c.name : c.description}\");"
78 end
79 f.puts " return map;"
80 f.puts " }"
81 f.puts "}"
82 f.puts "public final String toString() { return StringTable.descriptions.get(this); }"
83 end
84 f.puts "public final int value() { return (int) value; }"
85 f.puts "public final int intValue() { return (int) value; }"
86 f.puts "public final long longValue() { return value; }"
87 f.puts "public final boolean defined() { return true; }"
88 f.puts "}"
89 end
90 end
91 def gen_fake_constants(name, pkg, file_name, options = {})
92 meth = "gen_#{name.downcase}_java".to_sym
93 FileUtils.makedirs(File.dirname(file_name))
94 File.open(file_name, "w") do |f|
95 cg = send(meth, options)
96 names = cg.names
97
98 f.puts "// WARNING: This file is autogenerated. DO NOT EDIT!"
99 f.puts "// Generated #{Time.now}"
100 f.puts "package #{pkg};"
101 f.puts "public enum #{name} implements #{ICONSTANT} {";
102
103 min_value = cg.min_value
104 max_value = 0
105 names.each_with_index do |c, i|
106 max_value = if min_value == 0
107 if i == 0
108 cg.type ==:bitmask ? "0x0" : i + min_value
109 else
110 cg.type ==:bitmask ? "0x#{(1 << (i - 1)).to_s(16)}" : i + min_value
111 end
112 else
113 cg.type ==:bitmask ? "0x#{(1 << i).to_s(16)}" : i + min_value
114 end
115
116 f.puts "#{c}(#{max_value})#{i < (names.length - 1) ? ',' : ';'}"
117 end
118
119 f.puts "private final long value;"
120 f.puts "private #{name}(long value) { this.value = value; }"
121 f.puts "public static final long MIN_VALUE = #{min_value}L;"
122 f.puts "public static final long MAX_VALUE = #{max_value}L;"
123 f.puts "public final int value() { return (int) value; }"
124 f.puts "public final int intValue() { return (int) value; }"
125 f.puts "public final long longValue() { return value; }"
126 f.puts "public final boolean defined() { return true; }"
127 f.puts "}"
128 end
129 end
130 def gen_xplatform_constants(name, pkg, file_name, options = {})
131 meth = "gen_#{name.downcase}_java".to_sym
132 FileUtils.makedirs(File.dirname(file_name))
133 File.open(file_name, "w") do |f|
134 cg = send(meth, options)
135 names = cg.names
136
137 f.puts "// WARNING: This file is autogenerated. DO NOT EDIT!"
138 f.puts "// Generated #{Time.now}"
139 f.puts "package #{pkg};"
140 f.puts "public enum #{name} implements #{ICONSTANT} {";
141 names.each { |n| f.puts "#{n}," }
142 f.puts "__UNKNOWN_CONSTANT__;"
143 unknown_range = cg.unknown_range
144 f.puts "private static final ConstantResolver<#{name}> resolver = "
145 if cg.type == :bitmask
146 f.puts "ConstantResolver.getBitmaskResolver(#{name}.class);"
147 elsif !unknown_range.empty?
148 f.puts "ConstantResolver.getResolver(#{name}.class, #{unknown_range[:first]}, #{unknown_range[:last]});"
149 else
150 f.puts "ConstantResolver.getResolver(#{name}.class);"
151 end
152 f.puts "public final int value() { return (int) resolver.longValue(this); }"
153 f.puts "public final int intValue() { return (int) resolver.longValue(this); }"
154 f.puts "public final long longValue() { return resolver.longValue(this); }"
155 f.puts "public final String description() { return resolver.description(this); }"
156 f.puts "public final boolean defined() { return resolver.defined(this); }"
157 f.puts "public final String toString() { return description(); }"
158 f.puts "public static #{name} valueOf(long value) { "
159 f.puts " return resolver.valueOf(value);"
160 f.puts "}"
161 f.puts "}"
162 end
163 end
164 xplatform_files = []
165 platform_files = []
166 lplatform_files = []
167 fake_files = []
168 CONSTANTS.each do |name|
169 load File.join(File.dirname(__FILE__), "gen", "const", "#{name}.rb")
170 platform_files << File.join(PLATFORM_DIR, OS, "#{name}.java")
171 file File.join(PLATFORM_DIR, OS, "#{name}.java") do |t|
172 gen_platform_constants(name, "#{PLATFORM_PREFIX}.#{OS}", t.name)
173 end
174 lplatform_files << File.join(PLATFORM_DIR, OS, ARCH, "#{name}.java")
175 file File.join(PLATFORM_DIR, OS, ARCH, "#{name}.java") do |t|
176 gen_platform_constants(name, "#{PLATFORM_PREFIX}.#{OS}.#{ARCH}", t.name)
177 end
178 xplatform_files << File.join(PLATFORM_DIR, "#{name}.java")
179 file File.join(PLATFORM_DIR, "#{name}.java") do |t|
180 gen_xplatform_constants(name, PLATFORM_PREFIX, t.name)
181 end
182 fake_files << File.join(PLATFORM_DIR, "fake", "#{name}.java")
183 file File.join(PLATFORM_DIR, "fake", "#{name}.java") do |t|
184 gen_fake_constants(name, "#{PLATFORM_PREFIX}.fake", t.name)
185 end
186 end
187
188 task :default => :generate
189 task :generate => platform_files + lplatform_files + xplatform_files + fake_files
190 task :regen => [ :clean, :generate ]
191 task :clean => [ "clean:platform", "clean:platform", "clean:xplatform", "clean:fake" ]
192 namespace :clean do
193 task :platform do
194 FileUtils.rm_f(platform_files)
195 end
196 task :lplatform do
197 FileUtils.rm_f(lplatform_files)
198 end
199 task :xplatform do
200 FileUtils.rm_f(xplatform_files)
201 end
202 task :fake do
203 FileUtils.rm_f(fake_files)
204 end
205 end
206 namespace :generate do
207 desc "Generate missing architecture constants"
208 task :platform => platform_files
209 desc "Generate missing local architecture constants"
210 task :lplatform => lplatform_files
211 desc "Generate missing platform-independent constants"
212 task :xplatform => xplatform_files
213 desc "Generate missing fake constants"
214 task :fake => fake_files
215 end
216 namespace :regen do
217 desc "Force regeneration of architecture constants"
218 task :platform => [ "clean:platform" ] + platform_files
219 desc "Force regeneration of local architecture constants"
220 task :lplatform => [ "clean:lplatform" ] + lplatform_files
221 desc "Force regeneration of platform-independent constants"
222 task :xplatform => [ "clean:xplatform" ] + xplatform_files
223 desc "Force regeneration of fake constants"
224 task :fake => [ "clean:fake" ] + fake_files
225 end