-
-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
270 lines (236 loc) · 8.04 KB
/
CMakeLists.txt
File metadata and controls
270 lines (236 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
cmake_minimum_required(VERSION 3.15)
project(scalene VERSION 1.0 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# FetchContent for downloading dependencies
include(FetchContent)
# Fetch Heap-Layers (required for all platforms)
set(HEAP_LAYERS_DIR "${CMAKE_SOURCE_DIR}/vendor/Heap-Layers")
if(NOT EXISTS "${HEAP_LAYERS_DIR}/heaplayers.h")
message(STATUS "Fetching Heap-Layers...")
FetchContent_Declare(
heap_layers
GIT_REPOSITORY https://github.com/emeryberger/Heap-Layers.git
GIT_TAG master
GIT_SHALLOW TRUE
SOURCE_DIR ${HEAP_LAYERS_DIR}
)
FetchContent_MakeAvailable(heap_layers)
endif()
# Fetch printf library (required for all platforms)
set(PRINTF_DIR "${CMAKE_SOURCE_DIR}/vendor/printf")
if(NOT EXISTS "${PRINTF_DIR}/printf.cpp")
message(STATUS "Fetching printf library...")
FetchContent_Declare(
printf_lib
GIT_REPOSITORY https://github.com/mpaland/printf.git
GIT_TAG master
GIT_SHALLOW TRUE
SOURCE_DIR ${PRINTF_DIR}
)
FetchContent_MakeAvailable(printf_lib)
# Create printf.cpp symlink/copy from printf.c
if(WIN32)
file(COPY "${PRINTF_DIR}/printf.c" DESTINATION "${PRINTF_DIR}")
file(RENAME "${PRINTF_DIR}/printf.c" "${PRINTF_DIR}/printf.cpp")
# Actually we need to keep both, so copy instead
file(READ "${PRINTF_DIR}/printf.c" PRINTF_CONTENT)
file(WRITE "${PRINTF_DIR}/printf.cpp" "${PRINTF_CONTENT}")
else()
execute_process(
COMMAND ${CMAKE_COMMAND} -E create_symlink printf.c printf.cpp
WORKING_DIRECTORY ${PRINTF_DIR}
)
endif()
# Patch printf.h to comment out the macro definitions
file(READ "${PRINTF_DIR}/printf.h" PRINTF_H_CONTENT)
string(REPLACE "#define printf printf_" "//#define printf printf_" PRINTF_H_CONTENT "${PRINTF_H_CONTENT}")
string(REPLACE "#define vsnprintf vsnprintf_" "//#define vsnprintf vsnprintf_" PRINTF_H_CONTENT "${PRINTF_H_CONTENT}")
file(WRITE "${PRINTF_DIR}/printf.h" "${PRINTF_H_CONTENT}")
endif()
# Find Python
find_package(Python3 REQUIRED COMPONENTS Development)
# Detect architecture
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64|arm64")
set(SCALENE_ARCH "ARM64")
message(STATUS "Building for ARM64 architecture")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64")
set(SCALENE_ARCH "X64")
message(STATUS "Building for x86-64 architecture")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i[3-6]86|x86")
set(SCALENE_ARCH "X86")
message(STATUS "Building for x86 architecture")
else()
message(WARNING "Unknown architecture: ${CMAKE_SYSTEM_PROCESSOR}")
set(SCALENE_ARCH "UNKNOWN")
endif()
# Common include directories
set(SCALENE_INCLUDES
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/src/include
${CMAKE_SOURCE_DIR}/vendor/Heap-Layers
${CMAKE_SOURCE_DIR}/vendor/Heap-Layers/heaps
${CMAKE_SOURCE_DIR}/vendor/Heap-Layers/heaps/threads
${CMAKE_SOURCE_DIR}/vendor/Heap-Layers/heaps/utility
${CMAKE_SOURCE_DIR}/vendor/Heap-Layers/wrappers
${CMAKE_SOURCE_DIR}/vendor/Heap-Layers/utility
${CMAKE_SOURCE_DIR}/vendor/printf
${Python3_INCLUDE_DIRS}
)
# Platform-specific configuration
if(WIN32)
# Windows build
message(STATUS "Configuring Windows build")
# Download Microsoft Detours for native malloc/free hooking
FetchContent_Declare(
detours
GIT_REPOSITORY https://github.com/microsoft/Detours.git
GIT_TAG main
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(detours)
# Microsoft Detours sources
set(DETOURS_SOURCES
${detours_SOURCE_DIR}/src/detours.cpp
${detours_SOURCE_DIR}/src/modules.cpp
${detours_SOURCE_DIR}/src/disasm.cpp
${detours_SOURCE_DIR}/src/image.cpp
${detours_SOURCE_DIR}/src/creatwth.cpp
)
# Add architecture-specific disassembler
if(SCALENE_ARCH STREQUAL "ARM64")
list(APPEND DETOURS_SOURCES ${detours_SOURCE_DIR}/src/disolarm64.cpp)
message(STATUS "Using ARM64 Detours disassembler")
elseif(SCALENE_ARCH STREQUAL "X64")
list(APPEND DETOURS_SOURCES ${detours_SOURCE_DIR}/src/disolx64.cpp)
message(STATUS "Using x64 Detours disassembler")
elseif(SCALENE_ARCH STREQUAL "X86")
list(APPEND DETOURS_SOURCES ${detours_SOURCE_DIR}/src/disolx86.cpp)
message(STATUS "Using x86 Detours disassembler")
endif()
add_library(scalene SHARED
src/source/libscalene_windows.cpp
vendor/printf/printf.cpp
${DETOURS_SOURCES}
)
target_include_directories(scalene PRIVATE
${SCALENE_INCLUDES}
${detours_SOURCE_DIR}/src
)
target_compile_definitions(scalene PRIVATE
WIN32_LEAN_AND_MEAN
_REENTRANT=1
NDEBUG
HL_USE_XXREALLOC=1
SCALENE_LIBSCALENE_BUILD=1
_CRT_SECURE_NO_WARNINGS=1
)
target_compile_options(scalene PRIVATE
/W3
/O2
/EHsc
)
target_link_libraries(scalene PRIVATE
kernel32
user32
psapi
${Python3_LIBRARIES}
)
# Output to scalene directory (without Release/Debug subdirectory)
set_target_properties(scalene PROPERTIES
OUTPUT_NAME "libscalene"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/scalene"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/scalene"
# Prevent MSBuild from adding configuration subdirectories
LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_SOURCE_DIR}/scalene"
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_SOURCE_DIR}/scalene"
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_SOURCE_DIR}/scalene"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_SOURCE_DIR}/scalene"
)
elseif(APPLE)
# macOS build
message(STATUS "Configuring macOS build")
add_library(scalene SHARED
src/source/libscalene.cpp
vendor/Heap-Layers/wrappers/macwrapper.cpp
vendor/printf/printf.cpp
)
target_include_directories(scalene PRIVATE ${SCALENE_INCLUDES})
target_compile_definitions(scalene PRIVATE
_REENTRANT=1
NDEBUG
HL_USE_XXREALLOC=1
)
target_compile_options(scalene PRIVATE
-Wall
-O3
-fno-builtin-malloc
-fvisibility=hidden
-flto
-ftls-model=initial-exec
-ftemplate-depth=1024
)
# Universal binary support (x86_64 and arm64)
if(CMAKE_OSX_ARCHITECTURES)
# Use specified architectures
else()
# Default to native architecture
set(CMAKE_OSX_ARCHITECTURES "${CMAKE_SYSTEM_PROCESSOR}")
endif()
target_link_libraries(scalene PRIVATE
dl
pthread
)
set_target_properties(scalene PROPERTIES
OUTPUT_NAME "scalene"
PREFIX "lib"
SUFFIX ".dylib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/scalene"
)
else()
# Linux build
message(STATUS "Configuring Linux build")
add_library(scalene SHARED
src/source/libscalene.cpp
vendor/Heap-Layers/wrappers/gnuwrapper.cpp
vendor/printf/printf.cpp
)
target_include_directories(scalene PRIVATE
${SCALENE_INCLUDES}
/usr/include/nptl
)
target_compile_definitions(scalene PRIVATE
_REENTRANT=1
NDEBUG
HL_USE_XXREALLOC=1
)
target_compile_options(scalene PRIVATE
-Wall
-O3
-pipe
-fno-builtin-malloc
-fvisibility=hidden
-fPIC
-Bsymbolic
)
target_link_libraries(scalene PRIVATE
dl
pthread
)
set_target_properties(scalene PROPERTIES
OUTPUT_NAME "scalene"
PREFIX "lib"
SUFFIX ".so"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/scalene"
)
endif()
# Installation
install(TARGETS scalene
LIBRARY DESTINATION scalene
RUNTIME DESTINATION scalene
)
# Custom target for vendored dependencies (now handled automatically via FetchContent)
add_custom_target(vendor-deps
COMMAND ${CMAKE_COMMAND} -E echo "Vendor dependencies are managed automatically via FetchContent."
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)