Skip to content

Commit 370515d

Browse files
author
Dan Dees
committed
FIRST - add cmake build generator - CMakeLists.txt
- out of source build - no projects or build artifacts polluting source tree - supports CI/CD automation - generates many build types with simple command line option - https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html - ninja - FAST command line - visual studio project - msbuild - msys make - name - builds static, shared (DLL), and "reflection main" libraries - shared library includes 3rd party - no install needed of Detours as linked into DLL - support shared DLL and "relection" option DLL - creates library/header installation - installs as package - supports vcpkg, conan, CPM - supports nuget - https://cmake.org/cmake/help/latest/cpack_gen/nuget.html - builds 3rd party deps - automates debug and release builds - done with ninja/msbuild - exports config for use in other cmake project - MemoryModulePPConfigVersion.cmake - find_package(MemoryModulePP CONFIG) is enough to use in another project - https://cmake.org/cmake/help/latest/command/find_package.html - importing and exporting cmake project - https://cmake.org/cmake/help/latest/guide/importing-exporting/index.html - maintainable - LLMs such as ChatGPT can create/maintain CMakeLists.txt - no longer need to learn the book and best practices - CMakeList modern format with targets as objects - CMAKE_DEBUG_POSTIFIX allows debug and release dll/exe in same directory example ninja installation output ... -- Install configuration: "Debug" -- Install configuration: "Debug" -- Installing: x:/vs17-64/memorymodulepp/lib/MemoryModulePP-static_d.lib -- Installing: x:/vs17-64/memorymodulepp/include/BaseAddressIndex.h -- Installing: x:/vs17-64/memorymodulepp/include/ImportTable.h -- Installing: x:/vs17-64/memorymodulepp/include/Initialize.h -- Installing: x:/vs17-64/memorymodulepp/include/InvertedFunctionTable.h -- Installing: x:/vs17-64/memorymodulepp/include/LdrEntry.h -- Installing: x:/vs17-64/memorymodulepp/include/LoadDllMemoryApi.h -- Installing: x:/vs17-64/memorymodulepp/include/Loader.h -- Installing: x:/vs17-64/memorymodulepp/include/MemoryModule.h -- Installing: x:/vs17-64/memorymodulepp/include/MmpGlobalData.h -- Installing: x:/vs17-64/memorymodulepp/include/MmpDotNet.h -- Installing: x:/vs17-64/memorymodulepp/include/MmpTls.h -- Installing: x:/vs17-64/memorymodulepp/include/Utils.h -- Installing: x:/vs17-64/memorymodulepp/lib/MemoryModulePP-shared_d.lib -- Installing: x:/vs17-64/memorymodulepp/bin/MemoryModulePP-shared_d.dll -- Installing: x:/vs17-64/memorymodulepp/include/BaseAddressIndex.h -- Installing: x:/vs17-64/memorymodulepp/include/ImportTable.h -- Installing: x:/vs17-64/memorymodulepp/include/Initialize.h -- Installing: x:/vs17-64/memorymodulepp/include/InvertedFunctionTable.h -- Installing: x:/vs17-64/memorymodulepp/include/LdrEntry.h -- Installing: x:/vs17-64/memorymodulepp/include/LoadDllMemoryApi.h -- Installing: x:/vs17-64/memorymodulepp/include/Loader.h -- Installing: x:/vs17-64/memorymodulepp/include/MemoryModule.h -- Installing: x:/vs17-64/memorymodulepp/include/MmpGlobalData.h -- Installing: x:/vs17-64/memorymodulepp/include/MmpDotNet.h -- Installing: x:/vs17-64/memorymodulepp/include/MmpTls.h -- Installing: x:/vs17-64/memorymodulepp/include/Utils.h -- Installing: x:/vs17-64/memorymodulepp/lib/MemoryModulePP-relfect-shared_d.lib -- Installing: x:/vs17-64/memorymodulepp/bin/MemoryModulePP-relfect-shared_d.dll -- Installing: x:/vs17-64/memorymodulepp/include/BaseAddressIndex.h -- Installing: x:/vs17-64/memorymodulepp/include/ImportTable.h -- Installing: x:/vs17-64/memorymodulepp/include/Initialize.h -- Installing: x:/vs17-64/memorymodulepp/include/InvertedFunctionTable.h -- Installing: x:/vs17-64/memorymodulepp/include/LdrEntry.h -- Installing: x:/vs17-64/memorymodulepp/include/LoadDllMemoryApi.h -- Installing: x:/vs17-64/memorymodulepp/include/Loader.h -- Installing: x:/vs17-64/memorymodulepp/include/MemoryModule.h -- Installing: x:/vs17-64/memorymodulepp/include/MmpGlobalData.h -- Installing: x:/vs17-64/memorymodulepp/include/MmpDotNet.h -- Installing: x:/vs17-64/memorymodulepp/include/MmpTls.h -- Installing: x:/vs17-64/memorymodulepp/include/Utils.h -- Installing: x:/vs17-64/memorymodulepp/lib/cmake/MemoryModulePP/MemoryModulePPTargets.cmake -- Installing: x:/vs17-64/memorymodulepp/lib/cmake/MemoryModulePP/MemoryModulePPTargets-debug.cmake -- Installing: x:/vs17-64/memorymodulepp/lib/cmake/MemoryModulePP/MemoryModulePPConfig.cmake -- Installing: x:/vs17-64/memorymodulepp/lib/cmake/MemoryModulePP/MemoryModulePPConfigVersion.cmake
1 parent 588b48e commit 370515d

File tree

6 files changed

+269
-0
lines changed

6 files changed

+269
-0
lines changed

3rdparty/CMakelists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cmake_minimum_required(VERSION 3.30)
2+
project(Detours LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_STANDARD 23)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
set(libname lib_detours)
8+
set(DETOURS_SOURCE Detours)
9+
10+
add_library(${libname} STATIC)
11+
target_include_directories(${libname} PRIVATE ${DETOURS_SOURCE})
12+
target_sources(${libname} PRIVATE
13+
${DETOURS_SOURCE}/creatwth.cpp
14+
${DETOURS_SOURCE}/detours.cpp
15+
${DETOURS_SOURCE}/disasm.cpp
16+
${DETOURS_SOURCE}/disolarm.cpp
17+
${DETOURS_SOURCE}/disolarm64.cpp
18+
${DETOURS_SOURCE}/disolia64.cpp
19+
${DETOURS_SOURCE}/disolx64.cpp
20+
${DETOURS_SOURCE}/disolx86.cpp
21+
${DETOURS_SOURCE}/image.cpp
22+
${DETOURS_SOURCE}/modules.cpp
23+
${DETOURS_SOURCE}/uimports.cpp
24+
${DETOURS_SOURCE}/detours.h
25+
${DETOURS_SOURCE}/detver.h
26+
)
27+
28+
# This file is included and not compiled on its own
29+
set_property (
30+
SOURCE ${DETOURS_SOURCE}/uimports.cpp
31+
APPEND PROPERTY HEADER_FILE_ONLY true)

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.27)
2+
project(MemoryModulePP LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_STANDARD 23)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
# add _d to debug targets
8+
SET(CMAKE_DEBUG_POSTFIX _d)
9+
10+
# build spec done in subdirectories
11+
add_subdirectory(3rdparty)
12+
add_subdirectory(a)
13+
add_subdirectory(MemoryModule)
14+
add_subdirectory(test)

MemoryModule/CMakeLists.txt

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
cmake_minimum_required(VERSION 3.30)
2+
project(lib-MemoryModule LANGUAGES CXX)
3+
4+
# Define static library target
5+
set(static_libname "MemoryModulePP-static")
6+
add_library(${static_libname} STATIC)
7+
8+
# Define shared library target
9+
set(shared_libname "MemoryModulePP-shared")
10+
add_library(${shared_libname} SHARED)
11+
12+
# turn on "reflection"
13+
target_compile_definitions(${shared_libname} PRIVATE _USRDLL)
14+
15+
# link libraries for DLL
16+
target_link_libraries(${shared_libname} PRIVATE lib_detours)
17+
18+
# Common sources and headers for both libraries
19+
set(common_sources
20+
BaseAddressIndex.cpp
21+
ImportTable.cpp
22+
Initialize.cpp
23+
InvertedFunctionTable.cpp
24+
LdrEntry.cpp
25+
LoadDllMemoryApi.cpp
26+
Loader.cpp
27+
MemoryModule.cpp
28+
MmpDotNet.cpp
29+
MmpLdrpTls.cpp
30+
MmpTls.cpp
31+
MmpTlsFiber.cpp
32+
Utils.cpp
33+
34+
# exports
35+
MemoryModulePP.def
36+
)
37+
38+
set(common_public_headers
39+
BaseAddressIndex.h
40+
ImportTable.h
41+
Initialize.h
42+
InvertedFunctionTable.h
43+
LdrEntry.h
44+
LoadDllMemoryApi.h
45+
Loader.h
46+
MemoryModule.h
47+
MmpGlobalData.h
48+
MmpDotNet.h
49+
MmpTls.h
50+
Utils.h
51+
)
52+
53+
set(common_private_headers
54+
LoaderPrivate.h
55+
MmpTlsFiber.h
56+
MmpTlsp.h
57+
ReflectiveDLLInjection.h
58+
ReflectiveLoader.h
59+
)
60+
61+
# Specify sources and headers for static library
62+
target_sources(${static_libname}
63+
PRIVATE
64+
${common_sources}
65+
PUBLIC
66+
FILE_SET HEADERS
67+
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
68+
FILES
69+
${common_public_headers}
70+
PRIVATE
71+
FILE_SET private_headers
72+
TYPE HEADERS
73+
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
74+
FILES
75+
${common_private_headers}
76+
)
77+
78+
# Specify sources and headers for shared library
79+
target_sources(${shared_libname}
80+
PRIVATE
81+
${common_sources}
82+
PUBLIC
83+
FILE_SET HEADERS
84+
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
85+
FILES
86+
${common_public_headers}
87+
PRIVATE
88+
FILE_SET private_headers
89+
TYPE HEADERS
90+
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
91+
FILES
92+
${common_private_headers}
93+
)
94+
95+
# Set include directories for libraries
96+
foreach(libname ${static_libname} ${shared_libname})
97+
target_include_directories(${libname}
98+
PUBLIC
99+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
100+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
101+
$<INSTALL_INTERFACE:include>
102+
)
103+
104+
# Ensure C++ standard
105+
target_compile_features(${libname} PRIVATE cxx_std_23)
106+
107+
# key definitions
108+
target_compile_definitions(${libname} PRIVATE
109+
_MEMORY_MODULE
110+
_HAS_AUTO_INITIALIZE
111+
)
112+
endforeach()
113+
114+
# Install libraries and public headers
115+
install(TARGETS ${static_libname} ${shared_libname}
116+
EXPORT MemoryModulePPTargets
117+
LIBRARY DESTINATION lib
118+
ARCHIVE DESTINATION lib
119+
RUNTIME DESTINATION bin
120+
FILE_SET HEADERS DESTINATION include
121+
)
122+
123+
# Install export file
124+
install(EXPORT MemoryModulePPTargets
125+
FILE MemoryModulePPTargets.cmake
126+
NAMESPACE MemoryModulePP::
127+
DESTINATION lib/cmake/MemoryModulePP
128+
)
129+
130+
# Generate and install CMake config file
131+
include(CMakePackageConfigHelpers)
132+
write_basic_package_version_file(
133+
"${CMAKE_CURRENT_BINARY_DIR}/MemoryModulePPConfigVersion.cmake"
134+
VERSION 1.0.0
135+
COMPATIBILITY SameMajorVersion
136+
)
137+
138+
configure_file(
139+
"${CMAKE_CURRENT_SOURCE_DIR}/../MemoryModulePPConfig.cmake.in"
140+
"${CMAKE_CURRENT_BINARY_DIR}/MemoryModulePPConfig.cmake"
141+
@ONLY
142+
)
143+
144+
install(FILES
145+
"${CMAKE_CURRENT_BINARY_DIR}/MemoryModulePPConfig.cmake"
146+
"${CMAKE_CURRENT_BINARY_DIR}/MemoryModulePPConfigVersion.cmake"
147+
DESTINATION lib/cmake/MemoryModulePP
148+
)
149+
150+
# CPack configuration for packaging
151+
set(CPACK_PACKAGE_NAME "MemoryModulePP")
152+
set(CPACK_PACKAGE_VERSION "1.0.0")
153+
set(CPACK_PACKAGE_DESCRIPTION "A custom MemoryModulePP implementation")
154+
set(CPACK_PACKAGE_VENDOR "xAI")
155+
set(CPACK_GENERATOR "ZIP;TGZ")
156+
include(CPack)

MemoryModulePPConfig.cmake.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/LoadLibraryTargets.cmake")
4+
check_required_components(LoadLibrary)

a/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.30)
2+
project(a-sample LANGUAGES CXX)
3+
4+
set(libname "a")
5+
6+
add_library(${libname} SHARED)
7+
target_link_libraries(${libname} PRIVATE lib_detours)
8+
target_sources(${libname} PRIVATE
9+
# sources
10+
dllmain.cpp
11+
exception.cpp
12+
gdiplus.cpp
13+
thread.cpp
14+
15+
# exports
16+
m.def
17+
18+
# resources
19+
a.rc
20+
resource.h
21+
)
22+
23+
# put dll in local install dir
24+
set( INSTALL_DIR "${CMAKE_BINARY_DIR}/install")
25+
install(TARGETS ${libname}
26+
LIBRARY DESTINATION ${INSTALL_DIR}
27+
ARCHIVE DESTINATION ${INSTALL_DIR}
28+
RUNTIME DESTINATION ${INSTALL_DIR}
29+
)

test/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
cmake_minimum_required(VERSION 3.27)
2+
project(mmtest LANGUAGES CXX)
3+
4+
# Define executable targets
5+
set(static_exename "mmtest-static")
6+
add_executable(${static_exename})
7+
target_link_libraries(${static_exename} PRIVATE
8+
MemoryModulePP-static
9+
lib_detours
10+
)
11+
12+
set(shared_exename "mmtest-shared")
13+
add_executable(${shared_exename})
14+
target_link_libraries(${shared_exename} PRIVATE
15+
MemoryModulePP-shared
16+
)
17+
18+
# use install directory under build to assemble tests
19+
set( INSTALL_DIR "${CMAKE_BINARY_DIR}/install")
20+
21+
foreach(exename ${static_exename} ${shared_exename})
22+
23+
# common sources and libs
24+
target_sources(${exename} PRIVATE test.cpp)
25+
26+
# copy exes into install dir
27+
install(TARGETS ${exename} RUNTIME DESTINATION ${INSTALL_DIR})
28+
29+
# set a_d.dll path as command arg in VS debugger
30+
file(TO_NATIVE_PATH "$<TARGET_FILE:a>" A_DLL)
31+
set_target_properties(${exename} PROPERTIES
32+
"VS_DEBUGGER_COMMAND_ARGUMENTS"
33+
"${A_DLL}"
34+
)
35+
endforeach()

0 commit comments

Comments
 (0)