msvc-compiler-flags.patch 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. From 4f0218bcc0e311e37dd191329a6757bddb2ea97b Mon Sep 17 00:00:00 2001
  2. From: Jack Lloyd <[email protected]>
  3. Date: Sun, 1 Dec 2024 13:51:19 -0500
  4. Subject: [PATCH] Don't implicitly/always pass CXXFLAGS into LDFLAGS
  5. This was done to handle LTO (#4196 #4200) but causes problems especially for
  6. MSVC which in some (unclear) circumstances treats unknown flags to the linker as
  7. a hard error (#4451). Instead only pass CXXFLAGS into LDFLAGS when an extra
  8. option `--lto-cxxflags-to-ldflags` is provided to opt into this behavior.
  9. ---
  10. configure.py | 76 +++++++++++++++++++-------------------
  11. src/build-data/makefile.in | 2 +-
  12. src/build-data/ninja.in | 2 +-
  13. 3 files changed, 41 insertions(+), 39 deletions(-)
  14. diff --git a/configure.py b/configure.py
  15. index 8d8d85d56c9..35c78bc10f7 100755
  16. --- a/configure.py
  17. +++ b/configure.py
  18. @@ -379,6 +379,9 @@ def add_enable_disable_pair(group, what, default, msg=optparse.SUPPRESS_HELP):
  19. target_group.add_option('--extra-cxxflags', metavar='FLAGS', default=[], action='append',
  20. help='set extra compiler flags')
  21. + target_group.add_option('--lto-cxxflags-to-ldflags', default=False, action='store_true',
  22. + help='set all compilation flags also during linking (for LTO)')
  23. +
  24. target_group.add_option('--ldflags', metavar='FLAGS',
  25. help='set linker flags', default=None)
  26. @@ -1515,48 +1518,50 @@ def cc_lang_flags(self):
  27. def cc_lang_binary_linker_flags(self):
  28. return self.lang_binary_linker_flags
  29. - def cc_compile_flags(self, options, with_debug_info=None, enable_optimizations=None):
  30. - def gen_flags(with_debug_info, enable_optimizations):
  31. + def ldflags(self, options):
  32. + if options.ldflags:
  33. + yield options.ldflags
  34. +
  35. + if options.lto_cxxflags_to_ldflags:
  36. + yield from self.cc_compile_flags(options)
  37. - sanitizers_enabled = options.with_sanitizers or (len(options.enable_sanitizers) > 0)
  38. + def cc_compile_flags(self, options):
  39. + sanitizers_enabled = options.with_sanitizers or (len(options.enable_sanitizers) > 0)
  40. - if with_debug_info is None:
  41. - with_debug_info = options.with_debug_info
  42. - if enable_optimizations is None:
  43. - enable_optimizations = not options.no_optimizations
  44. + if options.cxxflags:
  45. + # CXXFLAGS is assumed to be the entire set of desired compilation flags
  46. + # if not the case the user should have used --extra-cxxflags
  47. + yield options.cxxflags
  48. + return
  49. - if with_debug_info:
  50. - yield self.debug_info_flags
  51. + if options.with_debug_info:
  52. + yield self.debug_info_flags
  53. - if enable_optimizations:
  54. - if options.optimize_for_size:
  55. - if self.size_optimization_flags != '':
  56. - yield self.size_optimization_flags
  57. - else:
  58. - logging.warning("No size optimization flags set for current compiler")
  59. - yield self.optimization_flags
  60. - elif sanitizers_enabled and self.sanitizer_optimization_flags != '':
  61. - yield self.sanitizer_optimization_flags
  62. + if not options.no_optimizations:
  63. + if options.optimize_for_size:
  64. + if self.size_optimization_flags != '':
  65. + yield self.size_optimization_flags
  66. else:
  67. + logging.warning("No size optimization flags set for current compiler")
  68. yield self.optimization_flags
  69. + elif sanitizers_enabled and self.sanitizer_optimization_flags != '':
  70. + yield self.sanitizer_optimization_flags
  71. + else:
  72. + yield self.optimization_flags
  73. - if options.arch in self.cpu_flags:
  74. - yield self.cpu_flags[options.arch]
  75. -
  76. - if options.arch in self.cpu_flags_no_debug:
  77. -
  78. - # Only enable these if no debug/sanitizer options enabled
  79. -
  80. - if not (options.debug_mode or sanitizers_enabled):
  81. - yield self.cpu_flags_no_debug[options.arch]
  82. + if options.arch in self.cpu_flags:
  83. + yield self.cpu_flags[options.arch]
  84. - for flag in options.extra_cxxflags:
  85. - yield flag
  86. + if options.arch in self.cpu_flags_no_debug:
  87. + # Only enable these if no debug/sanitizer options enabled
  88. + if not (options.debug_mode or sanitizers_enabled):
  89. + yield self.cpu_flags_no_debug[options.arch]
  90. - for definition in options.define_build_macro:
  91. - yield self.add_compile_definition_option + definition
  92. + for flag in options.extra_cxxflags:
  93. + yield flag
  94. - return (' '.join(gen_flags(with_debug_info, enable_optimizations))).strip()
  95. + for definition in options.define_build_macro:
  96. + yield self.add_compile_definition_option + definition
  97. @staticmethod
  98. def _so_link_search(osname, debug_info):
  99. @@ -2264,9 +2269,6 @@ def test_exe_extra_ldflags():
  100. 'sanitizer_types' : sorted(cc.sanitizer_types),
  101. - 'cc_compile_opt_flags': cc.cc_compile_flags(options, False, True),
  102. - 'cc_compile_debug_flags': cc.cc_compile_flags(options, True, False),
  103. -
  104. 'dash_o': cc.output_to_object,
  105. 'dash_c': cc.compile_flags,
  106. @@ -2274,8 +2276,8 @@ def test_exe_extra_ldflags():
  107. 'cc_lang_binary_linker_flags': cc.cc_lang_binary_linker_flags(),
  108. 'os_feature_macros': osinfo.macros(cc),
  109. 'cc_sysroot': sysroot_option(),
  110. - 'cc_compile_flags': options.cxxflags or cc.cc_compile_flags(options),
  111. - 'ldflags': options.ldflags or '',
  112. + 'cc_compile_flags': ' '.join(cc.cc_compile_flags(options)).strip(),
  113. + 'ldflags': ' '.join(cc.ldflags(options)).strip(),
  114. 'test_exe_extra_ldflags': test_exe_extra_ldflags(),
  115. 'extra_libs': extra_libs(options.extra_libs, cc),
  116. 'cc_warning_flags': cc.cc_warning_flags(options),
  117. diff --git a/src/build-data/makefile.in b/src/build-data/makefile.in
  118. index e59085667bb..9b3ac587477 100644
  119. --- a/src/build-data/makefile.in
  120. +++ b/src/build-data/makefile.in
  121. @@ -16,7 +16,7 @@ LANG_EXE_FLAGS = %{cc_lang_binary_linker_flags}
  122. CXXFLAGS = %{cc_compile_flags}
  123. WARN_FLAGS = %{cc_warning_flags}
  124. LIB_FLAGS = %{lib_flags}
  125. -LDFLAGS = %{ldflags} %{cc_compile_flags}
  126. +LDFLAGS = %{ldflags}
  127. EXE_LINK_CMD = %{exe_link_cmd}
  128. diff --git a/src/build-data/ninja.in b/src/build-data/ninja.in
  129. index a6279d3dea2..414663acfeb 100644
  130. --- a/src/build-data/ninja.in
  131. +++ b/src/build-data/ninja.in
  132. @@ -10,7 +10,7 @@ LANG_EXE_FLAGS = %{cc_lang_binary_linker_flags}
  133. CXXFLAGS = %{cc_compile_flags}
  134. WARN_FLAGS = %{cc_warning_flags}
  135. -LDFLAGS = %{ldflags} %{cc_compile_flags}
  136. +LDFLAGS = %{ldflags}
  137. EXE_LINK_CMD = %{exe_link_cmd}