aboutsummaryrefslogtreecommitdiff
path: root/rakelib/ffi.rake
blob: ad2832ee4ae241e18bc90d9a4d4736e0787d8619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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