windows.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. from . import permittedKwargs
  19. class WindowsModule(ExtensionModule):
  20. def detect_compiler(self, compilers):
  21. for l in ('c', 'cpp'):
  22. if l in compilers:
  23. return compilers[l]
  24. raise MesonException('Resource compilation requires a C or C++ compiler.')
  25. @permittedKwargs({'args', 'include_directories'})
  26. def compile_resources(self, state, args, kwargs):
  27. comp = self.detect_compiler(state.compilers)
  28. extra_args = mesonlib.stringlistify(kwargs.get('args', []))
  29. inc_dirs = kwargs.pop('include_directories', [])
  30. if not isinstance(inc_dirs, list):
  31. inc_dirs = [inc_dirs]
  32. for incd in inc_dirs:
  33. if not isinstance(incd.held_object, (str, build.IncludeDirs)):
  34. raise MesonException('Resource include dirs should be include_directories().')
  35. extra_args += get_include_args(inc_dirs)
  36. if comp.id == 'msvc':
  37. rescomp = dependencies.ExternalProgram('rc', silent=True)
  38. res_args = extra_args + ['/nologo', '/fo@OUTPUT@', '@INPUT@']
  39. suffix = 'res'
  40. else:
  41. m = 'Argument {!r} has a space which may not work with windres due to ' \
  42. 'a MinGW bug: https://sourceware.org/bugzilla/show_bug.cgi?id=4933'
  43. for arg in extra_args:
  44. if ' ' in arg:
  45. mlog.warning(m.format(arg))
  46. rescomp_name = None
  47. # FIXME: Does not handle `native: true` executables, see
  48. # https://github.com/mesonbuild/meson/issues/1531
  49. if state.environment.is_cross_build():
  50. # If cross compiling see if windres has been specified in the
  51. # cross file before trying to find it another way.
  52. rescomp_name = state.environment.cross_info.config['binaries'].get('windres')
  53. if rescomp_name is None:
  54. # Pick-up env var WINDRES if set. This is often used for
  55. # specifying an arch-specific windres.
  56. rescomp_name = os.environ.get('WINDRES', 'windres')
  57. rescomp = dependencies.ExternalProgram(rescomp_name, silent=True)
  58. res_args = extra_args + ['@INPUT@', '@OUTPUT@']
  59. suffix = 'o'
  60. if not rescomp.found():
  61. raise MesonException('Could not find Windows resource compiler %s.' % ' '.join(rescomp.get_command()))
  62. res_kwargs = {'output': '@BASENAME@.' + suffix,
  63. 'arguments': res_args}
  64. res_gen = build.Generator([rescomp], res_kwargs)
  65. res_output = res_gen.process_files('Windows resource', args, state)
  66. return ModuleReturnValue(res_output, [res_output])
  67. def initialize():
  68. return WindowsModule()