ref: 85b81a8a5802d56f60a77e2477a019f0792fe1fb
dir: /porting.md/
# Porting alien software *without* APE To get started on an easy port of a POSIX library (or a program), one can use [helpmeport](https://git.sr.ht/~ft/snippets/blob/master/helpmeport) script. It tries to generate the appopriate headers and a `mkfile`. cd coollib # sometimes, the source files are located in `src` subdir # if it's in the current directory, just specify `.` instead rm -rf mkfile src/plan9 # remove old stuff helpmeport src | rc # now there is `src/plan9` directory and `mkfile` # `mkfile` needs manual adjustments: open it and search for "FIXME" # make your choices (name of the library/program, etc) # clean up headers and sources, some of them might be platform-specific B mkfile # now try to build. there is 99% it won't work the first time mk Depending on the result of the build, some changes of the source code might be required. The script will provide `__plan9__` and eg `__amd64__` definitions to be used throughout, in case needed. Feel free to show your script patches and ideas to Sigrid. ## Common issues Before trying to run anything, check the pragmas. g pragma `#pragma pack(push)` and `#pragma pack(pop)` have to be replaced with `#pragma pack on` and `#pragma pack off`. `#pragma once` isn't supported either and, unless the project builds fine, the header's contents with the pragma have to be enclosed in: #ifndef _header_name_h_ #define _header_name_h_ .... #endif ### function not declared: ... If the function defined in the project, and has no arguments, make sure it's declared as `(void)` instead of `()`. ### alloca `alloca` allocates memory on stack, so that freeing isn't required. Plan 9 does not have that. It has to be replaced with `malloc`. `free` call HAS to be added, before leaving the function, or else you WILL have memory leaks. ### *_MAX or *_MIN not declared Grep `/sys/include` to see if it's already defined in APE: g UCHAR_MAX /sys/include/ape In most of the cases, you can just add it to `src/plan9/plan9.h`. Be careful if the size depends on the CPU you're targetting. ### Some weird stuff with types/signatures not matching during linking This is probably caused by the compiler getting confused with forward declarations of certain types. One object file seems to have one type, the other object file has a slightly different type with the same name. Your best bet is to check the functions that error report is about. One thing that has been found to help with cases like this (in case you're *sure* the types are correct): #ifdef __plan9__ #pragma incomplete struct my_cool_struct #endif placed after the type declaration. It will tell the compiler not to bother too much.