Hello Thomas,

On Wed, 9 Apr 2014, Thomas Preud'homme wrote:

> I have a patch to build a single cross-compiler by using --targetcpu but 
> given the potential of breakage of the build script I prefer to post it 
> here for now and ask you to try building tcc with it, either natively or 
> all the cross compilers to see if anything break.
> 
> You can also try building a single compiler to see if it works for you 
> or take a look at the patch. Any feedback welcome.

No cake yet, there are multiple problems.  Some of them are fixed by the 
attached second version of that patch (which applies on top of current 
mob).  The changes are:

* inconsistent use of X64, x86_64 and x86-64.  This now matters because
  sometimes directory names implies make targets and variables now.  I've 
  settled on x86_64, because from that we can infer also C macros
* configure would unconditionally put #define TCC_TARGET_$ARCH in
  config.h.  When building cross compilers that would define the native 
  arch and the cross arch for the same executable.  So go back to adding 
  the right define in the Makefile
* The correct spelling of the make function is 'filter-out' (not with 
  underscore)
* libtcc1.c needs access to size_t also for cross environment
  (no idea why it would be there before the patch), so include <stddef.h>

With that patch the following works on an x86_64 host:

% ./configure && make && make test

and

% linux32 ./configure CC="gcc -m32" \
  && sed -i -e 's/lib64/lib/' config.mak \
  && linux32 make CFLAGS=-g CC="gcc -m32" \
  && linux32 make CFLAGS=-g CC="gcc -m32" -k test

I.e. the two native compilers for such host.  Some host compilers compile 
as well:

% ./configure --targetcpu=i386 && make

The resulting i386-tcc cannot build binaries (with "./i386-tcc -B./" 
because libtcc1.a isn't found (seems it's not searched in ./lib/i386).  
This works before the patch.  Similar this other cross compiler:

% linux32 ./configure CC="gcc -m32" --targetcpu=x86_64 && make

  Produces a 32bit x86_64-tcc executable (correct), which can't link 
  executables, because it searches in /usr/lib for crt[1in].o and doesn't 
  find libgcc1.a

Also:

% ./configure --enable-cross && make
...
make -C lib cross TARGET=i386-win32 PROG_CROSS=
make[1]: Entering directory `/matz/git/tinycc/lib'
../ -B../win32 -I../include -c libtcc1.c -o i386-win32/libtcc1.o -I..  -g  
-DTCC_TARGET_I386 -DTCC_TARGET_PE
make[1]: execvp: ../: Permission denied
make[1]: *** [i386-win32/libtcc1.o] Error 127
make[1]: Leaving directory `/matz/git/tinycc/lib'
make: *** [lib/i386-win32/libtcc1.a] Error 2


This points to a general problem in how the Makefile infers some names for 
the compiler to build the target lib.  You have:

lib/%/libtcc1.a : FORCE $(filter $*%,$(PROGS_CROSS))
        $(MAKE) -C lib cross TARGET=$* PROG_CROSS=$(filter $*%,$(PROGS_CROSS))

The '%' is i386, i386-win32, x86_64-win32, x86_64 and arm.  But in 
$(PROGS_CROSS) there is only "i386-w64-mingw32-tcc" which doesn't match 
"i386-win32".  With arm it's even worse, because there we have four cross 
compilers in PROGS_CROSS, that all match "arm%".

So, that needs some more work still.  I've attached the patch I have up to 
now (need to leave now).  The result of interdiff of both patches (yours 
vs. mine) is below to easier see what exactly I've changed relative to 
yours.


Thanks for starting to clean up the cross stuff.


Ciao,
Michael.

-----------------------------
diff -u b/Makefile b/Makefile
--- b/Makefile
+++ b/Makefile
@@ -67,6 +67,7 @@
 NATIVE_DEFINES_$(CONFIG_arm_eabi) += -DTCC_ARM_EABI
 NATIVE_DEFINES_$(CONFIG_arm_vfp) += -DTCC_ARM_VFP
 NATIVE_DEFINES += $(NATIVE_DEFINES_yes)
+NATIVE_DEFINES += -DTCC_TARGET_$(ARCH)
 
 ifeq ($(TOP),.)
 
@@ -74,7 +75,7 @@
 WIN32_CROSS = i386-w64-mingw32-tcc$(EXESUF)
 WIN64_CROSS = x86_64-w64-mingw32-tcc$(EXESUF)
 WINCE_CROSS = arm-wince-mingw32ce-tcc$(EXESUF)
-X64_CROSS = x86_64-linux-gnu-tcc$(EXESUF)
+X86_64_CROSS = x86_64-linux-gnu-tcc$(EXESUF)
 ARM_FPA_CROSS = arm-linux-fpa-tcc$(EXESUF)
 ARM_FPA_LD_CROSS = arm-linux-fpa-ld-tcc$(EXESUF)
 ARM_VFP_CROSS = arm-linux-gnu-tcc$(EXESUF)
@@ -88,7 +89,7 @@
 $(WIN32_CROSS)_LINK = i386-win32-tcc$(EXESUF)
 $(WIN64_CROSS)_LINK = x86_64-win32-tcc$(EXESUF)
 $(WINCE_CROSS)_LINK = arm-win32-tcc$(EXESUF)
-$(X64_CROSS)_LINK = x86_64-tcc$(EXESUF)
+$(X86_64_CROSS)_LINK = x86_64-tcc$(EXESUF)
 $(ARM_FPA_CROSS)_LINK = arm-fpa-tcc$(EXESUF)
 $(ARM_FPA_LD_CROSS)_LINK = arm-fpa-ld-tcc$(EXESUF)
 $(ARM_VFP_CROSS)_LINK = arm-vfp-tcc$(EXESUF)
@@ -107,7 +108,7 @@
 I386_LIBTCC1_CROSS=lib/i386/libtcc1.a
 WIN32_LIBTCC1_CROSS=lib/i386-win32/libtcc1.a
 WIN64_LIBTCC1_CROSS=lib/x86_64-win32/libtcc1.a
-X86_64_LIBTCC1_CROSS=lib/x86-64/libtcc1.a
+X86_64_LIBTCC1_CROSS=lib/x86_64/libtcc1.a
 ARM_LIBTCC1_CROSS=lib/arm/libtcc1.a
 
 ifdef CONFIG_WIN64
@@ -116,7 +117,7 @@
 ARCH=WIN32
 endif
 
-TARGET_ALL=WIN32 WIN64 I386 X64 ARM C67
+TARGET_ALL=WIN32 WIN64 I386 X86_64 ARM C67
 ifdef TARGET_ARCH
 NATIVE_FILES=
 PROGS_CROSS=$($(TARGET_ARCH)_CROSS)
@@ -125,8 +126,8 @@
 else
 PROGS=tcc$(EXESUF)
 NATIVE_FILES=$($(ARCH)_FILES)
-PROGS_CROSS=$(foreach CROSS_ARCH,$(filter_out 
$(ARCH),$(TARGET_ALL)),$($(CROSS_ARCH)_CROSS))
-LIBTCC1_CROSS=$(foreach CROSS_ARCH,$(filter_out 
$(ARCH),$(TARGET_ALL)),$($(CROSS_ARCH)_LIBTCC1_CROSS))
+PROGS_CROSS=$(foreach CROSS_ARCH,$(filter-out 
$(ARCH),$(TARGET_ALL)),$($(CROSS_ARCH)_CROSS))
+LIBTCC1_CROSS=$(foreach CROSS_ARCH,$(filter-out 
$(ARCH),$(TARGET_ALL)),$($(CROSS_ARCH)_LIBTCC1_CROSS))
 LIBTCC1=libtcc1.a
 endif
 PROGS_CROSS_LINK=$(foreach PROG_CROSS,$(PROGS_CROSS),$($(PROG_CROSS)_LINK))
@@ -160,8 +161,8 @@
 
 $(I386_CROSS): DEFINES = -DTCC_TARGET_I386 \
     -DCONFIG_TCCDIR="\"$(tccdir)/i386\""
-$(X64_CROSS): DEFINES = -DTCC_TARGET_X86_64 \
-    -DCONFIG_TCCDIR="\"$(tccdir)/x86-64\""
+$(X86_64_CROSS): DEFINES = -DTCC_TARGET_X86_64 \
+    -DCONFIG_TCCDIR="\"$(tccdir)/x86_64\""
 $(WIN32_CROSS): DEFINES = -DTCC_TARGET_I386 -DTCC_TARGET_PE \
     -DCONFIG_TCCDIR="\"$(tccdir)/win32\"" \
     -DCONFIG_TCC_LIBPATHS="\"{B}/lib/32;{B}/lib\""
@@ -178,7 +179,7 @@
 $(ARM_EABI_CROSS): DEFINES = -DTCC_TARGET_ARM -DTCC_ARM_EABI -DTCC_ARM_VFP
 
 $(I386_CROSS): $(I386_FILES)
-$(X64_CROSS): $(X86_64_FILES)
+$(X86_64_CROSS): $(X86_64_FILES)
 $(WIN32_CROSS): $(WIN32_FILES)
 $(WIN64_CROSS): $(WIN64_FILES)
 $(WINCE_CROSS): $(WINCE_FILES)
diff -u b/configure b/configure
--- b/configure
+++ b/configure
@@ -80,7 +80,7 @@
       cpu="i386"
     ;;
     x86_64|amd64)
-      cpu="x86-64"
+      cpu="x86_64"
     ;;
     arm*)
       case "$cpu" in
@@ -445,7 +445,6 @@
 if test "$build_cpu" != "$target_cpu" ; then
   echo TARGET_ARCH="$TARGET_CPU" >> config.mak
 fi
-echo "#define TCC_TARGET_$TARGET_CPU 1" >> $TMPH
 if test "$target_cpu" = "arm" ; then
   echo "#define TCC_ARM_VERSION $target_cpuver" >> $TMPH
 fi
only in patch2:
unchanged:
--- a/lib/libtcc1.c
+++ b/lib/libtcc1.c
@@ -28,6 +28,7 @@ the Free Software Foundation, 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.  
 */
 
+#include <stddef.h>
 #include <stdint.h>
 
 #define W_TYPE_SIZE   32
diff --git a/Makefile b/Makefile
index 4f18567..eb962f1 100644
--- a/Makefile
+++ b/Makefile
@@ -61,25 +61,21 @@ LIBTCC=libtcc.a
 LINK_LIBTCC=
 endif
 
-CONFIG_$(ARCH) = yes
-NATIVE_DEFINES_$(CONFIG_i386) += -DTCC_TARGET_I386
-NATIVE_DEFINES_$(CONFIG_x86-64) += -DTCC_TARGET_X86_64
 NATIVE_DEFINES_$(CONFIG_WIN32) += -DTCC_TARGET_PE
 NATIVE_DEFINES_$(CONFIG_uClibc) += -DTCC_UCLIBC
-NATIVE_DEFINES_$(CONFIG_arm) += -DTCC_TARGET_ARM
 NATIVE_DEFINES_$(CONFIG_arm_eabihf) += -DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT
 NATIVE_DEFINES_$(CONFIG_arm_eabi) += -DTCC_ARM_EABI
 NATIVE_DEFINES_$(CONFIG_arm_vfp) += -DTCC_ARM_VFP
 NATIVE_DEFINES += $(NATIVE_DEFINES_yes)
+NATIVE_DEFINES += -DTCC_TARGET_$(ARCH)
 
 ifeq ($(TOP),.)
 
-PROGS=tcc$(EXESUF)
 I386_CROSS = i386-linux-gnu-tcc$(EXESUF)
 WIN32_CROSS = i386-w64-mingw32-tcc$(EXESUF)
 WIN64_CROSS = x86_64-w64-mingw32-tcc$(EXESUF)
 WINCE_CROSS = arm-wince-mingw32ce-tcc$(EXESUF)
-X64_CROSS = x86_64-linux-gnu-tcc$(EXESUF)
+X86_64_CROSS = x86_64-linux-gnu-tcc$(EXESUF)
 ARM_FPA_CROSS = arm-linux-fpa-tcc$(EXESUF)
 ARM_FPA_LD_CROSS = arm-linux-fpa-ld-tcc$(EXESUF)
 ARM_VFP_CROSS = arm-linux-gnu-tcc$(EXESUF)
@@ -93,7 +89,7 @@ $(I386_CROSS)_LINK = i386-tcc$(EXESUF)
 $(WIN32_CROSS)_LINK = i386-win32-tcc$(EXESUF)
 $(WIN64_CROSS)_LINK = x86_64-win32-tcc$(EXESUF)
 $(WINCE_CROSS)_LINK = arm-win32-tcc$(EXESUF)
-$(X64_CROSS)_LINK = x86_64-tcc$(EXESUF)
+$(X86_64_CROSS)_LINK = x86_64-tcc$(EXESUF)
 $(ARM_FPA_CROSS)_LINK = arm-fpa-tcc$(EXESUF)
 $(ARM_FPA_LD_CROSS)_LINK = arm-fpa-ld-tcc$(EXESUF)
 $(ARM_VFP_CROSS)_LINK = arm-vfp-tcc$(EXESUF)
@@ -109,33 +105,30 @@ X86_64_FILES = $(CORE_FILES) x86_64-gen.c i386-asm.c x86_64-asm.h
 ARM_FILES = $(CORE_FILES) arm-gen.c
 C67_FILES = $(CORE_FILES) c67-gen.c tcccoff.c
 
+I386_LIBTCC1_CROSS=lib/i386/libtcc1.a
+WIN32_LIBTCC1_CROSS=lib/i386-win32/libtcc1.a
+WIN64_LIBTCC1_CROSS=lib/x86_64-win32/libtcc1.a
+X86_64_LIBTCC1_CROSS=lib/x86_64/libtcc1.a
+ARM_LIBTCC1_CROSS=lib/arm/libtcc1.a
+
 ifdef CONFIG_WIN64
-PROGS+=tiny_impdef$(EXESUF) tiny_libmaker$(EXESUF)
-NATIVE_FILES=$(WIN64_FILES)
-PROGS_CROSS=$(WIN32_CROSS) $(I386_CROSS) $(X64_CROSS) $(ARM_CROSS) $(C67_CROSS)
-LIBTCC1_CROSS=lib/i386-win32/libtcc1.a
-LIBTCC1=libtcc1.a
+ARCH=WIN64
 else ifdef CONFIG_WIN32
-PROGS+=tiny_impdef$(EXESUF) tiny_libmaker$(EXESUF)
-NATIVE_FILES=$(WIN32_FILES)
-PROGS_CROSS=$(WIN64_CROSS) $(I386_CROSS) $(X64_CROSS) $(ARM_CROSS) $(C67_CROSS)
-LIBTCC1_CROSS=lib/x86_64-win32/libtcc1.a
-LIBTCC1=libtcc1.a
-else ifeq ($(ARCH),i386)
-NATIVE_FILES=$(I386_FILES)
-PROGS_CROSS=$(X64_CROSS) $(WIN32_CROSS) $(WIN64_CROSS) $(ARM_CROSS) $(C67_CROSS)
-LIBTCC1_CROSS=lib/i386-win32/libtcc1.a lib/x86_64-win32/libtcc1.a
-LIBTCC1=libtcc1.a
-else ifeq ($(ARCH),x86-64)
-NATIVE_FILES=$(X86_64_FILES)
-PROGS_CROSS=$(I386_CROSS) $(WIN32_CROSS) $(WIN64_CROSS) $(ARM_CROSS) $(C67_CROSS)
-LIBTCC1_CROSS=lib/i386-win32/libtcc1.a lib/x86_64-win32/libtcc1.a lib/i386/libtcc1.a
-LIBTCC1=libtcc1.a
-else ifeq ($(ARCH),arm)
-NATIVE_FILES=$(ARM_FILES)
-PROGS_CROSS=$(I386_CROSS) $(X64_CROSS) $(WIN32_CROSS) $(WIN64_CROSS) $(C67_CROSS)
+ARCH=WIN32
+endif
+
+TARGET_ALL=WIN32 WIN64 I386 X86_64 ARM C67
+ifdef TARGET_ARCH
+NATIVE_FILES=
+PROGS_CROSS=$($(TARGET_ARCH)_CROSS)
+LIBTCC1_CROSS=$($(TARGET_ARCH)_LIBTCC1_CROSS)
+LIBTCC1=
+else
+PROGS=tcc$(EXESUF)
+NATIVE_FILES=$($(ARCH)_FILES)
+PROGS_CROSS=$(foreach CROSS_ARCH,$(filter-out $(ARCH),$(TARGET_ALL)),$($(CROSS_ARCH)_CROSS))
+LIBTCC1_CROSS=$(foreach CROSS_ARCH,$(filter-out $(ARCH),$(TARGET_ALL)),$($(CROSS_ARCH)_LIBTCC1_CROSS))
 LIBTCC1=libtcc1.a
-LIBTCC1_CROSS=lib/i386-win32/libtcc1.a lib/x86_64-win32/libtcc1.a lib/i386/libtcc1.a
 endif
 PROGS_CROSS_LINK=$(foreach PROG_CROSS,$(PROGS_CROSS),$($(PROG_CROSS)_LINK))
 
@@ -168,7 +161,8 @@ tcc_p$(EXESUF): $(NATIVE_FILES)
 
 $(I386_CROSS): DEFINES = -DTCC_TARGET_I386 \
     -DCONFIG_TCCDIR="\"$(tccdir)/i386\""
-$(X64_CROSS): DEFINES = -DTCC_TARGET_X86_64
+$(X86_64_CROSS): DEFINES = -DTCC_TARGET_X86_64 \
+    -DCONFIG_TCCDIR="\"$(tccdir)/x86_64\""
 $(WIN32_CROSS): DEFINES = -DTCC_TARGET_I386 -DTCC_TARGET_PE \
     -DCONFIG_TCCDIR="\"$(tccdir)/win32\"" \
     -DCONFIG_TCC_LIBPATHS="\"{B}/lib/32;{B}/lib\""
@@ -176,6 +170,8 @@ $(WIN64_CROSS): DEFINES = -DTCC_TARGET_X86_64 -DTCC_TARGET_PE \
     -DCONFIG_TCCDIR="\"$(tccdir)/win32\"" \
     -DCONFIG_TCC_LIBPATHS="\"{B}/lib/64;{B}/lib\""
 $(WINCE_CROSS): DEFINES = -DTCC_TARGET_PE
+$(ARM_CROSS): DEFINES = -DTCC_TARGET_ARM \
+    -DCONFIG_TCCDIR="\"$(tccdir)/arm\""
 $(C67_CROSS): DEFINES = -DTCC_TARGET_C67
 $(ARM_FPA_CROSS): DEFINES = -DTCC_TARGET_ARM
 $(ARM_FPA_LD_CROSS)$(EXESUF): DEFINES = -DTCC_TARGET_ARM -DLDOUBLE_SIZE=12
@@ -183,7 +179,7 @@ $(ARM_VFP_CROSS): DEFINES = -DTCC_TARGET_ARM -DTCC_ARM_VFP
 $(ARM_EABI_CROSS): DEFINES = -DTCC_TARGET_ARM -DTCC_ARM_EABI -DTCC_ARM_VFP
 
 $(I386_CROSS): $(I386_FILES)
-$(X64_CROSS): $(X86_64_FILES)
+$(X86_64_CROSS): $(X86_64_FILES)
 $(WIN32_CROSS): $(WIN32_FILES)
 $(WIN64_CROSS): $(WIN64_FILES)
 $(WINCE_CROSS): $(WINCE_FILES)
@@ -227,8 +223,8 @@ tiny_libmaker$(EXESUF): win32/tools/tiny_libmaker.c
 # TinyCC runtime libraries
 libtcc1.a : FORCE
 	$(MAKE) -C lib native
-lib/%/libtcc1.a : FORCE $(PROGS_CROSS)
-	$(MAKE) -C lib cross TARGET=$*
+lib/%/libtcc1.a : FORCE $(filter $*%,$(PROGS_CROSS))
+	$(MAKE) -C lib cross TARGET=$* PROG_CROSS=$(filter $*%,$(PROGS_CROSS))
 
 FORCE:
 
@@ -273,18 +269,24 @@ endif
 	mkdir -p "$(docdir)"
 	-$(INSTALL) -m644 tcc-doc.html "$(docdir)"
 ifdef CONFIG_CROSS
+	for libpath in $(LIBTCC1_CROSS) ; do \
+		libdir="$${libpath%/libtcc1.a}" ; \
+		$(INSTALL) -m644 "$$libdir" $(tccdir) ; \
+		cp -r "$(tccdir)/include" "$(tccdir)/$${libdir#lib/}" ; \
+	done
+ifneq (,$(findstring $(WIN32_LIBTCC1_CROSS),$(LIBTCC1_CROSS)))
 	mkdir -p "$(tccdir)/win32/lib/32"
-	mkdir -p "$(tccdir)/win32/lib/64"
-ifneq ($(ARCH),i386)
-	mkdir -p "$(tccdir)/i386"
-	$(INSTALL) -m644 lib/i386/libtcc1.a "$(tccdir)/i386"
-	cp -r "$(tccdir)/include" "$(tccdir)/i386"
-endif
-	$(INSTALL) -m644 $(top_srcdir)/win32/lib/*.def "$(tccdir)/win32/lib"
 	$(INSTALL) -m644 lib/i386-win32/libtcc1.a "$(tccdir)/win32/lib/32"
+endif
+ifneq (,$(findstring $(WIN64_LIBTCC1_CROSS),$(LIBTCC1_CROSS)))
+	mkdir -p "$(tccdir)/win32/lib/64"
 	$(INSTALL) -m644 lib/x86_64-win32/libtcc1.a "$(tccdir)/win32/lib/64"
-	cp -r $(top_srcdir)/win32/include/. "$(tccdir)/win32/include"
-	cp -r "$(tccdir)/include" "$(tccdir)/win32"
+endif
+	if test -d $(tccdir)/win32 ; \
+		$(INSTALL) -m644 $(top_srcdir)/win32/lib/*.def "$(tccdir)/win32/lib" ; \
+		cp -r $(top_srcdir)/win32/include/. "$(tccdir)/win32/include" ; \
+		cp -r "$(tccdir)/include" "$(tccdir)/win32" ; \
+	fi
 endif
 
 uninstall:
@@ -297,9 +299,7 @@ uninstall:
 	rm -fv "$(libdir)/libtcc.so*"
 	rm -rf "$(tccdir)/win32"
 	-rmdir $(tccdir)/include
-ifneq ($(ARCH),i386)
-	rm -rf "$(tccdir)/i386"
-endif
+	$(foreach LIBDIR,$(dir $(LIBTCC1_CROSS)),rm -rf $(notdir $(LIBDIR)))
 else
 # on windows
 install: $(PROGS) $(TCCLIBS) $(TCCDOCS)
@@ -318,10 +318,14 @@ install: $(PROGS) $(TCCLIBS) $(TCCDOCS)
 	$(INSTALL) -m644 $(top_srcdir)/libtcc.h $(LIBTCC_EXTRA) "$(tccdir)/libtcc"
 	$(INSTALL) -m644 $(LIBTCC) $(tccdir)
 ifdef CONFIG_CROSS
+ifneq (,$(findstring $(WIN32_LIBTCC1_CROSS),$(LIBTCC1_CROSS)))
 	mkdir -p "$(tccdir)/lib/32"
+	$(INSTALL) -m644 lib/i386-win32/libtcc1.a "$(tccdir)/lib/32"
+endif
+ifneq (,$(findstring $(WIN64_LIBTCC1_CROSS),$(LIBTCC1_CROSS)))
 	mkdir -p "$(tccdir)/lib/64"
-	-$(INSTALL) -m644 lib/i386-win32/libtcc1.a "$(tccdir)/lib/32"
-	-$(INSTALL) -m644 lib/x86_64-win32/libtcc1.a "$(tccdir)/lib/64"
+	$(INSTALL) -m644 lib/x86_64-win32/libtcc1.a "$(tccdir)/lib/64"
+endif
 endif
 
 uninstall:
diff --git a/configure b/configure
index 8c44e5c..ba5e750 100755
--- a/configure
+++ b/configure
@@ -44,10 +44,11 @@ tcc_sysincludepaths=""
 tcc_libpaths=""
 tcc_crtprefix=""
 tcc_elfinterp=""
-tcc_lddir=
-confvars=
+tcc_lddir=""
+confvars=""
+target_cpu=""
 
-cpu=`uname -m`
+build_cpu=`uname -m`
 
 # OS specific
 targetos=`uname -s`
@@ -70,46 +71,52 @@ if test -z "$source_path" -o "$source_path" = "." ; then
     source_path_used="no"
 fi
 
-case "$cpu" in
-  i386|i486|i586|i686|i86pc|BePC|i686-AT386)
-    cpu="x86"
-  ;;
-  x86_64|amd64)
-    cpu="x86-64"
-  ;;
-  arm*)
-    case "$cpu" in
-      arm|armv4l)
-        cpuver=4
-      ;;
-      armv5tel|armv5tejl)
-        cpuver=5
-      ;;
-      armv6j|armv6l)
-        cpuver=6
-      ;;
-      armv7a|armv7l)
-        cpuver=7
-      ;;
-    esac
-    cpu="armv4l"
-  ;;
-  alpha)
-    cpu="alpha"
-  ;;
-  "Power Macintosh"|ppc|ppc64)
-    cpu="powerpc"
-  ;;
-  mips)
-    cpu="mips"
-  ;;
-  s390)
-    cpu="s390"
-  ;;
-  *)
-    cpu="unknown"
-  ;;
-esac
+classify_cpu ()
+{
+  cpu="$1"
+
+  case "$cpu" in
+    x86|i386|i486|i586|i686|i86pc|BePC|i686-AT386)
+      cpu="i386"
+    ;;
+    x86_64|amd64)
+      cpu="x86_64"
+    ;;
+    arm*)
+      case "$cpu" in
+        arm|armv4l)
+          cpuver=4
+        ;;
+        armv5tel|armv5tejl)
+          cpuver=5
+        ;;
+        armv6j|armv6l)
+          cpuver=6
+        ;;
+        armv7a|armv7l)
+          cpuver=7
+        ;;
+      esac
+      cpu="arm"
+    ;;
+    alpha)
+      cpu="alpha"
+    ;;
+    "Power Macintosh"|ppc|ppc64|powerpc)
+      cpu="ppc"
+    ;;
+    mips)
+      cpu="mips"
+    ;;
+    s390)
+      cpu="s390"
+    ;;
+    *)
+      echo "Unsupported CPU: $cpu"
+      exit 1
+    ;;
+  esac
+}
 
 for opt do
   eval opt=\"$opt\"
@@ -140,14 +147,6 @@ for opt do
   ;;
   --cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2`
   ;;
-  --cc=*) cc=`echo $opt | cut -d '=' -f 2`
-  ;;
-  --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}"
-  ;;
-  --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}"
-  ;;
-  --extra-libs=*) extralibs=${opt#--extra-libs=}
-  ;;
   --sysincludepaths=*) tcc_sysincludepaths=`echo $opt | cut -d '=' -f 2`
   ;;
   --libpaths=*) tcc_libpaths=`echo $opt | cut -d '=' -f 2`
@@ -156,13 +155,23 @@ for opt do
   ;;
   --elfinterp=*) tcc_elfinterp=`echo $opt | cut -d '=' -f 2`
   ;;
-  --cpu=*) cpu=`echo $opt | cut -d '=' -f 2`
+  --targetcpu=*) target_cpu=`echo $opt | cut -d '=' -f 2`
+  ;;
+  --cc=*) cc=`echo $opt | cut -d '=' -f 2`
+  ;;
+  --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}"
+  ;;
+  --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}"
+  ;;
+  --extra-libs=*) extralibs=${opt#--extra-libs=}
+  ;;
+  --cpu=*) build_cpu=`echo $opt | cut -d '=' -f 2`
   ;;
   --enable-gprof) gprof="yes"
   ;;
-  --enable-mingw32) mingw32="yes" ; cross_prefix="i686-pc-mingw32-" ; cpu=x86
+  --enable-mingw32) mingw32="yes" ; cross_prefix="i686-pc-mingw32-" ; build_cpu=i386
   ;;
-  --enable-cygwin) mingw32="yes" ; cygwin="yes" ; cross_prefix="mingw32-" ; cpu=x86
+  --enable-cygwin) mingw32="yes" ; cygwin="yes" ; cross_prefix="mingw32-" ; build_cpu=i386
   ;;
   --enable-cross) build_cross="yes"
   ;;
@@ -185,6 +194,17 @@ for opt do
   esac
 done
 
+classify_cpu "$build_cpu"
+build_cpu="$cpu"
+
+test -z "$target_cpu" && target_cpu=$build_cpu
+classify_cpu "$target_cpu"
+target_cpu="$cpu"
+target_cpuver="$cpuver"
+if test "$build_cpu" != "$target_cpu" ; then
+  build_cross="yes"
+fi
+
 # Checking for CFLAGS
 if test -z "$CFLAGS"; then
     CFLAGS="-Wall -g -O2"
@@ -270,6 +290,7 @@ Advanced options (experts only):
   --cc=CC                  use C compiler CC [$cc]
   --extra-cflags=          specify compiler flags [$CFLAGS]
   --extra-ldflags=         specify linker options []
+  --cpu=CPU                CPU [$build_cpu]
   --strip-binaries         strip symbol tables from resulting binaries
   --disable-static         make libtcc.so instead of libtcc.a
   --disable-rpath          disable use of -rpath with the above
@@ -283,6 +304,7 @@ Advanced options (experts only):
   --libpaths=...           specify system library paths, colon separated
   --crtprefix=...          specify locations of crt?.o, colon separated
   --elfinterp=...          specify elf interpreter
+  --targetcpu=...          Target CPU [CPU]
 EOF
 #echo "NOTE: The object files are build at the place where configure is launched"
 exit 1
@@ -294,7 +316,7 @@ strip="${cross_prefix}${strip}"
 
 CONFTEST=./conftest$EXESUF
 
-if test -z "$cross_prefix" ; then
+if test -z "$cross_prefix" -a "$build_cpu" = "$target_cpu" ; then
   if ! $cc -o $CONFTEST $source_path/conftest.c 2>/dev/null ; then
     echo "configure: error: '$cc' failed to compile conftest.c."
   else
@@ -310,7 +332,7 @@ if test -z "$cross_prefix" ; then
         tcc_lddir="lib64"
       fi
 
-      if test "$cpu" = "armv4l" ; then
+      if test "$build_cpu" = "arm" ; then
         if test "${triplet%eabihf}" != "$triplet" ; then
            confvars="$confvars arm_eabihf"
         elif test "${triplet%eabi}" != "$triplet" ; then
@@ -339,8 +361,8 @@ if test -z "$cross_prefix" ; then
   fi
 else
   # if cross compiling, cannot launch a program, so make a static guess
-  case $cpu in
-    powerpc|mips|s390)  bigendian=yes;;
+  case $build_cpu in
+    ppc|mips|s390)  bigendian=yes;;
   esac
 fi
 
@@ -355,8 +377,9 @@ Doc directory       $docdir
 Target root prefix  $sysroot
 Source path      $source_path
 C compiler       $cc
+Build CPU        $build_cpu
+Target CPU       $target_cpu
 Target OS        $targetos
-CPU              $cpu
 Big Endian       $bigendian
 gprof enabled    $gprof
 cross compilers  $build_cross
@@ -415,31 +438,15 @@ print_mak CONFIG_MULTIARCHDIR "$multiarch_triplet"
 echo "#define GCC_MAJOR $gcc_major" >> $TMPH
 echo "#define GCC_MINOR $gcc_minor" >> $TMPH
 
-if test "$cpu" = "x86" ; then
-  echo "ARCH=i386" >> config.mak
-  echo "#define HOST_I386 1" >> $TMPH
-elif test "$cpu" = "x86-64" ; then
-  echo "ARCH=x86-64" >> config.mak
-  echo "#define HOST_X86_64 1" >> $TMPH
-elif test "$cpu" = "armv4l" ; then
-  echo "ARCH=arm" >> config.mak
-  echo "#define HOST_ARM 1" >> $TMPH
-  echo "#define TCC_ARM_VERSION $cpuver" >> $TMPH
-elif test "$cpu" = "powerpc" ; then
-  echo "ARCH=ppc" >> config.mak
-  echo "#define HOST_PPC 1" >> $TMPH
-elif test "$cpu" = "mips" ; then
-  echo "ARCH=mips" >> config.mak
-  echo "#define HOST_MIPS 1" >> $TMPH
-elif test "$cpu" = "s390" ; then
-  echo "ARCH=s390" >> config.mak
-  echo "#define HOST_S390 1" >> $TMPH
-elif test "$cpu" = "alpha" ; then
-  echo "ARCH=alpha" >> config.mak
-  echo "#define HOST_ALPHA 1" >> $TMPH
-else
-  echo "Unsupported CPU"
-  exit 1
+BUILD_CPU="$(echo $build_cpu | tr '[:lower:]-' '[:upper:]_')"
+echo ARCH="$BUILD_CPU" >> config.mak
+echo "#define HOST_$BUILD_CPU 1" >> $TMPH
+TARGET_CPU="$(echo $target_cpu | tr '[:lower:]-' '[:upper:]_')"
+if test "$build_cpu" != "$target_cpu" ; then
+  echo TARGET_ARCH="$TARGET_CPU" >> config.mak
+fi
+if test "$target_cpu" = "arm" ; then
+  echo "#define TCC_ARM_VERSION $target_cpuver" >> $TMPH
 fi
 
 echo "TARGETOS=$targetos" >> config.mak
diff --git a/lib/Makefile b/lib/Makefile
index e9e12f1..9a14e3d 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -13,19 +13,19 @@ ifndef TARGET # native library
  ifdef CONFIG_WIN32
   TARGET = i386-win32
  else
- ifeq ($(ARCH),i386)
+ ifeq ($(ARCH),I386)
   TARGET = i386
   ifneq ($(TARGETOS),Darwin)
    XCC = $(CC)
   endif
  else
- ifeq ($(ARCH),x86-64)
+ ifeq ($(ARCH),X86_64)
   TARGET = x86_64
   ifneq ($(TARGETOS),Darwin)
    XCC = $(CC)
   endif
  else
- ifeq ($(ARCH),arm)
+ ifeq ($(ARCH),ARM)
   TARGET = arm
   XCC = $(CC)
  endif
@@ -42,7 +42,7 @@ native : ../libtcc1.a
 cross : $(DIR)/libtcc1.a
 
 native : TCC = $(TOP)/tcc$(EXESUF)
-cross : TCC = $(TOP)/$(TARGET)-tcc$(EXESUF)
+cross : TCC = $(TOP)/$(PROG_CROSS)$(EXESUF)
 
 I386_O = libtcc1.o alloca86.o alloca86-bt.o $(BCHECK_O)
 X86_64_O = libtcc1.o alloca86_64.o
diff --git a/lib/libtcc1.c b/lib/libtcc1.c
index 284965e..ed496b1 100644
--- a/lib/libtcc1.c
+++ b/lib/libtcc1.c
@@ -28,6 +28,7 @@ the Free Software Foundation, 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.  
 */
 
+#include <stddef.h>
 #include <stdint.h>
 
 #define W_TYPE_SIZE   32
diff --git a/tests/Makefile b/tests/Makefile
index ee0eafe..b17316e 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -29,7 +29,7 @@ endif
 # btest -- works on i386 (including win32)
 
 # bounds-checking is supported only on i386
-ifneq ($(ARCH),i386)
+ifneq ($(ARCH),I386)
  TESTS := $(filter-out $(BTESTS),$(TESTS))
 endif
 ifdef CONFIG_WIN32
@@ -38,8 +38,8 @@ endif
 ifeq ($(TARGETOS),Darwin)
  TESTS := $(filter-out hello-exe test3 $(BTESTS),$(TESTS))
 endif
-ifeq ($(ARCH),i386)
-else ifneq ($(ARCH),x86-64)
+ifeq ($(ARCH),I386)
+else ifneq ($(ARCH),X86_64)
  TESTS := $(filter-out vla_test-run,$(TESTS))
 endif
 
_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to