From 8826d32912b0c33bf96510191d544945c59d152a Mon Sep 17 00:00:00 2001 From: Fierelier Date: Thu, 22 Feb 2024 19:26:06 +0100 Subject: [PATCH] Add tinycc bootstrap --- tinycc-d2f8ceac/compile.c | 289 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 289 insertions(+) create mode 100644 tinycc-d2f8ceac/compile.c diff --git a/tinycc-d2f8ceac/compile.c b/tinycc-d2f8ceac/compile.c new file mode 100644 index 0000000..2d40945 --- /dev/null +++ b/tinycc-d2f8ceac/compile.c @@ -0,0 +1,289 @@ +// Put this script in tinycc, compile it, then run it. +#ifndef ENV_CC + #define ENV_CC "tcc" +#endif + +#ifndef ENV_CFLAGS + #define ENV_CFLAGS "-D__MTC_NONE" +#endif + +#include +#include +#include +#include + +#if PLATFORM_LINUX + #define ENV_PLATFORM "linux" + #include + #include +#elif PLATFORM_WINDOWS + #define ENV_PLATFORM "windows" + #include + #include +#else + #warning "ERROR: Please define a platform with -DPLATFORM_*." + #warning "" + #warning "Available platforms:" + #warning "* LINUX" + #warning "* WINDOWS" + #warning "" + #warning "Example: -DPLATFORM_LINUX" + #define ENV_QUIT +#endif + +#if ARCH_I386 + #if PLATFORM_LINUX + #define ARCH_TRIPLET "i386-linux-gnu" + #endif + #define ARCH_NAME "I386" +#elif ARCH_X86_64 + #if PLATFORM_LINUX + #define ARCH_TRIPLET "x86_64-linux-gnu" + #endif + #define ARCH_NAME "X86_64" +#else + #warning "ERROR: Please define an architecture with -DARCH_*." + #warning "" + #warning "Available architectures:" + #warning "* I386" + #warning "* X86_64" + #warning "" + #warning "Example: -DARCH_I386" + #define ENV_QUIT +#endif + +#if ENV_QUIT + #error "Compilation failed." +#endif + +void system_error(const char * command) { + printf("* %s\n",command); + int code = system(command); + if (code != 0) { + printf("Error %d, quitting.\n",code); + exit(code); + } +} + +int _call_proc(char * argv[]) { + #if PLATFORM_LINUX + pid_t pid; + int status; + pid = fork(); + + if (pid == -1) { + return -1; + } + + if (pid == 0) { // Child process + execvp(argv[0],argv); + // execvp only returns if an error occurs. + return -1; + } else { // Parent process + waitpid(pid,&status,0); + if (WIFEXITED(status) != 1) { return -1; } + return WEXITSTATUS(status); + } + + return -1; + #elif PLATFORM_WINDOWS + char * command; + long commandLength = 0; + int i; + for (i = 0; argv[i] != NULL; i++) { + commandLength += strlen(argv[i]) + 3; + int si; + for (si = 0; argv[i][si] != 0; si++) { + if (argv[i][si] == '"') { + commandLength++; + } + } + } + + commandLength += 1; + command = malloc(commandLength); + command[0] = 0; + + for (i = 0; argv[i] != NULL; i++) { + int si; + strcat(command,"\""); + for (si = 0; argv[i][si] != 0; si++) { + if (argv[i][si] == '"') { + strcat(command,"\\"); + } + strncat(command,(char *)&argv[i][si],1); + } + + strcat(command,"\" "); + } + command[commandLength - 2] = 0; + + // windows-specific section + STARTUPINFO si; + PROCESS_INFORMATION pi; + ZeroMemory(&si,sizeof(si)); + si.cb = sizeof(si); + ZeroMemory(&pi,sizeof(pi)); + + BOOL b = CreateProcess(NULL,command,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi); + free(command); + if (b == 0) { + return -1; + } + WaitForSingleObject(pi.hProcess,INFINITE); + DWORD status; + if (!GetExitCodeProcess(pi.hProcess,&status)) { + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + return -1; + } + + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + return status; + #endif +} + +int call_proc(char * file, ...) { + char ** argv = malloc(sizeof(char *)); + argv[0] = file; + + int i = 1; + char * str = file; + va_list args; + va_start(args,file); + + while (str != NULL) { + str = va_arg(args, char *); + argv = realloc(argv,sizeof(char *) * (i + 1)); + argv[i] = str; + i++; + } + + int code = _call_proc(argv); + free(argv); + va_end(args); + return code; +} + +void call_proc_error(char * file, ...) { + char ** argv = malloc(sizeof(char *)); + argv[0] = file; + + int i = 1; + char * str = file; + va_list args; + va_start(args,file); + + while (str != NULL) { + str = va_arg(args, char *); + argv = realloc(argv,sizeof(char *) * (i + 1)); + argv[i] = str; + i++; + } + + printf("* "); + int di; + for (di = 0; argv[di] != NULL; di++) { + printf("\""); + printf("%s",argv[di]); + printf("\" "); + } + printf("\n"); + fflush(stdout); + + int code = _call_proc(argv); + free(argv); + va_end(args); + if (code != 0) { + printf("Error %d, quitting.\n",code); + exit(code); + } +} + +int main (int argc, const char * argv[]) { + /*printf("* ENV_PLATFORM: '"ENV_PLATFORM"'\n"); + printf("* ENV_CC: '"ENV_CC"'\n"); + printf("* ENV_CFLAGS: '"ENV_CFLAGS"'\n"); + printf("\n");*/ + + #if PLATFORM_LINUX + call_proc_error(ENV_CC,"-o","tcc.o","-c","tcc.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-DTCC_GITHASH=\"2024-02-19 mob@d2f8ceac\"","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","libtcc.o","-c","libtcc.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-DC2STR","conftest.c","-o","c2str.exe",NULL); + call_proc_error("./c2str.exe","include/tccdefs.h","tccdefs_.h",NULL); + call_proc_error(ENV_CC,"-o","tccpp.o","-c","tccpp.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccgen.o","-c","tccgen.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccdbg.o","-c","tccdbg.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccelf.o","-c","tccelf.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccasm.o","-c","tccasm.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccrun.o","-c","tccrun.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","x86_64-gen.o","-c","x86_64-gen.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","x86_64-link.o","-c","x86_64-link.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","i386-asm.o","-c","i386-asm.c","-DCONFIG_TRIPLET=\""ARCH_TRIPLET"\"","-DTCC_TARGET_"ARCH_NAME,"-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-shared","-o","libtcc.a","libtcc.o","tccpp.o","tccgen.o","tccdbg.o","tccelf.o","tccasm.o","tccrun.o","x86_64-gen.o","x86_64-link.o","i386-asm.o",NULL); + call_proc_error(ENV_CC,"-o","tcc","tcc.o","libtcc.a","-lm","-ldl","-lpthread",NULL); + + + chdir("lib"); + call_proc_error("../tcc","-c","libtcc1.c","-o","libtcc1.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","alloca.S","-o","alloca.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","alloca-bt.S","-o","alloca-bt.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","stdatomic.c","-o","stdatomic.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","atomic.S","-o","atomic.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","builtin.c","-o","builtin.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","tcov.c","-o","tcov.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","va_list.c","-o","va_list.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","dsohandle.c","-o","dsohandle.o","-B..","-I..",NULL); + call_proc_error("../tcc","-ar","rcs","../libtcc1.a","libtcc1.o","alloca.o","alloca-bt.o","stdatomic.o","atomic.o","builtin.o","tcov.o","va_list.o","dsohandle.o",NULL); + call_proc_error("../tcc","-c","bt-exe.c","-o","../bt-exe.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","bt-log.c","-o","../bt-log.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","runmain.c","-o","../runmain.o","-B..","-I..",NULL); + call_proc_error("../tcc","-c","bcheck.c","-o","../bcheck.o","-B..","-I..","-bt",NULL); + chdir(".."); + #endif + + #if PLATFORM_WINDOWS + call_proc_error(ENV_CC,"-o","tcc.o","-c","tcc.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DONE_SOURCE=0","-DTCC_GITHASH=\"2024-02-22 master@9546894\"","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","libtcc.o","-c","libtcc.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-DC2STR","conftest.c","-o","c2str.exe",NULL); + call_proc_error("./c2str.exe","include/tccdefs.h","tccdefs_.h",NULL); + call_proc_error(ENV_CC,"-o","tccpp.o","-c","tccpp.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccgen.o","-c","tccgen.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccdbg.o","-c","tccdbg.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccelf.o","-c","tccelf.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccasm.o","-c","tccasm.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccrun.o","-c","tccrun.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","i386-gen.o","-c","i386-gen.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","i386-link.o","-c","i386-link.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","i386-asm.o","-c","i386-asm.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-o","tccpe.o","-c","tccpe.c","-DTCC_TARGET_"ARCH_NAME,"-DTCC_TARGET_PE","-DLIBTCC_AS_DLL","-DONE_SOURCE=0","-Wall","-O2","-Wdeclaration-after-statement","-fno-strict-aliasing","-Wno-pointer-sign","-Wno-sign-compare","-Wno-unused-result","-Wno-format-truncation","-Wno-stringop-truncation","-I.",NULL); + call_proc_error(ENV_CC,"-shared","-o","libtcc.dll","libtcc.o","tccpp.o","tccgen.o","tccdbg.o","tccelf.o","tccasm.o","tccrun.o","i386-gen.o","i386-link.o","i386-asm.o","tccpe.o","-static","-s",NULL); + call_proc_error(ENV_CC,"-o","tcc.exe","tcc.o","libtcc.dll","-static","-s",NULL); + call_proc_error("./tcc.exe","-impdef","libtcc.dll","-o","libtcc.def",NULL); + chdir("lib"); + call_proc_error("../tcc.exe","-c","libtcc1.c","-o","libtcc1.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","alloca.S","-o","alloca.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","alloca-bt.S","-o","alloca-bt.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","stdatomic.c","-o","stdatomic.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","atomic.S","-o","atomic.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","builtin.c","-o","builtin.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","tcov.c","-o","tcov.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","../win32/lib/chkstk.S","-o","chkstk.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","../win32/lib/crt1.c","-o","crt1.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","../win32/lib/crt1w.c","-o","crt1w.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","../win32/lib/wincrt1.c","-o","wincrt1.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","../win32/lib/wincrt1w.c","-o","wincrt1w.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","../win32/lib/dllcrt1.c","-o","dllcrt1.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","../win32/lib/dllmain.c","-o","dllmain.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-ar","rcs","../libtcc1.a","libtcc1.o","alloca.o","alloca-bt.o","stdatomic.o","atomic.o","builtin.o","tcov.o","chkstk.o","crt1.o","crt1w.o","wincrt1.o","wincrt1w.o","dllcrt1.o","dllmain.o",NULL); + call_proc_error("../tcc.exe","-c","bt-exe.c","-o","../bt-exe.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","bt-log.c","-o","../bt-log.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","runmain.c","-o","../runmain.o","-B../win32","-I../include","-I..",NULL); + call_proc_error("../tcc.exe","-c","bcheck.c","-o","../bcheck.o","-B../win32","-I../include","-I..","-bt",NULL); + call_proc_error("../tcc.exe","-c","bt-dll.c","-o","../bt-dll.o","-B../win32","-I../include","-I..",NULL); + chdir(".."); + #endif + + return 0; +}