Add Jmk2 build system, port lisp to it
diff --git a/.gitignore b/.gitignore
index 9da708c..acbda7d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,6 +9,7 @@
*.img
bin/*
!bin/jmk
+!bin/jmk2
**/Makefile
**/Jmk.options
.gitignore
diff --git a/bin/jmk2 b/bin/jmk2
new file mode 100755
index 0000000..a441ef9
--- /dev/null
+++ b/bin/jmk2
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+# Jay MaKe
+
+dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
+root="$(dirname $dir)"
+jmkscript="$root/share/jmk/jmk.tcl"
+options=""
+
+disable_gtags=false
+
+while getopts "hD:" arg; do
+ case $arg in
+ h) echo "Please read the JMK manual for more information." ;;
+ D) options="$options $(echo $OPTARG | sed 's/=/ /')" ;;
+ ?) echo "Invalid argument"
+ exit 1 ;;
+ esac
+done
+
+echo "$options"
+
+for file in $(find -name Jmk2); do
+ echo "$file" > /dev/stderr
+ outdir="$(dirname $file)"
+ cat - $file << EOF | tclsh - $options > "$outdir/Makefile"
+source {$root/share/jmk/jmk.tcl}
+set root {$root}
+set jmk_build_cmd {$0}
+set jmk_build_dir {$(pwd)}
+cd {$outdir}
+array set options {$options}
+EOF
+done
diff --git a/share/jmk/jmk.tcl b/share/jmk/jmk.tcl
index 36c65a9..d1de805 100644
--- a/share/jmk/jmk.tcl
+++ b/share/jmk/jmk.tcl
@@ -8,7 +8,11 @@
variable asm as
variable cc cc
-variable options
+# variable options
+
+variable src \$^
+variable first_src \$<
+variable target \$@
proc init {name {target {DEFAULT_TARGET}}} {
if {$target eq {DEFAULT_TARGET}} {
@@ -20,6 +24,10 @@
set jmk_name $name
set jmk_target $target
+
+ puts {MAKEFILE_DEPTH ?= 1}
+
+ rule all $target {}
}
proc preset {p} {
@@ -61,7 +69,82 @@
}
}
+proc log {category message} {
+ puts "\t@printf '\\e\[1;34m%8s\\e\[m %s\\n' '$category' '$message' > /dev/stderr"
+}
+
+proc cc {command} {
+ puts "\t@$::cc $command $::cflags"
+}
+
+proc asm {command} {
+ puts "\t@$::asm $command $::asmflags"
+}
+
+proc shell {command} {
+ puts "\t@$command"
+}
+
+proc rule {target deps does} {
+ puts ""
+ puts "$target: $deps"
+ eval $does
+}
+
+proc type {type} {
+ ::type::$type
+}
+
+proc srcs {args} {
+ puts ""
+ variable objs ""
+
+ foreach src $args {
+ variable obj [regsub -- {(.+)\.\w+} $src {\1.o}]
+ variable objs "$objs $obj"
+ }
+
+ puts "OBJECTS += $objs"
+}
+
+namespace eval type {
+ proc executable {} {
+ global jmk_target
+
+ rule $jmk_target "\$(OBJECTS)" {
+ log LD $::target
+ cc "-o $::target $::src"
+ }
+
+ helpers
+ }
+
+proc helpers {} {
+ rule .c.o {} {
+ log CC $::first_src
+ cc "-c $::first_src -o $::target"
+ }
+
+ rule .s.o {} {
+ log ASM $::first_src
+ asm "\$(ASMFLAGS) $::first_src -o $::target"
+ }
+
+ rule clean {} {
+ shell "rm -f **/*.o **/*.a *.so $::target \$(OBJECTS)"
+ }
+}
+}
+
namespace eval preset {
+ proc freestanding {} {
+ cflags -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding
+ }
+
+ proc optimize {} {
+ cflags -O2
+ }
+
proc 32 {} {
cflag -m32
asmflag -felf32
@@ -73,7 +156,7 @@
}
proc warn {} {
- cflag "-Wall -Wextra -Werror"
+ cflags -Wall -Wextra -Wno-unused-function -Wno-unused-variable -Wno-incompatible-pointer-types -Werror
}
proc nasm {} {
@@ -81,9 +164,3 @@
set asm nasm
}
}
-
-if {[catch {array set options $argv} msg]} {
- puts "Sorry, you must pass an even number of arguments to this script"
- puts "in the form <key> <value>"
- exit 1
-}
diff --git a/src/lisp/Jmk2 b/src/lisp/Jmk2
index 17f1910..b82e084 100644
--- a/src/lisp/Jmk2
+++ b/src/lisp/Jmk2
@@ -1,6 +1,6 @@
# -*- mode:tcl -*-
-source "../../share/jmk/jmk.tcl"
+# source "../../share/jmk/jmk.tcl"
init lisp
@@ -9,7 +9,10 @@
option NO_READLINE 0
-type executable
+srcs main.c lisp.c compiler.c lib/std.c plat/linux.c istream.c gc.c \
+ call_list.s error.c
+
+set lisp_libpath "$::root/lib/lisp"
if {$options(NO_READLINE) == 0} {
cflags -lreadline
@@ -20,14 +23,18 @@
set lua vendor/luajit/src/host/minilua
rule $lua ${lua}.c {
- log CC $source
- cc $source -o $target -lm
+ log CC $::src
+ cc "$::src -o $::target -lm"
}
rule compiler.c "compiler.dasc $lua" {
- log DYNASM $first_source
- shell $::lua vendor/luajit/dynasm/dynasm.lua -o $target $first_source
+ log DYNASM $::first_src
+ shell "$::lua vendor/luajit/dynasm/dynasm.lua -o $::target $::first_src"
}
-sources main.c lisp.c compiler.c lib/std.c plat/linux.c istream.c gc.c \
- call_list.s error.c
+rule repl lisp {
+ log LISP repl
+ shell "LISP_LIBRARY_PATH=$::lisp_libpath ./lisp $::root/lib/lisp/repl/repl.lisp"
+}
+
+type executable