From 7ab6e1021611cc6913ee4ab5faf1b31f928a65e9 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 13 Nov 2026 12:01:17 +0100 Subject: Create parser stubs --- rakelib/ffi.rake | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 rakelib/ffi.rake (limited to 'rakelib/ffi.rake') diff --git a/rakelib/ffi.rake b/rakelib/ffi.rake new file mode 100644 index 0000000..ad2832e --- /dev/null +++ b/rakelib/ffi.rake @@ -0,0 +1,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 -- cgit v1.2.3