C Header Translator for Delphi
Chet is a .h-to-.pas
translator powered by libclang for Delphi.
Unlike some other header translators, Chet uses the Clang compiler to parse header files, resulting in more accurate translations that require fewer manual adjustments.
Some notable features are:
#define
declarations to constants where possible..pas
file for an entire directory of .h
files. This reduces issues due to dependencies between header files..chet
project file for reuse.#ifdef
directives). This is both good and bad. It is good because it improves conversion accuracy. But it can be bad because it uses the system that Chet runs on to determine some conditional paths. For example, because Chet runs on Windows, it will parse code in #ifdef _WIN32
sections but skip any code in sections for other platforms.Since Chet uses an actual compiler, you will need to have a (minimal) C develop environment installed, as well as LLVM with Clang. Clang needs to be able to find the system headers for the development environment. These will usually be available if you have some version of Visual Studio with Visual C++ installed. The free (community) edition of Visual Studio suffices.
You can run Chet first to check for any errors related to missing dependencies. If you get any dependency errors when running the translator, then you can download the dependencies here:
You can use the pre-compiled 64-bit Windows Chet application in the Bin
directory.
If you want to compile Chet yourself, then you also need libclang for Delphi and make sure the Delphi IDE can find it (the Chet project will find it automatically if the Neslib.Clang
directory is at the same level as the Chet
directory).
Thank you for these contributions:
Chet is pretty straightforward. In many cases, you only need to provide a directory with header files, the name of the output .pas
file and select “Run Header Translator (F9)”.
For more control over the conversion process, you can specify various options, described below.
Any configuration options you set can be saved to a .chet
configuration file (which is a simple ini-file). This allows you to load the settings later to rerun the conversion (for example, when new versions of the header files have been released). You can load and save these configuration options using the File
menu.
To help pre-configure some settings for a new session, select File | New Project... (Ctrl+N)
. You enter the name of the project and Chet will pre-configure some settings based on the name you enter (although you can always modify those settings later).
The Run
menu just as the single option Run Header Translator
, which you can also activate with F9
.
The project page contains the most important configuration options:
.h
files here. The directory may be relative to the directory containing the .chet
project file. Click the ...
button to browse for a directory. It is recommended that you don’t use the directory with the original C source code. Instead, copy the header files to a separate directory just for conversion purposes. This makes it easier to delete header files you don’t want to convert, or to make edits to header files for conversion purposes..pas
file that will be generated. I single combined Pascal file will be generated for all parsed header files. The name may be relative to the directory containing the .chet
project file. Click the ...
button to open a save dialog.Windows.Winapi
), then you can list those units here. The will be added to the uses clause of the generated Pascal file.On this page you specify what platforms you want to target, and how you want to configure them.
LIB_MYLIB
, so the following declaration will be generated: const LIB_MYLIB = 'mylib.dll'
.Next are checkboxes for all platforms you want to target (32-bit Windows, 64-bit Windows, 32-bit macOS, 64-bit Linux, iOS and Android). For each platform that you check, you must enter the following options:
_
character).Here you can customize the Clang parsing process.
-D<define>
) and include search paths (-I<path>
). There are separate buttons to make it easier to add these. Refer to the Clang documentation for information about available command line arguments.This is where you customize the generated output.
cdecl
and stdcall
. In almost all cases, you should use the default cdecl
calling convention. Use stdcall
only for 32-bit Windows DLL’s which you know are compiled with the stdcall calling convention. Those are usually only Windows system DLL’s. Most 3rd party DLL’s use cdecl.char
type is ambiguous, it can be used as an 8-bit integer or a character in a text string. The typed versions signed char
and unsigned char
are always converted to Shortint
and Byte
respectively. But when no signed-ness is specified, you have the following options:char
to a cross-platform UTF8Char
.char
to an 8-bit signed Shortint
.char
to an 8-bit unsigned Byte
.begin
and procedure
). Here you specify how you want to convert these identifiers:public
) as identifiers in Delphi, but it looks strange and Delphi’s syntax highlighter treats them differently. So you usually want to treat these as reserved words as well.type MyEnum = Integer;
) and create constants for each option in the enumeration. This may be more applicable for some libraries.#define
‘s, see remarks below). Here you specify how to handle these:TODO
comment to the Delphi source code, as well as a commented-out version of the original declaration. Remember that you can view a list of all TODO’s in the Delphi IDE by selecting View | Tool Windows | To-Do List
.Note that only Doxygen style documentation comments are parsed by Clang. These are comments that follow any of these format conventions:
/// Comment (with 3 slashes)
/** Comment (with two stars) */
/*! Comment (with exclamation point) */
///< Comment (applies to preceding declaration)
/**< Comment (applies to preceding declaration) */
/*!< Comment (applies to preceding declaration) */
Chet tries to convert #define
declarations to constants if possible. This only works if:
#define ABS(x) (x < 0) ? -x : x
cannot be translated.#define FOO 3<<BAR
will be converted to const FOO = 3 shl BAR
.Here you can specify a list of symbols to ignore. These symbols will not be translated.
The most common use is to ignore #define
‘s that generate conversion errors, or functions you don’t need. You can also choose to ignore some types, but that may result in compilation errors later because expected types are missing.
Note that symbols are case-sensitive.
The final page just has a single button “Run Header Translator” (which you can also activate with F9
). It shows the progress of the translation process, as well as any errors that occurred while parsing the header files.
You can use these errors to fix the header files, add missing header files, or configure that parsing process by adding command line arguments (for example, by adding include search paths).
Chet is licensed under the Simplified BSD License. See License.txt for details.