Creating bindings for C libraries/fr
│
English (en) │
español (es) │
français (fr) │
日本語 (ja) │
русский (ru) │
Vue générale
[NdT: Je laisse le terme de bindings, plus parlant que commissionaire ou intermédiaire] Cette page décrit comment créer des pascal bindings pour les bibliothèques C. Normalement Pascal ne peut pas utiliser directement des bibliothèques C. Vous devez créer une traduction Pascal pour chaque fonction, type et variable C. Il y a l'outil H2Pas wui peut traduire automatiquement les choses les plus courantes en C. Et il existe une GUI pour Lazarus utilisant h2pas et autres outils pour automatiserleur création. Et la GUI aide a créer un ensemble de règles qui permet de mettre à jour les bindings. Donc la version suivante de la bibliothèque C peut être convertie plus facilement. Une caractéristique sympa de l'assistant h2pas est qu'il utilise automatiquement des fichiers temporaires, donc les fichiers header C ne seront jamais changés automatiquement.
Work flow
- Trouvez les fichiers header Cque vous voulez traduire.
- Créez un dossier de travail et donnez un nom à votre bindings
- Créez uun nouveau projet h2pas à l'aide de l'assistant de h2pas.
- ajoutez les fichiers .h au projet
- configurez les options de h2pas
- lancez l'assistant (pas trop fort)
- solutionnez les erreurs en ajoutant les outils de texte et en lançant à nouveau l'assistant
- lorsque h2pas se lance sans erreurs, testez la compilation et ajoutez des outils optionnels pour enjoliver la sortie
- écrivez quelques programmes de test pour essayer votre bindings
- publiez votre bindings sur lazarus-ccr ou le site du Free Pascal
Installation des outils
L'outil h2pas est livré avec toutes les installations normales des FPC.
Installez le package h2paswizard dans l'IDE de Lazarus. Allez dans "Composants -> Configurer les paquets installés...", sélectionnez dans la liste de droitele package H2PasWizard et cliquez sur 'Installer la sélection', puisn 'Sauver et reconstruire l'IDE'. Relancez l'IDE et vous aurez une nouvelle entrée de menu: Outils -> h2pas
Trouvez les fichiers header C
Les fichiers header C .h décrivent l'interface d'une bibliothèque C. Ils ne sont normalement pas livrés avec la bibliothèque. Vous devez avoir les sources ou le package de développement de la bibliothèque. Par exemple, les fichiers header C de la bibliothèque GTK se trouve dans le paquet gtk+-devel.
Exemple: MPICH2
Téléchargez mpich2-1.0.3.tar.gz depuis http://www-unix.mcs.anl.gov/mpi/mpich2/ et décompressez le. Les fichier .h se trouvent sous mpich2-1.0.3/src/include.
Créez un dossier de travail et nommez votre bindings
Créez un dossier avec un nom pratique. Un nom ne doit pas contenir de caractères spéciaux comme des espaces, des tréma allemands, des points ou des virgules. Copiez les fichiers .h.
Exemple: MPICH2
Le dossier h2p sera utilisé par les fichiers pascal. Le dossier h2p/c_sources sera utilisé par les fichiers .h.
mkdir -p h2p/c_sources cp mpich2-1.0.3/src/include/*.h h2p/c_sources/
Créez un nouveau projet h2pas avec l'assistant de h2pas
Ouvrez l'assistant de h2pas: "Outils -> h2pas". Cela va ouvrir une fenêtre. Vous pouvez alterner entre cette fenêtre et les autres fenêtres de l'IDE. Votre dernier projet h2pas sera chargé automatiquement. Pour créer un nouveau projet, cliquez sur "Configuration -> Nouveau/Effacer Configuration". Cliquez alors sur le bouton du bas "Sauver la configuration" et choisissez un nom de fichier.
Exemple: MPICH2
Cliquez sur "Configuration -> Nouveau/Effacer Configuration". Cliquez alors sur le bouton du bas "Sauver la configuration" et sauvez la en tant que h2p/mpi2.h2p.
Add the .h files to the h2pas-project
In the "C header files" page you can add/delete the .h files. You can enable/disable .h files to convert only part of the files.
Example: MPICH2
Click on "C header files -> Add .h files ..." and select "mpi.h". It will be enabled automatically.
Setup the h2pas options
Under "h2pas Options" you can set the parameters for the h2pas program.
Example: MPICH2
- Enable -e, -D, -p, -w and disable all others.
- The -l library path is "mpich".
- Output extension is ".pas"
- Output directory is h2pas/, which is the default, so leave it empty
Run the wizard
Click on the bottom button "Run h2pas". This will copy the <example>.h file to a temporary <example>.tmp.h file and run the tools listed under "Before h2pas". Then it runs h2pas to convert the <example>.tmp.h into <example>.inc or <example>.pas or whatever output extension you setup on the h2pas page. Then it runs the tools listed under "After h2pas" on the output files.
If h2pas finds a syntax error, the IDE will open the example.tmp.h file and jumps to the error line. h2pas often only reports 'syntax error', which is very unspecific. See Common problems when converting C header files.
Example: MPICH2
The h2pas wizard already contains all tools to convert all the specials of this header file, so h2pas runs without errors. But the created unit is not yet ready. Read further.
Add the following to the Undefines property:
MPI_INCLUDED MPIO_INCLUDE NEEDS_MPI_FINT MPI_BUILD_PROFILING MPICH_SUPPRESS_PROTOTYPES MPI_Wtime HAVE_MPICH2G2 FOO HAVE_PRAGMA_HP_SEC_DEF
Add the following to the Defines property:
MPICH2 MPICH_NEW_MPIIO
Publish your bindings on lazarus-ccr or Free Pascal
ToDo
Writing your own converter tools
Using the tool "Search and replace"
Many things like renaming a variable can be done by the Search and replace tool. Add the tool via the Add new tool button on either the 'Before h2pas' or the 'After h2pas' page. Then set the SearchFor, ReplaceWith, Options and Caption property.
Example: rename an identifier Tguint to guint
Property | Value |
---|---|
Caption | Rename Tguint to guint |
SearchFor | Tguint |
ReplaceWith | guint |
Options | [trtMatchCase,trtWholeWord] |
Example: rename several identifiers
Rename Tguint to guint, Tgulong to gulong, Tgint to gint:
Property | Value |
---|---|
Caption | Rename Tguint to guint |
SearchFor | gint|gulong) |
ReplaceWith | $1 |
Options | [trtMatchCase,trtWholeWord,trtRegExpr] |
Improving an existing tool
You found a bug and want to fix it or you want to extend one of the above tools. Great!
Most of the above tools are defined in the h2pasconvert unit, which is part of the h2paswizard package. Basically a tool needs a classname, a description and an Execute method. To test/debug a tool outside the IDE and save a lot of compilation time, see the project components/simpleideintf/examples/testh2pastool.lpi. Compile it and start it on a console/terminal with the filename of a .h file as first command parameter. For example:
./testh2pastool files/h2pastest.pas
Writing a custom tool
You can write your own conversion tools and register them in the IDE easily. Start a package and a add a new unit (say unit1) for your new class. See the existing tools as example and read the prior section. When you wrote a new tool and tested it with the above simpleideintf project, then register it in the IDE: Add a register procedure to your unit (unit1), like the following (pseudo code):
uses Classes, ..., IDETextConverter; type TYourToolClass = class(TCustomTextConverterTool) public class function ClassDescription: string; override; function Execute(aText: TIDETextConverter): TModalResult; override; end; procedure Register; implementation procedure Register; begin TextConverterToolClasses.RegisterClass(TYourToolClass); end;
Do not forget to enable the register checkbox of the unit in the package editor, otherwise the Register procedure will not be called by the IDE. Then install your package in the IDE.
Future work / Missing stuff
- A tool to reorder a unit to fix forward definitions. This requires a tool to remove unneeded IFDEFs.
- A tool to fix functions without parameter names by searching the .c files. This could also add a comment, in which c file it found the function.
- Find missing identifiers and let the user check which one to comment or replace with base types.
- Create a list of macro functions, which were half translated.