michael-hardeman / gdnative_ada

godot nativescript bindings in ada
MIT License
13 stars 1 forks source link

Re: Help on generating Ada bindings for C/C++ #9

Closed sanyaade-teachings closed 3 years ago

sanyaade-teachings commented 3 years ago

Good day to you all.

I am interested in generate Ada binding for C/C++ so I will be happy if you can provide short steps/description how on this done from your expertise and your lead. Many thanks!

I have seen the use of e.g ==> gcc -fdump-ada-spec ./test.h which out create bindings for .ads but want I want to know is if I have both test.h and test.c how can I auto generate both test.ads (spec) and test.adb (package body).

Many thanks!

God blesses!!!

Best regards, Sanyaade

michael-hardeman commented 3 years ago

Hello sanyaade,

You seem to already have the command you need. GCC is able to dump Ada specification files when given C header files. You typically don't need the C implementation files. For example here I am running this on the gdnative folder to dump the Ada specs. These files generally aren't perfect, and require a bit of massaging by hand to work well, but it's a great way to get started as it does most of the dull grunt work of porting the types and functions.

Here is an example of me using gcc to dump the specs for the gdnative c headers I used in this project. I had to manually clean them up quite a bit to get the final version I have in gdnative-thin.ads. I also showed how to resolve common include issues (using the -I flag).

This should generate a .ads file for each .h file. Simply take the .ads files generated and include them in your Ada project, then link the C dynamic link library. You should be able to start calling things from the C library.

C:\Users\user\Desktop\godot_headers\gdnative                         
λ dir                                                                          
 Volume in drive C has no label.                                               
 Volume Serial Number is 767E-DEA2                                             

 Directory of C:\Users\user\Desktop\godot_headers\gdnative           

01/12/2021  08:11 PM    <DIR>          .                                       
01/12/2021  08:11 PM    <DIR>          ..                                      
01/12/2021  08:11 PM             5,084 aabb.h                                  
01/12/2021  08:11 PM             6,648 array.h                                 
01/12/2021  08:11 PM             6,237 basis.h                                 
01/12/2021  08:11 PM             5,198 color.h                                 
01/12/2021  08:11 PM             4,904 dictionary.h                            
01/12/2021  08:11 PM             9,573 gdnative.h                              
01/12/2021  08:11 PM             3,913 node_path.h                             
01/12/2021  08:11 PM             4,780 plane.h                                 
01/12/2021  08:11 PM            25,379 pool_arrays.h                           
01/12/2021  08:11 PM             5,314 quat.h                                  
01/12/2021  08:11 PM             4,608 rect2.h                                 
01/12/2021  08:11 PM             3,086 rid.h                                   
01/12/2021  08:11 PM            17,105 string.h                                
01/12/2021  08:11 PM             3,524 string_name.h                           
01/12/2021  08:11 PM             5,326 transform.h                             
01/12/2021  08:11 PM             5,194 transform2d.h                           
01/12/2021  08:11 PM            11,332 variant.h                               
01/12/2021  08:11 PM             6,446 vector2.h                               
01/12/2021  08:11 PM             6,699 vector3.h                               
              19 File(s)        140,350 bytes                                  
               2 Dir(s)  873,865,965,568 bytes free                            

C:\Users\user\Desktop\godot_headers\gdnative                         
λ gcc -fdump-ada-spec gdnative.h                                               
gdnative.h:141:10: fatal error: gdnative/string.h: No such file or directory   
  141 | #include <gdnative/string.h>                                           
      |          ^~~~~~~~~~~~~~~~~~~                                           
compilation terminated.                                                        

C:\Users\user\Desktop\godot_headers\gdnative                         
λ gcc -fdump-ada-spec gdnative.h -I..                                          
sanyaade-teachings commented 3 years ago

@MichaelAllenHardeman :

Many thanks for your reply. Much appreciated!

I have been looking at other means of translating C source files to Ada. Surprisingly the -fdump-ada-spec work but I have to manually add keyword => "body" to those translated files so I need to get that file save it as -fdump-ada-spec-body, modify that version to work the way I want. This is a must for those 100s of source files.

Other options: merge into single file before -fdump-ada-spec. I need to tryout this as well plus all possible tricks!

Once again many thanks for your reply.

God blesses!!!

Best regards, Sanyaade

michael-hardeman commented 3 years ago

@sanyaade-teachings

-fdump-ada-spec is a tool for translating C header files so you can use built C libraries.

-fdump-ada-spec is not for translating C source code. While I'm sure there are tools that might attempt that I've never used them before. The translated code would probably not be desirable anyway as Ada gives you so much more power to express the meaning of your code to the compiler and to the reader.