Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: various cleanups and refining of POSIX platform definition
Browse files Browse the repository at this point in the history
build elf linker everywhere (selected by define)

don't check ld.so.conf on apple/osx
jaromil committed Jan 3, 2025
1 parent ceab4be commit d1b8564
Showing 9 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build/init.mk
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ CFLAGS ?= -O2 -fomit-frame-pointer ${cflags_stack_protect}

cflags := ${CFLAGS} ${cflags_includes}

SOURCES := src/file.o src/cjit.o \
SOURCES := src/file.o src/cjit.o src/elflinker.o \
src/main.o src/assets.o \
src/cwalk.o src/array.o \
src/muntar.o src/tinflate.o src/tinfgzip.o \
1 change: 0 additions & 1 deletion build/linux.mk
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ cc := gcc
cflags += -DLIBC_GNU -D_GNU_SOURCE
cflags += -DKILO_SUPPORTED
cflags += -DCJIT_BUILD_LINUX
SOURCES += src/elflinker.o

all: embed-posix cjit

6 changes: 3 additions & 3 deletions src/cjit.c
Original file line number Diff line number Diff line change
@@ -216,7 +216,7 @@ static bool cjit_setup(CJITState *cjit) {
}
#endif

#if defined(POSIX)
#if defined(UNIX)
read_ldsoconf(cjit->libpaths,"/etc/ld.so.conf");
read_ldsoconf_dir(cjit->libpaths,"/etc/ld.so.conf.d");
#endif
@@ -593,7 +593,7 @@ void cjit_define_symbol(CJITState *cjit, const char *sym, const char *value) {
if(cjit->verbose)_err("+D %s %s",sym,value?value:"");
}
void cjit_add_include_path(CJITState *cjit, const char *path) {
const char *restrict toadd = new_abspath(path);
char *restrict toadd = new_abspath(path);
if(!toadd) {
_err("%s: absolute path error: %s",__func__,path);
return;
@@ -604,7 +604,7 @@ void cjit_add_include_path(CJITState *cjit, const char *path) {
}
// TODO: temporary, to be reimplemented in linker.c
void cjit_add_library_path(CJITState *cjit, const char *path) {
const char *restrict toadd = new_abspath(path);
char *restrict toadd = new_abspath(path);
if(!toadd) {
_err("%s: absolute path error: %s",__func__,path);
return;
7 changes: 0 additions & 7 deletions src/cjit.h
Original file line number Diff line number Diff line change
@@ -91,10 +91,6 @@ extern bool extract_assets(CJITState *CJIT);
/////////////
// from file.c
extern char* file_load(const char *filename, unsigned int *len);
extern char *load_stdin();
extern char* dir_load(const char *path);
extern bool write_to_file(const char *path, const char *filename,
const char *buf, unsigned int len);

// terminal printing functions
extern void _out(const char *fmt, ...);
@@ -103,8 +99,5 @@ extern void _err(const char *fmt, ...);
/////////////
// from repl.c
extern int cjit_cli_tty(CJITState *cjit);
#ifdef KILO_SUPPORTED
extern int cjit_cli_kilo(CJITState *cjit);
#endif

#endif
5 changes: 5 additions & 0 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -171,6 +171,7 @@ bool write_to_file(const char *path, const char *filename, const char *buf, unsi
return true;
}

#if 0 // unused for now, dangerous to have if unnecessary
static int rm_ftw(const char *pathname,
const struct stat *sbuf,
int type, struct FTW *ftwb) {
@@ -205,7 +206,10 @@ bool rm_recursive(char *path) {
}
return true;
}
#endif


#if 0
#if !defined(WINDOWS)

static char *full_content = NULL;
@@ -273,3 +277,4 @@ char *dir_load(const char *path)


#endif
#endif // 0
12 changes: 7 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -31,6 +31,8 @@
#include <muntar.h>
#include <assets.h>

extern char *load_stdin();

#define MAX_ARG_STRING 1024
static int parse_value(char *str) {
int i = 0;
@@ -141,19 +143,19 @@ int main(int argc, char **argv) {
strcpy(CJIT->output_filename,opt.arg);
cjit_set_output(CJIT, EXE);
} else if (c == 'L') { // library path
if(!CJIT->quiet)_err("lib path: %s",opt.arg);
if(CJIT->verbose)_err("arg lib path: %s",opt.arg);
cjit_add_library_path(CJIT, opt.arg);
} else if (c == 'l') { // library link
if(!CJIT->quiet)_err("lib: %s",opt.arg);
if(CJIT->verbose)_err("arg lib: %s",opt.arg);
cjit_add_library(CJIT, opt.arg);
} else if (c == 'C') { // cflags compiler options
if(!CJIT->quiet)_err("cflags: %s",opt.arg);
if(CJIT->verbose)_err("arg cflags: %s",opt.arg);
cjit_set_tcc_options(CJIT->TCC, opt.arg);
} else if (c == 'I') { // include paths in cflags
if(!CJIT->quiet)_err("inc: %s",opt.arg);
if(CJIT->verbose)_err("arg inc: %s",opt.arg);
cjit_add_include_path(CJIT, opt.arg);
} else if (c == 'e') { // entry point (default main)
if(!CJIT->quiet)_err("entry: %s",opt.arg);
if(!CJIT->quiet)_err("entry function: %s",opt.arg);
if(CJIT->entry) free(CJIT->entry);
CJIT->entry = malloc(strlen(opt.arg)+1);
strcpy(CJIT->entry,opt.arg);
8 changes: 8 additions & 0 deletions src/platforms.h
Original file line number Diff line number Diff line change
@@ -19,18 +19,24 @@
#endif
#if defined(__linux__)
#define LINUX
#define POSIX
#define UNIX
#define PLATFORM "GNU/Linux"
#endif
#if defined(__APPLE__)
#define APPLE
#define POSIX
#define PLATFORM "Apple/OSX"
#endif
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#define BSD
#define POSIX
#define UNIX
#define PLATFORM "BSD"
#endif
#if defined(__ANDROID__)
#define ANDROID
#define POSIX
#define PLATFORM "Android"
#endif
#if defined(__EMSCRIPTEN__)
@@ -39,10 +45,12 @@
#endif
#if defined(__BEOS__) || defined(__HAIKU__)
#define BEOS
#define POSIX
#define PLATFORM "BEOS"
#endif
#if defined(__HAIKU__)
#define HAIKU
#define POSIX
#define PLATFORM "Haiku"
#endif
#endif
4 changes: 2 additions & 2 deletions test/bats_setup
Original file line number Diff line number Diff line change
@@ -7,9 +7,9 @@ setup() {
R=`pwd`
load "$T"/test_helper/bats_support/load
load "$T"/test_helper/bats_assert/load
CJIT="${R}/cjit"
[ -r "$CJIT" ] || CJIT="${R}/cjit.exe"
CJIT="${R}/cjit.exe"
[ -r "$CJIT" ] || CJIT="${R}/cjit.command"
[ -r "$CJIT" ] || CJIT="${R}/cjit"
[ -r "$CJIT" ] || {
>&2 echo "CJIT is not built, cannot run test suite"
exit 1
8 changes: 4 additions & 4 deletions test/windows.bats
Original file line number Diff line number Diff line change
@@ -3,26 +3,26 @@ load bats_setup
@test "Timeb.h inclusion for clock() in Windows" {
# see https://www.reddit.com/r/C_Programming/comments/1h1g4gc/comment/lzc9fta/
# /sys/timeb.h:132: error: include file 'sec_api/sys/timeb_s.h' not found
run ${CJIT} test/win_timeb.c
run ./cjit.exe test/win_timeb.c
assert_success
}

@test "BOM source file UTF8" {
run ${CJIT} -q test/hello-bom-utf8.c
run ./cjit.exe -q test/hello-bom-utf8.c
assert_failure
assert_line --partial 'UTF BOM detected in file: test/hello-bom-utf8.c'
assert_line --partial 'Encoding is not yet supported, execution aborted.'
}

@test "BOM source file UTF16 big endian" {
run ${CJIT} -q test/hello-bom-utf16-be.c
run ./cjit.exe -q test/hello-bom-utf16-be.c
assert_failure
assert_line --partial 'UTF BOM detected in file: test/hello-bom-utf16-be.c'
assert_line --partial 'Encoding is not yet supported, execution aborted.'
}

@test "BOM source file UTF16 little endian" {
run ${CJIT} -q test/hello-bom-utf16-le.c
run ./cjit.exe -q test/hello-bom-utf16-le.c
assert_failure
assert_line --partial 'UTF BOM detected in file: test/hello-bom-utf16-le.c'
assert_line --partial 'Encoding is not yet supported, execution aborted.'

0 comments on commit d1b8564

Please sign in to comment.