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

- old
+ new

@@ -3,18 +3,22 @@ require 'erb' require 'rbconfig' require 'tmpdir' require 'open3' require 'ptools' +require 'fileutils' +require 'memoist' # 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 + extend Memoist + # The version of the mkmf-lite library - MKMF_LITE_VERSION = '0.5.2' + MKMF_LITE_VERSION = '0.6.0' private # rubocop:disable Layout/LineLength def cpp_command @@ -22,10 +26,12 @@ raise 'Compiler not found' unless command command end # rubocop:enable Layout/LineLength + memoize :cpp_command + def cpp_source_file 'conftest.c' end def cpp_out_file @@ -34,20 +40,24 @@ else '-o conftest.exe' end end + memoize :cpp_out_file + # TODO: We should adjust this based on OS. For now we're using # arguments I think you'll typically see set on Linux and BSD. def cpp_libraries if RbConfig::CONFIG['LIBS'] RbConfig::CONFIG['LIBS'] + RbConfig::CONFIG['LIBRUBYARG'] else '-lrt -ldl -lcrypt -lm' end end + memoize :cpp_libraries + public # Check for the presence of the given +header+ file. You may optionally # provide a list of directories to search. # @@ -66,10 +76,12 @@ end try_to_compile(code, options) end + memoize :have_header + # Check for the presence of the given +function+ in the common header # files, or within any +headers+ that you provide. # # Returns true if found, or false if not found. # @@ -85,10 +97,12 @@ # Check for just the function pointer first. If that fails, then try # to compile with the function declaration. try_to_compile(ptr_code) || try_to_compile(std_code) end + memoize :have_func + # Checks whether or not the struct of type +struct_type+ contains the # +struct_member+. If it does not, or the struct type cannot be found, # then false is returned. # # An optional list of +headers+ may be specified, in addition to the @@ -100,10 +114,12 @@ code = erb.result(binding) try_to_compile(code) end + memoize :have_struct_member + # Returns the value of the given +constant+ (which could also be a macro) # using +headers+, or common headers if no headers are specified. # # If this method fails an error is raised. This could happen if the constant # can't be found and/or the header files do not include the indicated constant. @@ -114,10 +130,12 @@ code = erb.result(binding) try_to_execute(code) end + memoize :check_valueof + # Returns the sizeof +type+ using +headers+, or common headers if no # headers are specified. # # If this method fails an error is raised. This could happen if the type # can't be found and/or the header files do not include the indicated type. @@ -135,10 +153,12 @@ code = erb.result(binding) try_to_execute(code) end + memoize :check_sizeof + private # Take an array of header file names (or convert it to an array if it's a # single argument), add the COMMON_HEADERS, flatten it out and remove any # duplicates. @@ -206,12 +226,12 @@ else raise "Failed to compile source code with command '#{command}':\n===\n#{code}===" end end ensure - File.delete(cpp_source_file) if File.exist?(cpp_source_file) - File.delete(cpp_out_file) if File.exist?(cpp_out_file) + FileUtils.rm_f(cpp_source_file) + FileUtils.rm_f(cpp_out_file) $stderr.reopen(stderr_orig) $stdout.reopen(stdout_orig) end result @@ -245,11 +265,11 @@ $stderr.reopen(IO::NULL) $stdout.reopen(IO::NULL) boolean = system(command) end ensure - File.delete(cpp_source_file) if File.exist?(cpp_source_file) - File.delete(cpp_out_file) if File.exist?(cpp_out_file) + FileUtils.rm_f(cpp_source_file) + FileUtils.rm_f(cpp_out_file) $stdout.reopen(stdout_orig) $stderr.reopen(stderr_orig) end boolean