Skip to content

Commit

Permalink
Recognise more include search path opts when populating 'NMakeInclude…
Browse files Browse the repository at this point in the history
…SearchPath' to help intellisense

Added to existing '/I' and '-I' extraction with '-isystem', '/clang:-isystem', '/imsvc', '/external:I', which are forms of 'system' header include search path options for gcc, clang, clang-cl, and cl (msvc).

Factored 3 separate 'extract_...(...)' functions into one since they were always called together on the same args; a new combined '_extract_nmake_fields(...)' func avoids repeated iterations and checks.
  • Loading branch information
GertyP authored and jpakkane committed Sep 10, 2023
1 parent 1070867 commit bd3341f
Showing 1 changed file with 33 additions and 40 deletions.
73 changes: 33 additions & 40 deletions mesonbuild/backend/vs2010backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ def get_non_primary_lang_intellisense_fields(vslite_ctx: dict,
for src_lang, args_list in non_primary_build_args_per_src_lang:
if src_lang not in defs_paths_opts_per_lang_and_buildtype:
defs_paths_opts_per_lang_and_buildtype[src_lang] = {}
defs = Vs2010Backend.extract_nmake_preprocessor_defs(args_list)
paths = Vs2010Backend.extract_nmake_include_paths(args_list)
opts = Vs2010Backend.extract_intellisense_additional_compiler_options(args_list)
defs_paths_opts_per_lang_and_buildtype[src_lang][buildtype] = (defs, paths, opts)
defs_paths_opts_per_lang_and_buildtype[src_lang][buildtype] = Vs2010Backend._extract_nmake_fields(args_list)
return defs_paths_opts_per_lang_and_buildtype

class Vs2010Backend(backends.Backend):
Expand Down Expand Up @@ -1172,44 +1169,39 @@ def get_build_args(compiler, buildtype: str, optimization_level: str, debug: boo

return build_args

#Convert a list of compile arguments from -
# [ '-I..\\some\\dir\\include', '-I../../some/other/dir', '/MDd', '/W2', '/std:c++17', '/Od', '/Zi', '-DSOME_DEF=1', '-DANOTHER_DEF=someval', ...]
#to -
# 'SOME_DEF=1;ANOTHER_DEF=someval;'
#which is the format required by the visual studio project's NMakePreprocessorDefinitions field.
# Used in populating a simple nmake-style project's intellisense fields.
# Given a list of compile args, for example -
# [ '-I..\\some\\dir\\include', '-I../../some/other/dir', '/MDd', '/W2', '/std:c++17', '/Od', '/Zi', '-DSOME_DEF=1', '-DANOTHER_DEF=someval', ...]
# returns a tuple of pre-processor defs (for this example) -
# 'SOME_DEF=1;ANOTHER_DEF=someval;'
# and include paths, e.g. -
# '..\\some\\dir\\include;../../some/other/dir;'
# and finally any remaining compiler options, e.g. -
# '/MDd;/W2;/std:c++17;/Od/Zi'
@staticmethod
def extract_nmake_preprocessor_defs(captured_build_args: list[str]) -> str:
defs = ''
for arg in captured_build_args:
if arg.startswith(('-D', '/D')):
defs += arg[2:] + ';'
return defs
def _extract_nmake_fields(captured_build_args: list[str]) -> T.Tuple[str, str, str]:
include_dir_options = [
'-I',
'/I',
'-isystem', # regular gcc / clang option to denote system header include search paths
'/clang:-isystem', # clang-cl (msvc 'cl'-style clang wrapper) option to pass '-isystem' option to clang driver
'/imsvc', # clang-cl option to 'Add directory to system include search path'
'/external:I', # msvc cl option to add 'external' include search paths
]

#Convert a list of compile arguments from -
# [ '-I..\\some\\dir\\include', '-I../../some/other/dir', '/MDd', '/W2', '/std:c++17', '/Od', '/Zi', '-DSOME_DEF=1', '-DANOTHER_DEF=someval', ...]
#to -
# '..\\some\\dir\\include;../../some/other/dir;'
#which is the format required by the visual studio project's NMakePreprocessorDefinitions field.
@staticmethod
def extract_nmake_include_paths(captured_build_args: list[str]) -> str:
defs = ''
paths = ''
for arg in captured_build_args:
if arg.startswith(('-I', '/I')):
paths += arg[2:] + ';'
return paths

#Convert a list of compile arguments from -
# [ '-I..\\some\\dir\\include', '-I../../some/other/dir', '/MDd', '/W2', '/std:c++17', '/Od', '/Zi', '-DSOME_DEF=1', '-DANOTHER_DEF=someval', ...]
#to -
# '/MDd;/W2;/std:c++17;/Od/Zi'
#which is the format required by the visual studio project's NMakePreprocessorDefinitions field.
@staticmethod
def extract_intellisense_additional_compiler_options(captured_build_args: list[str]) -> str:
additional_opts = ''
for arg in captured_build_args:
if (not arg.startswith(('-D', '/D', '-I', '/I'))) and arg.startswith(('-', '/')):
additional_opts += arg + ';'
return additional_opts
if arg.startswith(('-D', '/D')):
defs += arg[2:] + ';'
else:
opt_match = next((opt for opt in include_dir_options if arg.startswith(opt)), None)
if opt_match:
paths += arg[len(opt_match):] + ';'
elif arg.startswith(('-', '/')):
additional_opts += arg + ';'
return (defs, paths, additional_opts)

@staticmethod
def get_nmake_base_meson_command_and_exe_search_paths() -> T.Tuple[str, str]:
Expand Down Expand Up @@ -1300,9 +1292,10 @@ def add_gen_lite_makefile_vcxproj_elements(self,
# We may not have any src files and so won't have a primary src language. In which case, we've nothing to fill in for this target's intellisense fields -
if primary_src_lang:
primary_src_type_build_args = captured_build_args[primary_src_lang]
ET.SubElement(per_config_prop_group, 'NMakePreprocessorDefinitions').text = Vs2010Backend.extract_nmake_preprocessor_defs(primary_src_type_build_args)
ET.SubElement(per_config_prop_group, 'NMakeIncludeSearchPath').text = Vs2010Backend.extract_nmake_include_paths(primary_src_type_build_args)
ET.SubElement(per_config_prop_group, 'AdditionalOptions').text = Vs2010Backend.extract_intellisense_additional_compiler_options(primary_src_type_build_args)
preproc_defs, inc_paths, other_compile_opts = Vs2010Backend._extract_nmake_fields(primary_src_type_build_args)
ET.SubElement(per_config_prop_group, 'NMakePreprocessorDefinitions').text = preproc_defs
ET.SubElement(per_config_prop_group, 'NMakeIncludeSearchPath').text = inc_paths
ET.SubElement(per_config_prop_group, 'AdditionalOptions').text = other_compile_opts

# Unless we explicitly specify the following empty path elements, the project is assigned a load of nasty defaults that fill these
# with values like -
Expand Down

0 comments on commit bd3341f

Please sign in to comment.