Author Topic: DLLC - C Includes  (Read 4705 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
DLLC - C Includes
« on: April 16, 2018, 03:59:00 AM »
Can I use C header files with DLLC and not define the API functions?

Unfortunately, ScriptBasic's typeless parameters make this problematic. DLLC needs to infer what params to use, one way or another.

JRS

  • Guest
Re: DLLC - C Includes
« Reply #1 on: April 16, 2018, 11:19:03 AM »
I wonder if I could parse the C headers in Script BASIC and generate the DLLC API definitions that way?

Where can I find the O2 C header parser routines?

Doesn't FreeBASIC use a standard tool to convert C header files to .bi includes? Do you think it might work for SB / DLLC?

Converting C Headers with swig

SWIG Documentation

It would seem parsing the output of SWIG to build DLLC API definition would be much easier than trying to parse the C header files directly.

« Last Edit: April 16, 2018, 02:15:26 PM by John »

JRS

  • Guest
Re: DLLC - C Includes
« Reply #2 on: April 16, 2018, 09:25:46 PM »
This is the SWIG output for the sqlite3_db_filename SQLite3 library function.

So far XML output seems the best way for Script BASIC to generate DLLC API definitions unless Charles can generate them with his existing C header file parser in O2.

sqlite3.h
Code: C
  1. /*
  2. ** CAPI3REF: Return The Filename For A Database Connection
  3. **
  4. ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  5. ** associated with database N of connection D.  ^The main database file
  6. ** has the name "main".  If there is no attached database N on the database
  7. ** connection D, or if database N is a temporary or in-memory database, then
  8. ** a NULL pointer is returned.
  9. **
  10. ** ^The filename returned by this function is the output of the
  11. ** xFullPathname method of the [VFS].  ^In other words, the filename
  12. ** will be an absolute pathname, even if the filename used
  13. ** to open the database originally was a URI or relative pathname.
  14. */
  15. SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
  16.  


Code: XML
  1.         <cdecl id="6706" addr="0x7fbbe2f6fcf0" >
  2.             <attributelist id="6707" addr="0x7fbbe2f6fcf0" >
  3.                 <attribute name="sym_name" value="sqlite3_db_filename" id="6708" addr="0x7fbbe2ea79d0" />
  4.                 <attribute name="name" value="sqlite3_db_filename" id="6709" addr="0x7fbbe2ea79d0" />
  5.                 <attribute name="decl" value="f(p.sqlite3,p.q(const).char).p." id="6710" addr="0x7fbbe2ea79d0" />
  6.                 <parmlist id="6711" addr="0x7fbbe2f6fb30" >
  7.                     <parm id="6712">
  8.                         <attributelist id="6713" addr="0x7fbbe2f6fb30" >
  9.                             <attribute name="name" value="db" id="6714" addr="0x7fbbe2ea79d0" />
  10.                             <attribute name="type" value="p.sqlite3" id="6715" addr="0x7fbbe2ea79d0" />
  11.                             <attribute name="compactdefargs" value="1" id="6716" addr="0x7fbbe2ea79d0" />
  12.                         </attributelist >
  13.                     </parm >
  14.                     <parm id="6717">
  15.                         <attributelist id="6718" addr="0x7fbbe2f6fc70" >
  16.                             <attribute name="name" value="zDbName" id="6719" addr="0x7fbbe2ea79d0" />
  17.                             <attribute name="type" value="p.q(const).char" id="6720" addr="0x7fbbe2ea79d0" />
  18.                         </attributelist >
  19.                     </parm >
  20.                 </parmlist >
  21.                 <attribute name="kind" value="function" id="6721" addr="0x7fbbe2ea79d0" />
  22.                 <attribute name="type" value="q(const).char" id="6722" addr="0x7fbbe2ea79d0" />
  23.                 <attribute name="sym_symtab" value="0x7fbbe2f0b8b0" id="6723" addr="0x7fbbe2f0b8b0" />
  24.                 <attribute name="sym_overname" value="__SWIG_0" id="6724" addr="0x7fbbe2ea79d0" />
  25.             </attributelist >
  26.          
  27.         </cdecl >
  28.  
« Last Edit: April 17, 2018, 04:24:10 PM by John »

JRS

  • Guest
Re: DLLC - C Includes
« Reply #3 on: April 17, 2018, 08:05:25 PM »
Here is my first pass at parsing the SWIG XML output from the sqlite3.h header file.

It seems that SWIG doesn't wrap static functions by default.

Code: Script BASIC
  1. OPEN "sqlite3_wrap.xml" FOR INPUT AS #1
  2. flen = FILELEN("sqlite3_wrap.xml")
  3. fdata = INPUT(flen,1)
  4. SPLITA fdata BY "</cdecl >\n" TO farr
  5. FOR idx = 0 TO UBOUND(farr)
  6.   IF farr[idx] LIKE "*<attribute name=\"kind\" value=\"function\"*" AND farr[idx] LIKE "*<attribute name=\"sym_name\" value=\"*\"*" THEN
  7.     PRINT JOKER(2),"\n"
  8.   END IF
  9. NEXT
  10.  

Code: [Select]
jrs@jrs-laptop:~/sb/source/extensions/sqlite$ ls -l sqlite3_wrap.xml
-rw-rw-r-- 1 jrs jrs 1268084 Apr 16 22:12 sqlite3_wrap.xml
jrs@jrs-laptop:~/sb/source/extensions/sqlite$ time scriba dllcdef.sb
sqlite3_libversion
sqlite3_sourceid
sqlite3_libversion_number
sqlite3_compileoption_used
sqlite3_compileoption_get
sqlite3_threadsafe
sqlite3_close
sqlite3_exec
SQLITE_ACCESS_EXISTS
sqlite3_shutdown
sqlite3_os_init
sqlite3_os_end
sqlite3_config
sqlite3_db_config
SQLITE_CONFIG_SINGLETHREAD
sqlite3_last_insert_rowid
sqlite3_changes
sqlite3_total_changes
sqlite3_interrupt
sqlite3_complete
sqlite3_complete16
sqlite3_busy_handler
sqlite3_busy_timeout
sqlite3_get_table
sqlite3_free_table
sqlite3_mprintf
sqlite3_vmprintf
sqlite3_snprintf
sqlite3_vsnprintf
sqlite3_malloc
sqlite3_realloc
sqlite3_free
sqlite3_memory_used
sqlite3_memory_highwater
sqlite3_randomness
sqlite3_set_authorizer
SQLITE_DENY
sqlite3_profile
sqlite3_progress_handler
sqlite3_open
sqlite3_open16
sqlite3_open_v2
sqlite3_uri_parameter
sqlite3_uri_boolean
sqlite3_uri_int64
sqlite3_errcode
sqlite3_extended_errcode
sqlite3_errmsg
sqlite3_errmsg16
sqlite3_limit
SQLITE_LIMIT_LENGTH
sqlite3_prepare_v2
sqlite3_prepare16
sqlite3_prepare16_v2
sqlite3_sql
sqlite3_stmt_readonly
sqlite3_stmt_busy
sqlite3_bind_blob
sqlite3_bind_double
sqlite3_bind_int
sqlite3_bind_int64
sqlite3_bind_null
sqlite3_bind_text
sqlite3_bind_text16
sqlite3_bind_value
sqlite3_bind_zeroblob
sqlite3_bind_parameter_count
sqlite3_bind_parameter_name
sqlite3_bind_parameter_index
sqlite3_clear_bindings
sqlite3_column_count
sqlite3_column_name
sqlite3_column_name16
sqlite3_column_database_name
sqlite3_column_database_name16
sqlite3_column_table_name
sqlite3_column_table_name16
sqlite3_column_origin_name
sqlite3_column_origin_name16
sqlite3_column_decltype
sqlite3_column_decltype16
sqlite3_step
sqlite3_data_count
SQLITE_INTEGER
sqlite3_column_bytes
sqlite3_column_bytes16
sqlite3_column_double
sqlite3_column_int
sqlite3_column_int64
sqlite3_column_text
sqlite3_column_text16
sqlite3_column_type
sqlite3_column_value
sqlite3_finalize
sqlite3_reset
sqlite3_create_function
sqlite3_create_function16
sqlite3_create_function_v2
SQLITE_UTF8
sqlite3_expired
sqlite3_transfer_bindings
sqlite3_global_recover
sqlite3_thread_cleanup
sqlite3_memory_alarm
sqlite3_value_blob
sqlite3_value_bytes
sqlite3_value_bytes16
sqlite3_value_double
sqlite3_value_int
sqlite3_value_int64
sqlite3_value_text
sqlite3_value_text16
sqlite3_value_text16le
sqlite3_value_text16be
sqlite3_value_type
sqlite3_value_numeric_type
sqlite3_aggregate_context
sqlite3_user_data
sqlite3_context_db_handle
sqlite3_get_auxdata
sqlite3_set_auxdata
sqlite3_result_blob
sqlite3_result_double
sqlite3_result_error
sqlite3_result_error16
sqlite3_result_error_toobig
sqlite3_result_error_nomem
sqlite3_result_error_code
sqlite3_result_int
sqlite3_result_int64
sqlite3_result_null
sqlite3_result_text
sqlite3_result_text16
sqlite3_result_text16le
sqlite3_result_text16be
sqlite3_result_value
sqlite3_result_zeroblob
sqlite3_create_collation
sqlite3_create_collation_v2
sqlite3_create_collation16
sqlite3_collation_needed
sqlite3_collation_needed16
sqlite3_sleep
sqlite3_get_autocommit
sqlite3_db_handle
sqlite3_db_filename
sqlite3_db_readonly
sqlite3_next_stmt
sqlite3_commit_hook
sqlite3_rollback_hook
sqlite3_update_hook
sqlite3_enable_shared_cache
sqlite3_release_memory
sqlite3_db_release_memory
sqlite3_soft_heap_limit64
sqlite3_soft_heap_limit
sqlite3_table_column_metadata
sqlite3_load_extension
sqlite3_enable_load_extension
sqlite3_auto_extension
sqlite3_reset_auto_extension
SQLITE_INDEX_CONSTRAINT_EQ
sqlite3_create_module_v2
sqlite3_declare_vtab
sqlite3_overload_function
sqlite3_blob_open
sqlite3_blob_reopen
sqlite3_blob_close
sqlite3_blob_bytes
sqlite3_blob_read
sqlite3_blob_write
sqlite3_vfs_find
sqlite3_vfs_register
sqlite3_vfs_unregister
sqlite3_mutex_alloc
sqlite3_mutex_free
sqlite3_mutex_enter
sqlite3_mutex_try
sqlite3_mutex_leave
sqlite3_mutex_held
sqlite3_mutex_notheld
SQLITE_MUTEX_FAST
sqlite3_file_control
sqlite3_test_control
SQLITE_TESTCTRL_FIRST
SQLITE_STATUS_MEMORY_USED
SQLITE_DBSTATUS_LOOKASIDE_USED
sqlite3_backup_init
sqlite3_backup_step
sqlite3_backup_finish
sqlite3_backup_remaining
sqlite3_backup_pagecount
sqlite3_unlock_notify
sqlite3_stricmp
sqlite3_strnicmp
sqlite3_log
sqlite3_wal_hook
sqlite3_wal_autocheckpoint
sqlite3_wal_checkpoint
sqlite3_wal_checkpoint_v2
SQLITE_CHECKPOINT_PASSIVE
SQLITE_VTAB_CONSTRAINT_SUPPORT
sqlite3_rtree_geometry_callback

real 0m0.389s
user 0m0.380s
sys 0m0.008s
jrs@jrs-laptop:~/sb/source/extensions/sqlite$ swig --help
Target Language Options
     -allegrocl      - Generate ALLEGROCL wrappers
     -chicken        - Generate CHICKEN wrappers
     -clisp          - Generate CLISP wrappers
     -cffi           - Generate CFFI wrappers
     -csharp         - Generate C# wrappers
     -d              - Generate D wrappers
     -go             - Generate Go wrappers
     -guile          - Generate Guile wrappers
     -java           - Generate Java wrappers
     -javascript     - Generate Javascript wrappers
     -lua            - Generate Lua wrappers
     -modula3        - Generate Modula 3 wrappers
     -mzscheme       - Generate Mzscheme wrappers
     -ocaml          - Generate Ocaml wrappers
     -octave         - Generate Octave wrappers
     -perl           - Generate Perl wrappers
     -php            - Generate PHP wrappers
     -pike           - Generate Pike wrappers
     -python         - Generate Python wrappers
     -r              - Generate R (aka GNU S) wrappers
     -ruby           - Generate Ruby wrappers
     -scilab         - Generate Scilab wrappers
     -sexp           - Generate Lisp S-Expressions wrappers
     -tcl            - Generate Tcl wrappers
     -uffi           - Generate Common Lisp / UFFI wrappers
     -xml            - Generate XML wrappers

General Options
     -addextern      - Add extra extern declarations
     -c++            - Enable C++ processing
     -co <file>      - Check <file> out of the SWIG library
     -copyctor       - Automatically generate copy constructors wherever possible
     -cpperraswarn   - Treat the preprocessor #error statement as #warning (default)
     -cppext <ext>   - Change file extension of generated C++ files to <ext>
                       (default is cxx, except for PHP which uses cpp)
     -copyright      - Display copyright notices
     -debug-classes  - Display information about the classes found in the interface
     -debug-module <n>- Display module parse tree at stages 1-4, <n> is a csv list of stages
     -debug-symtabs  - Display symbol tables information
     -debug-symbols  - Display target language symbols in the symbol tables
     -debug-csymbols - Display C symbols in the symbol tables
     -debug-lsymbols - Display target language layer symbols
     -debug-tags     - Display information about the tags found in the interface
     -debug-template - Display information for debugging templates
     -debug-top <n>  - Display entire parse tree at stages 1-4, <n> is a csv list of stages
     -debug-typedef  - Display information about the types and typedefs in the interface
     -debug-typemap  - Display typemap debugging information
     -debug-tmsearch - Display typemap search debugging information
     -debug-tmused   - Display typemaps used debugging information
     -directors      - Turn on director mode for all the classes, mainly for testing
     -dirprot        - Turn on wrapping of protected members for director classes (default)
     -D<symbol>      - Define a symbol <symbol> (for conditional compilation)
     -E              - Preprocess only, does not generate wrapper code
     -external-runtime [file] - Export the SWIG runtime stack
     -fakeversion <v>- Make SWIG fake the program version number to <v>
     -fcompact       - Compile in compact mode
     -features <list>- Set global features, where <list> is a comma separated list of
                       features, eg -features directors,autodoc=1
                       If no explicit value is given to the feature, a default of 1 is used
     -fastdispatch   - Enable fast dispatch mode to produce faster overload dispatcher code
     -Fmicrosoft     - Display error/warning messages in Microsoft format
     -Fstandard      - Display error/warning messages in commonly used format
     -fvirtual       - Compile in virtual elimination mode
     -help           - This output
     -I-             - Don't search the current directory
     -I<dir>         - Look for SWIG files in directory <dir>
     -ignoremissing  - Ignore missing include files
     -importall      - Follow all #include statements as imports
     -includeall     - Follow all #include statements
     -l<ifile>       - Include SWIG library file <ifile>
     -macroerrors    - Report errors inside macros
     -makedefault    - Create default constructors/destructors (the default)
     -M              - List all dependencies
     -MD             - Is equivalent to `-M -MF <file>', except `-E' is not implied
     -MF <file>      - Generate dependencies into <file> and continue generating wrappers
     -MM             - List dependencies, but omit files in SWIG library
     -MMD            - Like `-MD', but omit files in SWIG library
     -module <name>  - Set module name to <name>
     -MP             - Generate phony targets for all dependencies
     -MT <target>    - Set the target of the rule emitted by dependency generation
     -nocontract     - Turn off contract checking
     -nocpperraswarn - Do not treat the preprocessor #error statement as #warning
     -nodefault      - Do not generate default constructors nor default destructors
     -nodefaultctor  - Do not generate implicit default constructors
     -nodefaultdtor  - Do not generate implicit default destructors
     -nodirprot      - Do not wrap director protected members
     -noexcept       - Do not wrap exception specifiers
     -nofastdispatch - Disable fast dispatch mode (default)
     -nopreprocess   - Skip the preprocessor step
     -notemplatereduce - Disable reduction of the typedefs in templates
     -O              - Enable the optimization options:
                        -fastdispatch -fvirtual
     -o <outfile>    - Set name of C/C++ output file to <outfile>
     -oh <headfile>  - Set name of C++ output header file for directors to <headfile>
     -outcurrentdir  - Set default output dir to current dir instead of input file's path
     -outdir <dir>   - Set language specific files output directory to <dir>
     -pcreversion    - Display PCRE version information
     -small          - Compile in virtual elimination & compact mode
     -swiglib        - Report location of SWIG library and exit
     -templatereduce - Reduce all the typedefs in templates
     -v              - Run in verbose mode
     -version        - Display SWIG version number
     -Wall           - Remove all warning suppression, also implies -Wextra
     -Wallkw         - Enable keyword warnings for all the supported languages
     -Werror         - Treat warnings as errors
     -Wextra         - Adds the following additional warnings: 202,309,403,512,321,322
     -w<list>        - Suppress/add warning messages, eg -w401,+321 - see Warnings.html
     -xmlout <file>  - Write XML version of the parse tree to <file> after normal processing

Options can also be defined using the SWIG_FEATURES environment variable, for example:

  $ SWIG_FEATURES="-Wall"
  $ export SWIG_FEATURES
  $ swig -python interface.i

is equivalent to:

  $ swig -Wall -python interface.i


Tcl 8 Options (available with -tcl)
     -itcl           - Enable ITcl support
     -nosafe         - Leave out SafeInit module function.
     -prefix <name>  - Set a prefix <name> to be prepended to all names
     -namespace      - Build module into a Tcl 8 namespace
     -pkgversion     - Set package version


Note: 'swig -<lang> -help' displays options for a specific target language.

jrs@jrs-laptop:~/sb/source/extensions/sqlite$


« Last Edit: April 17, 2018, 09:41:40 PM by John »

JRS

  • Guest
Re: DLLC - C Includes
« Reply #4 on: April 18, 2018, 09:58:01 AM »
Charles,

I'm assuming at this point that DLLC will support creating and accessing C structures?

I was thinking of mapping C structures to SB associative arrays. (matrices)


« Last Edit: April 18, 2018, 10:14:04 AM by John »

Charles Pegge

  • Guest
Re: DLLC - C Includes
« Reply #5 on: April 19, 2018, 08:57:39 PM »
Hi John,

Yes, There's a rectangle demo in ProjectsC\ScriptBasic\DLLCtests\DllcSpec2.sb

JRS

  • Guest
Re: DLLC - C Includes
« Reply #6 on: April 19, 2018, 09:56:56 PM »
Thanks Charles!

I was pretty sure DLLC supported C structures. It also supports wide and bstr if I remember correctly.

I hope this C2SB include file builder makes DLLC easier to use.

Kirk Friend

  • Guest
Re: DLLC - C Includes
« Reply #7 on: April 04, 2019, 11:55:41 PM »
Hello,

Excuse my ignorance and resurrection of this old post as it is somewhat related to a topic I am interested in. Languages that interface with C or can use their header files is an interesting topic but one I am failing to understand. If header files are just declarations but not implementations what is the purpose of parsing them?

Let me create a simple scenario of a problem, if you have a very basic language that is self-contained or able to compile itself. How would one go about adding additional library and features such as ODBC, OpenGL, OpenCV ect?

JRS

  • Guest
Re: DLLC - C Includes
« Reply #8 on: April 05, 2019, 08:29:45 AM »
Charles wrote DLLC as a ScriptBasic extension module. It only works with SB. (custom DLLL format) It's a FFI dynamic API definition interface amoung other features.

Charles Pegge

  • Guest
Re: DLLC - C Includes
« Reply #9 on: April 06, 2019, 04:47:29 AM »
Hi Kirk,

Internally speaking, the key functions for interfacing any DLL are LoadLibrary, FreeLibrary, and GetProcAddress. The headers provide the names from which call locations are obtained at run-time.