diff options
| author | Eugen Wissner <belka@caraus.de> | 2026-11-13 12:01:17 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2026-11-13 13:15:30 +0100 |
| commit | 7ab6e1021611cc6913ee4ab5faf1b31f928a65e9 (patch) | |
| tree | b6e650890682831503c73608ac7fd370d2847d8a /rakelib/ffi.rake | |
| parent | 0b5bd8114255e04dae27a7528a3111d6923e5fbe (diff) | |
| download | elna-7ab6e1021611cc6913ee4ab5faf1b31f928a65e9.tar.gz | |
Create parser stubs
Diffstat (limited to 'rakelib/ffi.rake')
| -rw-r--r-- | rakelib/ffi.rake | 63 |
1 files changed, 63 insertions, 0 deletions
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 |
