|
| 1 | +# FindDirectX.cmake |
| 2 | +# |
| 3 | +# Finds DirectX SDK through the Windows SDK or legacy DirectX SDKs |
| 4 | +# |
| 5 | +# Sets up: |
| 6 | +# DirectX_FOUND - System has DirectX SDK |
| 7 | +# DirectX_INCLUDE_DIRS - DirectX include directories |
| 8 | +# DirectX_LIBRARIES - DirectX libraries |
| 9 | +# DirectX_D3D9_LIBRARY - Path to d3d9.lib |
| 10 | + |
| 11 | +include(FindPackageHandleStandardArgs) |
| 12 | + |
| 13 | +# Determine architecture |
| 14 | +if (CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 15 | + set(DIRECTX_ARCHITECTURE x64) |
| 16 | +else() |
| 17 | + set(DIRECTX_ARCHITECTURE x86) |
| 18 | +endif() |
| 19 | + |
| 20 | +# Windows SDK and DirectX SDK paths |
| 21 | +set(DIRECTX_ROOT_DIR |
| 22 | + "$ENV{WINDOWSSDKDIR}" # Primary Windows SDK location from environment |
| 23 | + "C:/Program Files (x86)/Windows Kits/10" # Windows 10+ SDK base path |
| 24 | + "C:/Program Files (x86)/Windows Kits/8.1" # Windows 8.1 SDK |
| 25 | + "C:/Program Files (x86)/Microsoft DirectX SDK (June 2010)" # Legacy DirectX SDK |
| 26 | + "C:/Program Files (x86)/Microsoft DirectX 9.0 SDK (Summer 2004)" # Very old DirectX SDK |
| 27 | + "C:/DXSDK" # Another common legacy DirectX SDK location |
| 28 | +) |
| 29 | + |
| 30 | +# Find DirectX headers |
| 31 | +find_path(DirectX_INCLUDE_DIR |
| 32 | + NAMES d3d9.h |
| 33 | + PATHS ${DIRECTX_ROOT_DIR} |
| 34 | + PATH_SUFFIXES |
| 35 | + "Include/*/um" # Windows SDK - any version |
| 36 | + "Include/um" # Windows SDK - fallback |
| 37 | + "Include" # Legacy DirectX SDK |
| 38 | +) |
| 39 | + |
| 40 | +# Find DirectX libraries |
| 41 | +find_library(DirectX_D3D9_LIBRARY |
| 42 | + NAMES d3d9 |
| 43 | + PATHS ${DIRECTX_ROOT_DIR} |
| 44 | + PATH_SUFFIXES |
| 45 | + "Lib/*/um/${DIRECTX_ARCHITECTURE}" # Windows SDK - any version |
| 46 | + "Lib/um/${DIRECTX_ARCHITECTURE}" # Windows SDK - fallback |
| 47 | + "Lib/${DIRECTX_ARCHITECTURE}" # Legacy DirectX SDK |
| 48 | +) |
| 49 | + |
| 50 | +# Handle the QUIETLY and REQUIRED arguments and set DirectX_FOUND |
| 51 | +find_package_handle_standard_args(DirectX |
| 52 | + REQUIRED_VARS |
| 53 | + DirectX_INCLUDE_DIR |
| 54 | + DirectX_D3D9_LIBRARY |
| 55 | + FAIL_MESSAGE "DirectX not found. Please ensure either Windows SDK or DirectX SDK is installed." |
| 56 | +) |
| 57 | + |
| 58 | +if(DirectX_FOUND) |
| 59 | + # Add some debug messages to help troubleshoot paths |
| 60 | + message(STATUS "DirectX Include Dir: ${DirectX_INCLUDE_DIR}") |
| 61 | + message(STATUS "DirectX D3D9 Library: ${DirectX_D3D9_LIBRARY}") |
| 62 | + |
| 63 | + set(DirectX_INCLUDE_DIRS ${DirectX_INCLUDE_DIR}) |
| 64 | + set(DirectX_LIBRARIES ${DirectX_D3D9_LIBRARY}) |
| 65 | + |
| 66 | + if(NOT TARGET DirectX::D3D9) |
| 67 | + add_library(DirectX::D3D9 UNKNOWN IMPORTED) |
| 68 | + set_target_properties(DirectX::D3D9 PROPERTIES |
| 69 | + IMPORTED_LOCATION "${DirectX_D3D9_LIBRARY}" |
| 70 | + INTERFACE_INCLUDE_DIRECTORIES "${DirectX_INCLUDE_DIR}" |
| 71 | + ) |
| 72 | + endif() |
| 73 | +endif() |
| 74 | + |
| 75 | +mark_as_advanced( |
| 76 | + DirectX_INCLUDE_DIR |
| 77 | + DirectX_D3D9_LIBRARY |
| 78 | +) |
0 commit comments