From 7278e994da58c10810699969876d13492170cbe0 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sat, 9 May 2026 17:40:43 +0900 Subject: [PATCH] stuff --- .gitignore | 2 +- GNUmakefile | 8 ++++---- mk/js.mk | 28 ++++++++++++++++++++-------- src/compiler/watcom.js | 18 ++++++++++++++---- src/config.h | 6 ++++-- src/js.c | 24 ++++++++++++++++-------- src/js/cmdline.js | 10 ++++++---- src/js/core.js | 42 +++++++++++++++++++++++++++++++++++++++++- src/js/postinit.js | 7 +++++++ src/js_console.c | 3 --- src/js_fs.c | 3 --- src/js_process.c | 3 --- src/system/unix.js | 8 ++++++++ src/system/windows.js | 8 ++++++++ 14 files changed, 129 insertions(+), 41 deletions(-) create mode 100644 src/js/postinit.js create mode 100644 src/system/unix.js create mode 100644 src/system/windows.js diff --git a/.gitignore b/.gitignore index 95d02ac..be2436e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,5 @@ /Makefile /*.make /src/compiler/*.c -/src/action/*.c +/src/system/*.c /src/js/*.c diff --git a/GNUmakefile b/GNUmakefile index ac01a47..8946410 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -56,7 +56,7 @@ all: pmake$(EXE) include mk/js.mk mk/js.mk: - find src/js src/action src/compiler -name "*.js" | while read js; do \ + find src/compiler src/system src/js -name "*.js" | while read js; do \ name="`echo $$js | cut -d/ -f2`_`echo $$js | $(NAME)`" ; \ c="`echo $$js | sed 's/\.js$$/.c/'`" ; \ o="`echo $$js | sed 's/\.js$$/.o/'`" ; \ @@ -81,7 +81,7 @@ src/config.h: echo "#ifndef __CONFIG_H__" > $@ echo "#define __CONFIG_H__" >> $@ echo "" >> $@ - for i in compiler action; do \ + for i in compiler system; do \ echo "#define `echo $$i | tr '[:lower:]' '[:upper:]'`S \\" >> $@ ; \ find src/$$i -name "*.js" | while read js; do \ echo " X(`echo $$js | $(NAME)`, $$i) \\" ; \ @@ -90,7 +90,7 @@ src/config.h: done echo "#define JS \\" >> $@ echo " COMPILERS \\" >> $@ - echo " ACTIONS" >> $@ + echo " SYSTEMS" >> $@ echo "" >> $@ echo "#endif" >> $@ @@ -103,4 +103,4 @@ pmake$(EXE): $(OBJS) clean: rm -f pmake pmake.exe find . -name "*.o" >/dev/null && rm -f $(shell find . -name "*.o") - rm -f src/compiler/*.c src/action/*.c src/js/*.c + rm -f src/compiler/*.c src/system/*.c src/js/*.c diff --git a/mk/js.mk b/mk/js.mk index 85d3ed2..03dc71a 100644 --- a/mk/js.mk +++ b/mk/js.mk @@ -1,11 +1,3 @@ -OBJS += src/js/core.o -src/js/core.c: src/js/core.js - xxd -njs_core -i src/js/core.js > src/js/core.c - -OBJS += src/js/cmdline.o -src/js/cmdline.c: src/js/cmdline.js - xxd -njs_cmdline -i src/js/cmdline.js > src/js/cmdline.c - OBJS += src/compiler/watcom.o src/compiler/watcom.c: src/compiler/watcom.js xxd -ncompiler_watcom -i src/compiler/watcom.js > src/compiler/watcom.c @@ -14,3 +6,23 @@ OBJS += src/compiler/gcc.o src/compiler/gcc.c: src/compiler/gcc.js xxd -ncompiler_gcc -i src/compiler/gcc.js > src/compiler/gcc.c +OBJS += src/system/unix.o +src/system/unix.c: src/system/unix.js + xxd -nsystem_unix -i src/system/unix.js > src/system/unix.c + +OBJS += src/system/windows.o +src/system/windows.c: src/system/windows.js + xxd -nsystem_windows -i src/system/windows.js > src/system/windows.c + +OBJS += src/js/core.o +src/js/core.c: src/js/core.js + xxd -njs_core -i src/js/core.js > src/js/core.c + +OBJS += src/js/cmdline.o +src/js/cmdline.c: src/js/cmdline.js + xxd -njs_cmdline -i src/js/cmdline.js > src/js/cmdline.c + +OBJS += src/js/postinit.o +src/js/postinit.c: src/js/postinit.js + xxd -njs_postinit -i src/js/postinit.js > src/js/postinit.c + diff --git a/src/compiler/watcom.js b/src/compiler/watcom.js index 4966d9b..ea72853 100644 --- a/src/compiler/watcom.js +++ b/src/compiler/watcom.js @@ -7,18 +7,20 @@ Watcom.description = "Open Watcom"; Watcom.output = function(target) { if(target instanceof pmake.LibraryProject) { return target.output + ".lib"; + } else if(target instanceof pmake.ExecutableProject) { + return target.output + ".exe"; } return pmake.Compiler.prototype.output.call(this, target); -} +}; - Watcom.compile = function(target, obj, src) { +Watcom.compile = function(target, obj, src) { function add_i(x) { return "-i=" + x; } const includes = pmake.includes(target).map(add_i).join(" "); - const flags = "-bt=nt -q " + includes + " -fo=" + obj + " " + src; + const flags = "-bt=nt -fr= -q " + includes + " -fo=" + obj + " " + src; if(src.endsWith(".c")) { return "wcc386 " + flags; @@ -36,15 +38,23 @@ Watcom.link = function(target, files) { return "file \"" + x + "\""; } + function add_lib(x) { + return x + ".lib"; + } + function add_library(x) { return "library \"" + x + "\""; } - return "wlink option quiet system nt name " + this.output(target) + " " + files.map(add_file).join(" ") + " " + pmake.libs(target).map(add_library).join(" "); + return "wlink option quiet system nt name " + this.output(target) + " " + files.map(add_file).join(" ") + " " + pmake.libs(target, add_lib).map(add_library).join(" "); } return ""; }; +Watcom.init = function() { + pmake.setSystem("Windows"); +}; + pmake.register(Watcom); })(); diff --git a/src/config.h b/src/config.h index ead5407..dd44759 100644 --- a/src/config.h +++ b/src/config.h @@ -5,10 +5,12 @@ X(watcom, compiler) \ X(gcc, compiler) -#define ACTIONS +#define SYSTEMS \ + X(unix, system) \ + X(windows, system) #define JS \ COMPILERS \ - ACTIONS + SYSTEMS #endif diff --git a/src/js.c b/src/js.c index ed5fee9..49bd7b0 100644 --- a/src/js.c +++ b/src/js.c @@ -3,14 +3,13 @@ duk_context* js_ctx; LOAD(js_core); +LOAD(js_postinit); LOAD(js_cmdline); -LOAD(js_end); static void js_argv_init(int argc, char** argv) { int arr; int i; - duk_push_global_object(js_ctx); arr = duk_push_array(js_ctx); for(i = 0; i < argc; i++) { @@ -19,16 +18,26 @@ static void js_argv_init(int argc, char** argv) { } duk_put_prop_string(js_ctx, 0, "ARGV"); +} - duk_pop(js_ctx); +static void js_win32_init(void) { +#ifdef _WIN32 + duk_push_number(js_ctx, 1); +#else + duk_push_number(js_ctx, 0); +#endif + + duk_put_prop_string(js_ctx, 0, "WIN32"); } int js_init(int argc, char** argv) { int st; js_ctx = duk_create_heap_default(); + duk_push_global_object(js_ctx); js_argv_init(argc, argv); + js_win32_init(); js_console_init(); js_process_init(); js_fs_init(); @@ -44,14 +53,13 @@ int js_init(int argc, char** argv) { return 1; \ } JS; -#undef X + + X(postinit, js); if((st = js_run("pmake.js")) != 0) return st; - if(duk_peval_lstring(js_ctx, js_cmdline, js_cmdline_len) != 0) { - printf("JS error: js/cmdline.js: %s\n", duk_safe_to_string(js_ctx, -1)); - return 1; - } + X(cmdline, js); +#undef X return 0; } diff --git a/src/js/cmdline.js b/src/js/cmdline.js index 7f02b3a..3a613e6 100644 --- a/src/js/cmdline.js +++ b/src/js/cmdline.js @@ -1,7 +1,5 @@ const PADDING = 24; -pmake.compiler = pmake.compilers["GCC"]; - for(var i = 1; i < ARGV.length; i++) { if(ARGV[i] == "--help") { var keys; @@ -79,6 +77,8 @@ for(var i = 1; i < ARGV.length; i++) { } } +main(); + (function() { function make(output) { const targets = pmake.resolveProjects(pmake.projects); @@ -134,8 +134,10 @@ function make(output) { mk.write("\n"); mk.write(pmake.compiler.output(self) + ": " + objs.join(" ") + "\n"); - mk.write(" @echo LINK " + pmake.compiler.output(self) + "\n"); - mk.write(" $(SILENT)" + pmake.compiler.link(self, objs) + "\n"); + if(!(self instanceof pmake.InterfaceProject)) { + mk.write(" @echo LINK " + pmake.compiler.output(self) + "\n"); + mk.write(" $(SILENT)" + pmake.compiler.link(self, objs) + "\n"); + } mk.write("\n"); for(var i = 0; i < objs.length; i++) { diff --git a/src/js/core.js b/src/js/core.js index 1d9a279..bce6fbc 100644 --- a/src/js/core.js +++ b/src/js/core.js @@ -21,6 +21,7 @@ if(!Array.prototype.flat) { const pmake = { VERSION : "0.0", compilers : {}, + systems : {}, options : {}, options_category : {}, projects : [] @@ -53,6 +54,8 @@ pmake.inherited = function(base, parent) { pmake.register = function(target) { if(target instanceof pmake.Compiler) { pmake.compilers[target.target] = target; + } else if(target instanceof pmake.System) { + pmake.systems[target.target] = target; } else if(target instanceof pmake.Option) { if(!pmake.options_category[target.category]) pmake.options_category[target.category] = {}; @@ -118,11 +121,26 @@ pmake.libs = function(target, if_str) { return targets.map(libs).flat(); }; +pmake.setSystem = function(target){ + pmake.system = pmake.systems[target]; + + if(pmake.system.init) pmake.system.init(); +}; + +pmake.setCompiler = function(target){ + pmake.compiler = pmake.compilers[target]; + + if(pmake.compiler.init) pmake.compiler.init(); +}; + (function() { const Compiler = function() { this.prefix = ""; }; +Compiler.prototype.init = function() { +}; + Compiler.prototype.output = function(target) { if((typeof target) == "string") { if(target.endsWith(".c")) return target.replace(/\.c$/, ".o"); @@ -180,6 +198,11 @@ POSIXCompiler.prototype.link = function(target, files) { return ""; } +const System = function() {}; + +System.prototype.init = function() { +}; + const Option = function() {}; const Project = function(target) { @@ -216,6 +239,7 @@ File.prototype.close = function() { pmake.Compiler = Compiler; pmake.POSIXCompiler = POSIXCompiler; +pmake.System = System; pmake.Option = Option; pmake.Project = Project; pmake.LibraryProject = LibraryProject; @@ -238,8 +262,24 @@ Compiler.options = function() { }; Compiler.action = function(value) { - pmake.compiler = pmake.compilers[value]; + pmake.setCompiler(value); +}; + +const System = new pmake.Option(); + +System.category = "General"; +System.target = "system"; +System.description = "Generate files for a different system; one of:"; +System.option = "VALUE"; + +System.options = function() { + return Object.keys(pmake.systems); +}; + +System.action = function(value) { + pmake.setSystem(value); }; pmake.register(Compiler); +pmake.register(System); })(); diff --git a/src/js/postinit.js b/src/js/postinit.js new file mode 100644 index 0000000..06a9fd1 --- /dev/null +++ b/src/js/postinit.js @@ -0,0 +1,7 @@ +pmake.setCompiler("GCC"); + +if(WIN32) { + pmake.setSystem("Windows"); +}else{ + pmake.setSystem("Unix"); +} diff --git a/src/js_console.c b/src/js_console.c index e24f8f4..0687b5d 100644 --- a/src/js_console.c +++ b/src/js_console.c @@ -9,13 +9,10 @@ static duk_ret_t js_console_log(duk_context* self) { void js_console_init(void) { int obj; - duk_push_global_object(js_ctx); obj = duk_push_object(js_ctx); duk_push_c_function(js_ctx, js_console_log, 1); duk_put_prop_string(js_ctx, obj, "log"); duk_put_prop_string(js_ctx, 0, "console"); - - duk_pop(js_ctx); } diff --git a/src/js_fs.c b/src/js_fs.c index c1c4126..948e5a4 100644 --- a/src/js_fs.c +++ b/src/js_fs.c @@ -98,7 +98,6 @@ static duk_ret_t js_fs_close(duk_context* self) { void js_fs_init(void) { int obj; - duk_push_global_object(js_ctx); obj = duk_push_object(js_ctx); duk_push_c_function(js_ctx, js_fs_glob, DUK_VARARGS); @@ -114,6 +113,4 @@ void js_fs_init(void) { duk_put_prop_string(js_ctx, obj, "close"); duk_put_prop_string(js_ctx, 0, "fs"); - - duk_pop(js_ctx); } diff --git a/src/js_process.c b/src/js_process.c index 70d1c7c..c47aa6d 100644 --- a/src/js_process.c +++ b/src/js_process.c @@ -30,7 +30,6 @@ static duk_ret_t js_process_system(duk_context* self) { void js_process_init(void) { int obj; - duk_push_global_object(js_ctx); obj = duk_push_object(js_ctx); duk_push_c_function(js_ctx, js_process_exit, DUK_VARARGS); @@ -40,6 +39,4 @@ void js_process_init(void) { duk_put_prop_string(js_ctx, obj, "system"); duk_put_prop_string(js_ctx, 0, "process"); - - duk_pop(js_ctx); } diff --git a/src/system/unix.js b/src/system/unix.js new file mode 100644 index 0000000..4f011d4 --- /dev/null +++ b/src/system/unix.js @@ -0,0 +1,8 @@ +(function() { +const Unix = new pmake.System(); + +Unix.target = "Unix"; +Unix.description = "Unix/Unix-like"; + +pmake.register(Unix); +})(); diff --git a/src/system/windows.js b/src/system/windows.js new file mode 100644 index 0000000..e15a006 --- /dev/null +++ b/src/system/windows.js @@ -0,0 +1,8 @@ +(function() { +const Windows = new pmake.System(); + +Windows.target = "Windows"; +Windows.description = "Windows"; + +pmake.register(Windows); +})();