tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tinycc-devel] Enabling TinyCC to Create Empty Archives


From: Ziyao
Subject: [Tinycc-devel] Enabling TinyCC to Create Empty Archives
Date: Mon, 4 Apr 2022 11:03:11 +0800
User-agent: Mutt/1.9.4 (2018-02-28)

Hi list,

On my machine (x86_64 Ubuntu 18.04LTS,GNU ar 2.30),it is OK to create an
empty archive with
        ar rc libempty.a
This will create libempty.a which only contains an ar archive header.

But TinyCC refuses to do that:
        tcc -ar rc libempty.a   # This will print the usage
I made a patch for that.

musl libc uses this feature to create dummy libraries(it combines all
libraries into a single file,rather than many libraries like libc.a,libm.a
etc.Dummy libraries are used to avoid linking errors when using common
linking flags like -lm).

I do not know whether there are other projects using this feature.But I
think it is a good idea to keep compatible with GNU ar.

Best wishes,
Ziyao

---------

diff --git a/tcctools.c b/tcctools.c
index 4567b81..7c643dc 100644
--- a/tcctools.c
+++ b/tcctools.c
@@ -61,7 +61,7 @@ static int contains_any(const char *s, const char *list) {
 }

 static int ar_usage(int ret) {
-    fprintf(stderr, "usage: tcc -ar [rcsv] lib file...\n");
+    fprintf(stderr, "usage: tcc -ar [rcsv] lib [file...]\n");
     fprintf(stderr, "create library ([abdioptxN] not supported).\n");
     return ret;
 }
@@ -115,8 +115,9 @@ ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
         }
     }

-    if (!i_obj)  // i_obj implies also i_lib. we require both.
+    if (!i_lib)  // i_obj implies also i_lib.
         ret = 1;
+    i_obj = i_obj ? i_obj : argc;  // An empty archive will be generated if no 
input file is given

     if (ret == 1)
         return ar_usage(ret);
@@ -242,6 +243,9 @@ ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
         hofs++, fpos = 1;
     // write header
     fwrite("!<arch>\n", 8, 1, fh);
+    // create an empty archive
+    if (!funccnt)
+        goto the_end;
     sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
     memcpy(&arhdr.ar_size, stmp, 10);
     fwrite(&arhdr, sizeof(arhdr), 1, fh);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]