Skip to content

Commit 218a5fa

Browse files
authored
Add FindDirectX.cmake module for robust SDK detection (#9)
1 parent 4ade2af commit 218a5fa

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,14 @@ target_sources(unassemblize PRIVATE
216216

217217
# Platform-specific configurations
218218
if(WINDOWS)
219+
find_package(DirectX REQUIRED)
219220
target_sources(unassemblize PRIVATE
220221
src/imguiclient/imguiwin32.cpp
221222
src/imguiclient/imguiwin32.h
222223
)
223224

224225
target_link_libraries(unassemblize PRIVATE
225-
d3d9.lib # TODO: figure out if any "FindDirectX" logic is needed here.
226+
DirectX::D3D9
226227
imgui_win32
227228
${DIASDK_LIBRARIES}
228229
)

cmake/FindDirectX.cmake

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
)

cmake/imgui.cmake

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ set(IMGUI_HEADERS
2323
)
2424

2525
if(WINDOWS)
26+
find_package(DirectX REQUIRED)
2627
add_library(imgui_win32 STATIC ${IMGUI_SOURCES}
2728
${IMGUI_DIR}/backends/imgui_impl_dx9.cpp
2829
${IMGUI_DIR}/backends/imgui_impl_win32.cpp
@@ -47,7 +48,7 @@ if(WINDOWS)
4748
)
4849

4950
# Windows uses DirectX, no need for OpenGL
50-
target_link_libraries(imgui_win32 PRIVATE d3d9)
51+
target_link_libraries(imgui_win32 PRIVATE DirectX::D3D9)
5152
set(IMGUI_LIBRARY imgui_win32)
5253
else()
5354
# Linux and macOS use OpenGL3

0 commit comments

Comments
 (0)