Avoid calculating the filter coefficients multiple times
[openal-soft.git] / cmake / FindWindowsSDK.cmake
blobe136b8976f8326e813e05ad38e8bd966a26caf53
1 # - Find the Windows SDK aka Platform SDK
3 # Relevant Wikipedia article: http://en.wikipedia.org/wiki/Microsoft_Windows_SDK
5 # Pass "COMPONENTS tools" to ignore Visual Studio version checks: in case
6 # you just want the tool binaries to run, rather than the libraries and headers
7 # for compiling.
9 # Variables:
10 #  WINDOWSSDK_FOUND - if any version of the windows or platform SDK was found that is usable with the current version of visual studio
11 #  WINDOWSSDK_LATEST_DIR
12 #  WINDOWSSDK_LATEST_NAME
13 #  WINDOWSSDK_FOUND_PREFERENCE - if we found an entry indicating a "preferred" SDK listed for this visual studio version
14 #  WINDOWSSDK_PREFERRED_DIR
15 #  WINDOWSSDK_PREFERRED_NAME
17 #  WINDOWSSDK_DIRS - contains no duplicates, ordered most recent first.
18 #  WINDOWSSDK_PREFERRED_FIRST_DIRS - contains no duplicates, ordered with preferred first, followed by the rest in descending recency
20 # Functions:
21 #  windowssdk_name_lookup(<directory> <output variable>) - Find the name corresponding with the SDK directory you pass in, or
22 #     NOTFOUND if not recognized. Your directory must be one of WINDOWSSDK_DIRS for this to work.
24 #  windowssdk_build_lookup(<directory> <output variable>) - Find the build version number corresponding with the SDK directory you pass in, or
25 #     NOTFOUND if not recognized. Your directory must be one of WINDOWSSDK_DIRS for this to work.
27 #  get_windowssdk_from_component(<file or dir> <output variable>) - Given a library or include dir,
28 #     find the Windows SDK root dir corresponding to it, or NOTFOUND if unrecognized.
30 #  get_windowssdk_library_dirs(<directory> <output variable>) - Find the architecture-appropriate
31 #     library directories corresponding to the SDK directory you pass in (or NOTFOUND if none)
33 #  get_windowssdk_library_dirs_multiple(<output variable> <directory> ...) - Find the architecture-appropriate
34 #     library directories corresponding to the SDK directories you pass in, in order, skipping those not found. NOTFOUND if none at all.
35 #     Good for passing WINDOWSSDK_DIRS or WINDOWSSDK_DIRS to if you really just want a file and don't care where from.
37 #  get_windowssdk_include_dirs(<directory> <output variable>) - Find the
38 #     include directories corresponding to the SDK directory you pass in (or NOTFOUND if none)
40 #  get_windowssdk_include_dirs_multiple(<output variable> <directory> ...) - Find the
41 #     include directories corresponding to the SDK directories you pass in, in order, skipping those not found. NOTFOUND if none at all.
42 #     Good for passing WINDOWSSDK_DIRS or WINDOWSSDK_DIRS to if you really just want a file and don't care where from.
44 # Requires these CMake modules:
45 #  FindPackageHandleStandardArgs (known included with CMake >=2.6.2)
47 # Original Author:
48 # 2012 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
49 # http://academic.cleardefinition.com
50 # Iowa State University HCI Graduate Program/VRAC
52 # Copyright Iowa State University 2012.
53 # Distributed under the Boost Software License, Version 1.0.
54 # (See accompanying file LICENSE_1_0.txt or copy at
55 # http://www.boost.org/LICENSE_1_0.txt)
57 set(_preferred_sdk_dirs) # pre-output
58 set(_win_sdk_dirs) # pre-output
59 set(_win_sdk_versanddirs) # pre-output
60 set(_win_sdk_buildsanddirs) # pre-output
61 set(_winsdk_vistaonly) # search parameters
62 set(_winsdk_kits) # search parameters
65 set(_WINDOWSSDK_ANNOUNCE OFF)
66 if(NOT WINDOWSSDK_FOUND AND (NOT WindowsSDK_FIND_QUIETLY))
67         set(_WINDOWSSDK_ANNOUNCE ON)
68 endif()
69 macro(_winsdk_announce)
70         if(_WINSDK_ANNOUNCE)
71                 message(STATUS ${ARGN})
72         endif()
73 endmacro()
75 set(_winsdk_win10vers
76         10.0.14393.0 # Redstone aka Win10 1607 "Anniversary Update"
77         10.0.10586.0 # TH2 aka Win10 1511
78         10.0.10240.0 # Win10 RTM
79         10.0.10150.0 # just ucrt
80         10.0.10056.0
83 if(WindowsSDK_FIND_COMPONENTS MATCHES "tools")
84         set(_WINDOWSSDK_IGNOREMSVC ON)
85         _winsdk_announce("Checking for tools from Windows/Platform SDKs...")
86 else()
87         set(_WINDOWSSDK_IGNOREMSVC OFF)
88         _winsdk_announce("Checking for Windows/Platform SDKs...")
89 endif()
91 # Appends to the three main pre-output lists used only if the path exists
92 # and is not already in the list.
93 function(_winsdk_conditional_append _vername _build _path)
94         if(("${_path}" MATCHES "registry") OR (NOT EXISTS "${_path}"))
95                 # Path invalid - do not add
96                 return()
97         endif()
98         list(FIND _win_sdk_dirs "${_path}" _win_sdk_idx)
99         if(_win_sdk_idx GREATER -1)
100                 # Path already in list - do not add
101                 return()
102         endif()
103         _winsdk_announce( " - ${_vername}, Build ${_build} @ ${_path}")
104         # Not yet in the list, so we'll add it
105         list(APPEND _win_sdk_dirs "${_path}")
106         set(_win_sdk_dirs "${_win_sdk_dirs}" CACHE INTERNAL "" FORCE)
107         list(APPEND
108                 _win_sdk_versanddirs
109                 "${_vername}"
110                 "${_path}")
111         set(_win_sdk_versanddirs "${_win_sdk_versanddirs}" CACHE INTERNAL "" FORCE)
112         list(APPEND
113                 _win_sdk_buildsanddirs
114                 "${_build}"
115                 "${_path}")
116         set(_win_sdk_buildsanddirs "${_win_sdk_buildsanddirs}" CACHE INTERNAL "" FORCE)
117 endfunction()
119 # Appends to the "preferred SDK" lists only if the path exists
120 function(_winsdk_conditional_append_preferred _info _path)
121         if(("${_path}" MATCHES "registry") OR (NOT EXISTS "${_path}"))
122                 # Path invalid - do not add
123                 return()
124         endif()
126         get_filename_component(_path "${_path}" ABSOLUTE)
128         list(FIND _win_sdk_preferred_sdk_dirs "${_path}" _win_sdk_idx)
129         if(_win_sdk_idx GREATER -1)
130                 # Path already in list - do not add
131                 return()
132         endif()
133         _winsdk_announce( " - Found \"preferred\" SDK ${_info} @ ${_path}")
134         # Not yet in the list, so we'll add it
135         list(APPEND _win_sdk_preferred_sdk_dirs "${_path}")
136         set(_win_sdk_preferred_sdk_dirs "${_win_sdk_dirs}" CACHE INTERNAL "" FORCE)
138         # Just in case we somehow missed it:
139         _winsdk_conditional_append("${_info}" "" "${_path}")
140 endfunction()
142 # Given a version like v7.0A, looks for an SDK in the registry under "Microsoft SDKs".
143 # If the given version might be in both HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows
144 # and HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots aka "Windows Kits",
145 # use this macro first, since these registry keys usually have more information.
147 # Pass a "default" build number as an extra argument in case we can't find it.
148 function(_winsdk_check_microsoft_sdks_registry _winsdkver)
149         set(SDKKEY "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\${_winsdkver}")
150         get_filename_component(_sdkdir
151                 "[${SDKKEY};InstallationFolder]"
152                 ABSOLUTE)
154         set(_sdkname "Windows SDK ${_winsdkver}")
156         # Default build number passed as extra argument
157         set(_build ${ARGN})
158         # See if the registry holds a Microsoft-mutilated, err, designated, product name
159         # (just using get_filename_component to execute the registry lookup)
160         get_filename_component(_sdkproductname
161                 "[${SDKKEY};ProductName]"
162                 NAME)
163         if(NOT "${_sdkproductname}" MATCHES "registry")
164                 # Got a product name
165                 set(_sdkname "${_sdkname} (${_sdkproductname})")
166         endif()
168         # try for a version to augment our name
169         # (just using get_filename_component to execute the registry lookup)
170         get_filename_component(_sdkver
171                 "[${SDKKEY};ProductVersion]"
172                 NAME)
173         if(NOT "${_sdkver}" MATCHES "registry" AND NOT MATCHES)
174                 # Got a version
175                 if(NOT "${_sdkver}" MATCHES "\\.\\.")
176                         # and it's not an invalid one with two dots in it:
177                         # use to override the default build
178                         set(_build ${_sdkver})
179                         if(NOT "${_sdkname}" MATCHES "${_sdkver}")
180                                 # Got a version that's not already in the name, let's use it to improve our name.
181                                 set(_sdkname "${_sdkname} (${_sdkver})")
182                         endif()
183                 endif()
184         endif()
185         _winsdk_conditional_append("${_sdkname}" "${_build}" "${_sdkdir}")
186 endfunction()
188 # Given a name for identification purposes, the build number, and a key (technically a "value name")
189 # corresponding to a Windows SDK packaged as a "Windows Kit", look for it
190 # in HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots
191 # Note that the key or "value name" tends to be something weird like KitsRoot81 -
192 # no easy way to predict, just have to observe them in the wild.
193 # Doesn't hurt to also try _winsdk_check_microsoft_sdks_registry for these:
194 # sometimes you get keys in both parts of the registry (in the wow64 portion especially),
195 # and the non-"Windows Kits" location is often more descriptive.
196 function(_winsdk_check_windows_kits_registry _winkit_name _winkit_build _winkit_key)
197         get_filename_component(_sdkdir
198                 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots;${_winkit_key}]"
199                 ABSOLUTE)
200         _winsdk_conditional_append("${_winkit_name}" "${_winkit_build}" "${_sdkdir}")
201 endfunction()
203 # Given a name for identification purposes and the build number
204 # corresponding to a Windows 10 SDK packaged as a "Windows Kit", look for it
205 # in HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots
206 # Doesn't hurt to also try _winsdk_check_microsoft_sdks_registry for these:
207 # sometimes you get keys in both parts of the registry (in the wow64 portion especially),
208 # and the non-"Windows Kits" location is often more descriptive.
209 function(_winsdk_check_win10_kits _winkit_build)
210         get_filename_component(_sdkdir
211                 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots;KitsRoot10]"
212                 ABSOLUTE)
213         if(("${_sdkdir}" MATCHES "registry") OR (NOT EXISTS "${_sdkdir}"))
214                 return() # not found
215         endif()
216         if(EXISTS "${_sdkdir}/Include/${_winkit_build}/um")
217                 _winsdk_conditional_append("Windows Kits 10 (Build ${_winkit_build})" "${_winkit_build}" "${_sdkdir}")
218         endif()
219 endfunction()
221 # Given a name for indentification purposes, the build number, and the associated package GUID,
222 # look in the registry under both HKLM and HKCU in \\SOFTWARE\\Microsoft\\MicrosoftSDK\\InstalledSDKs\\
223 # for that guid and the SDK it points to.
224 function(_winsdk_check_platformsdk_registry _platformsdkname _build _platformsdkguid)
225         foreach(_winsdk_hive HKEY_LOCAL_MACHINE HKEY_CURRENT_USER)
226                 get_filename_component(_sdkdir
227                         "[${_winsdk_hive}\\SOFTWARE\\Microsoft\\MicrosoftSDK\\InstalledSDKs\\${_platformsdkguid};Install Dir]"
228                         ABSOLUTE)
229                 _winsdk_conditional_append("${_platformsdkname} (${_build})" "${_build}" "${_sdkdir}")
230         endforeach()
231 endfunction()
234 # Detect toolchain information: to know whether it's OK to use Vista+ only SDKs
236 set(_winsdk_vistaonly_ok OFF)
237 if(MSVC AND NOT _WINDOWSSDK_IGNOREMSVC)
238         # VC 10 and older has broad target support
239         if(MSVC_VERSION LESS 1700)
240                 # VC 11 by default targets Vista and later only, so we can add a few more SDKs that (might?) only work on vista+
241         elseif("${CMAKE_VS_PLATFORM_TOOLSET}" MATCHES "_xp")
242                 # This is the XP-compatible v110+ toolset
243         elseif("${CMAKE_VS_PLATFORM_TOOLSET}" STREQUAL "v100" OR "${CMAKE_VS_PLATFORM_TOOLSET}" STREQUAL "v90")
244                 # This is the VS2010/VS2008 toolset
245         else()
246                 # OK, we're VC11 or newer and not using a backlevel or XP-compatible toolset.
247                 # These versions have no XP (and possibly Vista pre-SP1) support
248                 set(_winsdk_vistaonly_ok ON)
249                 if(_WINDOWSSDK_ANNOUNCE AND NOT _WINDOWSSDK_VISTAONLY_PESTERED)
250                         set(_WINDOWSSDK_VISTAONLY_PESTERED ON CACHE INTERNAL "" FORCE)
251                         message(STATUS "FindWindowsSDK: Detected Visual Studio 2012 or newer, not using the _xp toolset variant: including SDK versions that drop XP support in search!")
252                 endif()
253         endif()
254 endif()
255 if(_WINDOWSSDK_IGNOREMSVC)
256         set(_winsdk_vistaonly_ok ON)
257 endif()
260 # MSVC version checks - keeps messy conditionals in one place
261 # (messy because of _WINDOWSSDK_IGNOREMSVC)
263 set(_winsdk_msvc_greater_1200 OFF)
264 if(_WINDOWSSDK_IGNOREMSVC OR (MSVC AND (MSVC_VERSION GREATER 1200)))
265         set(_winsdk_msvc_greater_1200 ON)
266 endif()
267 # Newer than VS .NET/VS Toolkit 2003
268 set(_winsdk_msvc_greater_1310 OFF)
269 if(_WINDOWSSDK_IGNOREMSVC OR (MSVC AND (MSVC_VERSION GREATER 1310)))
270         set(_winsdk_msvc_greater_1310 ON)
271 endif()
273 # VS2005/2008
274 set(_winsdk_msvc_less_1600 OFF)
275 if(_WINDOWSSDK_IGNOREMSVC OR (MSVC AND (MSVC_VERSION LESS 1600)))
276         set(_winsdk_msvc_less_1600 ON)
277 endif()
279 # VS2013+
280 set(_winsdk_msvc_not_less_1800 OFF)
281 if(_WINDOWSSDK_IGNOREMSVC OR (MSVC AND (NOT MSVC_VERSION LESS 1800)))
282         set(_winsdk_msvc_not_less_1800 ON)
283 endif()
286 # START body of find module
288 if(_winsdk_msvc_greater_1310) # Newer than VS .NET/VS Toolkit 2003
289         ###
290         # Look for "preferred" SDKs
291         ###
293         # Environment variable for SDK dir
294         if(EXISTS "$ENV{WindowsSDKDir}" AND (NOT "$ENV{WindowsSDKDir}" STREQUAL ""))
295                 _winsdk_conditional_append_preferred("WindowsSDKDir environment variable" "$ENV{WindowsSDKDir}")
296         endif()
298         if(_winsdk_msvc_less_1600)
299                 # Per-user current Windows SDK for VS2005/2008
300                 get_filename_component(_sdkdir
301                         "[HKEY_CURRENT_USER\\Software\\Microsoft\\Microsoft SDKs\\Windows;CurrentInstallFolder]"
302                         ABSOLUTE)
303                 _winsdk_conditional_append_preferred("Per-user current Windows SDK" "${_sdkdir}")
305                 # System-wide current Windows SDK for VS2005/2008
306                 get_filename_component(_sdkdir
307                         "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows;CurrentInstallFolder]"
308                         ABSOLUTE)
309                 _winsdk_conditional_append_preferred("System-wide current Windows SDK" "${_sdkdir}")
310         endif()
312         ###
313         # Begin the massive list of SDK searching!
314         ###
315         if(_winsdk_vistaonly_ok AND _winsdk_msvc_not_less_1800)
316                 # These require at least Visual Studio 2013 (VC12)
318                 _winsdk_check_microsoft_sdks_registry(v10.0A)
320                 # Windows Software Development Kit (SDK) for Windows 10
321                 # Several different versions living in the same directory - if nothing else we can assume RTM (10240)
322                 _winsdk_check_microsoft_sdks_registry(v10.0 10.0.10240.0)
323                 foreach(_win10build ${_winsdk_win10vers})
324                         _winsdk_check_win10_kits(${_win10build})
325                 endforeach()
326         endif() # vista-only and 2013+
328         # Included in Visual Studio 2013
329         # Includes the v120_xp toolset
330         _winsdk_check_microsoft_sdks_registry(v8.1A 8.1.51636)
332         if(_winsdk_vistaonly_ok AND _winsdk_msvc_not_less_1800)
333                 # Windows Software Development Kit (SDK) for Windows 8.1
334                 # http://msdn.microsoft.com/en-gb/windows/desktop/bg162891
335                 _winsdk_check_microsoft_sdks_registry(v8.1 8.1.25984.0)
336                 _winsdk_check_windows_kits_registry("Windows Kits 8.1" 8.1.25984.0 KitsRoot81)
337         endif() # vista-only and 2013+
339         if(_winsdk_vistaonly_ok)
340                 # Included in Visual Studio 2012
341                 _winsdk_check_microsoft_sdks_registry(v8.0A 8.0.50727)
343                 # Microsoft Windows SDK for Windows 8 and .NET Framework 4.5
344                 # This is the first version to also include the DirectX SDK
345                 # http://msdn.microsoft.com/en-US/windows/desktop/hh852363.aspx
346                 _winsdk_check_microsoft_sdks_registry(v8.0 6.2.9200.16384)
347                 _winsdk_check_windows_kits_registry("Windows Kits 8.0" 6.2.9200.16384 KitsRoot)
348         endif() # vista-only
350         # Included with VS 2012 Update 1 or later
351         # Introduces v110_xp toolset
352         _winsdk_check_microsoft_sdks_registry(v7.1A 7.1.51106)
353         if(_winsdk_vistaonly_ok)
354                 # Microsoft Windows SDK for Windows 7 and .NET Framework 4
355                 # http://www.microsoft.com/downloads/en/details.aspx?FamilyID=6b6c21d2-2006-4afa-9702-529fa782d63b
356                 _winsdk_check_microsoft_sdks_registry(v7.1 7.1.7600.0.30514)
357         endif() # vista-only
359         # Included with VS 2010
360         _winsdk_check_microsoft_sdks_registry(v7.0A 6.1.7600.16385)
362         # Windows SDK for Windows 7 and .NET Framework 3.5 SP1
363         # Works with VC9
364         # http://www.microsoft.com/en-us/download/details.aspx?id=18950
365         _winsdk_check_microsoft_sdks_registry(v7.0 6.1.7600.16385)
367         # Two versions call themselves "v6.1":
368         # Older:
369         # Windows Vista Update & .NET 3.0 SDK
370         # http://www.microsoft.com/en-us/download/details.aspx?id=14477
372         # Newer:
373         # Windows Server 2008 & .NET 3.5 SDK
374         # may have broken VS9SP1? they recommend v7.0 instead, or a KB...
375         # http://www.microsoft.com/en-us/download/details.aspx?id=24826
376         _winsdk_check_microsoft_sdks_registry(v6.1 6.1.6000.16384.10)
378         # Included in VS 2008
379         _winsdk_check_microsoft_sdks_registry(v6.0A 6.1.6723.1)
381         # Microsoft Windows Software Development Kit for Windows Vista and .NET Framework 3.0 Runtime Components
382         # http://blogs.msdn.com/b/stanley/archive/2006/11/08/microsoft-windows-software-development-kit-for-windows-vista-and-net-framework-3-0-runtime-components.aspx
383         _winsdk_check_microsoft_sdks_registry(v6.0 6.0.6000.16384)
384 endif()
386 # Let's not forget the Platform SDKs, which sometimes are useful!
387 if(_winsdk_msvc_greater_1200)
388         _winsdk_check_platformsdk_registry("Microsoft Platform SDK for Windows Server 2003 R2" "5.2.3790.2075.51" "D2FF9F89-8AA2-4373-8A31-C838BF4DBBE1")
389         _winsdk_check_platformsdk_registry("Microsoft Platform SDK for Windows Server 2003 SP1" "5.2.3790.1830.15" "8F9E5EF3-A9A5-491B-A889-C58EFFECE8B3")
390 endif()
392 # Finally, look for "preferred" SDKs
394 if(_winsdk_msvc_greater_1310) # Newer than VS .NET/VS Toolkit 2003
397         # Environment variable for SDK dir
398         if(EXISTS "$ENV{WindowsSDKDir}" AND (NOT "$ENV{WindowsSDKDir}" STREQUAL ""))
399                 _winsdk_conditional_append_preferred("WindowsSDKDir environment variable" "$ENV{WindowsSDKDir}")
400         endif()
402         if(_winsdk_msvc_less_1600)
403                 # Per-user current Windows SDK for VS2005/2008
404                 get_filename_component(_sdkdir
405                         "[HKEY_CURRENT_USER\\Software\\Microsoft\\Microsoft SDKs\\Windows;CurrentInstallFolder]"
406                         ABSOLUTE)
407                 _winsdk_conditional_append_preferred("Per-user current Windows SDK" "${_sdkdir}")
409                 # System-wide current Windows SDK for VS2005/2008
410                 get_filename_component(_sdkdir
411                         "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows;CurrentInstallFolder]"
412                         ABSOLUTE)
413                 _winsdk_conditional_append_preferred("System-wide current Windows SDK" "${_sdkdir}")
414         endif()
415 endif()
418 function(windowssdk_name_lookup _dir _outvar)
419         list(FIND _win_sdk_versanddirs "${_dir}" _diridx)
420         math(EXPR _idx "${_diridx} - 1")
421         if(${_idx} GREATER -1)
422                 list(GET _win_sdk_versanddirs ${_idx} _ret)
423         else()
424                 set(_ret "NOTFOUND")
425         endif()
426         set(${_outvar} "${_ret}" PARENT_SCOPE)
427 endfunction()
429 function(windowssdk_build_lookup _dir _outvar)
430         list(FIND _win_sdk_buildsanddirs "${_dir}" _diridx)
431         math(EXPR _idx "${_diridx} - 1")
432         if(${_idx} GREATER -1)
433                 list(GET _win_sdk_buildsanddirs ${_idx} _ret)
434         else()
435                 set(_ret "NOTFOUND")
436         endif()
437         set(${_outvar} "${_ret}" PARENT_SCOPE)
438 endfunction()
440 # If we found something...
441 if(_win_sdk_dirs)
442         list(GET _win_sdk_dirs 0 WINDOWSSDK_LATEST_DIR)
443         windowssdk_name_lookup("${WINDOWSSDK_LATEST_DIR}"
444                 WINDOWSSDK_LATEST_NAME)
445         set(WINDOWSSDK_DIRS ${_win_sdk_dirs})
447         # Fallback, in case no preference found.
448         set(WINDOWSSDK_PREFERRED_DIR "${WINDOWSSDK_LATEST_DIR}")
449         set(WINDOWSSDK_PREFERRED_NAME "${WINDOWSSDK_LATEST_NAME}")
450         set(WINDOWSSDK_PREFERRED_FIRST_DIRS ${WINDOWSSDK_DIRS})
451         set(WINDOWSSDK_FOUND_PREFERENCE OFF)
452 endif()
454 # If we found indications of a user preference...
455 if(_win_sdk_preferred_sdk_dirs)
456         list(GET _win_sdk_preferred_sdk_dirs 0 WINDOWSSDK_PREFERRED_DIR)
457         windowssdk_name_lookup("${WINDOWSSDK_PREFERRED_DIR}"
458                 WINDOWSSDK_PREFERRED_NAME)
459         set(WINDOWSSDK_PREFERRED_FIRST_DIRS
460                 ${_win_sdk_preferred_sdk_dirs}
461                 ${_win_sdk_dirs})
462         list(REMOVE_DUPLICATES WINDOWSSDK_PREFERRED_FIRST_DIRS)
463         set(WINDOWSSDK_FOUND_PREFERENCE ON)
464 endif()
466 include(FindPackageHandleStandardArgs)
467 find_package_handle_standard_args(WindowsSDK
468         "No compatible version of the Windows SDK or Platform SDK found."
469         WINDOWSSDK_DIRS)
471 if(WINDOWSSDK_FOUND)
472         # Internal: Architecture-appropriate library directory names.
473         if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM")
474                 if(CMAKE_SIZEOF_VOID_P MATCHES "8")
475                         # Only supported in Win10 SDK and up.
476                         set(_winsdk_arch8 arm64) # what the WDK for Win8+ calls this architecture
477                 else()
478                         set(_winsdk_archbare /arm) # what the architecture used to be called in oldest SDKs
479                         set(_winsdk_arch arm) # what the architecture used to be called
480                         set(_winsdk_arch8 arm) # what the WDK for Win8+ calls this architecture
481                 endif()
482         else()
483                 if(CMAKE_SIZEOF_VOID_P MATCHES "8")
484                         set(_winsdk_archbare /x64) # what the architecture used to be called in oldest SDKs
485                         set(_winsdk_arch amd64) # what the architecture used to be called
486                         set(_winsdk_arch8 x64) # what the WDK for Win8+ calls this architecture
487                 else()
488                         set(_winsdk_archbare ) # what the architecture used to be called in oldest SDKs
489                         set(_winsdk_arch i386) # what the architecture used to be called
490                         set(_winsdk_arch8 x86) # what the WDK for Win8+ calls this architecture
491                 endif()
492         endif()
494         function(get_windowssdk_from_component _component _var)
495                 get_filename_component(_component "${_component}" ABSOLUTE)
496                 file(TO_CMAKE_PATH "${_component}" _component)
497                 foreach(_sdkdir ${WINDOWSSDK_DIRS})
498                         get_filename_component(_sdkdir "${_sdkdir}" ABSOLUTE)
499                         string(LENGTH "${_sdkdir}" _sdklen)
500                         file(RELATIVE_PATH _rel "${_sdkdir}" "${_component}")
501                         # If we don't have any "parent directory" items...
502                         if(NOT "${_rel}" MATCHES "[.][.]")
503                                 set(${_var} "${_sdkdir}" PARENT_SCOPE)
504                                 return()
505                         endif()
506                 endforeach()
507                 # Fail.
508                 set(${_var} "NOTFOUND" PARENT_SCOPE)
509         endfunction()
510         function(get_windowssdk_library_dirs _winsdk_dir _var)
511                 set(_dirs)
512                 set(_suffixes
513                         "lib${_winsdk_archbare}" # SDKs like 7.1A
514                         "lib/${_winsdk_arch}" # just because some SDKs have x86 dir and root dir
515                         "lib/w2k/${_winsdk_arch}" # Win2k min requirement
516                         "lib/wxp/${_winsdk_arch}" # WinXP min requirement
517                         "lib/wnet/${_winsdk_arch}" # Win Server 2003 min requirement
518                         "lib/wlh/${_winsdk_arch}"
519                         "lib/wlh/um/${_winsdk_arch8}" # Win Vista ("Long Horn") min requirement
520                         "lib/win7/${_winsdk_arch}"
521                         "lib/win7/um/${_winsdk_arch8}" # Win 7 min requirement
522                 )
523                 foreach(_ver
524                         wlh # Win Vista ("Long Horn") min requirement
525                         win7 # Win 7 min requirement
526                         win8 # Win 8 min requirement
527                         winv6.3 # Win 8.1 min requirement
528                 )
530                         list(APPEND _suffixes
531                                 "lib/${_ver}/${_winsdk_arch}"
532                                 "lib/${_ver}/um/${_winsdk_arch8}"
533                                 "lib/${_ver}/km/${_winsdk_arch8}"
534                         )
535                 endforeach()
537                 # Look for WDF libraries in Win10+ SDK
538                 foreach(_mode umdf kmdf)
539                         file(GLOB _wdfdirs RELATIVE "${_winsdk_dir}" "${_winsdk_dir}/lib/wdf/${_mode}/${_winsdk_arch8}/*")
540                         if(_wdfdirs)
541                                 list(APPEND _suffixes ${_wdfdirs})
542                         endif()
543                 endforeach()
545                 # Look in each Win10+ SDK version for the components
546                 foreach(_win10ver ${_winsdk_win10vers})
547                         foreach(_component um km ucrt mmos)
548                                 list(APPEND _suffixes "lib/${_win10ver}/${_component}/${_winsdk_arch8}")
549                         endforeach()
550                 endforeach()
552                 foreach(_suffix ${_suffixes})
553                         # Check to see if a library actually exists here.
554                         file(GLOB _libs "${_winsdk_dir}/${_suffix}/*.lib")
555                         if(_libs)
556                                 list(APPEND _dirs "${_winsdk_dir}/${_suffix}")
557                         endif()
558                 endforeach()
559                 if("${_dirs}" STREQUAL "")
560                         set(_dirs NOTFOUND)
561                 else()
562                         list(REMOVE_DUPLICATES _dirs)
563                 endif()
564                 set(${_var} ${_dirs} PARENT_SCOPE)
565         endfunction()
566         function(get_windowssdk_include_dirs _winsdk_dir _var)
567                 set(_dirs)
569                 set(_subdirs shared um winrt km wdf mmos ucrt)
570                 set(_suffixes Include)
572                 foreach(_dir ${_subdirs})
573                         list(APPEND _suffixes "Include/${_dir}")
574                 endforeach()
576                 foreach(_ver ${_winsdk_win10vers})
577                         foreach(_dir ${_subdirs})
578                                 list(APPEND _suffixes "Include/${_ver}/${_dir}")
579                         endforeach()
580                 endforeach()
582                 foreach(_suffix ${_suffixes})
583                         # Check to see if a header file actually exists here.
584                         file(GLOB _headers "${_winsdk_dir}/${_suffix}/*.h")
585                         if(_headers)
586                                 list(APPEND _dirs "${_winsdk_dir}/${_suffix}")
587                         endif()
588                 endforeach()
589                 if("${_dirs}" STREQUAL "")
590                         set(_dirs NOTFOUND)
591                 else()
592                         list(REMOVE_DUPLICATES _dirs)
593                 endif()
594                 set(${_var} ${_dirs} PARENT_SCOPE)
595         endfunction()
596         function(get_windowssdk_library_dirs_multiple _var)
597                 set(_dirs)
598                 foreach(_sdkdir ${ARGN})
599                         get_windowssdk_library_dirs("${_sdkdir}" _current_sdk_libdirs)
600                         if(_current_sdk_libdirs)
601                                 list(APPEND _dirs ${_current_sdk_libdirs})
602                         endif()
603                 endforeach()
604                 if("${_dirs}" STREQUAL "")
605                         set(_dirs NOTFOUND)
606                 else()
607                         list(REMOVE_DUPLICATES _dirs)
608                 endif()
609                 set(${_var} ${_dirs} PARENT_SCOPE)
610         endfunction()
611         function(get_windowssdk_include_dirs_multiple _var)
612                 set(_dirs)
613                 foreach(_sdkdir ${ARGN})
614                         get_windowssdk_include_dirs("${_sdkdir}" _current_sdk_incdirs)
615                         if(_current_sdk_libdirs)
616                                 list(APPEND _dirs ${_current_sdk_incdirs})
617                         endif()
618                 endforeach()
619                 if("${_dirs}" STREQUAL "")
620                         set(_dirs NOTFOUND)
621                 else()
622                         list(REMOVE_DUPLICATES _dirs)
623                 endif()
624                 set(${_var} ${_dirs} PARENT_SCOPE)
625         endfunction()
626 endif()