xref: /aosp_15_r20/prebuilts/build-tools/common/py3-stdlib/distutils/ccompiler.py (revision cda5da8d549138a6648c5ee6d7a49cf8f4a657be)
1*cda5da8dSAndroid Build Coastguard Worker"""distutils.ccompiler
2*cda5da8dSAndroid Build Coastguard Worker
3*cda5da8dSAndroid Build Coastguard WorkerContains CCompiler, an abstract base class that defines the interface
4*cda5da8dSAndroid Build Coastguard Workerfor the Distutils compiler abstraction model."""
5*cda5da8dSAndroid Build Coastguard Worker
6*cda5da8dSAndroid Build Coastguard Workerimport sys, os, re
7*cda5da8dSAndroid Build Coastguard Workerfrom distutils.errors import *
8*cda5da8dSAndroid Build Coastguard Workerfrom distutils.spawn import spawn
9*cda5da8dSAndroid Build Coastguard Workerfrom distutils.file_util import move_file
10*cda5da8dSAndroid Build Coastguard Workerfrom distutils.dir_util import mkpath
11*cda5da8dSAndroid Build Coastguard Workerfrom distutils.dep_util import newer_group
12*cda5da8dSAndroid Build Coastguard Workerfrom distutils.util import split_quoted, execute
13*cda5da8dSAndroid Build Coastguard Workerfrom distutils import log
14*cda5da8dSAndroid Build Coastguard Worker
15*cda5da8dSAndroid Build Coastguard Workerclass CCompiler:
16*cda5da8dSAndroid Build Coastguard Worker    """Abstract base class to define the interface that must be implemented
17*cda5da8dSAndroid Build Coastguard Worker    by real compiler classes.  Also has some utility methods used by
18*cda5da8dSAndroid Build Coastguard Worker    several compiler classes.
19*cda5da8dSAndroid Build Coastguard Worker
20*cda5da8dSAndroid Build Coastguard Worker    The basic idea behind a compiler abstraction class is that each
21*cda5da8dSAndroid Build Coastguard Worker    instance can be used for all the compile/link steps in building a
22*cda5da8dSAndroid Build Coastguard Worker    single project.  Thus, attributes common to all of those compile and
23*cda5da8dSAndroid Build Coastguard Worker    link steps -- include directories, macros to define, libraries to link
24*cda5da8dSAndroid Build Coastguard Worker    against, etc. -- are attributes of the compiler instance.  To allow for
25*cda5da8dSAndroid Build Coastguard Worker    variability in how individual files are treated, most of those
26*cda5da8dSAndroid Build Coastguard Worker    attributes may be varied on a per-compilation or per-link basis.
27*cda5da8dSAndroid Build Coastguard Worker    """
28*cda5da8dSAndroid Build Coastguard Worker
29*cda5da8dSAndroid Build Coastguard Worker    # 'compiler_type' is a class attribute that identifies this class.  It
30*cda5da8dSAndroid Build Coastguard Worker    # keeps code that wants to know what kind of compiler it's dealing with
31*cda5da8dSAndroid Build Coastguard Worker    # from having to import all possible compiler classes just to do an
32*cda5da8dSAndroid Build Coastguard Worker    # 'isinstance'.  In concrete CCompiler subclasses, 'compiler_type'
33*cda5da8dSAndroid Build Coastguard Worker    # should really, really be one of the keys of the 'compiler_class'
34*cda5da8dSAndroid Build Coastguard Worker    # dictionary (see below -- used by the 'new_compiler()' factory
35*cda5da8dSAndroid Build Coastguard Worker    # function) -- authors of new compiler interface classes are
36*cda5da8dSAndroid Build Coastguard Worker    # responsible for updating 'compiler_class'!
37*cda5da8dSAndroid Build Coastguard Worker    compiler_type = None
38*cda5da8dSAndroid Build Coastguard Worker
39*cda5da8dSAndroid Build Coastguard Worker    # XXX things not handled by this compiler abstraction model:
40*cda5da8dSAndroid Build Coastguard Worker    #   * client can't provide additional options for a compiler,
41*cda5da8dSAndroid Build Coastguard Worker    #     e.g. warning, optimization, debugging flags.  Perhaps this
42*cda5da8dSAndroid Build Coastguard Worker    #     should be the domain of concrete compiler abstraction classes
43*cda5da8dSAndroid Build Coastguard Worker    #     (UnixCCompiler, MSVCCompiler, etc.) -- or perhaps the base
44*cda5da8dSAndroid Build Coastguard Worker    #     class should have methods for the common ones.
45*cda5da8dSAndroid Build Coastguard Worker    #   * can't completely override the include or library searchg
46*cda5da8dSAndroid Build Coastguard Worker    #     path, ie. no "cc -I -Idir1 -Idir2" or "cc -L -Ldir1 -Ldir2".
47*cda5da8dSAndroid Build Coastguard Worker    #     I'm not sure how widely supported this is even by Unix
48*cda5da8dSAndroid Build Coastguard Worker    #     compilers, much less on other platforms.  And I'm even less
49*cda5da8dSAndroid Build Coastguard Worker    #     sure how useful it is; maybe for cross-compiling, but
50*cda5da8dSAndroid Build Coastguard Worker    #     support for that is a ways off.  (And anyways, cross
51*cda5da8dSAndroid Build Coastguard Worker    #     compilers probably have a dedicated binary with the
52*cda5da8dSAndroid Build Coastguard Worker    #     right paths compiled in.  I hope.)
53*cda5da8dSAndroid Build Coastguard Worker    #   * can't do really freaky things with the library list/library
54*cda5da8dSAndroid Build Coastguard Worker    #     dirs, e.g. "-Ldir1 -lfoo -Ldir2 -lfoo" to link against
55*cda5da8dSAndroid Build Coastguard Worker    #     different versions of libfoo.a in different locations.  I
56*cda5da8dSAndroid Build Coastguard Worker    #     think this is useless without the ability to null out the
57*cda5da8dSAndroid Build Coastguard Worker    #     library search path anyways.
58*cda5da8dSAndroid Build Coastguard Worker
59*cda5da8dSAndroid Build Coastguard Worker
60*cda5da8dSAndroid Build Coastguard Worker    # Subclasses that rely on the standard filename generation methods
61*cda5da8dSAndroid Build Coastguard Worker    # implemented below should override these; see the comment near
62*cda5da8dSAndroid Build Coastguard Worker    # those methods ('object_filenames()' et. al.) for details:
63*cda5da8dSAndroid Build Coastguard Worker    src_extensions = None               # list of strings
64*cda5da8dSAndroid Build Coastguard Worker    obj_extension = None                # string
65*cda5da8dSAndroid Build Coastguard Worker    static_lib_extension = None
66*cda5da8dSAndroid Build Coastguard Worker    shared_lib_extension = None         # string
67*cda5da8dSAndroid Build Coastguard Worker    static_lib_format = None            # format string
68*cda5da8dSAndroid Build Coastguard Worker    shared_lib_format = None            # prob. same as static_lib_format
69*cda5da8dSAndroid Build Coastguard Worker    exe_extension = None                # string
70*cda5da8dSAndroid Build Coastguard Worker
71*cda5da8dSAndroid Build Coastguard Worker    # Default language settings. language_map is used to detect a source
72*cda5da8dSAndroid Build Coastguard Worker    # file or Extension target language, checking source filenames.
73*cda5da8dSAndroid Build Coastguard Worker    # language_order is used to detect the language precedence, when deciding
74*cda5da8dSAndroid Build Coastguard Worker    # what language to use when mixing source types. For example, if some
75*cda5da8dSAndroid Build Coastguard Worker    # extension has two files with ".c" extension, and one with ".cpp", it
76*cda5da8dSAndroid Build Coastguard Worker    # is still linked as c++.
77*cda5da8dSAndroid Build Coastguard Worker    language_map = {".c"   : "c",
78*cda5da8dSAndroid Build Coastguard Worker                    ".cc"  : "c++",
79*cda5da8dSAndroid Build Coastguard Worker                    ".cpp" : "c++",
80*cda5da8dSAndroid Build Coastguard Worker                    ".cxx" : "c++",
81*cda5da8dSAndroid Build Coastguard Worker                    ".m"   : "objc",
82*cda5da8dSAndroid Build Coastguard Worker                   }
83*cda5da8dSAndroid Build Coastguard Worker    language_order = ["c++", "objc", "c"]
84*cda5da8dSAndroid Build Coastguard Worker
85*cda5da8dSAndroid Build Coastguard Worker    def __init__(self, verbose=0, dry_run=0, force=0):
86*cda5da8dSAndroid Build Coastguard Worker        self.dry_run = dry_run
87*cda5da8dSAndroid Build Coastguard Worker        self.force = force
88*cda5da8dSAndroid Build Coastguard Worker        self.verbose = verbose
89*cda5da8dSAndroid Build Coastguard Worker
90*cda5da8dSAndroid Build Coastguard Worker        # 'output_dir': a common output directory for object, library,
91*cda5da8dSAndroid Build Coastguard Worker        # shared object, and shared library files
92*cda5da8dSAndroid Build Coastguard Worker        self.output_dir = None
93*cda5da8dSAndroid Build Coastguard Worker
94*cda5da8dSAndroid Build Coastguard Worker        # 'macros': a list of macro definitions (or undefinitions).  A
95*cda5da8dSAndroid Build Coastguard Worker        # macro definition is a 2-tuple (name, value), where the value is
96*cda5da8dSAndroid Build Coastguard Worker        # either a string or None (no explicit value).  A macro
97*cda5da8dSAndroid Build Coastguard Worker        # undefinition is a 1-tuple (name,).
98*cda5da8dSAndroid Build Coastguard Worker        self.macros = []
99*cda5da8dSAndroid Build Coastguard Worker
100*cda5da8dSAndroid Build Coastguard Worker        # 'include_dirs': a list of directories to search for include files
101*cda5da8dSAndroid Build Coastguard Worker        self.include_dirs = []
102*cda5da8dSAndroid Build Coastguard Worker
103*cda5da8dSAndroid Build Coastguard Worker        # 'libraries': a list of libraries to include in any link
104*cda5da8dSAndroid Build Coastguard Worker        # (library names, not filenames: eg. "foo" not "libfoo.a")
105*cda5da8dSAndroid Build Coastguard Worker        self.libraries = []
106*cda5da8dSAndroid Build Coastguard Worker
107*cda5da8dSAndroid Build Coastguard Worker        # 'library_dirs': a list of directories to search for libraries
108*cda5da8dSAndroid Build Coastguard Worker        self.library_dirs = []
109*cda5da8dSAndroid Build Coastguard Worker
110*cda5da8dSAndroid Build Coastguard Worker        # 'runtime_library_dirs': a list of directories to search for
111*cda5da8dSAndroid Build Coastguard Worker        # shared libraries/objects at runtime
112*cda5da8dSAndroid Build Coastguard Worker        self.runtime_library_dirs = []
113*cda5da8dSAndroid Build Coastguard Worker
114*cda5da8dSAndroid Build Coastguard Worker        # 'objects': a list of object files (or similar, such as explicitly
115*cda5da8dSAndroid Build Coastguard Worker        # named library files) to include on any link
116*cda5da8dSAndroid Build Coastguard Worker        self.objects = []
117*cda5da8dSAndroid Build Coastguard Worker
118*cda5da8dSAndroid Build Coastguard Worker        for key in self.executables.keys():
119*cda5da8dSAndroid Build Coastguard Worker            self.set_executable(key, self.executables[key])
120*cda5da8dSAndroid Build Coastguard Worker
121*cda5da8dSAndroid Build Coastguard Worker    def set_executables(self, **kwargs):
122*cda5da8dSAndroid Build Coastguard Worker        """Define the executables (and options for them) that will be run
123*cda5da8dSAndroid Build Coastguard Worker        to perform the various stages of compilation.  The exact set of
124*cda5da8dSAndroid Build Coastguard Worker        executables that may be specified here depends on the compiler
125*cda5da8dSAndroid Build Coastguard Worker        class (via the 'executables' class attribute), but most will have:
126*cda5da8dSAndroid Build Coastguard Worker          compiler      the C/C++ compiler
127*cda5da8dSAndroid Build Coastguard Worker          linker_so     linker used to create shared objects and libraries
128*cda5da8dSAndroid Build Coastguard Worker          linker_exe    linker used to create binary executables
129*cda5da8dSAndroid Build Coastguard Worker          archiver      static library creator
130*cda5da8dSAndroid Build Coastguard Worker
131*cda5da8dSAndroid Build Coastguard Worker        On platforms with a command-line (Unix, DOS/Windows), each of these
132*cda5da8dSAndroid Build Coastguard Worker        is a string that will be split into executable name and (optional)
133*cda5da8dSAndroid Build Coastguard Worker        list of arguments.  (Splitting the string is done similarly to how
134*cda5da8dSAndroid Build Coastguard Worker        Unix shells operate: words are delimited by spaces, but quotes and
135*cda5da8dSAndroid Build Coastguard Worker        backslashes can override this.  See
136*cda5da8dSAndroid Build Coastguard Worker        'distutils.util.split_quoted()'.)
137*cda5da8dSAndroid Build Coastguard Worker        """
138*cda5da8dSAndroid Build Coastguard Worker
139*cda5da8dSAndroid Build Coastguard Worker        # Note that some CCompiler implementation classes will define class
140*cda5da8dSAndroid Build Coastguard Worker        # attributes 'cpp', 'cc', etc. with hard-coded executable names;
141*cda5da8dSAndroid Build Coastguard Worker        # this is appropriate when a compiler class is for exactly one
142*cda5da8dSAndroid Build Coastguard Worker        # compiler/OS combination (eg. MSVCCompiler).  Other compiler
143*cda5da8dSAndroid Build Coastguard Worker        # classes (UnixCCompiler, in particular) are driven by information
144*cda5da8dSAndroid Build Coastguard Worker        # discovered at run-time, since there are many different ways to do
145*cda5da8dSAndroid Build Coastguard Worker        # basically the same things with Unix C compilers.
146*cda5da8dSAndroid Build Coastguard Worker
147*cda5da8dSAndroid Build Coastguard Worker        for key in kwargs:
148*cda5da8dSAndroid Build Coastguard Worker            if key not in self.executables:
149*cda5da8dSAndroid Build Coastguard Worker                raise ValueError("unknown executable '%s' for class %s" %
150*cda5da8dSAndroid Build Coastguard Worker                      (key, self.__class__.__name__))
151*cda5da8dSAndroid Build Coastguard Worker            self.set_executable(key, kwargs[key])
152*cda5da8dSAndroid Build Coastguard Worker
153*cda5da8dSAndroid Build Coastguard Worker    def set_executable(self, key, value):
154*cda5da8dSAndroid Build Coastguard Worker        if isinstance(value, str):
155*cda5da8dSAndroid Build Coastguard Worker            setattr(self, key, split_quoted(value))
156*cda5da8dSAndroid Build Coastguard Worker        else:
157*cda5da8dSAndroid Build Coastguard Worker            setattr(self, key, value)
158*cda5da8dSAndroid Build Coastguard Worker
159*cda5da8dSAndroid Build Coastguard Worker    def _find_macro(self, name):
160*cda5da8dSAndroid Build Coastguard Worker        i = 0
161*cda5da8dSAndroid Build Coastguard Worker        for defn in self.macros:
162*cda5da8dSAndroid Build Coastguard Worker            if defn[0] == name:
163*cda5da8dSAndroid Build Coastguard Worker                return i
164*cda5da8dSAndroid Build Coastguard Worker            i += 1
165*cda5da8dSAndroid Build Coastguard Worker        return None
166*cda5da8dSAndroid Build Coastguard Worker
167*cda5da8dSAndroid Build Coastguard Worker    def _check_macro_definitions(self, definitions):
168*cda5da8dSAndroid Build Coastguard Worker        """Ensures that every element of 'definitions' is a valid macro
169*cda5da8dSAndroid Build Coastguard Worker        definition, ie. either (name,value) 2-tuple or a (name,) tuple.  Do
170*cda5da8dSAndroid Build Coastguard Worker        nothing if all definitions are OK, raise TypeError otherwise.
171*cda5da8dSAndroid Build Coastguard Worker        """
172*cda5da8dSAndroid Build Coastguard Worker        for defn in definitions:
173*cda5da8dSAndroid Build Coastguard Worker            if not (isinstance(defn, tuple) and
174*cda5da8dSAndroid Build Coastguard Worker                    (len(defn) in (1, 2) and
175*cda5da8dSAndroid Build Coastguard Worker                      (isinstance (defn[1], str) or defn[1] is None)) and
176*cda5da8dSAndroid Build Coastguard Worker                    isinstance (defn[0], str)):
177*cda5da8dSAndroid Build Coastguard Worker                raise TypeError(("invalid macro definition '%s': " % defn) + \
178*cda5da8dSAndroid Build Coastguard Worker                      "must be tuple (string,), (string, string), or " + \
179*cda5da8dSAndroid Build Coastguard Worker                      "(string, None)")
180*cda5da8dSAndroid Build Coastguard Worker
181*cda5da8dSAndroid Build Coastguard Worker
182*cda5da8dSAndroid Build Coastguard Worker    # -- Bookkeeping methods -------------------------------------------
183*cda5da8dSAndroid Build Coastguard Worker
184*cda5da8dSAndroid Build Coastguard Worker    def define_macro(self, name, value=None):
185*cda5da8dSAndroid Build Coastguard Worker        """Define a preprocessor macro for all compilations driven by this
186*cda5da8dSAndroid Build Coastguard Worker        compiler object.  The optional parameter 'value' should be a
187*cda5da8dSAndroid Build Coastguard Worker        string; if it is not supplied, then the macro will be defined
188*cda5da8dSAndroid Build Coastguard Worker        without an explicit value and the exact outcome depends on the
189*cda5da8dSAndroid Build Coastguard Worker        compiler used (XXX true? does ANSI say anything about this?)
190*cda5da8dSAndroid Build Coastguard Worker        """
191*cda5da8dSAndroid Build Coastguard Worker        # Delete from the list of macro definitions/undefinitions if
192*cda5da8dSAndroid Build Coastguard Worker        # already there (so that this one will take precedence).
193*cda5da8dSAndroid Build Coastguard Worker        i = self._find_macro (name)
194*cda5da8dSAndroid Build Coastguard Worker        if i is not None:
195*cda5da8dSAndroid Build Coastguard Worker            del self.macros[i]
196*cda5da8dSAndroid Build Coastguard Worker
197*cda5da8dSAndroid Build Coastguard Worker        self.macros.append((name, value))
198*cda5da8dSAndroid Build Coastguard Worker
199*cda5da8dSAndroid Build Coastguard Worker    def undefine_macro(self, name):
200*cda5da8dSAndroid Build Coastguard Worker        """Undefine a preprocessor macro for all compilations driven by
201*cda5da8dSAndroid Build Coastguard Worker        this compiler object.  If the same macro is defined by
202*cda5da8dSAndroid Build Coastguard Worker        'define_macro()' and undefined by 'undefine_macro()' the last call
203*cda5da8dSAndroid Build Coastguard Worker        takes precedence (including multiple redefinitions or
204*cda5da8dSAndroid Build Coastguard Worker        undefinitions).  If the macro is redefined/undefined on a
205*cda5da8dSAndroid Build Coastguard Worker        per-compilation basis (ie. in the call to 'compile()'), then that
206*cda5da8dSAndroid Build Coastguard Worker        takes precedence.
207*cda5da8dSAndroid Build Coastguard Worker        """
208*cda5da8dSAndroid Build Coastguard Worker        # Delete from the list of macro definitions/undefinitions if
209*cda5da8dSAndroid Build Coastguard Worker        # already there (so that this one will take precedence).
210*cda5da8dSAndroid Build Coastguard Worker        i = self._find_macro (name)
211*cda5da8dSAndroid Build Coastguard Worker        if i is not None:
212*cda5da8dSAndroid Build Coastguard Worker            del self.macros[i]
213*cda5da8dSAndroid Build Coastguard Worker
214*cda5da8dSAndroid Build Coastguard Worker        undefn = (name,)
215*cda5da8dSAndroid Build Coastguard Worker        self.macros.append(undefn)
216*cda5da8dSAndroid Build Coastguard Worker
217*cda5da8dSAndroid Build Coastguard Worker    def add_include_dir(self, dir):
218*cda5da8dSAndroid Build Coastguard Worker        """Add 'dir' to the list of directories that will be searched for
219*cda5da8dSAndroid Build Coastguard Worker        header files.  The compiler is instructed to search directories in
220*cda5da8dSAndroid Build Coastguard Worker        the order in which they are supplied by successive calls to
221*cda5da8dSAndroid Build Coastguard Worker        'add_include_dir()'.
222*cda5da8dSAndroid Build Coastguard Worker        """
223*cda5da8dSAndroid Build Coastguard Worker        self.include_dirs.append(dir)
224*cda5da8dSAndroid Build Coastguard Worker
225*cda5da8dSAndroid Build Coastguard Worker    def set_include_dirs(self, dirs):
226*cda5da8dSAndroid Build Coastguard Worker        """Set the list of directories that will be searched to 'dirs' (a
227*cda5da8dSAndroid Build Coastguard Worker        list of strings).  Overrides any preceding calls to
228*cda5da8dSAndroid Build Coastguard Worker        'add_include_dir()'; subsequence calls to 'add_include_dir()' add
229*cda5da8dSAndroid Build Coastguard Worker        to the list passed to 'set_include_dirs()'.  This does not affect
230*cda5da8dSAndroid Build Coastguard Worker        any list of standard include directories that the compiler may
231*cda5da8dSAndroid Build Coastguard Worker        search by default.
232*cda5da8dSAndroid Build Coastguard Worker        """
233*cda5da8dSAndroid Build Coastguard Worker        self.include_dirs = dirs[:]
234*cda5da8dSAndroid Build Coastguard Worker
235*cda5da8dSAndroid Build Coastguard Worker    def add_library(self, libname):
236*cda5da8dSAndroid Build Coastguard Worker        """Add 'libname' to the list of libraries that will be included in
237*cda5da8dSAndroid Build Coastguard Worker        all links driven by this compiler object.  Note that 'libname'
238*cda5da8dSAndroid Build Coastguard Worker        should *not* be the name of a file containing a library, but the
239*cda5da8dSAndroid Build Coastguard Worker        name of the library itself: the actual filename will be inferred by
240*cda5da8dSAndroid Build Coastguard Worker        the linker, the compiler, or the compiler class (depending on the
241*cda5da8dSAndroid Build Coastguard Worker        platform).
242*cda5da8dSAndroid Build Coastguard Worker
243*cda5da8dSAndroid Build Coastguard Worker        The linker will be instructed to link against libraries in the
244*cda5da8dSAndroid Build Coastguard Worker        order they were supplied to 'add_library()' and/or
245*cda5da8dSAndroid Build Coastguard Worker        'set_libraries()'.  It is perfectly valid to duplicate library
246*cda5da8dSAndroid Build Coastguard Worker        names; the linker will be instructed to link against libraries as
247*cda5da8dSAndroid Build Coastguard Worker        many times as they are mentioned.
248*cda5da8dSAndroid Build Coastguard Worker        """
249*cda5da8dSAndroid Build Coastguard Worker        self.libraries.append(libname)
250*cda5da8dSAndroid Build Coastguard Worker
251*cda5da8dSAndroid Build Coastguard Worker    def set_libraries(self, libnames):
252*cda5da8dSAndroid Build Coastguard Worker        """Set the list of libraries to be included in all links driven by
253*cda5da8dSAndroid Build Coastguard Worker        this compiler object to 'libnames' (a list of strings).  This does
254*cda5da8dSAndroid Build Coastguard Worker        not affect any standard system libraries that the linker may
255*cda5da8dSAndroid Build Coastguard Worker        include by default.
256*cda5da8dSAndroid Build Coastguard Worker        """
257*cda5da8dSAndroid Build Coastguard Worker        self.libraries = libnames[:]
258*cda5da8dSAndroid Build Coastguard Worker
259*cda5da8dSAndroid Build Coastguard Worker    def add_library_dir(self, dir):
260*cda5da8dSAndroid Build Coastguard Worker        """Add 'dir' to the list of directories that will be searched for
261*cda5da8dSAndroid Build Coastguard Worker        libraries specified to 'add_library()' and 'set_libraries()'.  The
262*cda5da8dSAndroid Build Coastguard Worker        linker will be instructed to search for libraries in the order they
263*cda5da8dSAndroid Build Coastguard Worker        are supplied to 'add_library_dir()' and/or 'set_library_dirs()'.
264*cda5da8dSAndroid Build Coastguard Worker        """
265*cda5da8dSAndroid Build Coastguard Worker        self.library_dirs.append(dir)
266*cda5da8dSAndroid Build Coastguard Worker
267*cda5da8dSAndroid Build Coastguard Worker    def set_library_dirs(self, dirs):
268*cda5da8dSAndroid Build Coastguard Worker        """Set the list of library search directories to 'dirs' (a list of
269*cda5da8dSAndroid Build Coastguard Worker        strings).  This does not affect any standard library search path
270*cda5da8dSAndroid Build Coastguard Worker        that the linker may search by default.
271*cda5da8dSAndroid Build Coastguard Worker        """
272*cda5da8dSAndroid Build Coastguard Worker        self.library_dirs = dirs[:]
273*cda5da8dSAndroid Build Coastguard Worker
274*cda5da8dSAndroid Build Coastguard Worker    def add_runtime_library_dir(self, dir):
275*cda5da8dSAndroid Build Coastguard Worker        """Add 'dir' to the list of directories that will be searched for
276*cda5da8dSAndroid Build Coastguard Worker        shared libraries at runtime.
277*cda5da8dSAndroid Build Coastguard Worker        """
278*cda5da8dSAndroid Build Coastguard Worker        self.runtime_library_dirs.append(dir)
279*cda5da8dSAndroid Build Coastguard Worker
280*cda5da8dSAndroid Build Coastguard Worker    def set_runtime_library_dirs(self, dirs):
281*cda5da8dSAndroid Build Coastguard Worker        """Set the list of directories to search for shared libraries at
282*cda5da8dSAndroid Build Coastguard Worker        runtime to 'dirs' (a list of strings).  This does not affect any
283*cda5da8dSAndroid Build Coastguard Worker        standard search path that the runtime linker may search by
284*cda5da8dSAndroid Build Coastguard Worker        default.
285*cda5da8dSAndroid Build Coastguard Worker        """
286*cda5da8dSAndroid Build Coastguard Worker        self.runtime_library_dirs = dirs[:]
287*cda5da8dSAndroid Build Coastguard Worker
288*cda5da8dSAndroid Build Coastguard Worker    def add_link_object(self, object):
289*cda5da8dSAndroid Build Coastguard Worker        """Add 'object' to the list of object files (or analogues, such as
290*cda5da8dSAndroid Build Coastguard Worker        explicitly named library files or the output of "resource
291*cda5da8dSAndroid Build Coastguard Worker        compilers") to be included in every link driven by this compiler
292*cda5da8dSAndroid Build Coastguard Worker        object.
293*cda5da8dSAndroid Build Coastguard Worker        """
294*cda5da8dSAndroid Build Coastguard Worker        self.objects.append(object)
295*cda5da8dSAndroid Build Coastguard Worker
296*cda5da8dSAndroid Build Coastguard Worker    def set_link_objects(self, objects):
297*cda5da8dSAndroid Build Coastguard Worker        """Set the list of object files (or analogues) to be included in
298*cda5da8dSAndroid Build Coastguard Worker        every link to 'objects'.  This does not affect any standard object
299*cda5da8dSAndroid Build Coastguard Worker        files that the linker may include by default (such as system
300*cda5da8dSAndroid Build Coastguard Worker        libraries).
301*cda5da8dSAndroid Build Coastguard Worker        """
302*cda5da8dSAndroid Build Coastguard Worker        self.objects = objects[:]
303*cda5da8dSAndroid Build Coastguard Worker
304*cda5da8dSAndroid Build Coastguard Worker
305*cda5da8dSAndroid Build Coastguard Worker    # -- Private utility methods --------------------------------------
306*cda5da8dSAndroid Build Coastguard Worker    # (here for the convenience of subclasses)
307*cda5da8dSAndroid Build Coastguard Worker
308*cda5da8dSAndroid Build Coastguard Worker    # Helper method to prep compiler in subclass compile() methods
309*cda5da8dSAndroid Build Coastguard Worker
310*cda5da8dSAndroid Build Coastguard Worker    def _setup_compile(self, outdir, macros, incdirs, sources, depends,
311*cda5da8dSAndroid Build Coastguard Worker                       extra):
312*cda5da8dSAndroid Build Coastguard Worker        """Process arguments and decide which source files to compile."""
313*cda5da8dSAndroid Build Coastguard Worker        if outdir is None:
314*cda5da8dSAndroid Build Coastguard Worker            outdir = self.output_dir
315*cda5da8dSAndroid Build Coastguard Worker        elif not isinstance(outdir, str):
316*cda5da8dSAndroid Build Coastguard Worker            raise TypeError("'output_dir' must be a string or None")
317*cda5da8dSAndroid Build Coastguard Worker
318*cda5da8dSAndroid Build Coastguard Worker        if macros is None:
319*cda5da8dSAndroid Build Coastguard Worker            macros = self.macros
320*cda5da8dSAndroid Build Coastguard Worker        elif isinstance(macros, list):
321*cda5da8dSAndroid Build Coastguard Worker            macros = macros + (self.macros or [])
322*cda5da8dSAndroid Build Coastguard Worker        else:
323*cda5da8dSAndroid Build Coastguard Worker            raise TypeError("'macros' (if supplied) must be a list of tuples")
324*cda5da8dSAndroid Build Coastguard Worker
325*cda5da8dSAndroid Build Coastguard Worker        if incdirs is None:
326*cda5da8dSAndroid Build Coastguard Worker            incdirs = self.include_dirs
327*cda5da8dSAndroid Build Coastguard Worker        elif isinstance(incdirs, (list, tuple)):
328*cda5da8dSAndroid Build Coastguard Worker            incdirs = list(incdirs) + (self.include_dirs or [])
329*cda5da8dSAndroid Build Coastguard Worker        else:
330*cda5da8dSAndroid Build Coastguard Worker            raise TypeError(
331*cda5da8dSAndroid Build Coastguard Worker                  "'include_dirs' (if supplied) must be a list of strings")
332*cda5da8dSAndroid Build Coastguard Worker
333*cda5da8dSAndroid Build Coastguard Worker        if extra is None:
334*cda5da8dSAndroid Build Coastguard Worker            extra = []
335*cda5da8dSAndroid Build Coastguard Worker
336*cda5da8dSAndroid Build Coastguard Worker        # Get the list of expected output (object) files
337*cda5da8dSAndroid Build Coastguard Worker        objects = self.object_filenames(sources, strip_dir=0,
338*cda5da8dSAndroid Build Coastguard Worker                                        output_dir=outdir)
339*cda5da8dSAndroid Build Coastguard Worker        assert len(objects) == len(sources)
340*cda5da8dSAndroid Build Coastguard Worker
341*cda5da8dSAndroid Build Coastguard Worker        pp_opts = gen_preprocess_options(macros, incdirs)
342*cda5da8dSAndroid Build Coastguard Worker
343*cda5da8dSAndroid Build Coastguard Worker        build = {}
344*cda5da8dSAndroid Build Coastguard Worker        for i in range(len(sources)):
345*cda5da8dSAndroid Build Coastguard Worker            src = sources[i]
346*cda5da8dSAndroid Build Coastguard Worker            obj = objects[i]
347*cda5da8dSAndroid Build Coastguard Worker            ext = os.path.splitext(src)[1]
348*cda5da8dSAndroid Build Coastguard Worker            self.mkpath(os.path.dirname(obj))
349*cda5da8dSAndroid Build Coastguard Worker            build[obj] = (src, ext)
350*cda5da8dSAndroid Build Coastguard Worker
351*cda5da8dSAndroid Build Coastguard Worker        return macros, objects, extra, pp_opts, build
352*cda5da8dSAndroid Build Coastguard Worker
353*cda5da8dSAndroid Build Coastguard Worker    def _get_cc_args(self, pp_opts, debug, before):
354*cda5da8dSAndroid Build Coastguard Worker        # works for unixccompiler, cygwinccompiler
355*cda5da8dSAndroid Build Coastguard Worker        cc_args = pp_opts + ['-c']
356*cda5da8dSAndroid Build Coastguard Worker        if debug:
357*cda5da8dSAndroid Build Coastguard Worker            cc_args[:0] = ['-g']
358*cda5da8dSAndroid Build Coastguard Worker        if before:
359*cda5da8dSAndroid Build Coastguard Worker            cc_args[:0] = before
360*cda5da8dSAndroid Build Coastguard Worker        return cc_args
361*cda5da8dSAndroid Build Coastguard Worker
362*cda5da8dSAndroid Build Coastguard Worker    def _fix_compile_args(self, output_dir, macros, include_dirs):
363*cda5da8dSAndroid Build Coastguard Worker        """Typecheck and fix-up some of the arguments to the 'compile()'
364*cda5da8dSAndroid Build Coastguard Worker        method, and return fixed-up values.  Specifically: if 'output_dir'
365*cda5da8dSAndroid Build Coastguard Worker        is None, replaces it with 'self.output_dir'; ensures that 'macros'
366*cda5da8dSAndroid Build Coastguard Worker        is a list, and augments it with 'self.macros'; ensures that
367*cda5da8dSAndroid Build Coastguard Worker        'include_dirs' is a list, and augments it with 'self.include_dirs'.
368*cda5da8dSAndroid Build Coastguard Worker        Guarantees that the returned values are of the correct type,
369*cda5da8dSAndroid Build Coastguard Worker        i.e. for 'output_dir' either string or None, and for 'macros' and
370*cda5da8dSAndroid Build Coastguard Worker        'include_dirs' either list or None.
371*cda5da8dSAndroid Build Coastguard Worker        """
372*cda5da8dSAndroid Build Coastguard Worker        if output_dir is None:
373*cda5da8dSAndroid Build Coastguard Worker            output_dir = self.output_dir
374*cda5da8dSAndroid Build Coastguard Worker        elif not isinstance(output_dir, str):
375*cda5da8dSAndroid Build Coastguard Worker            raise TypeError("'output_dir' must be a string or None")
376*cda5da8dSAndroid Build Coastguard Worker
377*cda5da8dSAndroid Build Coastguard Worker        if macros is None:
378*cda5da8dSAndroid Build Coastguard Worker            macros = self.macros
379*cda5da8dSAndroid Build Coastguard Worker        elif isinstance(macros, list):
380*cda5da8dSAndroid Build Coastguard Worker            macros = macros + (self.macros or [])
381*cda5da8dSAndroid Build Coastguard Worker        else:
382*cda5da8dSAndroid Build Coastguard Worker            raise TypeError("'macros' (if supplied) must be a list of tuples")
383*cda5da8dSAndroid Build Coastguard Worker
384*cda5da8dSAndroid Build Coastguard Worker        if include_dirs is None:
385*cda5da8dSAndroid Build Coastguard Worker            include_dirs = self.include_dirs
386*cda5da8dSAndroid Build Coastguard Worker        elif isinstance(include_dirs, (list, tuple)):
387*cda5da8dSAndroid Build Coastguard Worker            include_dirs = list(include_dirs) + (self.include_dirs or [])
388*cda5da8dSAndroid Build Coastguard Worker        else:
389*cda5da8dSAndroid Build Coastguard Worker            raise TypeError(
390*cda5da8dSAndroid Build Coastguard Worker                  "'include_dirs' (if supplied) must be a list of strings")
391*cda5da8dSAndroid Build Coastguard Worker
392*cda5da8dSAndroid Build Coastguard Worker        return output_dir, macros, include_dirs
393*cda5da8dSAndroid Build Coastguard Worker
394*cda5da8dSAndroid Build Coastguard Worker    def _prep_compile(self, sources, output_dir, depends=None):
395*cda5da8dSAndroid Build Coastguard Worker        """Decide which source files must be recompiled.
396*cda5da8dSAndroid Build Coastguard Worker
397*cda5da8dSAndroid Build Coastguard Worker        Determine the list of object files corresponding to 'sources',
398*cda5da8dSAndroid Build Coastguard Worker        and figure out which ones really need to be recompiled.
399*cda5da8dSAndroid Build Coastguard Worker        Return a list of all object files and a dictionary telling
400*cda5da8dSAndroid Build Coastguard Worker        which source files can be skipped.
401*cda5da8dSAndroid Build Coastguard Worker        """
402*cda5da8dSAndroid Build Coastguard Worker        # Get the list of expected output (object) files
403*cda5da8dSAndroid Build Coastguard Worker        objects = self.object_filenames(sources, output_dir=output_dir)
404*cda5da8dSAndroid Build Coastguard Worker        assert len(objects) == len(sources)
405*cda5da8dSAndroid Build Coastguard Worker
406*cda5da8dSAndroid Build Coastguard Worker        # Return an empty dict for the "which source files can be skipped"
407*cda5da8dSAndroid Build Coastguard Worker        # return value to preserve API compatibility.
408*cda5da8dSAndroid Build Coastguard Worker        return objects, {}
409*cda5da8dSAndroid Build Coastguard Worker
410*cda5da8dSAndroid Build Coastguard Worker    def _fix_object_args(self, objects, output_dir):
411*cda5da8dSAndroid Build Coastguard Worker        """Typecheck and fix up some arguments supplied to various methods.
412*cda5da8dSAndroid Build Coastguard Worker        Specifically: ensure that 'objects' is a list; if output_dir is
413*cda5da8dSAndroid Build Coastguard Worker        None, replace with self.output_dir.  Return fixed versions of
414*cda5da8dSAndroid Build Coastguard Worker        'objects' and 'output_dir'.
415*cda5da8dSAndroid Build Coastguard Worker        """
416*cda5da8dSAndroid Build Coastguard Worker        if not isinstance(objects, (list, tuple)):
417*cda5da8dSAndroid Build Coastguard Worker            raise TypeError("'objects' must be a list or tuple of strings")
418*cda5da8dSAndroid Build Coastguard Worker        objects = list(objects)
419*cda5da8dSAndroid Build Coastguard Worker
420*cda5da8dSAndroid Build Coastguard Worker        if output_dir is None:
421*cda5da8dSAndroid Build Coastguard Worker            output_dir = self.output_dir
422*cda5da8dSAndroid Build Coastguard Worker        elif not isinstance(output_dir, str):
423*cda5da8dSAndroid Build Coastguard Worker            raise TypeError("'output_dir' must be a string or None")
424*cda5da8dSAndroid Build Coastguard Worker
425*cda5da8dSAndroid Build Coastguard Worker        return (objects, output_dir)
426*cda5da8dSAndroid Build Coastguard Worker
427*cda5da8dSAndroid Build Coastguard Worker    def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
428*cda5da8dSAndroid Build Coastguard Worker        """Typecheck and fix up some of the arguments supplied to the
429*cda5da8dSAndroid Build Coastguard Worker        'link_*' methods.  Specifically: ensure that all arguments are
430*cda5da8dSAndroid Build Coastguard Worker        lists, and augment them with their permanent versions
431*cda5da8dSAndroid Build Coastguard Worker        (eg. 'self.libraries' augments 'libraries').  Return a tuple with
432*cda5da8dSAndroid Build Coastguard Worker        fixed versions of all arguments.
433*cda5da8dSAndroid Build Coastguard Worker        """
434*cda5da8dSAndroid Build Coastguard Worker        if libraries is None:
435*cda5da8dSAndroid Build Coastguard Worker            libraries = self.libraries
436*cda5da8dSAndroid Build Coastguard Worker        elif isinstance(libraries, (list, tuple)):
437*cda5da8dSAndroid Build Coastguard Worker            libraries = list (libraries) + (self.libraries or [])
438*cda5da8dSAndroid Build Coastguard Worker        else:
439*cda5da8dSAndroid Build Coastguard Worker            raise TypeError(
440*cda5da8dSAndroid Build Coastguard Worker                  "'libraries' (if supplied) must be a list of strings")
441*cda5da8dSAndroid Build Coastguard Worker
442*cda5da8dSAndroid Build Coastguard Worker        if library_dirs is None:
443*cda5da8dSAndroid Build Coastguard Worker            library_dirs = self.library_dirs
444*cda5da8dSAndroid Build Coastguard Worker        elif isinstance(library_dirs, (list, tuple)):
445*cda5da8dSAndroid Build Coastguard Worker            library_dirs = list (library_dirs) + (self.library_dirs or [])
446*cda5da8dSAndroid Build Coastguard Worker        else:
447*cda5da8dSAndroid Build Coastguard Worker            raise TypeError(
448*cda5da8dSAndroid Build Coastguard Worker                  "'library_dirs' (if supplied) must be a list of strings")
449*cda5da8dSAndroid Build Coastguard Worker
450*cda5da8dSAndroid Build Coastguard Worker        if runtime_library_dirs is None:
451*cda5da8dSAndroid Build Coastguard Worker            runtime_library_dirs = self.runtime_library_dirs
452*cda5da8dSAndroid Build Coastguard Worker        elif isinstance(runtime_library_dirs, (list, tuple)):
453*cda5da8dSAndroid Build Coastguard Worker            runtime_library_dirs = (list(runtime_library_dirs) +
454*cda5da8dSAndroid Build Coastguard Worker                                    (self.runtime_library_dirs or []))
455*cda5da8dSAndroid Build Coastguard Worker        else:
456*cda5da8dSAndroid Build Coastguard Worker            raise TypeError("'runtime_library_dirs' (if supplied) "
457*cda5da8dSAndroid Build Coastguard Worker                            "must be a list of strings")
458*cda5da8dSAndroid Build Coastguard Worker
459*cda5da8dSAndroid Build Coastguard Worker        return (libraries, library_dirs, runtime_library_dirs)
460*cda5da8dSAndroid Build Coastguard Worker
461*cda5da8dSAndroid Build Coastguard Worker    def _need_link(self, objects, output_file):
462*cda5da8dSAndroid Build Coastguard Worker        """Return true if we need to relink the files listed in 'objects'
463*cda5da8dSAndroid Build Coastguard Worker        to recreate 'output_file'.
464*cda5da8dSAndroid Build Coastguard Worker        """
465*cda5da8dSAndroid Build Coastguard Worker        if self.force:
466*cda5da8dSAndroid Build Coastguard Worker            return True
467*cda5da8dSAndroid Build Coastguard Worker        else:
468*cda5da8dSAndroid Build Coastguard Worker            if self.dry_run:
469*cda5da8dSAndroid Build Coastguard Worker                newer = newer_group (objects, output_file, missing='newer')
470*cda5da8dSAndroid Build Coastguard Worker            else:
471*cda5da8dSAndroid Build Coastguard Worker                newer = newer_group (objects, output_file)
472*cda5da8dSAndroid Build Coastguard Worker            return newer
473*cda5da8dSAndroid Build Coastguard Worker
474*cda5da8dSAndroid Build Coastguard Worker    def detect_language(self, sources):
475*cda5da8dSAndroid Build Coastguard Worker        """Detect the language of a given file, or list of files. Uses
476*cda5da8dSAndroid Build Coastguard Worker        language_map, and language_order to do the job.
477*cda5da8dSAndroid Build Coastguard Worker        """
478*cda5da8dSAndroid Build Coastguard Worker        if not isinstance(sources, list):
479*cda5da8dSAndroid Build Coastguard Worker            sources = [sources]
480*cda5da8dSAndroid Build Coastguard Worker        lang = None
481*cda5da8dSAndroid Build Coastguard Worker        index = len(self.language_order)
482*cda5da8dSAndroid Build Coastguard Worker        for source in sources:
483*cda5da8dSAndroid Build Coastguard Worker            base, ext = os.path.splitext(source)
484*cda5da8dSAndroid Build Coastguard Worker            extlang = self.language_map.get(ext)
485*cda5da8dSAndroid Build Coastguard Worker            try:
486*cda5da8dSAndroid Build Coastguard Worker                extindex = self.language_order.index(extlang)
487*cda5da8dSAndroid Build Coastguard Worker                if extindex < index:
488*cda5da8dSAndroid Build Coastguard Worker                    lang = extlang
489*cda5da8dSAndroid Build Coastguard Worker                    index = extindex
490*cda5da8dSAndroid Build Coastguard Worker            except ValueError:
491*cda5da8dSAndroid Build Coastguard Worker                pass
492*cda5da8dSAndroid Build Coastguard Worker        return lang
493*cda5da8dSAndroid Build Coastguard Worker
494*cda5da8dSAndroid Build Coastguard Worker
495*cda5da8dSAndroid Build Coastguard Worker    # -- Worker methods ------------------------------------------------
496*cda5da8dSAndroid Build Coastguard Worker    # (must be implemented by subclasses)
497*cda5da8dSAndroid Build Coastguard Worker
498*cda5da8dSAndroid Build Coastguard Worker    def preprocess(self, source, output_file=None, macros=None,
499*cda5da8dSAndroid Build Coastguard Worker                   include_dirs=None, extra_preargs=None, extra_postargs=None):
500*cda5da8dSAndroid Build Coastguard Worker        """Preprocess a single C/C++ source file, named in 'source'.
501*cda5da8dSAndroid Build Coastguard Worker        Output will be written to file named 'output_file', or stdout if
502*cda5da8dSAndroid Build Coastguard Worker        'output_file' not supplied.  'macros' is a list of macro
503*cda5da8dSAndroid Build Coastguard Worker        definitions as for 'compile()', which will augment the macros set
504*cda5da8dSAndroid Build Coastguard Worker        with 'define_macro()' and 'undefine_macro()'.  'include_dirs' is a
505*cda5da8dSAndroid Build Coastguard Worker        list of directory names that will be added to the default list.
506*cda5da8dSAndroid Build Coastguard Worker
507*cda5da8dSAndroid Build Coastguard Worker        Raises PreprocessError on failure.
508*cda5da8dSAndroid Build Coastguard Worker        """
509*cda5da8dSAndroid Build Coastguard Worker        pass
510*cda5da8dSAndroid Build Coastguard Worker
511*cda5da8dSAndroid Build Coastguard Worker    def compile(self, sources, output_dir=None, macros=None,
512*cda5da8dSAndroid Build Coastguard Worker                include_dirs=None, debug=0, extra_preargs=None,
513*cda5da8dSAndroid Build Coastguard Worker                extra_postargs=None, depends=None):
514*cda5da8dSAndroid Build Coastguard Worker        """Compile one or more source files.
515*cda5da8dSAndroid Build Coastguard Worker
516*cda5da8dSAndroid Build Coastguard Worker        'sources' must be a list of filenames, most likely C/C++
517*cda5da8dSAndroid Build Coastguard Worker        files, but in reality anything that can be handled by a
518*cda5da8dSAndroid Build Coastguard Worker        particular compiler and compiler class (eg. MSVCCompiler can
519*cda5da8dSAndroid Build Coastguard Worker        handle resource files in 'sources').  Return a list of object
520*cda5da8dSAndroid Build Coastguard Worker        filenames, one per source filename in 'sources'.  Depending on
521*cda5da8dSAndroid Build Coastguard Worker        the implementation, not all source files will necessarily be
522*cda5da8dSAndroid Build Coastguard Worker        compiled, but all corresponding object filenames will be
523*cda5da8dSAndroid Build Coastguard Worker        returned.
524*cda5da8dSAndroid Build Coastguard Worker
525*cda5da8dSAndroid Build Coastguard Worker        If 'output_dir' is given, object files will be put under it, while
526*cda5da8dSAndroid Build Coastguard Worker        retaining their original path component.  That is, "foo/bar.c"
527*cda5da8dSAndroid Build Coastguard Worker        normally compiles to "foo/bar.o" (for a Unix implementation); if
528*cda5da8dSAndroid Build Coastguard Worker        'output_dir' is "build", then it would compile to
529*cda5da8dSAndroid Build Coastguard Worker        "build/foo/bar.o".
530*cda5da8dSAndroid Build Coastguard Worker
531*cda5da8dSAndroid Build Coastguard Worker        'macros', if given, must be a list of macro definitions.  A macro
532*cda5da8dSAndroid Build Coastguard Worker        definition is either a (name, value) 2-tuple or a (name,) 1-tuple.
533*cda5da8dSAndroid Build Coastguard Worker        The former defines a macro; if the value is None, the macro is
534*cda5da8dSAndroid Build Coastguard Worker        defined without an explicit value.  The 1-tuple case undefines a
535*cda5da8dSAndroid Build Coastguard Worker        macro.  Later definitions/redefinitions/ undefinitions take
536*cda5da8dSAndroid Build Coastguard Worker        precedence.
537*cda5da8dSAndroid Build Coastguard Worker
538*cda5da8dSAndroid Build Coastguard Worker        'include_dirs', if given, must be a list of strings, the
539*cda5da8dSAndroid Build Coastguard Worker        directories to add to the default include file search path for this
540*cda5da8dSAndroid Build Coastguard Worker        compilation only.
541*cda5da8dSAndroid Build Coastguard Worker
542*cda5da8dSAndroid Build Coastguard Worker        'debug' is a boolean; if true, the compiler will be instructed to
543*cda5da8dSAndroid Build Coastguard Worker        output debug symbols in (or alongside) the object file(s).
544*cda5da8dSAndroid Build Coastguard Worker
545*cda5da8dSAndroid Build Coastguard Worker        'extra_preargs' and 'extra_postargs' are implementation- dependent.
546*cda5da8dSAndroid Build Coastguard Worker        On platforms that have the notion of a command-line (e.g. Unix,
547*cda5da8dSAndroid Build Coastguard Worker        DOS/Windows), they are most likely lists of strings: extra
548*cda5da8dSAndroid Build Coastguard Worker        command-line arguments to prepend/append to the compiler command
549*cda5da8dSAndroid Build Coastguard Worker        line.  On other platforms, consult the implementation class
550*cda5da8dSAndroid Build Coastguard Worker        documentation.  In any event, they are intended as an escape hatch
551*cda5da8dSAndroid Build Coastguard Worker        for those occasions when the abstract compiler framework doesn't
552*cda5da8dSAndroid Build Coastguard Worker        cut the mustard.
553*cda5da8dSAndroid Build Coastguard Worker
554*cda5da8dSAndroid Build Coastguard Worker        'depends', if given, is a list of filenames that all targets
555*cda5da8dSAndroid Build Coastguard Worker        depend on.  If a source file is older than any file in
556*cda5da8dSAndroid Build Coastguard Worker        depends, then the source file will be recompiled.  This
557*cda5da8dSAndroid Build Coastguard Worker        supports dependency tracking, but only at a coarse
558*cda5da8dSAndroid Build Coastguard Worker        granularity.
559*cda5da8dSAndroid Build Coastguard Worker
560*cda5da8dSAndroid Build Coastguard Worker        Raises CompileError on failure.
561*cda5da8dSAndroid Build Coastguard Worker        """
562*cda5da8dSAndroid Build Coastguard Worker        # A concrete compiler class can either override this method
563*cda5da8dSAndroid Build Coastguard Worker        # entirely or implement _compile().
564*cda5da8dSAndroid Build Coastguard Worker        macros, objects, extra_postargs, pp_opts, build = \
565*cda5da8dSAndroid Build Coastguard Worker                self._setup_compile(output_dir, macros, include_dirs, sources,
566*cda5da8dSAndroid Build Coastguard Worker                                    depends, extra_postargs)
567*cda5da8dSAndroid Build Coastguard Worker        cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
568*cda5da8dSAndroid Build Coastguard Worker
569*cda5da8dSAndroid Build Coastguard Worker        for obj in objects:
570*cda5da8dSAndroid Build Coastguard Worker            try:
571*cda5da8dSAndroid Build Coastguard Worker                src, ext = build[obj]
572*cda5da8dSAndroid Build Coastguard Worker            except KeyError:
573*cda5da8dSAndroid Build Coastguard Worker                continue
574*cda5da8dSAndroid Build Coastguard Worker            self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
575*cda5da8dSAndroid Build Coastguard Worker
576*cda5da8dSAndroid Build Coastguard Worker        # Return *all* object filenames, not just the ones we just built.
577*cda5da8dSAndroid Build Coastguard Worker        return objects
578*cda5da8dSAndroid Build Coastguard Worker
579*cda5da8dSAndroid Build Coastguard Worker    def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
580*cda5da8dSAndroid Build Coastguard Worker        """Compile 'src' to product 'obj'."""
581*cda5da8dSAndroid Build Coastguard Worker        # A concrete compiler class that does not override compile()
582*cda5da8dSAndroid Build Coastguard Worker        # should implement _compile().
583*cda5da8dSAndroid Build Coastguard Worker        pass
584*cda5da8dSAndroid Build Coastguard Worker
585*cda5da8dSAndroid Build Coastguard Worker    def create_static_lib(self, objects, output_libname, output_dir=None,
586*cda5da8dSAndroid Build Coastguard Worker                          debug=0, target_lang=None):
587*cda5da8dSAndroid Build Coastguard Worker        """Link a bunch of stuff together to create a static library file.
588*cda5da8dSAndroid Build Coastguard Worker        The "bunch of stuff" consists of the list of object files supplied
589*cda5da8dSAndroid Build Coastguard Worker        as 'objects', the extra object files supplied to
590*cda5da8dSAndroid Build Coastguard Worker        'add_link_object()' and/or 'set_link_objects()', the libraries
591*cda5da8dSAndroid Build Coastguard Worker        supplied to 'add_library()' and/or 'set_libraries()', and the
592*cda5da8dSAndroid Build Coastguard Worker        libraries supplied as 'libraries' (if any).
593*cda5da8dSAndroid Build Coastguard Worker
594*cda5da8dSAndroid Build Coastguard Worker        'output_libname' should be a library name, not a filename; the
595*cda5da8dSAndroid Build Coastguard Worker        filename will be inferred from the library name.  'output_dir' is
596*cda5da8dSAndroid Build Coastguard Worker        the directory where the library file will be put.
597*cda5da8dSAndroid Build Coastguard Worker
598*cda5da8dSAndroid Build Coastguard Worker        'debug' is a boolean; if true, debugging information will be
599*cda5da8dSAndroid Build Coastguard Worker        included in the library (note that on most platforms, it is the
600*cda5da8dSAndroid Build Coastguard Worker        compile step where this matters: the 'debug' flag is included here
601*cda5da8dSAndroid Build Coastguard Worker        just for consistency).
602*cda5da8dSAndroid Build Coastguard Worker
603*cda5da8dSAndroid Build Coastguard Worker        'target_lang' is the target language for which the given objects
604*cda5da8dSAndroid Build Coastguard Worker        are being compiled. This allows specific linkage time treatment of
605*cda5da8dSAndroid Build Coastguard Worker        certain languages.
606*cda5da8dSAndroid Build Coastguard Worker
607*cda5da8dSAndroid Build Coastguard Worker        Raises LibError on failure.
608*cda5da8dSAndroid Build Coastguard Worker        """
609*cda5da8dSAndroid Build Coastguard Worker        pass
610*cda5da8dSAndroid Build Coastguard Worker
611*cda5da8dSAndroid Build Coastguard Worker
612*cda5da8dSAndroid Build Coastguard Worker    # values for target_desc parameter in link()
613*cda5da8dSAndroid Build Coastguard Worker    SHARED_OBJECT = "shared_object"
614*cda5da8dSAndroid Build Coastguard Worker    SHARED_LIBRARY = "shared_library"
615*cda5da8dSAndroid Build Coastguard Worker    EXECUTABLE = "executable"
616*cda5da8dSAndroid Build Coastguard Worker
617*cda5da8dSAndroid Build Coastguard Worker    def link(self,
618*cda5da8dSAndroid Build Coastguard Worker             target_desc,
619*cda5da8dSAndroid Build Coastguard Worker             objects,
620*cda5da8dSAndroid Build Coastguard Worker             output_filename,
621*cda5da8dSAndroid Build Coastguard Worker             output_dir=None,
622*cda5da8dSAndroid Build Coastguard Worker             libraries=None,
623*cda5da8dSAndroid Build Coastguard Worker             library_dirs=None,
624*cda5da8dSAndroid Build Coastguard Worker             runtime_library_dirs=None,
625*cda5da8dSAndroid Build Coastguard Worker             export_symbols=None,
626*cda5da8dSAndroid Build Coastguard Worker             debug=0,
627*cda5da8dSAndroid Build Coastguard Worker             extra_preargs=None,
628*cda5da8dSAndroid Build Coastguard Worker             extra_postargs=None,
629*cda5da8dSAndroid Build Coastguard Worker             build_temp=None,
630*cda5da8dSAndroid Build Coastguard Worker             target_lang=None):
631*cda5da8dSAndroid Build Coastguard Worker        """Link a bunch of stuff together to create an executable or
632*cda5da8dSAndroid Build Coastguard Worker        shared library file.
633*cda5da8dSAndroid Build Coastguard Worker
634*cda5da8dSAndroid Build Coastguard Worker        The "bunch of stuff" consists of the list of object files supplied
635*cda5da8dSAndroid Build Coastguard Worker        as 'objects'.  'output_filename' should be a filename.  If
636*cda5da8dSAndroid Build Coastguard Worker        'output_dir' is supplied, 'output_filename' is relative to it
637*cda5da8dSAndroid Build Coastguard Worker        (i.e. 'output_filename' can provide directory components if
638*cda5da8dSAndroid Build Coastguard Worker        needed).
639*cda5da8dSAndroid Build Coastguard Worker
640*cda5da8dSAndroid Build Coastguard Worker        'libraries' is a list of libraries to link against.  These are
641*cda5da8dSAndroid Build Coastguard Worker        library names, not filenames, since they're translated into
642*cda5da8dSAndroid Build Coastguard Worker        filenames in a platform-specific way (eg. "foo" becomes "libfoo.a"
643*cda5da8dSAndroid Build Coastguard Worker        on Unix and "foo.lib" on DOS/Windows).  However, they can include a
644*cda5da8dSAndroid Build Coastguard Worker        directory component, which means the linker will look in that
645*cda5da8dSAndroid Build Coastguard Worker        specific directory rather than searching all the normal locations.
646*cda5da8dSAndroid Build Coastguard Worker
647*cda5da8dSAndroid Build Coastguard Worker        'library_dirs', if supplied, should be a list of directories to
648*cda5da8dSAndroid Build Coastguard Worker        search for libraries that were specified as bare library names
649*cda5da8dSAndroid Build Coastguard Worker        (ie. no directory component).  These are on top of the system
650*cda5da8dSAndroid Build Coastguard Worker        default and those supplied to 'add_library_dir()' and/or
651*cda5da8dSAndroid Build Coastguard Worker        'set_library_dirs()'.  'runtime_library_dirs' is a list of
652*cda5da8dSAndroid Build Coastguard Worker        directories that will be embedded into the shared library and used
653*cda5da8dSAndroid Build Coastguard Worker        to search for other shared libraries that *it* depends on at
654*cda5da8dSAndroid Build Coastguard Worker        run-time.  (This may only be relevant on Unix.)
655*cda5da8dSAndroid Build Coastguard Worker
656*cda5da8dSAndroid Build Coastguard Worker        'export_symbols' is a list of symbols that the shared library will
657*cda5da8dSAndroid Build Coastguard Worker        export.  (This appears to be relevant only on Windows.)
658*cda5da8dSAndroid Build Coastguard Worker
659*cda5da8dSAndroid Build Coastguard Worker        'debug' is as for 'compile()' and 'create_static_lib()', with the
660*cda5da8dSAndroid Build Coastguard Worker        slight distinction that it actually matters on most platforms (as
661*cda5da8dSAndroid Build Coastguard Worker        opposed to 'create_static_lib()', which includes a 'debug' flag
662*cda5da8dSAndroid Build Coastguard Worker        mostly for form's sake).
663*cda5da8dSAndroid Build Coastguard Worker
664*cda5da8dSAndroid Build Coastguard Worker        'extra_preargs' and 'extra_postargs' are as for 'compile()' (except
665*cda5da8dSAndroid Build Coastguard Worker        of course that they supply command-line arguments for the
666*cda5da8dSAndroid Build Coastguard Worker        particular linker being used).
667*cda5da8dSAndroid Build Coastguard Worker
668*cda5da8dSAndroid Build Coastguard Worker        'target_lang' is the target language for which the given objects
669*cda5da8dSAndroid Build Coastguard Worker        are being compiled. This allows specific linkage time treatment of
670*cda5da8dSAndroid Build Coastguard Worker        certain languages.
671*cda5da8dSAndroid Build Coastguard Worker
672*cda5da8dSAndroid Build Coastguard Worker        Raises LinkError on failure.
673*cda5da8dSAndroid Build Coastguard Worker        """
674*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
675*cda5da8dSAndroid Build Coastguard Worker
676*cda5da8dSAndroid Build Coastguard Worker
677*cda5da8dSAndroid Build Coastguard Worker    # Old 'link_*()' methods, rewritten to use the new 'link()' method.
678*cda5da8dSAndroid Build Coastguard Worker
679*cda5da8dSAndroid Build Coastguard Worker    def link_shared_lib(self,
680*cda5da8dSAndroid Build Coastguard Worker                        objects,
681*cda5da8dSAndroid Build Coastguard Worker                        output_libname,
682*cda5da8dSAndroid Build Coastguard Worker                        output_dir=None,
683*cda5da8dSAndroid Build Coastguard Worker                        libraries=None,
684*cda5da8dSAndroid Build Coastguard Worker                        library_dirs=None,
685*cda5da8dSAndroid Build Coastguard Worker                        runtime_library_dirs=None,
686*cda5da8dSAndroid Build Coastguard Worker                        export_symbols=None,
687*cda5da8dSAndroid Build Coastguard Worker                        debug=0,
688*cda5da8dSAndroid Build Coastguard Worker                        extra_preargs=None,
689*cda5da8dSAndroid Build Coastguard Worker                        extra_postargs=None,
690*cda5da8dSAndroid Build Coastguard Worker                        build_temp=None,
691*cda5da8dSAndroid Build Coastguard Worker                        target_lang=None):
692*cda5da8dSAndroid Build Coastguard Worker        self.link(CCompiler.SHARED_LIBRARY, objects,
693*cda5da8dSAndroid Build Coastguard Worker                  self.library_filename(output_libname, lib_type='shared'),
694*cda5da8dSAndroid Build Coastguard Worker                  output_dir,
695*cda5da8dSAndroid Build Coastguard Worker                  libraries, library_dirs, runtime_library_dirs,
696*cda5da8dSAndroid Build Coastguard Worker                  export_symbols, debug,
697*cda5da8dSAndroid Build Coastguard Worker                  extra_preargs, extra_postargs, build_temp, target_lang)
698*cda5da8dSAndroid Build Coastguard Worker
699*cda5da8dSAndroid Build Coastguard Worker
700*cda5da8dSAndroid Build Coastguard Worker    def link_shared_object(self,
701*cda5da8dSAndroid Build Coastguard Worker                           objects,
702*cda5da8dSAndroid Build Coastguard Worker                           output_filename,
703*cda5da8dSAndroid Build Coastguard Worker                           output_dir=None,
704*cda5da8dSAndroid Build Coastguard Worker                           libraries=None,
705*cda5da8dSAndroid Build Coastguard Worker                           library_dirs=None,
706*cda5da8dSAndroid Build Coastguard Worker                           runtime_library_dirs=None,
707*cda5da8dSAndroid Build Coastguard Worker                           export_symbols=None,
708*cda5da8dSAndroid Build Coastguard Worker                           debug=0,
709*cda5da8dSAndroid Build Coastguard Worker                           extra_preargs=None,
710*cda5da8dSAndroid Build Coastguard Worker                           extra_postargs=None,
711*cda5da8dSAndroid Build Coastguard Worker                           build_temp=None,
712*cda5da8dSAndroid Build Coastguard Worker                           target_lang=None):
713*cda5da8dSAndroid Build Coastguard Worker        self.link(CCompiler.SHARED_OBJECT, objects,
714*cda5da8dSAndroid Build Coastguard Worker                  output_filename, output_dir,
715*cda5da8dSAndroid Build Coastguard Worker                  libraries, library_dirs, runtime_library_dirs,
716*cda5da8dSAndroid Build Coastguard Worker                  export_symbols, debug,
717*cda5da8dSAndroid Build Coastguard Worker                  extra_preargs, extra_postargs, build_temp, target_lang)
718*cda5da8dSAndroid Build Coastguard Worker
719*cda5da8dSAndroid Build Coastguard Worker
720*cda5da8dSAndroid Build Coastguard Worker    def link_executable(self,
721*cda5da8dSAndroid Build Coastguard Worker                        objects,
722*cda5da8dSAndroid Build Coastguard Worker                        output_progname,
723*cda5da8dSAndroid Build Coastguard Worker                        output_dir=None,
724*cda5da8dSAndroid Build Coastguard Worker                        libraries=None,
725*cda5da8dSAndroid Build Coastguard Worker                        library_dirs=None,
726*cda5da8dSAndroid Build Coastguard Worker                        runtime_library_dirs=None,
727*cda5da8dSAndroid Build Coastguard Worker                        debug=0,
728*cda5da8dSAndroid Build Coastguard Worker                        extra_preargs=None,
729*cda5da8dSAndroid Build Coastguard Worker                        extra_postargs=None,
730*cda5da8dSAndroid Build Coastguard Worker                        target_lang=None):
731*cda5da8dSAndroid Build Coastguard Worker        self.link(CCompiler.EXECUTABLE, objects,
732*cda5da8dSAndroid Build Coastguard Worker                  self.executable_filename(output_progname), output_dir,
733*cda5da8dSAndroid Build Coastguard Worker                  libraries, library_dirs, runtime_library_dirs, None,
734*cda5da8dSAndroid Build Coastguard Worker                  debug, extra_preargs, extra_postargs, None, target_lang)
735*cda5da8dSAndroid Build Coastguard Worker
736*cda5da8dSAndroid Build Coastguard Worker
737*cda5da8dSAndroid Build Coastguard Worker    # -- Miscellaneous methods -----------------------------------------
738*cda5da8dSAndroid Build Coastguard Worker    # These are all used by the 'gen_lib_options() function; there is
739*cda5da8dSAndroid Build Coastguard Worker    # no appropriate default implementation so subclasses should
740*cda5da8dSAndroid Build Coastguard Worker    # implement all of these.
741*cda5da8dSAndroid Build Coastguard Worker
742*cda5da8dSAndroid Build Coastguard Worker    def library_dir_option(self, dir):
743*cda5da8dSAndroid Build Coastguard Worker        """Return the compiler option to add 'dir' to the list of
744*cda5da8dSAndroid Build Coastguard Worker        directories searched for libraries.
745*cda5da8dSAndroid Build Coastguard Worker        """
746*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
747*cda5da8dSAndroid Build Coastguard Worker
748*cda5da8dSAndroid Build Coastguard Worker    def runtime_library_dir_option(self, dir):
749*cda5da8dSAndroid Build Coastguard Worker        """Return the compiler option to add 'dir' to the list of
750*cda5da8dSAndroid Build Coastguard Worker        directories searched for runtime libraries.
751*cda5da8dSAndroid Build Coastguard Worker        """
752*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
753*cda5da8dSAndroid Build Coastguard Worker
754*cda5da8dSAndroid Build Coastguard Worker    def library_option(self, lib):
755*cda5da8dSAndroid Build Coastguard Worker        """Return the compiler option to add 'lib' to the list of libraries
756*cda5da8dSAndroid Build Coastguard Worker        linked into the shared library or executable.
757*cda5da8dSAndroid Build Coastguard Worker        """
758*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
759*cda5da8dSAndroid Build Coastguard Worker
760*cda5da8dSAndroid Build Coastguard Worker    def has_function(self, funcname, includes=None, include_dirs=None,
761*cda5da8dSAndroid Build Coastguard Worker                     libraries=None, library_dirs=None):
762*cda5da8dSAndroid Build Coastguard Worker        """Return a boolean indicating whether funcname is supported on
763*cda5da8dSAndroid Build Coastguard Worker        the current platform.  The optional arguments can be used to
764*cda5da8dSAndroid Build Coastguard Worker        augment the compilation environment.
765*cda5da8dSAndroid Build Coastguard Worker        """
766*cda5da8dSAndroid Build Coastguard Worker        # this can't be included at module scope because it tries to
767*cda5da8dSAndroid Build Coastguard Worker        # import math which might not be available at that point - maybe
768*cda5da8dSAndroid Build Coastguard Worker        # the necessary logic should just be inlined?
769*cda5da8dSAndroid Build Coastguard Worker        import tempfile
770*cda5da8dSAndroid Build Coastguard Worker        if includes is None:
771*cda5da8dSAndroid Build Coastguard Worker            includes = []
772*cda5da8dSAndroid Build Coastguard Worker        if include_dirs is None:
773*cda5da8dSAndroid Build Coastguard Worker            include_dirs = []
774*cda5da8dSAndroid Build Coastguard Worker        if libraries is None:
775*cda5da8dSAndroid Build Coastguard Worker            libraries = []
776*cda5da8dSAndroid Build Coastguard Worker        if library_dirs is None:
777*cda5da8dSAndroid Build Coastguard Worker            library_dirs = []
778*cda5da8dSAndroid Build Coastguard Worker        fd, fname = tempfile.mkstemp(".c", funcname, text=True)
779*cda5da8dSAndroid Build Coastguard Worker        f = os.fdopen(fd, "w")
780*cda5da8dSAndroid Build Coastguard Worker        try:
781*cda5da8dSAndroid Build Coastguard Worker            for incl in includes:
782*cda5da8dSAndroid Build Coastguard Worker                f.write("""#include "%s"\n""" % incl)
783*cda5da8dSAndroid Build Coastguard Worker            f.write("""\
784*cda5da8dSAndroid Build Coastguard Workerint main (int argc, char **argv) {
785*cda5da8dSAndroid Build Coastguard Worker    %s();
786*cda5da8dSAndroid Build Coastguard Worker    return 0;
787*cda5da8dSAndroid Build Coastguard Worker}
788*cda5da8dSAndroid Build Coastguard Worker""" % funcname)
789*cda5da8dSAndroid Build Coastguard Worker        finally:
790*cda5da8dSAndroid Build Coastguard Worker            f.close()
791*cda5da8dSAndroid Build Coastguard Worker        try:
792*cda5da8dSAndroid Build Coastguard Worker            objects = self.compile([fname], include_dirs=include_dirs)
793*cda5da8dSAndroid Build Coastguard Worker        except CompileError:
794*cda5da8dSAndroid Build Coastguard Worker            return False
795*cda5da8dSAndroid Build Coastguard Worker
796*cda5da8dSAndroid Build Coastguard Worker        try:
797*cda5da8dSAndroid Build Coastguard Worker            self.link_executable(objects, "a.out",
798*cda5da8dSAndroid Build Coastguard Worker                                 libraries=libraries,
799*cda5da8dSAndroid Build Coastguard Worker                                 library_dirs=library_dirs)
800*cda5da8dSAndroid Build Coastguard Worker        except (LinkError, TypeError):
801*cda5da8dSAndroid Build Coastguard Worker            return False
802*cda5da8dSAndroid Build Coastguard Worker        return True
803*cda5da8dSAndroid Build Coastguard Worker
804*cda5da8dSAndroid Build Coastguard Worker    def find_library_file (self, dirs, lib, debug=0):
805*cda5da8dSAndroid Build Coastguard Worker        """Search the specified list of directories for a static or shared
806*cda5da8dSAndroid Build Coastguard Worker        library file 'lib' and return the full path to that file.  If
807*cda5da8dSAndroid Build Coastguard Worker        'debug' true, look for a debugging version (if that makes sense on
808*cda5da8dSAndroid Build Coastguard Worker        the current platform).  Return None if 'lib' wasn't found in any of
809*cda5da8dSAndroid Build Coastguard Worker        the specified directories.
810*cda5da8dSAndroid Build Coastguard Worker        """
811*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
812*cda5da8dSAndroid Build Coastguard Worker
813*cda5da8dSAndroid Build Coastguard Worker    # -- Filename generation methods -----------------------------------
814*cda5da8dSAndroid Build Coastguard Worker
815*cda5da8dSAndroid Build Coastguard Worker    # The default implementation of the filename generating methods are
816*cda5da8dSAndroid Build Coastguard Worker    # prejudiced towards the Unix/DOS/Windows view of the world:
817*cda5da8dSAndroid Build Coastguard Worker    #   * object files are named by replacing the source file extension
818*cda5da8dSAndroid Build Coastguard Worker    #     (eg. .c/.cpp -> .o/.obj)
819*cda5da8dSAndroid Build Coastguard Worker    #   * library files (shared or static) are named by plugging the
820*cda5da8dSAndroid Build Coastguard Worker    #     library name and extension into a format string, eg.
821*cda5da8dSAndroid Build Coastguard Worker    #     "lib%s.%s" % (lib_name, ".a") for Unix static libraries
822*cda5da8dSAndroid Build Coastguard Worker    #   * executables are named by appending an extension (possibly
823*cda5da8dSAndroid Build Coastguard Worker    #     empty) to the program name: eg. progname + ".exe" for
824*cda5da8dSAndroid Build Coastguard Worker    #     Windows
825*cda5da8dSAndroid Build Coastguard Worker    #
826*cda5da8dSAndroid Build Coastguard Worker    # To reduce redundant code, these methods expect to find
827*cda5da8dSAndroid Build Coastguard Worker    # several attributes in the current object (presumably defined
828*cda5da8dSAndroid Build Coastguard Worker    # as class attributes):
829*cda5da8dSAndroid Build Coastguard Worker    #   * src_extensions -
830*cda5da8dSAndroid Build Coastguard Worker    #     list of C/C++ source file extensions, eg. ['.c', '.cpp']
831*cda5da8dSAndroid Build Coastguard Worker    #   * obj_extension -
832*cda5da8dSAndroid Build Coastguard Worker    #     object file extension, eg. '.o' or '.obj'
833*cda5da8dSAndroid Build Coastguard Worker    #   * static_lib_extension -
834*cda5da8dSAndroid Build Coastguard Worker    #     extension for static library files, eg. '.a' or '.lib'
835*cda5da8dSAndroid Build Coastguard Worker    #   * shared_lib_extension -
836*cda5da8dSAndroid Build Coastguard Worker    #     extension for shared library/object files, eg. '.so', '.dll'
837*cda5da8dSAndroid Build Coastguard Worker    #   * static_lib_format -
838*cda5da8dSAndroid Build Coastguard Worker    #     format string for generating static library filenames,
839*cda5da8dSAndroid Build Coastguard Worker    #     eg. 'lib%s.%s' or '%s.%s'
840*cda5da8dSAndroid Build Coastguard Worker    #   * shared_lib_format
841*cda5da8dSAndroid Build Coastguard Worker    #     format string for generating shared library filenames
842*cda5da8dSAndroid Build Coastguard Worker    #     (probably same as static_lib_format, since the extension
843*cda5da8dSAndroid Build Coastguard Worker    #     is one of the intended parameters to the format string)
844*cda5da8dSAndroid Build Coastguard Worker    #   * exe_extension -
845*cda5da8dSAndroid Build Coastguard Worker    #     extension for executable files, eg. '' or '.exe'
846*cda5da8dSAndroid Build Coastguard Worker
847*cda5da8dSAndroid Build Coastguard Worker    def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
848*cda5da8dSAndroid Build Coastguard Worker        if output_dir is None:
849*cda5da8dSAndroid Build Coastguard Worker            output_dir = ''
850*cda5da8dSAndroid Build Coastguard Worker        obj_names = []
851*cda5da8dSAndroid Build Coastguard Worker        for src_name in source_filenames:
852*cda5da8dSAndroid Build Coastguard Worker            base, ext = os.path.splitext(src_name)
853*cda5da8dSAndroid Build Coastguard Worker            base = os.path.splitdrive(base)[1] # Chop off the drive
854*cda5da8dSAndroid Build Coastguard Worker            base = base[os.path.isabs(base):]  # If abs, chop off leading /
855*cda5da8dSAndroid Build Coastguard Worker            if ext not in self.src_extensions:
856*cda5da8dSAndroid Build Coastguard Worker                raise UnknownFileError(
857*cda5da8dSAndroid Build Coastguard Worker                      "unknown file type '%s' (from '%s')" % (ext, src_name))
858*cda5da8dSAndroid Build Coastguard Worker            if strip_dir:
859*cda5da8dSAndroid Build Coastguard Worker                base = os.path.basename(base)
860*cda5da8dSAndroid Build Coastguard Worker            obj_names.append(os.path.join(output_dir,
861*cda5da8dSAndroid Build Coastguard Worker                                          base + self.obj_extension))
862*cda5da8dSAndroid Build Coastguard Worker        return obj_names
863*cda5da8dSAndroid Build Coastguard Worker
864*cda5da8dSAndroid Build Coastguard Worker    def shared_object_filename(self, basename, strip_dir=0, output_dir=''):
865*cda5da8dSAndroid Build Coastguard Worker        assert output_dir is not None
866*cda5da8dSAndroid Build Coastguard Worker        if strip_dir:
867*cda5da8dSAndroid Build Coastguard Worker            basename = os.path.basename(basename)
868*cda5da8dSAndroid Build Coastguard Worker        return os.path.join(output_dir, basename + self.shared_lib_extension)
869*cda5da8dSAndroid Build Coastguard Worker
870*cda5da8dSAndroid Build Coastguard Worker    def executable_filename(self, basename, strip_dir=0, output_dir=''):
871*cda5da8dSAndroid Build Coastguard Worker        assert output_dir is not None
872*cda5da8dSAndroid Build Coastguard Worker        if strip_dir:
873*cda5da8dSAndroid Build Coastguard Worker            basename = os.path.basename(basename)
874*cda5da8dSAndroid Build Coastguard Worker        return os.path.join(output_dir, basename + (self.exe_extension or ''))
875*cda5da8dSAndroid Build Coastguard Worker
876*cda5da8dSAndroid Build Coastguard Worker    def library_filename(self, libname, lib_type='static',     # or 'shared'
877*cda5da8dSAndroid Build Coastguard Worker                         strip_dir=0, output_dir=''):
878*cda5da8dSAndroid Build Coastguard Worker        assert output_dir is not None
879*cda5da8dSAndroid Build Coastguard Worker        if lib_type not in ("static", "shared", "dylib", "xcode_stub"):
880*cda5da8dSAndroid Build Coastguard Worker            raise ValueError(
881*cda5da8dSAndroid Build Coastguard Worker                  "'lib_type' must be \"static\", \"shared\", \"dylib\", or \"xcode_stub\"")
882*cda5da8dSAndroid Build Coastguard Worker        fmt = getattr(self, lib_type + "_lib_format")
883*cda5da8dSAndroid Build Coastguard Worker        ext = getattr(self, lib_type + "_lib_extension")
884*cda5da8dSAndroid Build Coastguard Worker
885*cda5da8dSAndroid Build Coastguard Worker        dir, base = os.path.split(libname)
886*cda5da8dSAndroid Build Coastguard Worker        filename = fmt % (base, ext)
887*cda5da8dSAndroid Build Coastguard Worker        if strip_dir:
888*cda5da8dSAndroid Build Coastguard Worker            dir = ''
889*cda5da8dSAndroid Build Coastguard Worker
890*cda5da8dSAndroid Build Coastguard Worker        return os.path.join(output_dir, dir, filename)
891*cda5da8dSAndroid Build Coastguard Worker
892*cda5da8dSAndroid Build Coastguard Worker
893*cda5da8dSAndroid Build Coastguard Worker    # -- Utility methods -----------------------------------------------
894*cda5da8dSAndroid Build Coastguard Worker
895*cda5da8dSAndroid Build Coastguard Worker    def announce(self, msg, level=1):
896*cda5da8dSAndroid Build Coastguard Worker        log.debug(msg)
897*cda5da8dSAndroid Build Coastguard Worker
898*cda5da8dSAndroid Build Coastguard Worker    def debug_print(self, msg):
899*cda5da8dSAndroid Build Coastguard Worker        from distutils.debug import DEBUG
900*cda5da8dSAndroid Build Coastguard Worker        if DEBUG:
901*cda5da8dSAndroid Build Coastguard Worker            print(msg)
902*cda5da8dSAndroid Build Coastguard Worker
903*cda5da8dSAndroid Build Coastguard Worker    def warn(self, msg):
904*cda5da8dSAndroid Build Coastguard Worker        sys.stderr.write("warning: %s\n" % msg)
905*cda5da8dSAndroid Build Coastguard Worker
906*cda5da8dSAndroid Build Coastguard Worker    def execute(self, func, args, msg=None, level=1):
907*cda5da8dSAndroid Build Coastguard Worker        execute(func, args, msg, self.dry_run)
908*cda5da8dSAndroid Build Coastguard Worker
909*cda5da8dSAndroid Build Coastguard Worker    def spawn(self, cmd):
910*cda5da8dSAndroid Build Coastguard Worker        spawn(cmd, dry_run=self.dry_run)
911*cda5da8dSAndroid Build Coastguard Worker
912*cda5da8dSAndroid Build Coastguard Worker    def move_file(self, src, dst):
913*cda5da8dSAndroid Build Coastguard Worker        return move_file(src, dst, dry_run=self.dry_run)
914*cda5da8dSAndroid Build Coastguard Worker
915*cda5da8dSAndroid Build Coastguard Worker    def mkpath (self, name, mode=0o777):
916*cda5da8dSAndroid Build Coastguard Worker        mkpath(name, mode, dry_run=self.dry_run)
917*cda5da8dSAndroid Build Coastguard Worker
918*cda5da8dSAndroid Build Coastguard Worker
919*cda5da8dSAndroid Build Coastguard Worker# Map a sys.platform/os.name ('posix', 'nt') to the default compiler
920*cda5da8dSAndroid Build Coastguard Worker# type for that platform. Keys are interpreted as re match
921*cda5da8dSAndroid Build Coastguard Worker# patterns. Order is important; platform mappings are preferred over
922*cda5da8dSAndroid Build Coastguard Worker# OS names.
923*cda5da8dSAndroid Build Coastguard Worker_default_compilers = (
924*cda5da8dSAndroid Build Coastguard Worker
925*cda5da8dSAndroid Build Coastguard Worker    # Platform string mappings
926*cda5da8dSAndroid Build Coastguard Worker
927*cda5da8dSAndroid Build Coastguard Worker    # on a cygwin built python we can use gcc like an ordinary UNIXish
928*cda5da8dSAndroid Build Coastguard Worker    # compiler
929*cda5da8dSAndroid Build Coastguard Worker    ('cygwin.*', 'unix'),
930*cda5da8dSAndroid Build Coastguard Worker
931*cda5da8dSAndroid Build Coastguard Worker    # OS name mappings
932*cda5da8dSAndroid Build Coastguard Worker    ('posix', 'unix'),
933*cda5da8dSAndroid Build Coastguard Worker    ('nt', 'msvc'),
934*cda5da8dSAndroid Build Coastguard Worker
935*cda5da8dSAndroid Build Coastguard Worker    )
936*cda5da8dSAndroid Build Coastguard Worker
937*cda5da8dSAndroid Build Coastguard Workerdef get_default_compiler(osname=None, platform=None):
938*cda5da8dSAndroid Build Coastguard Worker    """Determine the default compiler to use for the given platform.
939*cda5da8dSAndroid Build Coastguard Worker
940*cda5da8dSAndroid Build Coastguard Worker       osname should be one of the standard Python OS names (i.e. the
941*cda5da8dSAndroid Build Coastguard Worker       ones returned by os.name) and platform the common value
942*cda5da8dSAndroid Build Coastguard Worker       returned by sys.platform for the platform in question.
943*cda5da8dSAndroid Build Coastguard Worker
944*cda5da8dSAndroid Build Coastguard Worker       The default values are os.name and sys.platform in case the
945*cda5da8dSAndroid Build Coastguard Worker       parameters are not given.
946*cda5da8dSAndroid Build Coastguard Worker    """
947*cda5da8dSAndroid Build Coastguard Worker    if osname is None:
948*cda5da8dSAndroid Build Coastguard Worker        osname = os.name
949*cda5da8dSAndroid Build Coastguard Worker    if platform is None:
950*cda5da8dSAndroid Build Coastguard Worker        platform = sys.platform
951*cda5da8dSAndroid Build Coastguard Worker    for pattern, compiler in _default_compilers:
952*cda5da8dSAndroid Build Coastguard Worker        if re.match(pattern, platform) is not None or \
953*cda5da8dSAndroid Build Coastguard Worker           re.match(pattern, osname) is not None:
954*cda5da8dSAndroid Build Coastguard Worker            return compiler
955*cda5da8dSAndroid Build Coastguard Worker    # Default to Unix compiler
956*cda5da8dSAndroid Build Coastguard Worker    return 'unix'
957*cda5da8dSAndroid Build Coastguard Worker
958*cda5da8dSAndroid Build Coastguard Worker# Map compiler types to (module_name, class_name) pairs -- ie. where to
959*cda5da8dSAndroid Build Coastguard Worker# find the code that implements an interface to this compiler.  (The module
960*cda5da8dSAndroid Build Coastguard Worker# is assumed to be in the 'distutils' package.)
961*cda5da8dSAndroid Build Coastguard Workercompiler_class = { 'unix':    ('unixccompiler', 'UnixCCompiler',
962*cda5da8dSAndroid Build Coastguard Worker                               "standard UNIX-style compiler"),
963*cda5da8dSAndroid Build Coastguard Worker                   'msvc':    ('_msvccompiler', 'MSVCCompiler',
964*cda5da8dSAndroid Build Coastguard Worker                               "Microsoft Visual C++"),
965*cda5da8dSAndroid Build Coastguard Worker                   'cygwin':  ('cygwinccompiler', 'CygwinCCompiler',
966*cda5da8dSAndroid Build Coastguard Worker                               "Cygwin port of GNU C Compiler for Win32"),
967*cda5da8dSAndroid Build Coastguard Worker                   'mingw32': ('cygwinccompiler', 'Mingw32CCompiler',
968*cda5da8dSAndroid Build Coastguard Worker                               "Mingw32 port of GNU C Compiler for Win32"),
969*cda5da8dSAndroid Build Coastguard Worker                   'bcpp':    ('bcppcompiler', 'BCPPCompiler',
970*cda5da8dSAndroid Build Coastguard Worker                               "Borland C++ Compiler"),
971*cda5da8dSAndroid Build Coastguard Worker                 }
972*cda5da8dSAndroid Build Coastguard Worker
973*cda5da8dSAndroid Build Coastguard Workerdef show_compilers():
974*cda5da8dSAndroid Build Coastguard Worker    """Print list of available compilers (used by the "--help-compiler"
975*cda5da8dSAndroid Build Coastguard Worker    options to "build", "build_ext", "build_clib").
976*cda5da8dSAndroid Build Coastguard Worker    """
977*cda5da8dSAndroid Build Coastguard Worker    # XXX this "knows" that the compiler option it's describing is
978*cda5da8dSAndroid Build Coastguard Worker    # "--compiler", which just happens to be the case for the three
979*cda5da8dSAndroid Build Coastguard Worker    # commands that use it.
980*cda5da8dSAndroid Build Coastguard Worker    from distutils.fancy_getopt import FancyGetopt
981*cda5da8dSAndroid Build Coastguard Worker    compilers = []
982*cda5da8dSAndroid Build Coastguard Worker    for compiler in compiler_class.keys():
983*cda5da8dSAndroid Build Coastguard Worker        compilers.append(("compiler="+compiler, None,
984*cda5da8dSAndroid Build Coastguard Worker                          compiler_class[compiler][2]))
985*cda5da8dSAndroid Build Coastguard Worker    compilers.sort()
986*cda5da8dSAndroid Build Coastguard Worker    pretty_printer = FancyGetopt(compilers)
987*cda5da8dSAndroid Build Coastguard Worker    pretty_printer.print_help("List of available compilers:")
988*cda5da8dSAndroid Build Coastguard Worker
989*cda5da8dSAndroid Build Coastguard Worker
990*cda5da8dSAndroid Build Coastguard Workerdef new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
991*cda5da8dSAndroid Build Coastguard Worker    """Generate an instance of some CCompiler subclass for the supplied
992*cda5da8dSAndroid Build Coastguard Worker    platform/compiler combination.  'plat' defaults to 'os.name'
993*cda5da8dSAndroid Build Coastguard Worker    (eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler
994*cda5da8dSAndroid Build Coastguard Worker    for that platform.  Currently only 'posix' and 'nt' are supported, and
995*cda5da8dSAndroid Build Coastguard Worker    the default compilers are "traditional Unix interface" (UnixCCompiler
996*cda5da8dSAndroid Build Coastguard Worker    class) and Visual C++ (MSVCCompiler class).  Note that it's perfectly
997*cda5da8dSAndroid Build Coastguard Worker    possible to ask for a Unix compiler object under Windows, and a
998*cda5da8dSAndroid Build Coastguard Worker    Microsoft compiler object under Unix -- if you supply a value for
999*cda5da8dSAndroid Build Coastguard Worker    'compiler', 'plat' is ignored.
1000*cda5da8dSAndroid Build Coastguard Worker    """
1001*cda5da8dSAndroid Build Coastguard Worker    if plat is None:
1002*cda5da8dSAndroid Build Coastguard Worker        plat = os.name
1003*cda5da8dSAndroid Build Coastguard Worker
1004*cda5da8dSAndroid Build Coastguard Worker    try:
1005*cda5da8dSAndroid Build Coastguard Worker        if compiler is None:
1006*cda5da8dSAndroid Build Coastguard Worker            compiler = get_default_compiler(plat)
1007*cda5da8dSAndroid Build Coastguard Worker
1008*cda5da8dSAndroid Build Coastguard Worker        (module_name, class_name, long_description) = compiler_class[compiler]
1009*cda5da8dSAndroid Build Coastguard Worker    except KeyError:
1010*cda5da8dSAndroid Build Coastguard Worker        msg = "don't know how to compile C/C++ code on platform '%s'" % plat
1011*cda5da8dSAndroid Build Coastguard Worker        if compiler is not None:
1012*cda5da8dSAndroid Build Coastguard Worker            msg = msg + " with '%s' compiler" % compiler
1013*cda5da8dSAndroid Build Coastguard Worker        raise DistutilsPlatformError(msg)
1014*cda5da8dSAndroid Build Coastguard Worker
1015*cda5da8dSAndroid Build Coastguard Worker    try:
1016*cda5da8dSAndroid Build Coastguard Worker        module_name = "distutils." + module_name
1017*cda5da8dSAndroid Build Coastguard Worker        __import__ (module_name)
1018*cda5da8dSAndroid Build Coastguard Worker        module = sys.modules[module_name]
1019*cda5da8dSAndroid Build Coastguard Worker        klass = vars(module)[class_name]
1020*cda5da8dSAndroid Build Coastguard Worker    except ImportError:
1021*cda5da8dSAndroid Build Coastguard Worker        raise DistutilsModuleError(
1022*cda5da8dSAndroid Build Coastguard Worker              "can't compile C/C++ code: unable to load module '%s'" % \
1023*cda5da8dSAndroid Build Coastguard Worker              module_name)
1024*cda5da8dSAndroid Build Coastguard Worker    except KeyError:
1025*cda5da8dSAndroid Build Coastguard Worker        raise DistutilsModuleError(
1026*cda5da8dSAndroid Build Coastguard Worker               "can't compile C/C++ code: unable to find class '%s' "
1027*cda5da8dSAndroid Build Coastguard Worker               "in module '%s'" % (class_name, module_name))
1028*cda5da8dSAndroid Build Coastguard Worker
1029*cda5da8dSAndroid Build Coastguard Worker    # XXX The None is necessary to preserve backwards compatibility
1030*cda5da8dSAndroid Build Coastguard Worker    # with classes that expect verbose to be the first positional
1031*cda5da8dSAndroid Build Coastguard Worker    # argument.
1032*cda5da8dSAndroid Build Coastguard Worker    return klass(None, dry_run, force)
1033*cda5da8dSAndroid Build Coastguard Worker
1034*cda5da8dSAndroid Build Coastguard Worker
1035*cda5da8dSAndroid Build Coastguard Workerdef gen_preprocess_options(macros, include_dirs):
1036*cda5da8dSAndroid Build Coastguard Worker    """Generate C pre-processor options (-D, -U, -I) as used by at least
1037*cda5da8dSAndroid Build Coastguard Worker    two types of compilers: the typical Unix compiler and Visual C++.
1038*cda5da8dSAndroid Build Coastguard Worker    'macros' is the usual thing, a list of 1- or 2-tuples, where (name,)
1039*cda5da8dSAndroid Build Coastguard Worker    means undefine (-U) macro 'name', and (name,value) means define (-D)
1040*cda5da8dSAndroid Build Coastguard Worker    macro 'name' to 'value'.  'include_dirs' is just a list of directory
1041*cda5da8dSAndroid Build Coastguard Worker    names to be added to the header file search path (-I).  Returns a list
1042*cda5da8dSAndroid Build Coastguard Worker    of command-line options suitable for either Unix compilers or Visual
1043*cda5da8dSAndroid Build Coastguard Worker    C++.
1044*cda5da8dSAndroid Build Coastguard Worker    """
1045*cda5da8dSAndroid Build Coastguard Worker    # XXX it would be nice (mainly aesthetic, and so we don't generate
1046*cda5da8dSAndroid Build Coastguard Worker    # stupid-looking command lines) to go over 'macros' and eliminate
1047*cda5da8dSAndroid Build Coastguard Worker    # redundant definitions/undefinitions (ie. ensure that only the
1048*cda5da8dSAndroid Build Coastguard Worker    # latest mention of a particular macro winds up on the command
1049*cda5da8dSAndroid Build Coastguard Worker    # line).  I don't think it's essential, though, since most (all?)
1050*cda5da8dSAndroid Build Coastguard Worker    # Unix C compilers only pay attention to the latest -D or -U
1051*cda5da8dSAndroid Build Coastguard Worker    # mention of a macro on their command line.  Similar situation for
1052*cda5da8dSAndroid Build Coastguard Worker    # 'include_dirs'.  I'm punting on both for now.  Anyways, weeding out
1053*cda5da8dSAndroid Build Coastguard Worker    # redundancies like this should probably be the province of
1054*cda5da8dSAndroid Build Coastguard Worker    # CCompiler, since the data structures used are inherited from it
1055*cda5da8dSAndroid Build Coastguard Worker    # and therefore common to all CCompiler classes.
1056*cda5da8dSAndroid Build Coastguard Worker    pp_opts = []
1057*cda5da8dSAndroid Build Coastguard Worker    for macro in macros:
1058*cda5da8dSAndroid Build Coastguard Worker        if not (isinstance(macro, tuple) and 1 <= len(macro) <= 2):
1059*cda5da8dSAndroid Build Coastguard Worker            raise TypeError(
1060*cda5da8dSAndroid Build Coastguard Worker                  "bad macro definition '%s': "
1061*cda5da8dSAndroid Build Coastguard Worker                  "each element of 'macros' list must be a 1- or 2-tuple"
1062*cda5da8dSAndroid Build Coastguard Worker                  % macro)
1063*cda5da8dSAndroid Build Coastguard Worker
1064*cda5da8dSAndroid Build Coastguard Worker        if len(macro) == 1:        # undefine this macro
1065*cda5da8dSAndroid Build Coastguard Worker            pp_opts.append("-U%s" % macro[0])
1066*cda5da8dSAndroid Build Coastguard Worker        elif len(macro) == 2:
1067*cda5da8dSAndroid Build Coastguard Worker            if macro[1] is None:    # define with no explicit value
1068*cda5da8dSAndroid Build Coastguard Worker                pp_opts.append("-D%s" % macro[0])
1069*cda5da8dSAndroid Build Coastguard Worker            else:
1070*cda5da8dSAndroid Build Coastguard Worker                # XXX *don't* need to be clever about quoting the
1071*cda5da8dSAndroid Build Coastguard Worker                # macro value here, because we're going to avoid the
1072*cda5da8dSAndroid Build Coastguard Worker                # shell at all costs when we spawn the command!
1073*cda5da8dSAndroid Build Coastguard Worker                pp_opts.append("-D%s=%s" % macro)
1074*cda5da8dSAndroid Build Coastguard Worker
1075*cda5da8dSAndroid Build Coastguard Worker    for dir in include_dirs:
1076*cda5da8dSAndroid Build Coastguard Worker        pp_opts.append("-I%s" % dir)
1077*cda5da8dSAndroid Build Coastguard Worker    return pp_opts
1078*cda5da8dSAndroid Build Coastguard Worker
1079*cda5da8dSAndroid Build Coastguard Worker
1080*cda5da8dSAndroid Build Coastguard Workerdef gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries):
1081*cda5da8dSAndroid Build Coastguard Worker    """Generate linker options for searching library directories and
1082*cda5da8dSAndroid Build Coastguard Worker    linking with specific libraries.  'libraries' and 'library_dirs' are,
1083*cda5da8dSAndroid Build Coastguard Worker    respectively, lists of library names (not filenames!) and search
1084*cda5da8dSAndroid Build Coastguard Worker    directories.  Returns a list of command-line options suitable for use
1085*cda5da8dSAndroid Build Coastguard Worker    with some compiler (depending on the two format strings passed in).
1086*cda5da8dSAndroid Build Coastguard Worker    """
1087*cda5da8dSAndroid Build Coastguard Worker    lib_opts = []
1088*cda5da8dSAndroid Build Coastguard Worker
1089*cda5da8dSAndroid Build Coastguard Worker    for dir in library_dirs:
1090*cda5da8dSAndroid Build Coastguard Worker        lib_opts.append(compiler.library_dir_option(dir))
1091*cda5da8dSAndroid Build Coastguard Worker
1092*cda5da8dSAndroid Build Coastguard Worker    for dir in runtime_library_dirs:
1093*cda5da8dSAndroid Build Coastguard Worker        opt = compiler.runtime_library_dir_option(dir)
1094*cda5da8dSAndroid Build Coastguard Worker        if isinstance(opt, list):
1095*cda5da8dSAndroid Build Coastguard Worker            lib_opts = lib_opts + opt
1096*cda5da8dSAndroid Build Coastguard Worker        else:
1097*cda5da8dSAndroid Build Coastguard Worker            lib_opts.append(opt)
1098*cda5da8dSAndroid Build Coastguard Worker
1099*cda5da8dSAndroid Build Coastguard Worker    # XXX it's important that we *not* remove redundant library mentions!
1100*cda5da8dSAndroid Build Coastguard Worker    # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to
1101*cda5da8dSAndroid Build Coastguard Worker    # resolve all symbols.  I just hope we never have to say "-lfoo obj.o
1102*cda5da8dSAndroid Build Coastguard Worker    # -lbar" to get things to work -- that's certainly a possibility, but a
1103*cda5da8dSAndroid Build Coastguard Worker    # pretty nasty way to arrange your C code.
1104*cda5da8dSAndroid Build Coastguard Worker
1105*cda5da8dSAndroid Build Coastguard Worker    for lib in libraries:
1106*cda5da8dSAndroid Build Coastguard Worker        (lib_dir, lib_name) = os.path.split(lib)
1107*cda5da8dSAndroid Build Coastguard Worker        if lib_dir:
1108*cda5da8dSAndroid Build Coastguard Worker            lib_file = compiler.find_library_file([lib_dir], lib_name)
1109*cda5da8dSAndroid Build Coastguard Worker            if lib_file:
1110*cda5da8dSAndroid Build Coastguard Worker                lib_opts.append(lib_file)
1111*cda5da8dSAndroid Build Coastguard Worker            else:
1112*cda5da8dSAndroid Build Coastguard Worker                compiler.warn("no library file corresponding to "
1113*cda5da8dSAndroid Build Coastguard Worker                              "'%s' found (skipping)" % lib)
1114*cda5da8dSAndroid Build Coastguard Worker        else:
1115*cda5da8dSAndroid Build Coastguard Worker            lib_opts.append(compiler.library_option (lib))
1116*cda5da8dSAndroid Build Coastguard Worker    return lib_opts
1117