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