# This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. -} # frozen_string_literal: true require 'ffi/clang' MUSL_BASE = Pathname.new "#{Dir.home}/Projects/gh/musl" MUSL_INCLUDES = ['include', 'arch/aarch64'] .map { |musl_include| "#{MUSL_BASE}/#{musl_include}" } .freeze desc 'Generate bindings' task :ffi do state = FFIState.new MUSL_INCLUDES state.read_file "#{MUSL_BASE}/include/fenv.h" end # Collects binding. class FFIState def initialize(includes) @index = FFI::Clang::Index.new @clang_args = includes.map { |include_path| "-I#{include_path}" } end def read_file(include_path) tu = @index.parse_translation_unit include_path, @clang_args, [], [:detailed_preprocessing_record] # Verify there're now parse errors. tu.diagnostics.each do |diag| puts "[#{diag.severity}] #{diag.spelling} at #{diag.location.line}:#{diag.location.column}" end tu.cursor.visit_children do |cursor, _parent| # Skip compiler internals. next if cursor.location.file.nil? read_definition cursor end end private def read_definition(cursor) case cursor.kind when :cursor_function read_function cursor when :cursor_struct puts "Struct #{cursor.spelling}" when :cursor_macro_definition puts "Define #{cursor.spelling}" when :cursor_inclusion_directive # else # puts "Current child #{cursor.kind}" end end def read_function(cursor) puts "Function #{cursor.spelling}" end end