summaryrefslogtreecommitdiff
path: root/giscanner/ccompiler.py
diff options
context:
space:
mode:
authorChun-wei Fan <fanchunwei@src.gnome.org>2015-03-10 13:15:52 +0800
committerChun-wei Fan <fanchunwei@src.gnome.org>2015-07-30 11:01:01 +0800
commitad8fc51441b331c7db573201dd283a423081c042 (patch)
treef54c33c452d90e3c1b814b366e9072563236069c /giscanner/ccompiler.py
parent0638f9f130fe080f945d07cde5cc01835a80553a (diff)
downloadgobject-introspection-ad8fc51441b331c7db573201dd283a423081c042.tar.gz
sourcescanner.py: Use Distutils for Preprocessing
Add a preprocess() function in ccompiler.py so that it will call the preprocess() method of the distutils.ccompiler class, and make use of it from sourcescanner.py. As we would need to set up the options (include paths, macros, undefs) to pass into the preprocessor (and later for the compiler), we have a new private function that translates what we have from the rest of giscanner so that it could be passed to distutils.ccompiler in a way that it understands. Also, as the MSVCCompiler classes in distutils do not provide a preprocess() implementation, we provide our own so that we can use it when preprocessing, via distutils, through subclassing MSVCCompiler. https://bugzilla.gnome.org/show_bug.cgi?id=728313
Diffstat (limited to 'giscanner/ccompiler.py')
-rw-r--r--giscanner/ccompiler.py64
1 files changed, 63 insertions, 1 deletions
diff --git a/giscanner/ccompiler.py b/giscanner/ccompiler.py
index 884fc2cc..f9105fe7 100644
--- a/giscanner/ccompiler.py
+++ b/giscanner/ccompiler.py
@@ -61,7 +61,15 @@ class CCompiler(object):
compiler_name = distutils.ccompiler.get_default_compiler()
# Now, create the distutils ccompiler instance based on the info we have.
- self.compiler = distutils.ccompiler.new_compiler(compiler=compiler_name)
+ if compiler_name == 'msvc':
+ # For MSVC, we need to create a instance of a subclass of distutil's
+ # MSVC9Compiler class, as it does not provide a preprocess()
+ # implementation
+ from . import msvccompiler
+ self.compiler = msvccompiler.get_msvc_compiler()
+
+ else:
+ self.compiler = distutils.ccompiler.new_compiler(compiler=compiler_name)
customize_compiler(self.compiler)
# customize_compiler from distutils only does customization
@@ -147,6 +155,31 @@ class CCompiler(object):
else:
args.append('-l' + library)
+ def preprocess(self, source, output, cpp_options):
+ extra_postargs = ['-C']
+ (include_paths, macros, postargs) = self._set_cpp_options(cpp_options)
+
+ # We always want to include the current path
+ include_dirs = ['.']
+
+ include_dirs.extend(include_paths)
+ extra_postargs.extend(postargs)
+
+ # Define these macros when using Visual C++ to silence many warnings,
+ # and prevent stepping on many Visual Studio-specific items, so that
+ # we don't have to handle them specifically in scannerlexer.l
+ if self.check_is_msvc():
+ macros.append(('_USE_DECLSPECS_FOR_SAL', None))
+ macros.append(('_CRT_SECURE_NO_WARNINGS', None))
+ macros.append(('_CRT_NONSTDC_NO_WARNINGS', None))
+ macros.append(('SAL_NO_ATTRIBUTE_DECLARATIONS', None))
+
+ self.compiler.preprocess(source=source,
+ output_file=output,
+ macros=macros,
+ include_dirs=include_dirs,
+ extra_postargs=extra_postargs)
+
def resolve_windows_libs(self, libraries, options):
args = []
libsearch = []
@@ -238,3 +271,32 @@ class CCompiler(object):
return True
else:
return False
+
+ # Private APIs
+ def _set_cpp_options(self, options):
+ includes = []
+ macros = []
+ other_options = []
+
+ for o in options:
+ option = utils.cflag_real_include_path(o)
+ if option.startswith('-I'):
+ includes.append(option[len('-I'):])
+ elif option.startswith('-D'):
+ macro = option[len('-D'):]
+ macro_index = macro.find('=')
+ if macro_index == -1:
+ macro_name = macro
+ macro_value = None
+ else:
+ macro_name = macro[:macro_index]
+ macro_value = macro[macro_index + 1:]
+ macros.append((macro_name, macro_value))
+ elif option.startswith('-U'):
+ macros.append((option[len('-U'):],))
+ else:
+ # We expect the preprocessor to remove macros. If debugging is turned
+ # up high enough that won't happen, so don't add those flags. Bug #720504
+ if option not in ['-g3', '-ggdb3', '-gstabs3', '-gcoff3', '-gxcoff3', '-gvms3']:
+ other_options.append(option)
+ return (includes, macros, other_options)