lib/mkmf/lite.rb in mkmf-lite-0.5.1 vs lib/mkmf/lite.rb in mkmf-lite-0.5.2

- old
+ new

@@ -1,23 +1,30 @@ +# frozen_string_literal: true + require 'erb' require 'rbconfig' require 'tmpdir' require 'open3' require 'ptools' +# The Mkmf module serves as a namespace only. module Mkmf + # The Lite module scopes the Mkmf module to differentiate it from the + # Mkmf module in the standard library. module Lite # The version of the mkmf-lite library - MKMF_LITE_VERSION = '0.5.1'.freeze + MKMF_LITE_VERSION = '0.5.2' private + # rubocop:disable Layout/LineLength def cpp_command command = RbConfig::CONFIG['CC'] || RbConfig::CONFIG['CPP'] || File.which('cc') || File.which('gcc') || File.which('cl') - raise "Compiler not found" unless command + raise 'Compiler not found' unless command command end + # rubocop:enable Layout/LineLength def cpp_source_file 'conftest.c' end @@ -51,11 +58,11 @@ code = erb.result(binding) if directories.empty? options = nil else - options = "" + options = '' directories.each{ |dir| options += "-I#{dir} " } options.rstrip! end try_to_compile(code, options) @@ -154,13 +161,11 @@ else headers += common_headers.split end headers = headers.flatten.uniq - headers = headers.map{ |h| "#include <#{h}>" }.join("\n") - - headers + headers.map{ |h| "#include <#{h}>" }.join("\n") end # Create a temporary bit of C source code in the temp directory, and # try to compile it. If it succeeds attempt to run the generated code. # The code generated is expected to print a number to STDOUT, which @@ -175,38 +180,38 @@ result = 0 stderr_orig = $stderr.dup stdout_orig = $stdout.dup - Dir.chdir(Dir.tmpdir){ - File.open(cpp_source_file, 'w'){ |fh| fh.write(code) } + Dir.chdir(Dir.tmpdir) do + File.write(cpp_source_file, code) - command = cpp_command + ' ' - command += cpp_out_file + ' ' + command = "#{cpp_command} " + command += "#{cpp_out_file} " command += cpp_source_file # Temporarily close these $stderr.reopen(IO::NULL) $stdout.reopen(IO::NULL) if system(command) $stdout.reopen(stdout_orig) # We need this back for open3 to work. - conftest = File::ALT_SEPARATOR ? "conftest.exe" : "./conftest.exe" + conftest = File::ALT_SEPARATOR ? 'conftest.exe' : './conftest.exe' Open3.popen3(conftest) do |stdin, stdout, stderr| stdin.close stderr.close result = stdout.gets.chomp.to_i end else - raise "Failed to compile source code with command '#{command}':\n===\n" + code + "===" + raise "Failed to compile source code with command '#{command}':\n===\n#{code}===" end - } + end ensure - File.delete(cpp_source_file) if File.exists?(cpp_source_file) - File.delete(cpp_out_file) if File.exists?(cpp_out_file) + File.delete(cpp_source_file) if File.exist?(cpp_source_file) + File.delete(cpp_out_file) if File.exist?(cpp_out_file) $stderr.reopen(stderr_orig) $stdout.reopen(stdout_orig) end result @@ -217,45 +222,45 @@ # false. # # Note that $stderr is temporarily redirected to the null device because # we don't actually care about the reason for failure. # - def try_to_compile(code, command_options=nil) + def try_to_compile(code, command_options = nil) begin boolean = false stderr_orig = $stderr.dup stdout_orig = $stdout.dup - Dir.chdir(Dir.tmpdir){ - File.open(cpp_source_file, 'w'){ |fh| fh.write(code) } + Dir.chdir(Dir.tmpdir) do + File.write(cpp_source_file, code) if command_options - command = cpp_command + ' ' + command_options + ' ' + command = "#{cpp_command} #{command_options} " else - command = cpp_command + ' ' + command = "#{cpp_command} " end - command += cpp_out_file + ' ' + command += "#{cpp_out_file} " command += cpp_source_file $stderr.reopen(IO::NULL) $stdout.reopen(IO::NULL) boolean = system(command) - } + end ensure - File.delete(cpp_source_file) if File.exists?(cpp_source_file) - File.delete(cpp_out_file) if File.exists?(cpp_out_file) + File.delete(cpp_source_file) if File.exist?(cpp_source_file) + File.delete(cpp_out_file) if File.exist?(cpp_out_file) $stdout.reopen(stdout_orig) $stderr.reopen(stderr_orig) end boolean end # Slurp the contents of the template file for evaluation later. # def read_template(file) - IO.read(get_template_file(file)) + File.read(get_template_file(file)) end # Retrieve the path to the template +file+ name. # def get_template_file(file)