windows.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright 2015 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import os
  12. from .. import mlog
  13. from .. import mesonlib, dependencies, build
  14. from ..mesonlib import MesonException
  15. from . import get_include_args
  16. from . import ModuleReturnValue
  17. from . import ExtensionModule
  18. class WindowsModule(ExtensionModule):
  19. def detect_compiler(self, compilers):
  20. for l in ('c', 'cpp'):
  21. if l in compilers:
  22. return compilers[l]
  23. raise MesonException('Resource compilation requires a C or C++ compiler.')
  24. def compile_resources(self, state, args, kwargs):
  25. comp = self.detect_compiler(state.compilers)
  26. extra_args = mesonlib.stringlistify(kwargs.get('args', []))
  27. inc_dirs = kwargs.pop('include_directories', [])
  28. if not isinstance(inc_dirs, list):
  29. inc_dirs = [inc_dirs]
  30. for incd in inc_dirs:
  31. if not isinstance(incd.held_object, (str, build.IncludeDirs)):
  32. raise MesonException('Resource include dirs should be include_directories().')
  33. extra_args += get_include_args(inc_dirs)
  34. if comp.id == 'msvc':
  35. rescomp = dependencies.ExternalProgram('rc', silent=True)
  36. res_args = extra_args + ['/nologo', '/fo@OUTPUT@', '@INPUT@']
  37. suffix = 'res'
  38. else:
  39. m = 'Argument {!r} has a space which may not work with windres due to ' \
  40. 'a MinGW bug: https://sourceware.org/bugzilla/show_bug.cgi?id=4933'
  41. for arg in extra_args:
  42. if ' ' in arg:
  43. mlog.warning(m.format(arg))
  44. rescomp_name = None
  45. # FIXME: Does not handle `native: true` executables, see
  46. # https://github.com/mesonbuild/meson/issues/1531
  47. if state.environment.is_cross_build():
  48. # If cross compiling see if windres has been specified in the
  49. # cross file before trying to find it another way.
  50. rescomp_name = state.environment.cross_info.config['binaries'].get('windres')
  51. if rescomp_name is None:
  52. # Pick-up env var WINDRES if set. This is often used for
  53. # specifying an arch-specific windres.
  54. rescomp_name = os.environ.get('WINDRES', 'windres')
  55. rescomp = dependencies.ExternalProgram(rescomp_name, silent=True)
  56. res_args = extra_args + ['@INPUT@', '@OUTPUT@']
  57. suffix = 'o'
  58. if not rescomp.found():
  59. raise MesonException('Could not find Windows resource compiler %s.' % ' '.join(rescomp.get_command()))
  60. res_kwargs = {'output': '@BASENAME@.' + suffix,
  61. 'arguments': res_args}
  62. res_gen = build.Generator([rescomp], res_kwargs)
  63. res_output = res_gen.process_files('Windows resource', args, state)
  64. return ModuleReturnValue(res_output, [res_output])
  65. def initialize():
  66. return WindowsModule()