ffi-clang

FORK: Ruby FFI bindings for my custom patched clang 8.0.
git clone https://git.neptards.moe/neptards/ffi-clang.git
Log | Files | Refs | README

lib.rb (2952B)


      1 # Copyright, 2010-2012 by Jari Bakken.
      2 # Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
      3 #
      4 # Permission is hereby granted, free of charge, to any person obtaining a copy
      5 # of this software and associated documentation files (the "Software"), to deal
      6 # in the Software without restriction, including without limitation the rights
      7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8 # copies of the Software, and to permit persons to whom the Software is
      9 # furnished to do so, subject to the following conditions:
     10 #
     11 # The above copyright notice and this permission notice shall be included in
     12 # all copies or substantial portions of the Software.
     13 #
     14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     20 # THE SOFTWARE.
     21 
     22 require 'mkmf'
     23 
     24 module FFI
     25 	module Clang
     26 		module Lib
     27 			extend FFI::Library
     28 
     29 			# Use LLVM_CONFIG if it was explicitly specified:
     30 			llvm_config = ENV['LLVM_CONFIG']
     31 
     32 			# If we aren't building for a specific version (e.g. travis) try to find llvm-config
     33 			unless ENV['LLVM_VERSION']
     34 				 llvm_config ||= MakeMakefile.find_executable0("llvm-config")
     35 			end
     36 
     37 			libs = []
     38 			if ENV['LIBCLANG']
     39 				libs << ENV['LIBCLANG']
     40 			else
     41 				begin
     42 					xcode_dir = `xcode-select -p`.chomp
     43 					%W[
     44 					#{xcode_dir}/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib
     45 					#{xcode_dir}/usr/lib/libclang.dylib
     46 				].each do |f|
     47 						if File.exist? f
     48 							libs << f
     49 							break
     50 						end
     51 					end
     52 				rescue Errno::ENOENT
     53 					# Ignore
     54 				end
     55 
     56 				libs << "clang"
     57 
     58 				if llvm_config
     59 					llvm_library_dir = `#{llvm_config} --libdir`.chomp
     60 					platform = FFI::Clang.platform
     61 
     62 					case platform
     63 					when :darwin
     64 						libs << llvm_library_dir + '/libclang.dylib'
     65 					when :windows
     66 						llvm_bin_dir = `#{llvm_config} --bindir`.chomp
     67 						libs << llvm_bin_dir + '/libclang.dll'
     68 					else
     69 						libs << llvm_library_dir + '/libclang.so'
     70 					end
     71 				end
     72 			end
     73 
     74 			ffi_lib libs
     75 
     76 			def self.bitmask_from(enum, opts)
     77 				bitmask = 0
     78 
     79 				opts.each do |key, value|
     80 					if int = enum[key]
     81 						bitmask |= int
     82 					else
     83 						raise Error, "unknown option: #{key.inspect}, expected one of #{enum.symbols}"
     84 					end
     85 				end
     86 
     87 				bitmask
     88 			end
     89 
     90 			def self.opts_from(enum, bitmask)
     91 				bit = 1
     92 				opts = {}
     93 				while bitmask != 0
     94 					if bitmask & 1
     95 						if sym = enum[bit]
     96 							opts[sym] = true
     97 						else
     98 							raise Error, "unknown values: #{bit}, expected one of #{enum.symbols}"
     99 						end
    100 					end
    101 					bitmask >>= 1
    102 					bit <<= 1
    103 				end
    104 				opts
    105 			end
    106 		end
    107 	end
    108 end