1 /**
2  * A module defining an abstract library.
3  * Implementations for various formats are in separate `libXXX.d` modules.
4  *
5  * Copyright:   Copyright (C) 1999-2023 by The D Language Foundation, All Rights Reserved
6  * Authors:     $(LINK2 https://www.digitalmars.com, Walter Bright)
7  * License:     $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
8  * Source:      $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/lib.d, _lib.d)
9  * Documentation:  https://dlang.org/phobos/dmd_lib.html
10  * Coverage:    https://codecov.io/gh/dlang/dmd/src/master/src/dmd/lib.d
11  */
12 
13 module dmd.lib;
14 
15 import core.stdc.stdio;
16 
17 import dmd.common.outbuffer;
18 import dmd.errorsink;
19 import dmd.location;
20 import dmd.target : Target;
21 
22 import dmd.libelf;
23 import dmd.libmach;
24 import dmd.libmscoff;
25 import dmd.libomf;
26 
27 private enum LOG = false;
28 
29 class Library
30 {
31     const(char)[] lib_ext;      // library file extension
32     ErrorSink eSink;            // where the error messages go
33 
34     static Library factory(Target.ObjectFormat of, const char[] lib_ext, ErrorSink eSink)
35     {
36         Library lib;
37         final switch (of)
38         {
39             case Target.ObjectFormat.elf:   lib = LibElf_factory();     break;
40             case Target.ObjectFormat.macho: lib = LibMach_factory();    break;
41             case Target.ObjectFormat.coff:  lib = LibMSCoff_factory();  break;
42             case Target.ObjectFormat.omf:   lib = LibOMF_factory();     break;
43         }
44         lib.lib_ext = lib_ext;
45         lib.eSink = eSink;
46         return lib;
47     }
48 
49     abstract void addObject(const(char)[] module_name, const ubyte[] buf);
50 
51     abstract void writeLibToBuffer(ref OutBuffer libbuf);
52 
53 
54     /***********************************
55      * Set library file name
56      * Params:
57      *  filename = name of library file
58      */
59     final void setFilename(const char[] filename)
60     {
61         static if (LOG)
62         {
63             printf("LibElf::setFilename(filename = '%.*s')\n",
64                    cast(int)filename.length, filename.ptr);
65         }
66 
67         loc = Loc(filename.ptr, 0, 0);
68     }
69 
70     /*************
71      * Retrieve library file name
72      * Returns:
73      *  filename = name of library file
74      */
75     final const(char)* getFilename() const
76     {
77         return loc.filename;
78     }
79 
80   public:
81     Loc loc;                  // the filename of the library
82 }