Skip to content

Instantly share code, notes, and snippets.

@stibear
Created April 23, 2016 12:36
Show Gist options
  • Save stibear/fa10d30736a97ece794249b5baf67a68 to your computer and use it in GitHub Desktop.
Save stibear/fa10d30736a97ece794249b5baf67a68 to your computer and use it in GitHub Desktop.
$ make clean && make test-nostdlib
rm -f src/load_piclib.c src/init_contrib.c
rm -f lib/libbenz.so
rm -f extlib/benz/var.o extlib/benz/symbol.o extlib/benz/string.o extlib/benz/gc.o extlib/benz/debug.o extlib/benz/blob.o extlib/benz/data.o extlib/benz/record.o extlib/benz/number.o extlib/benz/char.o extlib/benz/port.o extlib/benz/cont.o extlib/benz/write.o extlib/benz/load.o extlib/benz/file.o extlib/benz/boot.o extlib/benz/dict.o extlib/benz/vector.o extlib/benz/bool.o extlib/benz/proc.o extlib/benz/pair.o extlib/benz/value.o extlib/benz/error.o extlib/benz/read.o extlib/benz/lib.o extlib/benz/state.o extlib/benz/weak.o extlib/benz/eval.o
rm -f src/main.o src/load_piclib.o src/init_contrib.o
rm -f contrib/10.callcc/callcc.o contrib/10.math/math.o contrib/10.roundtrip/emyg_dtoa.o contrib/10.roundtrip/emyg_atod.o contrib/20.r7rs/src/r7rs.o contrib/20.r7rs/src/file.o contrib/20.r7rs/src/load.o contrib/20.r7rs/src/system.o contrib/20.r7rs/src/time.o contrib/30.random/src/random.o contrib/30.random/src/mt19937ar.o contrib/30.regexp/src/regexp.o contrib/40.srfi/src/0.o contrib/40.srfi/src/106.o contrib/60.repl/repl.o
cc -I extlib/benz/include -D'PIC_USE_LIBC=0' -D'PIC_USE_STDIO=0' -D'PIC_USE_WRITE=0' -ffreestanding -nostdlib -Os -shared -std=c89 -pedantic -Wall -Wextra -Werror -o lib/libbenz-tiny.so extlib/benz/var.c extlib/benz/symbol.c extlib/benz/string.c extlib/benz/gc.c extlib/benz/debug.c extlib/benz/blob.c extlib/benz/data.c extlib/benz/record.c extlib/benz/number.c extlib/benz/char.c extlib/benz/port.c extlib/benz/cont.c extlib/benz/write.c extlib/benz/load.c extlib/benz/file.c extlib/benz/boot.c extlib/benz/dict.c extlib/benz/vector.c extlib/benz/bool.c extlib/benz/proc.c extlib/benz/pair.c extlib/benz/value.c extlib/benz/error.c extlib/benz/read.c extlib/benz/lib.c extlib/benz/state.c extlib/benz/weak.c extlib/benz/eval.c etc/libc_polyfill.c -fno-stack-protector
C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/5.3.0/../../../../i686-w64-mingw32/bin/ld.exe: warning: cannot find entry symbol _DllMainCRTStartup@12; defaulting to 68081000
C:\msys64\tmp\ccwTNqQn.o:cont.c:(.text+0x5cc): undefined reference to `_setjmp3'
C:\msys64\tmp\ccjiBK9C.o:load.c:(.text+0xf8): undefined reference to `_setjmp3'
C:\msys64\tmp\ccQ2Gu4w.o:read.c:(.text+0x2117): undefined reference to `_setjmp3'
C:\msys64\tmp\ccQ2Gu4w.o:read.c:(.text+0x233e): undefined reference to `_setjmp3'
C:\msys64\tmp\ccYWhTyF.o:eval.c:(.text+0x618a): undefined reference to `_setjmp3'
collect2.exe: error: ld returned 1 exit status
Makefile:84: ターゲット 'test-nostdlib' のレシピで失敗しました
make: *** [test-nostdlib] エラー 1
$ git diff HEAD
diff --git a/Makefile b/Makefile
index e0ca11d..6f87068 100644
--- a/Makefile
+++ b/Makefile
@@ -22,6 +22,10 @@ TEST_RUNNER = bin/picrin
CFLAGS += -I./extlib/benz/include -Wall -Wextra
LDFLAGS += -lm
+UNAME = $(shell uname -s)
+ifneq (, $(findstring MINGW, $(UNAME)))
+ LDFLAGS += -lws2_32 -lregex
+endif
prefix ?= /usr/local
@@ -70,8 +74,14 @@ test: test-contribs test-nostdlib test-issue
test-contribs: bin/picrin $(CONTRIB_TESTS)
+ifneq (, $(findstring MINGW, $(UNAME)))
+ FPIC =
+else
+ FPIC = -fPIC
+endif
+
test-nostdlib:
- $(CC) -I extlib/benz/include -D'PIC_USE_LIBC=0' -D'PIC_USE_STDIO=0' -D'PIC_USE_WRITE=0' -ffreestanding -nostdlib -Os -fPIC -shared -std=c89 -pedantic -Wall -Wextra -Werror -o lib/libbenz-tiny.so $(BENZ_SRCS) etc/libc_polyfill.c -fno-stack-protector
+ $(CC) -I extlib/benz/include -D'PIC_USE_LIBC=0' -D'PIC_USE_STDIO=0' -D'PIC_USE_WRITE=0' -ffreestanding -nostdlib -Os $(FPIC) -shared -std=c89 -pedantic -Wall -Wextra -Werror -o lib/libbenz-tiny.so $(BENZ_SRCS) etc/libc_polyfill.c -fno-stack-protector
strip lib/libbenz-tiny.so
ls -lh lib/libbenz-tiny.so
rm -f lib/libbenz-tiny.so
diff --git a/contrib/10.roundtrip/emyg_atod.c b/contrib/10.roundtrip/emyg_atod.c
index f742e15..4912400 100644
--- a/contrib/10.roundtrip/emyg_atod.c
+++ b/contrib/10.roundtrip/emyg_atod.c
@@ -44,6 +44,10 @@
#include <ctype.h> // used by emyg_strtod
#include <math.h> // scalb()
+#ifdef __MINGW32__
+# define scalb _scalb
+#endif
+
#include "emyg_pow5.h" // in separate file to support alternate implementations, and it's big
#ifdef TESTING_QUOREM
diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c
index 2ffec60..f138f5c 100644
--- a/contrib/40.srfi/src/106.c
+++ b/contrib/40.srfi/src/106.c
@@ -2,12 +2,19 @@
#include "picrin/extra.h"
#include <errno.h>
-#include <netdb.h>
-#include <netinet/in.h>
+
+#ifndef __MINGW32__
+# include <netdb.h>
+# include <netinet/in.h>
+# include <sys/socket.h>
+#else
+# include <winsock2.h>
+# include <ws2tcpip.h>
+#endif
+
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
-#include <sys/socket.h>
#include <unistd.h>
#ifndef EWOULDBLOCK
@@ -22,7 +29,11 @@ PIC_INLINE void
socket_close(struct pic_socket_t *sock)
{
if (sock != NULL && sock->fd != -1) {
+#ifdef __MINGW32__
+ closesocket(sock->fd);
+#else
close(sock->fd);
+#endif
sock->fd = -1;
}
}
@@ -65,6 +76,10 @@ pic_socket_make_socket(pic_state *pic)
int result;
struct addrinfo hints, *ai, *it;
struct pic_socket_t *sock;
+#ifdef __MINGW32__
+ WSADATA wsaData;
+ WSAStartup(MAKEWORD(2,0), &wsaData);
+#endif
pic_get_args(pic, "zziiii", &node, &service, &family, &socktype, &flags, &protocol);
@@ -86,9 +101,11 @@ pic_socket_make_socket(pic_state *pic)
result = getaddrinfo(node, service, &hints, &ai);
} while (result == EAI_AGAIN);
if (result) {
+#ifndef __MINGW32__
if (result == EAI_SYSTEM) {
pic_error(pic, strerror(errno), 0);
}
+#endif
pic_error(pic, gai_strerror(result), 0);
}
@@ -108,8 +125,8 @@ pic_socket_make_socket(pic_state *pic)
it->ai_socktype == SOCK_SEQPACKET) {
/* TODO: Backlog should be configurable. */
if (listen(fd, 8) == 0) {
- sock->fd = fd;
- break;
+ sock->fd = fd;
+ break;
}
} else {
sock->fd = fd;
@@ -122,8 +139,12 @@ pic_socket_make_socket(pic_state *pic)
break;
}
}
-
- close(fd);
+
+#ifdef __MINGW32__
+ closesocket(fd);
+#else
+ close(fd);
+#endif
}
freeaddrinfo(ai);
@@ -132,6 +153,10 @@ pic_socket_make_socket(pic_state *pic)
pic_error(pic, strerror(errno), 0);
}
+#ifdef __MINGW32__
+ // WSACleanup();
+#endif
+
return pic_data_value(pic, sock, &socket_type);
}
@stibear
Copy link
Author

stibear commented Apr 23, 2016

Winsock2のWSACleanup()を呼ぶタイミングが分からない

@SaitoAtsushi
Copy link

Gauche でどうなっているか確かめてみると、プロセスが終了するときに WSACleanup を呼ぶようになってました。 より正確に言うとプロセス終了時に呼出すハンドラとして登録していました。 そういったハンドラ登録 API は Picrin には無いらしい (?) ので、そのあたりの整備を先にする必要があるかもしれませんね。

WSAStartup も (ソケットを作るときではなく) モジュールをロードするときに呼出すようにするのが筋が良いと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment