aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: d9ed9ddbed40d06a9e28891d8561f91370ec64c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# CleanC 0.1
Improved C interface for Clean

Vanilla Clean requires the programmer to write a separate interface for every C function that is ever going to be called from Clean, directly using the ABC instruction `ccall`. This library removes the need for ABC instructions *entirely*, and allows the programmer to call C functions given their name as a `String`.

Copyright © 2016 Camil Staps. Licensed under MIT, see LICENSE for more details.

### Installation & Usage

To install, simply run:

    make

To use CleanC, you will need to do the following:

 - Add `import CleanC` to your Clean program. 
 - Link the C object file you want to call code from.
 - Link `CleanC.o`, `libelf` and `libffi` (may need to install the latter two).
 - Disable linker optimisation and stripping of the application.
 - Add the Generics library to the search path.

This gives a clm command line of:

    clm -I "$CLEAN_HOME/lib/Generics" -ns -no-opt-link -l CleanC.o -l myfile.o -l -libelf -l -libffi mymodule

This assumes you have set `$CLEAN_HOME` to the path to your Clean installation.

### Example

See `test.icl`.

### Warning
This is Bad Code. To be able to call a C function by its name, we need to look through `/proc/self/exe` using libelf.

Since we need `/proc/self/exe`, this is probably linux-specific. Also check the [libffi][libffi] list of supported platforms.

### Acknowledgements
Code to look up a function pointer by its name was provided by [Falaina on Stack Overflow](http://stackoverflow.com/a/1118808/1544337). Calling a function pointer with extra arguments is possible using [libffi][libffi], as suggested by [FUZxxl](http://stackoverflow.com/a/35088746/1544337).

### Todo

 - Call functions from shared libraries
 - Improved interface to call Clean from C
 - Look into Darwin support

### Changelog

 - **0.1** (2016-01-31)
    - Initial version

[libffi]: https://sourceware.org/libffi/