Skip to content

Commit

Permalink
feat: basic support for compiling source to object files
Browse files Browse the repository at this point in the history
added cwalk to handle paths cross-platform

exposed TCC function to compile to file
  • Loading branch information
jaromil committed Dec 28, 2024
1 parent 168a538 commit a54d522
Show file tree
Hide file tree
Showing 8 changed files with 2,093 additions and 27 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
cjit
*.o
src/embed*
src/assets.*
src/embed_*
.build_done*
dl
cjit.res
cjit.rc
cjit_source
pristine
cjit-demo*
20 changes: 16 additions & 4 deletions REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ precedence = "aggregate"
SPDX-FileCopyrightText = "2024 Dyne.org foundation"
SPDX-License-Identifier = "GPL-3.0-or-later"

[[annotations]]
path = "src/cwalk.*"
precedence = "aggregate"
SPDX-FileCopyrightText = "2024 Leonard Iklé"
SPDX-License-Identifier = "MIT"

[[annotations]]
path = "src/array.*"
precedence = "aggregate"
SPDX-FileCopyrightText = "2015-2020 Sun Dro (s.kalatoz@gmail.com)"
SPDX-License-Identifier = "MIT"

[[annotations]]
path = [".*", ".github/**"]
precedence = "aggregate"
Expand All @@ -30,25 +42,25 @@ SPDX-License-Identifier = "MIT"
[[annotations]]
path = "examples/tinyc.games/res/PixelatedEleganceRegular.ttf"
precedence = "aggregate"
SPDX-FileCopyrightText = " 2024 GGBotNet"
SPDX-FileCopyrightText = "2024 GGBotNet"
SPDX-License-Identifier = "MIT-0"

[[annotations]]
path = "test/dmon.h"
precedence = "aggregate"
SPDX-FileCopyrightText = " 2019-2023 Sepehr Taghdisian"
SPDX-FileCopyrightText = "2019-2023 Sepehr Taghdisian"
SPDX-License-Identifier = "BSD-2-Clause"

[[annotations]]
path = "assets/stb/**"
precedence = "aggregate"
SPDX-FileCopyrightText = " 2009-2021 Sean Barrett"
SPDX-FileCopyrightText = "2009-2021 Sean Barrett"
SPDX-License-Identifier = "MIT-0"

[[annotations]]
path = "assets/win32ports/**"
precedence = "aggregate"
SPDX-FileCopyrightText = " 2019 win32ports"
SPDX-FileCopyrightText = "2019 win32ports"
SPDX-License-Identifier = "MIT-0"

[[annotations]]
Expand Down
2 changes: 1 addition & 1 deletion build/init.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cflags := ${CFLAGS} ${cflags_includes}

SOURCES := src/file.o src/cjit.o \
src/main.o src/assets.o \
src/repl.o \
src/cwalk.o src/repl.o \
src/muntar.o src/tinflate.o src/tinfgzip.o \
src/embed_libtcc1.a.o src/embed_include.o
#src/embed_source.o
Expand Down
44 changes: 38 additions & 6 deletions src/cjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@
*/

#include <cjit.h>
#include <libtcc.h>
#include <cwalk.h>

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>

#include <ketopt.h>
#include <muntar.h>

#define MAX_PATH 260 // rather short paths
#define MAX_STRING 20480 // max 20KiB strings
Expand Down Expand Up @@ -130,8 +128,16 @@ CJITState* cjit_new() {
}
// error handler callback for TCC
tcc_set_error_func(cjit->TCC, stderr, cjit_tcc_handle_error);
return(cjit);
}

bool cjit_setup(CJITState *cjit) {
// set output in memory for just in time execution
tcc_set_output_type(cjit->TCC, TCC_OUTPUT_MEMORY);

tcc_set_output_type(cjit->TCC,
cjit->tcc_output?
cjit->tcc_output:
TCC_OUTPUT_MEMORY);
#if defined(LIBC_MUSL)
tcc_add_libc_symbols(cjit->TCC);
#endif
Expand Down Expand Up @@ -184,8 +190,33 @@ CJITState* cjit_new() {
free(sdkpath);
}
#endif
return(true);
}

return(cjit);
int cjit_compile_obj(CJITState *cjit, const char *_path) {
char *restrict tmp;
const char *basename;
char *ext;
size_t len;
cwk_path_get_basename((char*)_path, &basename, &len);
tmp = malloc(len+2);
strncpy(tmp,basename,len+1);
// _err("basename: %s",tmp);
if( !cwk_path_get_extension(tmp,(const char**)&ext,&len) ) {
_err("Filename has no extension: %s",basename);
return 1;
}
// _err("extension: %s",ext);
if( *(ext+1) != 'c' ) {
_err("Filename has wrong extension: %s",ext);
return 1;
}
strcpy(ext,".o");

Check warning on line 214 in src/cjit.c

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] src/cjit.c#L214

Almost always, snprintf is better than strcpy [runtime/printf] [4]
Raw output
src/cjit.c:214:  Almost always, snprintf is better than strcpy  [runtime/printf] [4]
tcc_add_file(cjit->TCC, _path);
_err("Compiling: %s -> %s",_path,tmp);
tcc_output_file(cjit->TCC,tmp);
free(tmp);
return 0;
}

int cjit_exec(CJITState *cjit, int argc, char **argv) {
Expand Down Expand Up @@ -263,6 +294,7 @@ void cjit_free(CJITState *cjit) {
if(cjit->tmpdir) free(cjit->tmpdir);
if(cjit->write_pid) free(cjit->write_pid);
if(cjit->entry) free(cjit->entry);
if(cjit->output_filename) free(cjit->output_filename);
if(cjit->TCC) tcc_delete(cjit->TCC);
free(cjit);
}
Expand Down
10 changes: 10 additions & 0 deletions src/cjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@ struct CJITState {
bool live; // live coding mode
bool quiet; // print less to stderr
bool fresh; // tempdir is freshly created and needs to be populated
int tcc_output; //
// #define TCC_OUTPUT_MEMORY 1 /* output will be run in memory */
// #define TCC_OUTPUT_EXE 2 /* executable file */
// #define TCC_OUTPUT_DLL 4 /* dynamic library */
// #define TCC_OUTPUT_OBJ 3 /* object file */
// #define TCC_OUTPUT_PREPROCESS 5 /* only preprocess */
char *output_filename; // output in case of compilation mode

Check warning on line 43 in src/cjit.h

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] src/cjit.h#L43

Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]
Raw output
src/cjit.h:43:  Redundant blank line at the end of a code block should be deleted.  [whitespace/blank_line] [3]
};
typedef struct CJITState CJITState;

extern CJITState* cjit_new();

int cjit_compile_obj(CJITState *cjit, const char *_path);

extern int cjit_exec(CJITState *cjit, int argc, char **argv);

extern void cjit_free(CJITState *CJIT);
Expand Down
Loading

0 comments on commit a54d522

Please sign in to comment.