@@ -181,47 +181,65 @@ function(_add_variant_c_compile_flags)
181181 if (optimized OR CFLAGS_FORCE_BUILD_OPTIMIZED)
182182 list (APPEND result "-O2" )
183183
184- # Add -momit-leaf-frame-pointer on x86.
185- if ("${CFLAGS_ARCH} " STREQUAL "i386" OR "${CFLAGS_ARCH} " STREQUAL "x86_64" )
186- list (APPEND result "-momit-leaf-frame-pointer" )
184+ # Omit leaf frame pointers on x86.
185+ if ("${CFLAGS_ARCH} " STREQUAL "i386" OR "${CFLAGS_ARCH} " STREQUAL "i686" )
186+ if (NOT SWIFT_COMPILER_IS_MSVC_LIKE)
187+ list (APPEND result "-momit-leaf-frame-pointer" )
188+ else ()
189+ list (APPEND result "/Oy" )
190+ endif ()
187191 endif ()
188192 else ()
189- list (APPEND result "-O0" )
190- endif ()
191- is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE} " debuginfo)
192- if (debuginfo)
193- _compute_lto_flag("${CFLAGS_ENABLE_LTO} " _lto_flag_out)
194- if (_lto_flag_out)
195- list (APPEND result "-gline-tables-only" )
193+ if (NOT SWIFT_COMPILER_IS_MSVC_LIKE)
194+ list (APPEND result "-O0" )
196195 else ()
197- list (APPEND result "-g " )
196+ list (APPEND result "/Od " )
198197 endif ()
199- else ()
200- list (APPEND result "-g0" )
201198 endif ()
202199
203- if ("${CFLAGS_SDK} " STREQUAL "WINDOWS" )
204- list (APPEND result -Xclang;--dependent-lib=oldnames)
205- # TODO(compnerd) handle /MT, /MTd, /MD, /MDd
206- if ("${CMAKE_BUILD_TYPE} " STREQUAL "RELEASE" )
207- list (APPEND result "-D_MD" )
208- list (APPEND result -Xclang;--dependent-lib=msvcrt)
200+ # CMake automatically adds the flags for debug info if we use MSVC/clang-cl.
201+ if (NOT SWIFT_COMPILER_IS_MSVC_LIKE)
202+ is_build_type_with_debuginfo("${CFLAGS_BUILD_TYPE} " debuginfo)
203+ if (debuginfo)
204+ _compute_lto_flag("${CFLAGS_ENABLE_LTO} " _lto_flag_out)
205+ if (_lto_flag_out)
206+ list (APPEND result "-gline-tables-only" )
207+ else ()
208+ list (APPEND result "-g" )
209+ endif ()
209210 else ()
210- list (APPEND result "-D_MDd" )
211- list (APPEND result -Xclang;--dependent-lib=msvcrtd)
211+ list (APPEND result "-g0" )
212212 endif ()
213- list (APPEND result -fno-pic)
214213 endif ()
215214
216215 if ("${CFLAGS_SDK} " STREQUAL "WINDOWS" )
216+ # MSVC doesn't support -Xclang. We don't need to manually specify
217+ # -D_MD or D_MDd either, as CMake does this automatically.
218+ if (NOT "${CMAKE_C_COMPILER_ID} " STREQUAL "MSVC" )
219+ list (APPEND result -Xclang;--dependent-lib=oldnames)
220+ # TODO(compnerd) handle /MT, /MTd, /MD, /MDd
221+ if ("${CMAKE_BUILD_TYPE} " STREQUAL "RELEASE" )
222+ list (APPEND result "-D_MD" )
223+ list (APPEND result -Xclang;--dependent-lib=msvcrt)
224+ else ()
225+ list (APPEND result "-D_MDd" )
226+ list (APPEND result -Xclang;--dependent-lib=msvcrtd)
227+ endif ()
228+ endif ()
229+
230+ # MSVC/clang-cl don't support -fno-pic or -fms-compatability-version.
231+ if (NOT SWIFT_COMPILER_IS_MSVC_LIKE)
232+ list (APPEND result -fno-pic)
233+ list (APPEND result "-fms-compatibility-version=1900" )
234+ endif ()
235+
217236 list (APPEND result "-DLLVM_ON_WIN32" )
218237 list (APPEND result "-D_CRT_SECURE_NO_WARNINGS" )
219238 list (APPEND result "-D_CRT_NONSTDC_NO_WARNINGS" )
220239 # TODO(compnerd) permit building for different families
221240 list (APPEND result "-D_CRT_USE_WINAPI_FAMILY_DESKTOP_APP" )
222241 # TODO(compnerd) handle /MT
223242 list (APPEND result "-D_DLL" )
224- list (APPEND result "-fms-compatibility-version=1900" )
225243 endif ()
226244
227245 if (CFLAGS_ENABLE_ASSERTIONS)
@@ -321,9 +339,12 @@ function(_add_variant_link_flags)
321339 elseif ("${LFLAGS_SDK} " STREQUAL "CYGWIN" )
322340 # No extra libraries required.
323341 elseif ("${LFLAGS_SDK} " STREQUAL "WINDOWS" )
324- # NOTE: we do not use "/MD" or "/MDd" and select the runtime via linker
325- # options. This causes conflicts.
326- list (APPEND result "-nostdlib" )
342+ # We don't need to add -nostdlib using MSVC or clang-cl, as MSVC and clang-cl rely on auto-linking entirely.
343+ if (NOT SWIFT_COMPILER_IS_MSVC_LIKE)
344+ # NOTE: we do not use "/MD" or "/MDd" and select the runtime via linker
345+ # options. This causes conflicts.
346+ list (APPEND result "-nostdlib" )
347+ endif ()
327348 elseif ("${LFLAGS_SDK} " STREQUAL "ANDROID" )
328349 list (APPEND result
329350 "-ldl"
@@ -351,15 +372,17 @@ function(_add_variant_link_flags)
351372 if (NOT "${SWIFT_${LFLAGS_SDK} _ICU_I18N}" STREQUAL "" )
352373 list (APPEND library_search_directories "${SWIFT_${sdk} _ICU_I18N}" )
353374 endif ()
354-
355- if (SWIFT_ENABLE_GOLD_LINKER AND
356- "${SWIFT_SDK_${LFLAGS_SDK} _OBJECT_FORMAT}" STREQUAL "ELF" )
357- list (APPEND result "-fuse-ld=gold" )
358- endif ()
359- if (SWIFT_ENABLE_LLD_LINKER OR
360- ("${LFLAGS_SDK} " STREQUAL "WINDOWS" AND
361- NOT "${CMAKE_SYSTEM_NAME} " STREQUAL "WINDOWS" ))
362- list (APPEND result "-fuse-ld=lld" )
375+
376+ if (NOT SWIFT_COMPILER_IS_MSVC_LIKE)
377+ if (SWIFT_ENABLE_GOLD_LINKER AND
378+ "${SWIFT_SDK_${LFLAGS_SDK} _OBJECT_FORMAT}" STREQUAL "ELF" )
379+ list (APPEND result "-fuse-ld=gold" )
380+ endif ()
381+ if (SWIFT_ENABLE_LLD_LINKER OR
382+ ("${LFLAGS_SDK} " STREQUAL "WINDOWS" AND
383+ NOT "${CMAKE_SYSTEM_NAME} " STREQUAL "WINDOWS" ))
384+ list (APPEND result "-fuse-ld=lld" )
385+ endif ()
363386 endif ()
364387
365388 set ("${LFLAGS_RESULT_VAR_NAME} " "${result} " PARENT_SCOPE)
0 commit comments