diff --git a/Makefile b/Makefile index df63ae1..8b883f4 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,15 @@ package: clean-package: rm -rf *.tgz +test-java: tmp/test/libtest.o tmp/test/libtest.so tmp/test/libtest.a + mkdir -p tmp/test + cp kawa.jar tmp/test/ + cp -r foreign tmp/test/ + cp tests/*.scm tmp/test/ + cp tests/c-include/libtest.h tmp/test/ + cd tmp/test \ + && ${JAVA_HOME}/bin/java --add-exports java.base/jdk.internal.foreign.abi=ALL-UNNAMED --add-exports java.base/jdk.internal.foreign.layout=ALL-UNNAMED --add-exports java.base/jdk.internal.foreign=ALL-UNNAMED --enable-native-access=ALL-UNNAMED --enable-preview -jar kawa.jar --r7rs --full-tailcalls -Dkawa.import.path=*.sld:./snow/*.sld:./snow/retropikzel/*.sld ${TESTNAME}.scm + test-compile-r7rs: tmp/test/libtest.o tmp/test/libtest.so tmp/test/libtest.a make ${COMPILE_R7RS} cp -r foreign tmp/test/ diff --git a/README.md b/README.md index 9e072f5..44772fa 100644 --- a/README.md +++ b/README.md @@ -210,17 +210,13 @@ Options: Example: - (cond-expand - (windows (define-c-library libc-stdlib - '("stdlib.h") - "ucrtbase" - '((additional-versions ("0" "6")) - (additiona-paths ("."))))) - (else (define-c-library libc-stdlib - (list "stdlib.h") - "c" - '((additional-versions ("0" "6")) - (additiona-paths (".")))))) + (define-c-library libc + (list "stdlib.h") + "c" + '((additional-versions ("" "0" "6")) + (additional-paths (".")))) + +Note that libc is exported by this library so you might not need to load it. #### Notes @@ -245,9 +241,9 @@ Defines a new foreign function to be used from Scheme code. Example: (cond-expand - (windows (define-c-library libc-stdlib '("stdlib.h") "ucrtbase" '())) - (else (define-c-library libc-stdlib '("stdlib.h") "c" '("6")))) - (define-c-procedure c-puts libc-stdlib 'puts 'int '(pointer)) + (windows (define-c-library libc '("stdlib.h") "ucrtbase" '())) + (else (define-c-library libc '("stdlib.h") "c" '("6")))) + (define-c-procedure c-puts libc 'puts 'int '(pointer)) (c-puts "Message brought to you by foreign-c!") #### Notes @@ -591,6 +587,24 @@ UTF-8 encoding of the given string. Returns a newly allocated (unless empty) string whose character sequence is encoded by the given c-bytevector. +### Utilities + +**libc** + +Since the library uses C standard internally, and that is most likely library +to have different name on different operating systems. For example libc.so on +Linux, ucrtbase.dll on windows and libroot.so on Haiku. It makes sense to +export it, saving the users the trouble of figuring out which named shared +library they should load. + +See foreign/c/libc.scm to see which headers are included and what shared +libraries are loaded. + +Example: + + (define-c-procedure c-puts libc 'puts 'int '(pointer)) + (c-puts "Message brought to you by foreign-c!") + ### Environment variables Setting environment variables like this on Windows works for this library: diff --git a/foreign/c.sld b/foreign/c.sld index 70941aa..f70fa19 100644 --- a/foreign/c.sld +++ b/foreign/c.sld @@ -193,6 +193,9 @@ bytevector->c-bytevector c-bytevector->bytevector + ;;;; Utilities + libc + ;; TODO endianness native-endianness make-c-bytevector @@ -317,12 +320,14 @@ (include "c/primitives/ypsilon.scm"))) (cond-expand (chicken-6 (include-relative "c/main.scm") + (include-relative "c/libc.scm") (include-relative "c/c-bytevectors.scm") (include-relative "c/pointer.scm") ;(include-relative "c/array.scm") ;(include-relative "c/struct.scm") ) (else (include "c/main.scm") + (include "c/libc.scm") ;(include "c/struct.scm") (include "c/c-bytevectors.scm") (include "c/pointer.scm") diff --git a/foreign/c/libc.scm b/foreign/c/libc.scm new file mode 100644 index 0000000..8c80ac4 --- /dev/null +++ b/foreign/c/libc.scm @@ -0,0 +1,13 @@ +(cond-expand + (windows (define-c-library libc + '("stdio.h" "string.h") + "ucrtbase" + '((additional-versions ("0" "6"))))) + (else + (define c-library "c") + (when (get-environment-variable "BE_HOST_CPU") + (set! c-library "root")) + (define-c-library libc + '("stdio.h" "string.h") + c-library + '((additional-versions ("" "0" "6" "7")))))) diff --git a/foreign/c/main.scm b/foreign/c/main.scm index 30d0905..af2782c 100644 --- a/foreign/c/main.scm +++ b/foreign/c/main.scm @@ -103,7 +103,9 @@ "/usr/lib" "/usr/lib64" ; NetBSD - "/usr/pkg/lib"))))) + "/usr/pkg/lib" + ; Haiku + "/boot/system/lib"))))) (auto-load-versions (list "")) (paths (append auto-load-paths additional-paths)) (versions (append additional-versions auto-load-versions)) diff --git a/foreign/c/pointer.scm b/foreign/c/pointer.scm index 375a692..c65171a 100644 --- a/foreign/c/pointer.scm +++ b/foreign/c/pointer.scm @@ -1,13 +1,3 @@ -(cond-expand - (windows (define-c-library libc - '("stdlib.h" "string.h") - "ucrtbase" - '((additional-versions ("0" "6"))))) - (else (define-c-library libc - '("stdlib.h" "string.h") - "c" - '((additional-versions ("0" "6")))))) - (define-c-procedure c-calloc libc 'calloc 'pointer '(int int)) (define-c-procedure c-memset-address->pointer libc 'memset 'pointer '(uint64 uint8 int)) (define-c-procedure c-memset-pointer->address libc 'memset 'uint64 '(pointer uint8 int)) diff --git a/kawa.jar b/kawa.jar new file mode 100644 index 0000000..31e7afc Binary files /dev/null and b/kawa.jar differ diff --git a/tests/primitives.scm b/tests/primitives.scm index 8e250fd..ea57c5a 100644 --- a/tests/primitives.scm +++ b/tests/primitives.scm @@ -214,18 +214,6 @@ (print-header 'define-c-library) -(cond-expand - (windows (define-c-library libc - '("stdio.h" "string.h") - "ucrtbase" - '((additional-versions ("0" "6"))))) - (else (define-c-library libc - '("stdio.h" "string.h") - "c" - '((additional-versions ("0" "6")))))) - -(debug libc) - (define-c-library c-testlib '("libtest.h") "test"