Skip to content

Commit

Permalink
Merge pull request #132 from AvirukBasak/patches-windows
Browse files Browse the repository at this point in the history
v1.5 beta: patches and fixes
  • Loading branch information
AvirukBasak committed Jun 21, 2024
2 parents 5f5d271 + fe9e14c commit 1d225ca
Show file tree
Hide file tree
Showing 17 changed files with 269 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/libshsc"]
path = lib/libshsc
url = https://github.com/AvirukBasak/shsc-c-interop
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ WRN_ERR_FLAGS := -Wall\
-Wno-unused-but-set-variable\
-Wno-unused-label\
-Wno-int-in-bool-context
ASAN_FLAGS := -fsanitize=address
ASAN_FLAGS := -fsanitize=address
ASAN_OPTIONS := ASAN_OPTIONS=detect_leaks=1:$\
fast_unwind_on_malloc=0:$\
strict_init_order=true:$\
Expand All @@ -33,16 +33,16 @@ ASAN_OPTIONS := ASAN_OPTIONS=detect_leaks=1:$\
halt_on_error=0

CC := gcc
CFLAGS := $(WRN_ERR_FLAGS) -Ofast -export-dynamic
CDBGFLAGS := $(WRN_ERR_FLAGS) -g $(ASAN_FLAGS) -D DEBUG -export-dynamic
CFLAGS := $(WRN_ERR_FLAGS) -Ofast
CDBGFLAGS := $(WRN_ERR_FLAGS) -g $(ASAN_FLAGS) -D DEBUG
DBG := gdb -q

INCLUDE := -I $(INCLUDE_DIR) -I $(LIB_DIR) -I $(SRC_DIR)
LIB := -L$(LIB_DIR) -lm -lavl -ldl
DBG_LIB := -L$(LIB_DIR) -lm -lavl-dbg -ldl
LIB := -L$(LIB_DIR) -lm -lavl -lshsc
DBG_LIB := -L$(LIB_DIR) -lm -lavl-dbg -lshsc-dbg

LIBRARIES := $(LIB_DIR)/libavl.a
DBG_LIBRARIES := $(LIB_DIR)/libavl-dbg.a
LIBRARIES := $(LIB_DIR)/libavl.a $(LIB_DIR)/libshsc.a
DBG_LIBRARIES := $(LIB_DIR)/libavl-dbg.a $(LIB_DIR)/libshsc-dbg.a

## targets
# Detect the operating system
Expand Down
4 changes: 2 additions & 2 deletions examples/interop_with_c.shsc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
proc main()
var mod = io:libopen("add.so")
var add = io:libsym(mod, "add")
var mod = io:libopen("examples/add")
var add = io:libsym(mod, "shsc_add")
var res = add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
io:print(res, lf)
end
2 changes: 1 addition & 1 deletion include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdbool.h>

#define VERSION "1.4 Beta"
#define VERSION "1.5 Beta"
#define AUTHORS "Aviruk Basak"
#define GLOBAL_BYTES_BUFFER_LEN (128)

Expand Down
2 changes: 1 addition & 1 deletion include/runtime/data/DataStr.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ char *rt_DataStr_getref_errnull(const rt_DataStr_t *str, int64_t idx);
/** data can be updated by assigning a char to the returned pointer */
rt_Data_t *rt_DataStr_getref(const rt_DataStr_t *str, int64_t idx);
void rt_DataStr_del_index(rt_DataStr_t *str, int64_t idx);
void rt_DataStr_del_char(rt_DataStr_t *str, char ch);
void rt_DataStr_del_val(rt_DataStr_t *str, char var);
char *rt_DataStr_tostr(const rt_DataStr_t *str);

#endif
Empty file removed include/runtime/util/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions include/runtime/util/libloader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef RT_UTIL_LIBLOADER_H
#define RT_UTIL_LIBLOADER_H

void rt_util_libloader(void *handle);

#endif
6 changes: 6 additions & 0 deletions include/runtime/util/loadfn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef RT_UTIL_LOADFN_H
#define RT_UTIL_LOADFN_H

void *rt_util_loadfn(const void *handle, const char *fn_name);

#endif
23 changes: 12 additions & 11 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ HEADEREXT := h

## directories

SRC_DIR := src
BUILD_DIR := build
TARGET_DIR := target
LIB_DIR := lib
TEST_DIR := tests
INCLUDE_DIR := include

## compiler options

Expand All @@ -20,12 +15,6 @@ CFLAGS := -Wall -Ofast
CDBGFLAGS := -Wall -g -fsanitize=address -D DEBUG
DBG := gdb -q

## targets

TARGET_NAME := $(EXEC_NAME)
TARGET := $(TARGET_DIR)/$(TARGET_NAME)-rel
DBG_TARGET := $(TARGET_DIR)/$(TARGET_NAME)-dbg

## go on listing your libraries below for compilation

## libdummy
Expand All @@ -44,12 +33,24 @@ $(LIB_DIR)/libavl.a:
$(LIB_DIR)/libavl-dbg.a:
@cd libavl && $(MAKE) dbg

## libshsc

$(LIB_DIR)/libshsc.a:
@cd libshsc && $(MAKE)
@cp libshsc/target/libshsc.a ./libshsc.a

$(LIB_DIR)/libshsc-dbg.a:
@cd libshsc && $(MAKE) dbg
@cp libshsc/target/libshsc-dbg.a ./libshsc-dbg.a

## cleanup

clean:
@cd libdummy && $(MAKE) clean
@cd libavl && $(MAKE) clean
@cd libshsc && $(MAKE) clean

cleaner:
@cd libdummy && $(MAKE) cleaner
@cd libavl && $(MAKE) cleaner
@cd libshsc && $(MAKE) cleaner
1 change: 1 addition & 0 deletions lib/libshsc
Submodule libshsc added at 8ceb35
4 changes: 2 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ CDBGFLAGS := $(WRN_ERR_FLAGS) -g $(ASAN_FLAGS) -D DEBUG
DBG := gdb -q

INCLUDE := -I $(INCLUDE_DIR) -I $(LIB_DIR) -I $(SRC_DIR)
LIB := -L$(LIB_DIR) -lm -lavl -ldl
DBG_LIB := -L$(LIB_DIR) -lm -lavl-dbg -ldl
LIB := -L$(LIB_DIR) -lm -lavl
DBG_LIB := -L$(LIB_DIR) -lm -lavl-dbg

HEADERS := $(shell find $(INCLUDE_DIR)/ -name "*."$(HEADEREXT))

Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/constructors.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "ast.h"
#include "ast/nodes.h"
#include "ast/nodes/enums.h"
#include "ast/util/ModuleStack.c.h"
#include "ast/util/ModuleStack.h"
#include "errcodes.h"
#include "globals.h"
#include "io.h"
Expand Down
3 changes: 3 additions & 0 deletions src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ int rt_exec(int argc, char **argv)
#include "runtime/functions.c.h"
#include "runtime/operators.c.h"
#include "runtime/VarTable.c.h"

#include "runtime/util/libloader.c.h"
#include "runtime/util/loadfn.c.h"
40 changes: 24 additions & 16 deletions src/runtime/data/DataLibHandle.c.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#ifndef RT_DATA_LIBHANDLE_C_H
#define RT_DATA_LIBHANDLE_C_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "io.h"
#include "errcodes.h"
#include "runtime/data/Data.h"
#include "runtime/data/DataLibHandle.h"
#include "runtime/io.h"
#include "runtime/util/libloader.h"
#include "runtime/util/loadfn.h"

#ifdef _WIN32
#include <windows.h>
Expand All @@ -16,12 +21,24 @@

rt_DataLibHandle_t *rt_DataLibHandle_init(char *file_name)
{
const char *dot = strrchr(file_name, '.');

/* if filename ends with .dll or .so throw error */
if (dot && ( !strcmp(dot, ".dll") || !strcmp(dot, ".so") )) {
rt_throw("library file name should not end with '%s'", dot);
}

file_name = realloc(file_name, strlen(file_name) + 16);
if (!file_name) io_errndie("rt_DataLibHandle_init:" ERR_MSG_MALLOCFAIL);

#ifdef _WIN32
strcat(file_name, ".dll");
void *handle = LoadLibrary(file_name);
if (!handle) {
rt_throw("failed to open library '%s'", file_name);
}
#else
strcat(file_name, ".so");
void *handle = dlopen(file_name, RTLD_LAZY);
if (!handle) {
rt_throw("%s", dlerror());
Expand All @@ -33,6 +50,10 @@ rt_DataLibHandle_t *rt_DataLibHandle_init(char *file_name)
/* rc is kept at 0 unless the runtime assigns
a variable to the data */
libhandle->rc = 0;

// call libloader to intilize function pointers
rt_util_libloader(libhandle->handle);

return libhandle;
}

Expand Down Expand Up @@ -65,8 +86,8 @@ void rt_DataLibHandle_destroy(rt_DataLibHandle_t **ptr)

#ifdef _WIN32
// On Windows, use FreeLibrary to close the library.
int result = FreeLibrary((HMODULE) libhandle->handle);
if (!result) rt_throw("failed to close library '%s'", libhandle->file_name);
int result = FreeLibrary((HMODULE) handle->handle);
if (!result) rt_throw("failed to close library '%s'", handle->file_name);
#else
// On Unix-like systems, use dlclose to close the library.
int result = !dlclose(handle->handle);
Expand All @@ -84,20 +105,7 @@ void rt_DataLibHandle_destroy(rt_DataLibHandle_t **ptr)

rt_fn_NativeFunction_t rt_DataLibHandle_lookup(const rt_DataLibHandle_t *handle, const char *fname)
{
#ifdef _WIN32
// On Windows, use GetProcAddress to get the symbol from the library handle.
void *fnptr = GetProcAddress((HMODULE) handle->handle, fname);
if (!fnptr) {
rt_throw("failed to get symbol '%s' from '%s'", fname, handle->file_name);
}
#else
// On Unix-like systems, use dlsym to get the symbol from the library handle.
void *fnptr = dlsym(handle->handle, fname);
if (!fnptr) {
rt_throw("%s", dlerror());
}
#endif

void *fnptr = rt_util_loadfn(handle->handle, fname);
return fnptr;
}

Expand Down
Empty file removed src/runtime/util/.gitkeep
Empty file.
Loading

0 comments on commit 1d225ca

Please sign in to comment.