forked from idris-lang/Idris2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request idris-lang#3162 from AdamHarries/compilerEnvs
[fix] correctly pass environment vars to the RefC compiler
- Loading branch information
Showing
11 changed files
with
114 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import System.FFI | ||
|
||
libexternal : String -> String | ||
libexternal fn = "C:" ++ fn ++ ",libexternalc,externalc.h" | ||
|
||
%foreign (libexternal "add") | ||
add : Int -> Int -> Int | ||
|
||
%foreign (libexternal "fastfibsum") | ||
fastfibsum : Int -> Int | ||
|
||
main : IO () | ||
main = do | ||
printLn $ show (add 50 23) | ||
printLn $ show ([fastfibsum x | x <- [0..10]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"73" | ||
"[0, 1, 2, 4, 7, 12, 20, 33, 54, 88, 143]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
all: libexternalc.so | ||
|
||
externalc.o: externalc.c externalc.h | ||
$(CC) -c -fPIC $< -o $@ | ||
|
||
libexternalc.so: externalc.o | ||
$(CC) $< -shared -o $@ | ||
|
||
clean: | ||
rm -f externalc.o externalc.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "externalc.h" | ||
|
||
int add(int x, int y) { return x + y; } | ||
|
||
int fastfibsum(int x) { | ||
int acc = 0; | ||
int p = 0; | ||
int c = 1; | ||
int tmp; | ||
for (; 0 <= --x;) { | ||
acc += c; | ||
tmp = c; | ||
c = c + p; | ||
p = tmp; | ||
} | ||
return acc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
int add(int x, int y); | ||
|
||
int fastfibsum(int x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
. ../../testutils.sh | ||
|
||
# This test checks to make sure that Idris2's command line handling for the RefC backend is correct. | ||
# It checks that: | ||
# 1) Idris2 correctly finds `CFLAGS` and `LDFLAGS` in the environment, | ||
# 2) The values in `CFLAGS` and `LDFLAGS` are separated correctly to be passed to the compiler | ||
# | ||
# (1) is achieved by compiling a c library (`externalc`) in a separate folder, and then explicitly | ||
# pointing the compiler to the header files (with `-I./library/`) and shared library (with `-L./library`) | ||
# and requesting that libexternalc.{so,dylib,dll} is linked (with `-lexternalc`). We additionally point the | ||
# dynamic library loader to the correct location with `DYLD_LIBRARY_PATH`. | ||
# | ||
# (2) is achieved by passing multiple options, separated by spaces, in each of `CFLAGS` and `LDFLAGS`. | ||
# These options are `-O3` for the c flags, and `-Wl,-pie` for the linker flags. They do not change the | ||
# semantics of the resulting code (`-O3` simply optimises it more, and `-Wl,-S` removes debugging information | ||
# from the final executable), but do check that we correctly split up the environment variables when we | ||
# pass them to the C compiler. | ||
|
||
cd ./library/ | ||
make > /dev/null | ||
cd .. | ||
|
||
export CFLAGS="-I./library/ -O3" | ||
export LDFLAGS="-L./library/ -Wl,-S" | ||
export LDLIBS="-lexternalc" | ||
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:./library/" | ||
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:./library/" | ||
|
||
idris2 --cg refc -o cffi Main.idr > /dev/null | ||
|
||
./build/exec/cffi |