Skip to content

Commit 1297c7b

Browse files
committed
updated for macos support and FFI library detection
1 parent 48f3bdf commit 1297c7b

13 files changed

Lines changed: 77 additions & 16 deletions

File tree

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,22 @@ $ make check # runs the standard tests
9898

9999
### On MacOS Systems
100100

101-
Due to some library incompatibilities, the surefire way to build kscript on MacOS is:
101+
The simplest way is to just build like this:
102102

103103
```shell
104-
$ ./configure --with-ffi off --with-readline off
104+
$ ./configure
105105
$ make bin/ks -j16
106106
```
107107

108-
Some builds (external shared libraries, extra modules) may not work directly (TODO/WIP), but the basic functionality and the standard library should work just fine
108+
If you install `libffi`, you can build with FFI support like this:
109+
110+
```shell
111+
$ brew install libffi
112+
$ export CFLAGS="-I/opt/homebrew/opt/libffi/include"
113+
$ export LDFLAGS="-L/opt/homebrew/opt/libffi/lib"
114+
$ ./configure --with-ffi on
115+
$ make bin/ks -j16
116+
```
109117

110118
### On Windows
111119

configure

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ WITH_pthreads="auto"
3737
WITH_readline="auto"
3838
WITH_fftw3="auto"
3939
WITH_opengl="auto"
40+
WITH_glfw="auto"
4041
WITH_x="auto"
4142

4243
#CHECKTO="/dev/null"
@@ -69,6 +70,7 @@ case $1 in
6970
echo " --with-ffi V Whether or not to use ffi from libffi (Foreign Function Interface) for C-function interop (default: auto)"
7071
echo " --with-fftw3 V Whether or not to use FFTW3 for Fast-Fourier-Transforms (default: auto)"
7172
echo " --with-opengl V Whether or not to use OpenGL for graphics acceleration (default: auto)"
73+
echo " --with-glfw V Whether or not to use GLFW for graphics acceleration (default: auto)"
7274
echo " --with-x V Whether or not to use XLib for graphics (default: auto)"
7375
echo ""
7476
echo "Any questions, comments, or concerns can be sent to:"
@@ -275,13 +277,16 @@ check_clib() {
275277
with="$2"
276278
cflags="$3"
277279
ldflags="$4"
280+
prog="$5"
278281

279282
eval "HAVE_$1"=\"off\"
280283

281284
if [ "x$2" = 'xoff' ]; then
282285
printf "Not searching for '%s'\n" "$1"
283286
else
284287
printf "Searching for '%s' ...\n" "$1"
288+
289+
echo "$prog" > $_check_c
285290

286291
# compile and run it
287292
echo $CC $CFLAGS $3 $DEFS $_check_c $LDFLAGS $4 -o $_check_o >> $CHECKTO
@@ -441,6 +446,13 @@ int main(int argc, char** argv) {
441446
}
442447
"
443448

449+
check_clib glfw "$WITH_glfw" "" "-lglfw" "
450+
#include <GLFW/glfw3.h>
451+
452+
int main(int argc, char** argv) {
453+
return 0;
454+
}
455+
"
444456

445457
echo ""
446458
echo " -- Structures -- "
@@ -579,6 +591,10 @@ check_func() {
579591
#include <glob.h>
580592
#endif
581593
594+
#ifdef KS_HAVE_DIRENT_H
595+
#include <dirent.h>
596+
#endif
597+
582598
int main(int argc, char** argv) {
583599
int i = 0;
584600
time_t t;
@@ -606,6 +622,9 @@ int main(int argc, char** argv) {
606622
607623
struct stat st;
608624
FILE* F;
625+
#ifdef KS_HAVE_DIRENT_H
626+
DIR* D;
627+
#endif
609628
610629
$assignto $1($3);
611630
return 0;
@@ -729,7 +748,7 @@ check_func "mkdir" "i" "s,i"
729748
check_func "remove" "i" "s"
730749
check_func "chdir" "i" "s"
731750

732-
check_func "opendir" "i" "s,&st"
751+
check_func "opendir" "D" "s"
733752

734753
check_func "glob" "i" "s, i, NULL, NULL"
735754

@@ -1110,11 +1129,11 @@ for dir in src/extmodules/*; do
11101129
printf "libksm_%s_C := \$(wildcard %s/*.c)\n" "$mn" "$dir" >> $T
11111130
printf "libksm_%s_O := \$(patsubst %%.c,.tmp/%%.o,\$(libksm_%s_C))" "$mn" "$mn" >> $T
11121131
echo "" >> $T
1113-
printf "ksm_%s%s: \$(libksm_%s_O) \$(all_H)" "$mn" "$EXTSHARED" "$mn" >> $T
1132+
printf "ksm_%s%s: \$(libksm_%s_O) \$(all_H) \$(libks_SHARED)" "$mn" "$EXTSHARED" "$mn" >> $T
11141133
echo "" >> $T
11151134
echo " @mkdir -p \$(dir \$@)" >> $T
11161135
echo " \$(CC) -L./lib \\" >> $T
1117-
printf " \$(libksm_%s_O) \\" "$mn" >> $T
1136+
printf " \$(libksm_%s_O) -lks \\" "$mn" >> $T
11181137
echo "" >> $T
11191138
echo " \$(LDFLAGS) -fPIC -shared -o \$@" >> $T
11201139
done

examples/ffi/basic.ks

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,38 @@
77

88
import ffi
99

10+
11+
# what paths to use
12+
cands = [
13+
'libc.so.6',
14+
'/usr/lib/libSystem.dylib',
15+
]
16+
17+
func attempt_open(cand) {
18+
try {
19+
ret ffi.open(cand)
20+
} catch {
21+
ret none
22+
}
23+
}
24+
25+
1026
# C library
11-
lib = ffi.open('libc.so.6')
27+
lib = none
28+
for cand in cands {
29+
lib = attempt_open(cand)
30+
if lib != none {
31+
print("Successfully opened:", cand)
32+
break
33+
} else {
34+
print("Failed to open:", cand)
35+
}
36+
}
37+
38+
if lib == none {
39+
throw Error("Failed to open any of the candidates")
40+
}
41+
1242

1343
# Load the 'puts' function, with the given signature
1444
cputs = lib.load('puts', ffi.func[ffi.int, (ffi.ptr[ffi.char], )])

include/ks/ffi.h renamed to include/ks/ksffi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* ks/ffi.h - header for the 'ffi' (Foreign Function Interface) module in kscript
1+
/* ks/ksffi.h - header for the 'ffi' (Foreign Function Interface) module in kscript
22
*
33
* Provides wrappers for interfacing with C functions/libraries, through Foreign Function Interface
44
*

include/ks/nuklear.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
#include <ks/inuklear_xlib_gl3.h>
7070

7171

72-
#elif defined(HS_KAVE_glfw)
72+
#elif defined(KS_HAVE_glfw)
7373
#define KSNK_DO 1
7474

7575

src/inter.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ static void handle_sigint(int signum) {
224224

225225
/* Regenerate prompt and clear text */
226226
rl_on_new_line();
227+
#ifndef __APPLE__
227228
rl_replace_line("", 0);
229+
#endif
228230
rl_set_prompt(prompt0->data);
229231
rl_redisplay();
230232

@@ -274,8 +276,10 @@ bool ks_inter() {
274276
// don't append a space
275277
rl_completion_append_character = '\0';
276278

279+
#ifndef __APPLE__
277280
rl_catch_signals = 0;
278281
rl_clear_signals();
282+
#endif
279283

280284
#else
281285

src/modules/ffi/dll.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author: Cade Brown <cade@kscript.org>
55
*/
66
#include <ks/impl.h>
7-
#include <ks/ffi.h>
7+
#include <ks/ksffi.h>
88

99
#define T_NAME "ffi.DLL"
1010

src/modules/ffi/float.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author: Cade Brown <cade@kscript.org>
55
*/
66
#include <ks/impl.h>
7-
#include <ks/ffi.h>
7+
#include <ks/ksffi.h>
88

99
/* Type definitions */
1010

src/modules/ffi/func.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author: Cade Brown <cade@kscript.org>
55
*/
66
#include <ks/impl.h>
7-
#include <ks/ffi.h>
7+
#include <ks/ksffi.h>
88

99
#define T_NAME "ffi.func"
1010

src/modules/ffi/int.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author: Cade Brown <cade@kscript.org>
55
*/
66
#include <ks/impl.h>
7-
#include <ks/ffi.h>
7+
#include <ks/ksffi.h>
88

99
/* Type definitions */
1010

0 commit comments

Comments
 (0)