Two patches are attached to help with cross-compiling with tinycc and
cross-compiling tinycc itself:

tcc-9999-library_path.patch to override system include and library search path
tcc-9999-makefile_cross.patch to address cross-compilation issues

Furthermore it helped to disable bounds-checker and backtrace options.

Since i maintain a complete distribution compiled with just tinycc containing
~500 builds with patches and hacks applied each i cannot refine all those
in detail to comply with various quality assurance acceptance criteria.

If anyone got an idea how to improve upon those two patches then suggestions are
welcome.

On 2025-04-08 08:36, Ludvig Christensen via Tinycc-devel wrote:
>    Hi everyone!
> 
>    I am currently working on my master's thesis on [1]diverse
>    double-compiling and have opted for using tcc.
> 
>    Right now I am trying to use x86_64-tcc (targeting x86_64 Linux) on
>    macOS 15.1 to compile a native tcc for x86_64 Linux.
>    The full build process is the following: (1) clang(16.0.6, target
>    x86_64-apple-darwin24.1.0) -> (2) x86_64-tcc (0.9.28rc, target x86_64
>    Linux) -> (3) tcc (0.9.28rc, target x86_64 Linux)
> 
>    I run into the problem when going from 2 to 3 where using: "./configure
>    --cc=x86_64-tcc ..." causes c2str from conftest.c to be compiled for
>    the cross target and thus not be able to run on the build os.
> 
>    Has anyone tried something like this and know of a good solution or
>    configuration options?
>    Otherwise I guess it should be possible to use a native(for build os)
>    c2str and omitting that compilation step in the makefile.
> 
>    Thanks and kind regards,
> 
>    Ludvig Christensen
>      __________________________________________________________________
> 
>    Observera att avsändaren inte är anställd vid Kungliga Tekniska
>    högskolan. KTH ansvarar inte för åsikter, påståenden eller
>    personuppgifter som förmedlas i detta meddelande.
>    Please note that the sender is not employed by KTH Royal Institute of
>    Technology. KTH is not responsible for the opinions, claims or personal
>    data conveyed in this message.
> 
> References
> 
>    1. https://dwheeler.com/trusting-trust/

> _______________________________________________
> Tinycc-devel mailing list
> Tinycc-devel@nongnu.org
> https://lists.nongnu.org/mailman/listinfo/tinycc-devel

diff --git a/libtcc.c b/libtcc.c
index f686feb5..c9c687c3 100644
--- a/libtcc.c
+++ b/libtcc.c
@@ -890,6 +890,7 @@ LIBTCCAPI void tcc_delete(TCCState *s1)
 
 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
 {
+    char *cpath;
 #ifdef CONFIG_TCC_PIE
     if (output_type == TCC_OUTPUT_EXE)
         output_type |= TCC_OUTPUT_DYN;
@@ -897,9 +898,17 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int 
output_type)
     s->output_type = output_type;
 
     if (!s->nostdinc) {
-        /* default include paths */
-        /* -isystem paths have already been handled */
-        tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
+        /* override include path with TCC_CPATH as stdinc */
+        /* helpful to sanitize cross-development includes */
+        cpath = getenv("TCC_CPATH");
+        if(cpath != NULL) {
+            tcc_add_sysinclude_path(s, cpath);
+        }
+        else {
+            /* default include paths */
+            /* -isystem paths have already been handled */
+            tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
+        }
     }
 
     if (output_type == TCC_OUTPUT_PREPROCESS) {
@@ -916,7 +925,7 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int 
output_type)
         return 0;
     }
 
-    tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
+//    tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
 
 #ifdef TCC_TARGET_PE
 # ifdef TCC_IS_NATIVE
@@ -928,8 +937,17 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int 
output_type)
     tcc_add_macos_sdkpath(s);
 # endif
 #else
+
+    char *crt_path = getenv("TCC_LIBRARY_PATH");
+    if(crt_path != NULL) {
+        tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, crt_path);
+    }
+    else {
+        /* paths for crt objects */
+        tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, 
CONFIG_TCC_CRTPREFIX);
+    }
+
     /* paths for crt objects */
-    tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
     if (output_type != TCC_OUTPUT_MEMORY && !s->nostdlib)
         tccelf_add_crtbegin(s);
 #endif
diff --git a/tcc.c b/tcc.c
index 838f41d9..b215c36f 100644
--- a/tcc.c
+++ b/tcc.c
@@ -221,18 +221,33 @@ static void set_environment(TCCState *s)
 {
     char * path;
 
-    path = getenv("C_INCLUDE_PATH");
-    if(path != NULL) {
-        tcc_add_sysinclude_path(s, path);
-    }
-    path = getenv("CPATH");
+    path = getenv("TCC_CPATH");
     if(path != NULL) {
         tcc_add_include_path(s, path);
     }
-    path = getenv("LIBRARY_PATH");
+    else {
+        path = getenv("C_INCLUDE_PATH");
+        if(path != NULL) {
+            tcc_add_sysinclude_path(s, path);
+        }
+        path = getenv("CPATH");
+        if(path != NULL) {
+            tcc_add_include_path(s, path);
+        }
+    }
+    path = getenv("TCC_LIBRARY_PATH");
     if(path != NULL) {
         tcc_add_library_path(s, path);
     }
+    else {
+        path = getenv("LIBRARY_PATH");
+        if(path != NULL) {
+            tcc_add_library_path(s, path);
+        }
+        else {
+            tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
+        }
+    }
 }
 
 static char *default_outputfile(TCCState *s, const char *first_file)
diff --git a/Makefile b/Makefile
index 2a7b774b..9df68db0 100644
--- a/Makefile
+++ b/Makefile
@@ -156,8 +156,8 @@ endif
 
 # --------------------------------------------
 
-T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)
-X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)
+T = $(or $(TCROSS_TARGET),$(NATIVE_TARGET),unknown)
+X = $(if $(TCROSS_TARGET),$(TCROSS_TARGET)-)
 
 DEFINES += $(DEF-$T)
 DEFINES += $(if $(ROOT-$T),-DCONFIG_SYSROOT="\"$(ROOT-$T)\"")
@@ -253,7 +253,7 @@ endif
 
 # convert "include/tccdefs.h" to "tccdefs_.h"
 %_.h : include/%.h conftest.c
-       $S$(CC) -DC2STR $(filter %.c,$^) -o c2str.exe && ./c2str.exe $< $@
+       TCC_LIBRARY_PATH="/usr/lib/tcc:/usr/lib" tcc -static -DC2STR $(filter 
%.c,$^) -o c2str.exe && ./c2str.exe $< $@
 
 # target specific object rule
 $(X)%.o : %.c $(LIBTCC_INC)
@@ -275,9 +275,9 @@ tcc$(EXESUF): tcc.o $(LIBTCC)
 # to the same goals and only remakes it once, but that doesn't work over
 # sub-makes like in this target)
 %-tcc$(EXESUF): $(TCCDEFS_H) FORCE
-       @$(MAKE) --no-print-directory $@ CROSS_TARGET=$* ONE_SOURCE=$(or 
$(ONE_SOURCE),yes)
+       @$(MAKE) --no-print-directory $@ TCROSS_TARGET=$* ONE_SOURCE=$(or 
$(ONE_SOURCE),yes)
 
-$(CROSS_TARGET)-tcc$(EXESUF): $(TCC_FILES)
+$(TCROSS_TARGET)-tcc$(EXESUF): $(TCC_FILES)
        $S$(CC) -o $@ $^ $(LIBS) $(LDFLAGS)
 
 # profiling version
@@ -319,7 +319,7 @@ libtcc1.a : tcc$(EXESUF) FORCE
 
 # Cross libtcc1.a
 %-libtcc1.a : %-tcc$(EXESUF) FORCE
-       @$(MAKE) -C lib CROSS_TARGET=$*
+       @$(MAKE) -C lib TCROSS_TARGET=$*
 
 .PRECIOUS: %-libtcc1.a
 FORCE:
diff --git a/lib/Makefile b/lib/Makefile
index 6524d829..4a183af1 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -3,19 +3,23 @@
 #
 
 TOP = ..
+
+# cross-compile, otherwise XTOP=TOP
+XTOP ?= ..
+
 include $(TOP)/Makefile
 VPATH = $(TOPSRC)/lib $(TOPSRC)/win32/lib
-T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)
-X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)
+T = $(or $(TCROSS_TARGET),$(NATIVE_TARGET),unknown)
+X = $(if $(TCROSS_TARGET),$(TCROSS_TARGET)-)
 XCFG = $(or $(findstring -win,$T),-unx)
 S = $(if $(findstring yes,$(SILENT)),@$(info * $@))
 
 TCC = $(TOP)/$(X)tcc$(EXESUF)
-XTCC ?= $(TOP)/$(X)tcc$(EXESUF)
+XTCC ?= $(XTOP)/$(X)tcc$(EXESUF)
 XCC = $(XTCC)
 XAR = $(XTCC) -ar
 XFLAGS-unx = -B$(TOPSRC)
-XFLAGS-win = -B$(TOPSRC)/win32 -I$(TOPSRC)/include
+XFLAGS-win = -B$(TOPSRC)/win32 -I$(TOPSRC)/include -I$(TOPSRC)/win32/include/ 
-I$(TOPSRC)/win32/include/winapi
 XFLAGS = $(XFLAGS$(XCFG))
 BFLAGS = -bt
 

Attachment: signature.asc
Description: Digital signature

_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to