Best practice for using h2pas on a "clever" kernel interface binding lazarus / 21-General ---------------------------------------------------------------------- MarkMLl · 2021-02-20 11:28 With no particular urgency other than that it's been discussed in another thread, I'm taking a look at using libgpiod to interface to the "new" GPIO interface provided by Linux >=4.8. This is supposed to confer various advantages over the classic /sys/class/gpio which is now deprecated, my preference would be to invoke the kernel interface directly since that would avoid an external dependency but in the interim I'm considering a simple binding. The problem is that gpiod.h uses various "clever" things beloved by the new generation of C/C++ programmers to apply various GCC-specific attributes etc., and h2pas doesn't like what it finds. I've tried running the .h file through the C preprocessor (cpp) and h2pas likes that even less. What is best practice here: to manually substitute C macros etc. until h2pas runs without error? Platform is Linux x86_64 on a PC, FPC 3.2.0. MarkMLl ---------------------------------------------------------------------- Bi0T1N · 2021-02-20 13:23 In most cases h2pas can only convert simple header files without manual work. So it is to be expected that you need to adapt several parts of the converted unit. For your unit: I'd convert a recent version (it also has less macros): https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/ Remove GPIOD_API from the end of the functionsYou get the converted pascal unit with 17 errors and these errors need to be checked and probably adapted by hand For instance, sometimes it cannot convert things automatically and you've to convert it like Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i /** * @brief Open a gpiochip by path. * @param path Path to the gpiochip device file. * @return GPIO chip handle or NULL if an error occurred. */struct gpiod_chip *gpiod_chip_open(const char *path);just gives Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i (* error struct gpiod_chip *gpiod_chip_open(const char *path);in declaration at line 81 *)so you need to adapt it to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function gpiod_chip_open(path:Pchar): Pgpiod_chip; h2pas also seems to have issues with lines breaks/functions declarations over multiple lines (maybe it's also just because of the return type) as following functions are also not converted: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i struct gpiod_line *gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset); struct gpiod_line_bulk *gpiod_chip_get_lines(struct gpiod_chip *chip, unsigned int *offsets,                     unsigned int num_offsets);the converted unit just contains: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i (* error struct gpiod_line *in declaration at line 117 *) (* error struct gpiod_line_bulk *in declaration at line 128 *) It also fails at converting the enums Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i /** * @brief Possible direction settings. */enum {        GPIOD_LINE_DIRECTION_INPUT = 1,        /**< Direction is input - we're reading the state of a GPIO line. */        GPIOD_LINE_DIRECTION_OUTPUT,        /**< Direction is output - we're driving the GPIO line. */};which should probably be a const in Pascal (or an enumerated type depending on what you favour) and converts the macro GPIOD_BIT into a function Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     function GPIOD_BIT(nr : longint) : longint;    begin      GPIOD_BIT:=1 shl nr;    end; instead of using it to calculate the appropriate numbers used in the C enum. PS: I used h2pas -e -p -pr -s gpiod.h with the gpiod.h from the kernel git for the above examples. ---------------------------------------------------------------------- MarkMLl · 2021-02-20 13:44 Quote from: Bi0T1N on February 20, 2021, 02:23:53 pmIn most cases h2pas can only convert simple header files without manual work. So it is to be expected that you need to adapt several parts of the converted unit. For your unit: I'd convert a recent version (it also has less macros): https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/ Remove GPIOD_API from the end of the functionsYou get the converted pascal unit with 17 errors and these errors need to be checked and probably adapted by hand Thanks for those (and the rest). I'd missed that there was a "less clever" implementation and I think it's telling that it was the kernel developers who insisted on it. I was going to start off with a manual substitution of that GPIOD_API, I'll come back to it later. MarkMLl ---------------------------------------------------------------------- Bi0T1N · 2021-02-20 14:29 Quote from: MarkMLl on February 20, 2021, 02:44:11 pmQuote from: Bi0T1N on February 20, 2021, 02:23:53 pmIn most cases h2pas can only convert simple header files without manual work. So it is to be expected that you need to adapt several parts of the converted unit. For your unit: I'd convert a recent version (it also has less macros): https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/ Remove GPIOD_API from the end of the functionsYou get the converted pascal unit with 17 errors and these errors need to be checked and probably adapted by handI'd missed that there was a "less clever" implementation and I think it's telling that it was the kernel developers who insisted on it. I was going to start off with a manual substitution of that GPIOD_API, I'll come back to it later. They simply removed the deprecated API function macro - probably with all the deprecated functions before it was added to the Linux kernel. The Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i __attribute__ ((visibility ("default"))) macro seems quite useful (https://gcc.gnu.org/wiki/Visibility), so maybe h2pas should be updated so that it can detect and ignore it automatically as it'll probably be used more often in new header files. Btw the libgpiod functionality seems to be quite nice and simple - maybe worth to add the translated header to FPC as a package? ---------------------------------------------------------------------- MarkMLl · 2021-02-20 17:18 Quote from: Bi0T1N on February 20, 2021, 03:29:17 pmBtw the libgpiod functionality seems to be quite nice and simple - maybe worth to add the translated header to FPC as a package? Maybe, but my preference would be to transcribe the binding to Pascal to remove the dependency on an external library... at least for my own use. By and large I've found iopl interfaces to be fairly stable and easy to redo, with the notable exception of some (IIRC) hidraw stuff which had so many packing peculiarities that there really was no alternative to using something compiled by the same tools as the kernel. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2021-02-21 20:52 Quote from: Bi0T1N on February 20, 2021, 02:23:53 pmIn most cases h2pas can only convert simple header files without manual work. So it is to be expected that you need to adapt several parts of the converted unit. For your unit: I'd convert a recent version (it also has less macros): https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/ Remove GPIOD_API from the end of the functionsYou get the converted pascal unit with 17 errors and these errors need to be checked and probably adapted by hand The version I used initially was what's shipped with Debian, but it looks as though they're rather a long way behind the kernel. H2pas works provided that GPIOD_API is removed, various struct* results are converted to psomething (psomething should not be predefined, h2pas prefers to translate an erroneous program) and the enums are replaced in situ with #defines. I tried to take a look at the h2pas source to work out what it disliked about the enums but... well, I only hope its author can make more sense of it or we're all screwed. MarkMLl ---------------------------------------------------------------------- avra · 2021-02-22 08:15 Quote from: MarkMLl on February 21, 2021, 09:52:02 pmI tried to take a look at the h2pas source to work out what it disliked about the enums but... well, I only hope its author can make more sense of it or we're all screwed.There are alternatives: https://forum.lazarus.freepascal.org/index.php/topic,12763.msg66330.ht ml#msg66330 https://github.com/WouterVanNifterick/C-To-Delphi https://github.com/neslib/Chet EDIT: Updated https://wiki.freepascal.org/C_to_Pascal with C-To-Delphi and Chet ---------------------------------------------------------------------- MarkMLl · 2021-02-22 08:22 Quote from: avra on February 22, 2021, 09:15:10 amQuote from: MarkMLl on February 21, 2021, 09:52:02 pmI tried to take a look at the h2pas source to work out what it disliked about the enums but... well, I only hope its author can make more sense of it or we're all screwed.There are alternatives: https://forum.lazarus.freepascal.org/index.php/topic,12763.msg66330.ht ml#msg66330 https://github.com/WouterVanNifterick/C-To-Delphi Thanks very much, I've just eyeballed those and will take a more detailed look presently. I'm going to tidy yesterday's conversion up manually and then try running it through my program that will generate static- and dynamically-linked interface units. https://forum.lazarus.freepascal.o rg/index.php/topic,53348.msg394506.html#msg394506 MarkMLl ---------------------------------------------------------------------- PascalDragon · 2021-02-22 08:34 Quote from: MarkMLl on February 21, 2021, 09:52:02 pmI tried to take a look at the h2pas source to work out what it disliked about the enums but... well, I only hope its author can make more sense of it or we're all screwed. It utilizes the TPLY generator (in utils/tply) to generate the parser itself, so the main parser functionality is indeed... well... voodoo like. The "source" files for the parser are the h2pas.y and the scan.l. ---------------------------------------------------------------------- MarkMLl · 2021-02-22 08:46 Quote from: PascalDragon on February 22, 2021, 09:34:09 amQuote from: MarkMLl on February 21, 2021, 09:52:02 pmI tried to take a look at the h2pas source to work out what it disliked about the enums but... well, I only hope its author can make more sense of it or we're all screwed. It utilizes the TPLY generator (in utils/tply) to generate the parser itself, so the main parser functionality is indeed... well... voodoo like. The "source" files for the parser are the h2pas.y and the scan.l. Thanks for that. I saw it but couldn't work out how things hung together... my major focus at the time was trying to find the hidden option that caused h2pas to generate more useful error messages. MarkMLl ---------------------------------------------------------------------- avra · 2021-02-22 08:52 Quote from: MarkMLl on February 22, 2021, 09:22:46 amThanks very much, I've just eyeballed those and will take a more detailed look presently.You're welcome. I've just updated the list with Chet which is worth looking at. It is libclang based so should be able to convert more then others. There's no perfect converter so I often combine results of 2-3 converters with some manual work. ---------------------------------------------------------------------- ccrause · 2021-02-22 09:53 Subscribe. I also do header conversions from time to time and used h2pas in the past (because of the Lazarus integration).  H2pas does suffer for example when macro templates are used in e.g. struct definitions, and other annoyances that I've forgotten now.  Forced me to learn more about C and its macros than I thought I needed to know to work around the problems. Would love to know which of the alternatives are worth pursuing. ---------------------------------------------------------------------- avra · 2021-02-22 11:33 Quote from: ccrause on February 22, 2021, 10:53:11 amWould love to know which of the alternatives are worth pursuing.Besides h2pas I use Chet, C To Pascal Converter and C-To-Delphi. I usually let them all do the work, and then compare source code side by side, and based on my personal preference I use bits of code from one or the other tool and mix them. Some tool makes nice structures, some other does a better job with defines, macros and enumerators, some produce more natural pascal like code and some produce c-like pascal code but can handle more code then others. That way up to 80-90% of C header conversion work can be automated. ---------------------------------------------------------------------- MarkMLl · 2021-02-22 13:52 Quote from: Bi0T1N on February 20, 2021, 03:29:17 pmBtw the libgpiod functionality seems to be quite nice and simple - maybe worth to add the translated header to FPC as a package? Attached are static and dynamic interfaces for libgpiod in case anybody wants to play with them. They compile and the static version links, but I've not started significant testing yet and it will probably take me a bit of time to wrap my head round the API. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2021-02-22 18:34 Quote from: avra on February 22, 2021, 09:15:10 amhttps://github.com/WouterVanNifterick/C-To-Delphi I suspect that's probably deceptively flashy, and that a full language translator is probably overkill. But the obvious question is whether anybody's succeeded in compiling it with Lazarus/FPC. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2021-02-23 08:09 Quote from: MarkMLl on February 22, 2021, 07:34:23 pmQuote from: avra on February 22, 2021, 09:15:10 amhttps://github.com/WouterVanNifterick/C-To-Delphi I suspect that's probably deceptively flashy, and that a full language translator is probably overkill. But the obvious question is whether anybody's succeeded in compiling it with Lazarus/FPC. With just a quick look I can see that it uses some functionality (both features and classes) that FPC does not yet support so that would have to be ported... ---------------------------------------------------------------------- MarkMLl · 2021-02-23 08:22 Quote from: PascalDragon on February 23, 2021, 09:09:49 amQuote from: MarkMLl on February 22, 2021, 07:34:23 pmQuote from: avra on February 22, 2021, 09:15:10 amhttps://github.com/WouterVanNifterick/C-To-Delphi I suspect that's probably deceptively flashy, and that a full language translator is probably overkill. But the obvious question is whether anybody's succeeded in compiling it with Lazarus/FPC. With just a quick look I can see that it uses some functionality (both features and classes) that FPC does not yet support so that would have to be ported... Much my suspicion. That's not something I can realistically do: I'm quite simply not sufficiently up-to-speed on what Delphi's doing these days, and I've got my hands full with- among other things- trying to sort out details of GPIO naming convention (the gpiod API fails to report the base of a group of lines). MarkMLl ---------------------------------------------------------------------- MarkMLl · 2021-02-23 10:38 I've got a substantial number of entry points named like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   function gpiod_is_gpiochip_device(path:Pchar):boolean; CDECL__ GPIODLIB__  function gpiod_chip_open(path:Pchar):pgpiod_chip; CDECL__ GPIODLIB__  procedure gpiod_chip_close(chip:Pgpiod_chip); CDECL__ GPIODLIB__  function gpiod_chip_name(chip:Pgpiod_chip):Pchar; CDECL__ GPIODLIB__  That's from an include file which is incorporated into automatically- generated units for static and dynamic linkage. The objective is that these will always be accessed using either a unit name or an object reference, i.e. like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   chipHandle := Gpiod.gpiod_chip_open(PChar('/dev/gpiochip' + guiTestForm.PortName));  gpios := Gpiod.gpiod_chip_num_lines(chipHandle);  In principle I could trim the leading gpiod_ in each function name as redundant, but in order to avoid ambiguity is there any way that I could mark the static unit as always requiring fully-qualified references, i.e. so that Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   chipHandle := chip_open(PChar('/dev/gpiochip' + guiTestForm.PortName));  gpios := chip_num_lines(chipHandle);  was not accepted by the compiler? MarkMLl ---------------------------------------------------------------------- PascalDragon · 2021-02-23 12:17 Quote from: MarkMLl on February 23, 2021, 11:38:38 amIn principle I could trim the leading gpiod_ in each function name as redundant, but in order to avoid ambiguity is there any way that I could mark the static unit as always requiring fully-qualified references, i.e. so that Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   chipHandle := chip_open(PChar('/dev/gpiochip' + guiTestForm.PortName));  gpios := chip_num_lines(chipHandle);  was not accepted by the compiler? No, you can't. ---------------------------------------------------------------------- avra · 2021-02-23 14:18 Quote from: MarkMLl on February 22, 2021, 07:34:23 pmQuote from: avra on February 22, 2021, 09:15:10 amhttps://github.com/WouterVanNifterick/C-To-Delphi I suspect that's probably deceptively flashy, and that a full language translator is probably overkill. But the obvious question is whether anybody's succeeded in compiling it with Lazarus/FPC. It is Delphi application and I did not suggest that you need to compile it under Lazarus. On the right side of the site you can see Releases link. Press it and download executable. Nothing more, nothing less. I use it mostly for quick translations because of it's live feature which translates on idle, and as an alternative translation to choose from. ---------------------------------------------------------------------- MarkMLl · 2021-02-26 14:18 I'm taking another look at the varargs aspect of this, and would appreciate a reality check. The documentation of class helpers says that a class helper can't define a (new) field or as a consequence a property referring to a field... Does this apply to fields defined in the base class, or it it only stating the obvious that since fields can't be defined in a helper a property referring to a newly-defined field can't be created? MarkMLl