windows.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, build
  14. from ..mesonlib import MesonException, extract_as_list
  15. from . import get_include_args
  16. from . import ModuleReturnValue
  17. from . import ExtensionModule
  18. from ..interpreter import CustomTargetHolder
  19. from ..interpreterbase import permittedKwargs, FeatureNewKwargs
  20. from ..dependencies import ExternalProgram
  21. class WindowsModule(ExtensionModule):
  22. def detect_compiler(self, compilers):
  23. for l in ('c', 'cpp'):
  24. if l in compilers:
  25. return compilers[l]
  26. raise MesonException('Resource compilation requires a C or C++ compiler.')
  27. @FeatureNewKwargs('windows.compile_resources', '0.47.0', ['depend_files', 'depends'])
  28. @permittedKwargs({'args', 'include_directories', 'depend_files', 'depends'})
  29. def compile_resources(self, state, args, kwargs):
  30. comp = self.detect_compiler(state.compilers)
  31. extra_args = mesonlib.stringlistify(kwargs.get('args', []))
  32. wrc_depend_files = extract_as_list(kwargs, 'depend_files', pop = True)
  33. wrc_depends = extract_as_list(kwargs, 'depends', pop = True)
  34. for d in wrc_depends:
  35. if isinstance(d, CustomTargetHolder):
  36. extra_args += get_include_args([d.outdir_include()])
  37. inc_dirs = extract_as_list(kwargs, 'include_directories', pop = True)
  38. for incd in inc_dirs:
  39. if not isinstance(incd.held_object, (str, build.IncludeDirs)):
  40. raise MesonException('Resource include dirs should be include_directories().')
  41. extra_args += get_include_args(inc_dirs)
  42. if comp.id == 'msvc':
  43. rescomp = ExternalProgram('rc', silent=True)
  44. res_args = extra_args + ['/nologo', '/fo@OUTPUT@', '@INPUT@']
  45. suffix = 'res'
  46. else:
  47. m = 'Argument {!r} has a space which may not work with windres due to ' \
  48. 'a MinGW bug: https://sourceware.org/bugzilla/show_bug.cgi?id=4933'
  49. for arg in extra_args:
  50. if ' ' in arg:
  51. mlog.warning(m.format(arg))
  52. rescomp = None
  53. # FIXME: Does not handle `native: true` executables, see
  54. # https://github.com/mesonbuild/meson/issues/1531
  55. if state.environment.is_cross_build():
  56. # If cross compiling see if windres has been specified in the
  57. # cross file before trying to find it another way.
  58. cross_info = state.environment.cross_info
  59. rescomp = ExternalProgram.from_cross_info(cross_info, 'windres')
  60. if not rescomp or not rescomp.found():
  61. # Pick-up env var WINDRES if set. This is often used for
  62. # specifying an arch-specific windres.
  63. rescomp = ExternalProgram(os.environ.get('WINDRES', 'windres'), silent=True)
  64. res_args = extra_args + ['@INPUT@', '@OUTPUT@']
  65. suffix = 'o'
  66. if not rescomp.found():
  67. raise MesonException('Could not find Windows resource compiler {!r}'
  68. ''.format(rescomp.get_path()))
  69. res_targets = []
  70. def add_target(src):
  71. if isinstance(src, list):
  72. for subsrc in src:
  73. add_target(subsrc)
  74. return
  75. if hasattr(src, 'held_object'):
  76. src = src.held_object
  77. res_kwargs = {
  78. 'output': '@BASENAME@.' + suffix,
  79. 'input': [src],
  80. 'command': [rescomp] + res_args,
  81. 'depend_files': wrc_depend_files,
  82. 'depends': wrc_depends,
  83. }
  84. if isinstance(src, str):
  85. name = 'file {!r}'.format(os.path.join(state.subdir, src))
  86. elif isinstance(src, mesonlib.File):
  87. name = 'file {!r}'.format(src.relative_name())
  88. elif isinstance(src, build.CustomTarget):
  89. if len(src.get_outputs()) > 1:
  90. raise MesonException('windows.compile_resources does not accept custom targets with more than 1 output.')
  91. name = 'target {!r}'.format(src.get_id())
  92. else:
  93. raise MesonException('Unexpected source type {!r}. windows.compile_resources accepts only strings, files, custom targets, and lists thereof.'.format(src))
  94. # Path separators are not allowed in target names
  95. name = name.replace('/', '_').replace('\\', '_')
  96. # instruct binutils windres to generate a preprocessor depfile
  97. if comp.id != 'msvc':
  98. res_kwargs['depfile'] = res_kwargs['output'] + '.d'
  99. res_kwargs['command'] += ['--preprocessor-arg=-MD', '--preprocessor-arg=-MQ@OUTPUT@', '--preprocessor-arg=-MF@DEPFILE@']
  100. res_targets.append(build.CustomTarget('Windows resource for ' + name, state.subdir, state.subproject, res_kwargs))
  101. add_target(args)
  102. return ModuleReturnValue(res_targets, [res_targets])
  103. def initialize(*args, **kwargs):
  104. return WindowsModule(*args, **kwargs)