Add a script for frankenbuild

self.sh modifies source files for chibicc so that they are
more friendly for self-hosting. The script is used to create
a chibicc binary that consists of object files partly genearted
by gcc and the others by chibicc.

chibicc can now compile main.c and type.c with the script.
This commit is contained in:
Rui Ueyama 2019-08-24 12:08:12 +09:00
parent ac88c3b38a
commit ed1e26670a
4 changed files with 64 additions and 6 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
tmp*
a.out
chibicc
chibicc-gen*

View File

@ -7,15 +7,23 @@ chibicc: $(OBJS)
$(OBJS): chibi.h
test: chibicc
chibicc-gen2: chibicc $(SRCS) chibi.h
./self.sh
extern.o: tests-extern
gcc -xc -c -o extern.o tests-extern
test: chibicc extern.o
./chibicc tests > tmp.s
echo 'int ext1; int *ext2; int char_fn() { return 257; }' \
'int static_fn() { return 5; }' | \
gcc -xc -c -o tmp2.o -
gcc -static -o tmp tmp.s tmp2.o
gcc -static -o tmp tmp.s extern.o
./tmp
test-gen2: chibicc-gen2 extern.o
./chibicc-gen2 tests > tmp.s
gcc -static -o tmp tmp.s extern.o
./tmp
clean:
rm -f chibicc *.o *~ tmp*
rm -rf chibicc chibicc-gen* *.o *~ tmp*
.PHONY: test clean

44
self.sh Executable file
View File

@ -0,0 +1,44 @@
#!/bin/bash -x
TMP=tmp-self
mkdir -p $TMP
expand() {
file=$1
cat <<EOF > $TMP/$1
typedef struct FILE FILE;
extern FILE *stdout;
extern FILE *stderr;
void *malloc(long size);
void *calloc(long nmemb, long size);
int *__errno_location();
char *strerror(int errnum);
FILE *fopen(char *pathname, char *mode);
long fread(void *ptr, long size, long nmemb, FILE *stream);
int feof(FILE *stream);
static void assert() {}
int strcmp(char *s1, char *s2);
EOF
grep -v '^#' chibi.h >> $TMP/$1
grep -v '^#' $1 >> $TMP/$1
sed -i 's/\bbool\b/_Bool/g' $TMP/$1
sed -i 's/\berrno\b/*__errno_location()/g' $TMP/$1
sed -i 's/\btrue\b/1/g; s/\bfalse\b/0/g;' $TMP/$1
sed -i 's/\bNULL\b/0/g' $TMP/$1
sed -i 's/, \.\.\.//g' $TMP/$1
./chibicc $TMP/$1 > $TMP/${1%.c}.s
gcc -c -o $TMP/${1%.c}.o $TMP/${1%.c}.s
}
cp *.c $TMP
for i in $TMP/*.c; do
gcc -I. -c -o ${i%.c}.o $i
done
expand main.c
expand type.c
gcc -static -o chibicc-gen2 $TMP/*.o

5
tests-extern Normal file
View File

@ -0,0 +1,5 @@
// -*- c -*-
int ext1;
int *ext2;
int static_fn() { return 5; }