vnmakarov / mir

A lightweight JIT compiler based on MIR (Medium Internal Representation) and C11 JIT compiler and interpreter based on MIR
MIT License
2.24k stars 147 forks source link

Issues when trying to compile/build with cosmopolitan libc #297

Open mingodad opened 1 year ago

mingodad commented 1 year ago

I understand that this use case is not the one intended by this project but it seems to reveal some edge cases when trying to compile/build with cosmopolitan libc (https://github.com/jart/cosmopolitan).

I manged to compile/build c2m using cosmopolitan libc with this shell script:

# run gcc compiler in freestanding mode
base_dir="mir-source-path"
do_compile() {
    gcc -g -Os -static -fno-pie -no-pie -nostdlib -nostdinc -pipe \
      -fno-omit-frame-pointer -pg -mnop-mcount -mno-tls-direct-seg-refs \
      -include cosmopolitan.h \
      -DWHITHOUT_STD_HEADERS -I$base_dir \
      -Wno-abi -fsigned-char -fno-tree-sra -fno-ipa-cp-clone -DxMIR_PARALLEL_GEN \
      -c -o $2 $1
}
do_link() {
    gcc -g -Os -static -fno-pie -no-pie -nostdlib -nostdinc -pipe \
      -fno-omit-frame-pointer -pg -mnop-mcount -mno-tls-direct-seg-refs \
      -DBASECC='"$(GCC)"' \
      -o $1.com.dbg $2 -Wl,--gc-sections -fuse-ld=bfd -Wl,--gc-sections \
      -Wl,-T,ape.lds crt.o ape-no-modify-self.o cosmopolitan.a
    objcopy -S -O binary $1.com.dbg $1.com
}

do_compile $base_dir/mir.c mir.o
do_compile $base_dir/mir-gen.c mir-gen.o
do_compile $base_dir/c2mir/c2mir.c c2mir.o
do_compile $base_dir/c2mir/c2mir-driver.c c2mir-driver.o

do_link c2m "mir.o mir-gen.o c2mir.o c2mir-driver.o"

And adding a conditional macro wrapper around standard headers like this:

/* This file is a part of MIR project.
   Copyright (C) 2018-2021 Vladimir Makarov <vmakarov.gcc@gmail.com>.
*/

#ifndef MIR_H

#define MIR_H

#ifdef __cplusplus
extern "C" {
#endif

#if defined(_WIN32) && !defined(_WIN64)
#error "MIR does not work on 32-bit Windows"
#endif

#ifndef WHITHOUT_STD_HEADERS ///wrapper to conditionally exclude
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#endif ///wrapper end
#include "mir-dlist.h"
#include "mir-varr.h"
#include "mir-htab.h"

Then when I try to compile one example using cosmopolitan.h (even with the default c2m) I'm getting several warning/errors like:

./ape.elf ./c2m.com exprtk_functional_test-small.c
...
./cosmopolitan.h:363:25: syntax error on identifier (expected ';'):
./cosmopolitan.h:363:25: syntax error on identifier (expected '<declarator>'):
./cosmopolitan.h:375:23: syntax error on identifier (expected ';'):
./cosmopolitan.h:375:23: syntax error on identifier (expected '<declarator>'):
./cosmopolitan.h:395:9: syntax error on struct (expected '<declarator>'):
./cosmopolitan.h:1728:18: syntax error on const (expected 'identifier'):
./cosmopolitan.h:1728:18: syntax error on const (expected '<declarator>'):
./cosmopolitan.h:1944:16: syntax error on ( (expected '<declarator>'):
./cosmopolitan.h:1945:16: syntax error on ( (expected '<declarator>'):
./cosmopolitan.h:1947:12: syntax error on void (expected 'identifier'):
...

Test file:

#include "cosmopolitan.h"
int main(int argc, char *argv[]) { 
  printf("hello world\n");
  return 0;
}