build system: convert bin2h call from Ruby executable to built-in function

This commit is contained in:
Moritz Bunkus 2014-01-01 21:41:01 +01:00
parent a77be987e3
commit 3d015db49b
3 changed files with 29 additions and 32 deletions

View File

@ -200,7 +200,8 @@ rule '.o' => '.rc' do |t|
end end
rule '.h' => '.png' do |t| rule '.h' => '.png' do |t|
runq " BIN2H #{t.source}", "#{c(:top_srcdir)}/rake.d/bin/bin2h.rb #{t.source} #{t.name}" puts " BIN2H #{t.source}" if !ENV['V'].to_bool
bin2h t.source, t.name
end end
# Resources depend on the manifest.xml file for Windows builds. # Resources depend on the manifest.xml file for Windows builds.

View File

@ -1,31 +0,0 @@
#!/usr/bin/env ruby
if ARGV.size != 2
puts "Syntax: bin2h.rb source.ext destination.h"
exit 1
end
bin_name = File.basename(ARGV[0]).gsub(/[^a-z\d]/i, '_').gsub(/_+/, '_') + '_bin'
File.open(ARGV[1], "w") do |file|
file.puts <<EOT
// Automatically generated. Do not modify.
#ifndef BIN2H__#{bin_name.upcase}_INCLUDED
#define BIN2H__#{bin_name.upcase}_INCLUDED
static unsigned char #{bin_name}[] = {
EOT
data = IO.binread(ARGV[0]).unpack("C*")
data.each_with_index do |byte, idx|
file.write sprintf("0x%02x", byte)
file.write(((idx + 1) % 13) != 0 ? ", " : ",\n") unless (idx == data.size - 1)
end
file.puts <<EOT
};
#endif // BIN2H__#{bin_name.upcase}_INCLUDED
EOT
end

View File

@ -70,6 +70,33 @@ def runq(msg, cmdline, options = {})
run cmdline, options.clone.merge(:dont_echo => !verbose) run cmdline, options.clone.merge(:dont_echo => !verbose)
end end
def bin2h bin_file_name, h_file_name
bin_name = File.basename(bin_file_name).gsub(/[^a-z\d]/i, '_').gsub(/_+/, '_') + '_bin'
File.open(h_file_name, "w") do |file|
file.puts <<EOT
// Automatically generated. Do not modify.
#ifndef BIN2H__#{bin_name.upcase}_INCLUDED
#define BIN2H__#{bin_name.upcase}_INCLUDED
static unsigned char #{bin_name}[] = {
EOT
data = IO.binread(bin_file_name).unpack("C*")
data.each_with_index do |byte, idx|
file.write sprintf("0x%02x", byte)
file.write(((idx + 1) % 13) != 0 ? ", " : ",\n") unless (idx == data.size - 1)
end
file.puts <<EOT
};
#endif // BIN2H__#{bin_name.upcase}_INCLUDED
EOT
end
end
def create_dependency_dirs def create_dependency_dirs
[ $dependency_dir, $dependency_tmp_dir ].each do |dir| [ $dependency_dir, $dependency_tmp_dir ].each do |dir|
File.unlink(dir) if FileTest.exist?(dir) && !FileTest.directory?(dir) File.unlink(dir) if FileTest.exist?(dir) && !FileTest.directory?(dir)