Let eclipse generator use MSVS compiler flags on Windows.

Patch from Slava Chigrin <vchigrin@yandex-team.ru>!
Review URL: https://codereview.chromium.org/23606009/



git-svn-id: http://gyp.googlecode.com/svn/trunk@1739 78cadc50-ecff-11dd-a971-7dbc132099af
diff --git a/pylib/gyp/generator/eclipse.py b/pylib/gyp/generator/eclipse.py
index 08425da..07c1fa6 100644
--- a/pylib/gyp/generator/eclipse.py
+++ b/pylib/gyp/generator/eclipse.py
@@ -22,6 +22,7 @@
 import subprocess
 import gyp
 import gyp.common
+import gyp.msvs_emulation
 import shlex
 
 generator_wants_static_library_dependencies_adjusted = False
@@ -52,7 +53,30 @@
   generator_flags = params.get('generator_flags', {})
   for key, val in generator_flags.items():
     default_variables.setdefault(key, val)
-  default_variables.setdefault('OS', gyp.common.GetFlavor(params))
+  flavor = gyp.common.GetFlavor(params)
+  default_variables.setdefault('OS', flavor)
+  if flavor == 'win':
+    # Copy additional generator configuration data from VS, which is shared
+    # by the Eclipse generator.
+    import gyp.generator.msvs as msvs_generator
+    generator_additional_non_configuration_keys = getattr(msvs_generator,
+        'generator_additional_non_configuration_keys', [])
+    generator_additional_path_sections = getattr(msvs_generator,
+        'generator_additional_path_sections', [])
+
+    # Set a variable so conditions can be based on msvs_version.
+    msvs_version = gyp.msvs_emulation.GetVSVersion(generator_flags)
+    default_variables['MSVS_VERSION'] = msvs_version.ShortName()
+
+    # To determine processor word size on Windows, in addition to checking
+    # PROCESSOR_ARCHITECTURE (which reflects the word size of the current
+    # process), it is also necessary to check PROCESSOR_ARCHITEW6432 (which
+    # contains the actual word size of the system when running thru WOW64).
+    if ('64' in os.environ.get('PROCESSOR_ARCHITECTURE', '') or
+        '64' in os.environ.get('PROCESSOR_ARCHITEW6432', '')):
+      default_variables['MSVS_OS_BITS'] = 64
+    else:
+      default_variables['MSVS_OS_BITS'] = 32
 
 
 def CalculateGeneratorInputInfo(params):
@@ -65,7 +89,7 @@
 
 
 def GetAllIncludeDirectories(target_list, target_dicts,
-                             shared_intermediate_dirs, config_name):
+                             shared_intermediate_dirs, config_name, params):
   """Calculate the set of include directories to be used.
 
   Returns:
@@ -76,6 +100,9 @@
   gyp_includes_set = set()
   compiler_includes_list = []
 
+  flavor = gyp.common.GetFlavor(params)
+  if flavor == 'win':
+    generator_flags = params.get('generator_flags', {})
   for target_name in target_list:
     target = target_dicts[target_name]
     if config_name in target['configurations']:
@@ -85,7 +112,11 @@
       # may be done in gyp files to force certain includes to come at the end.
       # TODO(jgreenwald): Change the gyp files to not abuse cflags for this, and
       # remove this.
-      cflags = config['cflags']
+      if flavor == 'win':
+        msvs_settings = gyp.msvs_emulation.MsvsSettings(target, generator_flags)
+        cflags = msvs_settings.GetCflags(config_name)
+      else:
+        cflags = config['cflags']
       for cflag in cflags:
         include_dir = ''
         if cflag.startswith('-I'):
@@ -146,7 +177,7 @@
   return 'gcc'
 
 
-def GetAllDefines(target_list, target_dicts, data, config_name):
+def GetAllDefines(target_list, target_dicts, data, config_name, params):
   """Calculate the defines for a project.
 
   Returns:
@@ -156,22 +187,33 @@
 
   # Get defines declared in the gyp files.
   all_defines = {}
+  flavor = gyp.common.GetFlavor(params)
+  if flavor == 'win':
+    generator_flags = params.get('generator_flags', {})
   for target_name in target_list:
     target = target_dicts[target_name]
 
+    if flavor == 'win':
+      msvs_settings = gyp.msvs_emulation.MsvsSettings(target, generator_flags)
+      extra_defines = msvs_settings.GetComputedDefines(config_name)
+    else:
+      extra_defines = []
     if config_name in target['configurations']:
       config = target['configurations'][config_name]
-      for define in config['defines']:
-        split_define = define.split('=', 1)
-        if len(split_define) == 1:
-          split_define.append('1')
-        if split_define[0].strip() in all_defines:
-          # Already defined
-          continue
-
-        all_defines[split_define[0].strip()] = split_define[1].strip()
-
+      target_defines = config['defines']
+    else:
+      target_defines = []
+    for define in target_defines + extra_defines:
+      split_define = define.split('=', 1)
+      if len(split_define) == 1:
+        split_define.append('1')
+      if split_define[0].strip() in all_defines:
+        # Already defined
+        continue
+      all_defines[split_define[0].strip()] = split_define[1].strip()
   # Get default compiler defines (if possible).
+  if flavor == 'win':
+    return all_defines  # Default defines already processed in the loop above.
   cc_target = GetCompilerPath(target_list, target_dicts, data)
   if cc_target:
     command = shlex.split(cc_target)
@@ -250,9 +292,10 @@
   eclipse_langs = ['C++ Source File', 'C Source File', 'Assembly Source File',
                    'GNU C++', 'GNU C', 'Assembly']
   include_dirs = GetAllIncludeDirectories(target_list, target_dicts,
-                                          shared_intermediate_dirs, config_name)
+                                          shared_intermediate_dirs, config_name,
+                                          params)
   WriteIncludePaths(out, eclipse_langs, include_dirs)
-  defines = GetAllDefines(target_list, target_dicts, data, config_name)
+  defines = GetAllDefines(target_list, target_dicts, data, config_name, params)
   WriteMacros(out, eclipse_langs, defines)
 
   out.write('</cdtprojectproperties>\n')