[llvm-bugs] [Bug 52197] New: Please add a "always replace file" option ("rcL") when concatenating archives

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Oct 17 05:37:34 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=52197

            Bug ID: 52197
           Summary: Please add a "always replace file" option ("rcL") when
                    concatenating archives
           Product: tools
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: llvm-ar
          Assignee: unassignedbugs at nondot.org
          Reporter: jujjyl at gmail.com
                CC: llvm-bugs at lists.llvm.org

If one wants to create a new .a library file with .o files, one can use

  llvm-ar rc lib.a 1.o 2.o

command, and llvm-ar will delete the old file lib.a if it exists, and place 1.o
and 2.o into it.

If one wants to append files to an existing .a file without deleting the old
file, one can use command

  llvm-ar qc lib.a 1.o 2.o

That works nice and symmetric.

However sometimes when linking, one wants to link the file lib.a to a
yetAnotherLib.a file. If one uses either of the above commands to do this, e.g.

  llvm-ar rc yetAnotherLib.a lib.a 3.o 4.o

the archiver will place the file lib.a nested inside yetAnotherLib.a file, i.e.
in a form

yetAnotherLib.a:
  3.o
  4.o
  lib.a:
    1.o
    2.o

Sometimes this is undesirable, but one would instead want to concatenate/append
the contents without nesting, i.e. to have the following flattened result:

yetAnotherLib.a:
  1.o
  2.o
  3.o
  4.o

Llvm-ar does have an option to achieve this exactly, namely

  [L] - add archive's contents

so one could attempt to do

  llvm-ar rcL yetAnotherLib.a lib.a 3.o 4.o

to get the flattened output. But llvm-ar will complain

  llvm-ar.exe: error: the 'L' modifier is only applicable to the 'q' operation

Fair enough, then trying

  llvm-ar qcL yetAnotherLib.a lib.a 3.o 4.o

will cause the desired flat output to be generated.

However if one blindly changes the 'r' command to a 'q' command, it will
introduce a subtle but critical bug to a build system using it. Namely, the 'q'
command will concatenate files to an existing library, so if yetAnotherLib.a
already existed on disk from a previous build, it will accumulate more files
into it, keeping the old files around.

To get the desired "rcL" behavior, one will need to run two commands:

  rm yetAnotherLib.a
  llvm-ar qcL yetAnotherLib.a lib.a 3.o 4.o

to erase any old contents. This can be a bit tricky for some build systems to
implement that aren't e.g. based on shell expansions.

Would it be possible to add support for a "llvm-ar rcL" combination of flags,
which would always create a new archive, while still concatenating all input .a
files to the build, instead of adding them in a nested form?

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20211017/0639b03f/attachment.html>


More information about the llvm-bugs mailing list