Skip to content

Commit

Permalink
prevent usage same binary file simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
maierru committed Jan 4, 2022
1 parent 517b658 commit 9552253
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/ffi/tools/const_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ def const(name, format = nil, cast = '', ruby_name = nil, converter = nil,
# @return [nil]
# @raise if a constant is missing and +:required+ was set to +true+ (see {#initialize})
def calculate(options = {})
binary = File.join Dir.tmpdir, "rb_const_gen_bin_#{Process.pid}"
binary_path = nil

Tempfile.open("#{@prefix}.const_generator") do |f|
binary_path = f.path + ".bin"
@includes.each do |inc|
f.puts "#include <#{inc}>"
end
Expand All @@ -125,16 +126,16 @@ def calculate(options = {})
f.flush

cc = ENV['CC'] || 'gcc'
output = `#{cc} #{options[:cppflags]} -D_DARWIN_USE_64_BIT_INODE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -x c -Wall -Werror #{f.path} -o #{binary} 2>&1`
output = `#{cc} #{options[:cppflags]} -D_DARWIN_USE_64_BIT_INODE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -x c -Wall -Werror #{f.path} -o #{binary_path} 2>&1`

unless $?.success? then
output = output.split("\n").map { |l| "\t#{l}" }.join "\n"
raise "Compilation error generating constants #{@prefix}:\n#{output}"
end
end

output = `#{binary}`
File.unlink(binary + (FFI::Platform.windows? ? ".exe" : ""))
output = `#{binary_path}`
File.unlink(binary_path + (FFI::Platform.windows? ? ".exe" : ""))
output.each_line do |line|
line =~ /^(\S+)\s(.*)$/
const = @constants[$1]
Expand Down

0 comments on commit 9552253

Please sign in to comment.