cmake_minimum_required(VERSION 3.7)
if ("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
set(DEBUG ON)
endif ("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
if (RISCV64)
return()
endif (RISCV64)
if ("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
if (DEBUG)
set(CMAKE_CONFIGURATION_TYPES "Debug" CACHE STRING "" FORCE)
else ()
set(CMAKE_CONFIGURATION_TYPES "RelWithDebInfo" CACHE STRING "" FORCE)
endif ()
string(TOUPPER "${CMAKE_CONFIGURATION_TYPES}" upper)
set(location_suffix "_${upper}")
else ("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
set(location_suffix "")
endif ("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
project(DynamoRIO_samples)
include(CheckIncludeFile)
if ("${CMAKE_VERSION}" VERSION_EQUAL "3.9" OR
"${CMAKE_VERSION}" VERSION_GREATER "3.9")
cmake_policy(SET CMP0068 NEW)
endif ()
set(output_dir "${PROJECT_BINARY_DIR}/bin")
set(output_dir "${PROJECT_BINARY_DIR}/../bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${output_dir}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${output_dir}")
if ("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
foreach (config ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER "${config}" config_upper)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${config_upper}
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${config_upper}
"${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${config_upper}
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
endforeach ()
endif ()
if (BUILD_TESTS)
set(SHOW_RESULTS_DEFAULT OFF)
else (BUILD_TESTS)
set(SHOW_RESULTS_DEFAULT ON)
endif (BUILD_TESTS)
option(SHOW_RESULTS
"Display client results in pop-up (Windows) or console message (Linux)"
${SHOW_RESULTS_DEFAULT})
if (SHOW_RESULTS)
add_definitions(-DSHOW_RESULTS)
endif (SHOW_RESULTS)
option(SHOW_SYMBOLS "Use symbol lookup in clients that support it" ON)
if (SHOW_SYMBOLS)
add_definitions(-DSHOW_SYMBOLS)
endif (SHOW_SYMBOLS)
if (NOT DEFINED GENERATE_PDBS)
set(GENERATE_PDBS ON)
endif (NOT DEFINED GENERATE_PDBS)
option(OPTIMIZE_SAMPLES
"Build samples with optimizations to increase the chances of clean call inlining (overrides debug flags)"
ON)
if (WIN32)
set(OPT_CFLAGS "/O2")
else (WIN32)
set(OPT_CFLAGS "-O2")
endif (WIN32)
if (DEBUG)
set(OPT_CFLAGS "${OPT_CFLAGS} -DDEBUG")
endif (DEBUG)
if (NOT DEFINED DynamoRIO_DIR)
set(DynamoRIO_DIR "${PROJECT_SOURCE_DIR}/../cmake" CACHE PATH
"DynamoRIO installation's cmake directory")
endif (NOT DEFINED DynamoRIO_DIR)
find_package(DynamoRIO ${VERSION_NUMBER_MAJOR}.${VERSION_NUMBER_MINOR})
if (NOT DynamoRIO_FOUND)
message(FATAL_ERROR "DynamoRIO package required to build")
endif(NOT DynamoRIO_FOUND)
if (WIN32)
set(CL_CFLAGS "/GS- /wd4100 /wd4127 /wd4054")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CL_CFLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CL_CFLAGS}")
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif (WIN32)
if (OPTIMIZE_SAMPLES)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPT_CFLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPT_CFLAGS}")
endif ()
function (add_sample_client name source_file_list extension_list)
add_library(${name} SHARED ${source_file_list})
configure_DynamoRIO_client(${name})
foreach (ext ${extension_list})
use_DynamoRIO_extension(${name} ${ext})
endforeach (ext)
if (NOT DynamoRIO_INTERNAL OR NOT "${CMAKE_GENERATOR}" MATCHES "Ninja")
DynamoRIO_get_full_path(path ${name} "${location_suffix}")
add_custom_command(TARGET ${name}
POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E echo "Usage: pass to drconfig or drrun: -c ${path}"
VERBATIM)
endif ()
get_property(sample_list GLOBAL PROPERTY DynamoRIO_sample_list)
set_property(GLOBAL PROPERTY DynamoRIO_sample_list
"${sample_list};${name}")
set(srcs ${srcs} ${source_file_list} PARENT_SCOPE)
set(tgts ${tgts} ${name} PARENT_SCOPE)
if (BUILD_TESTS)
add_library(${name}_test SHARED ${source_file_list})
configure_DynamoRIO_client(${name}_test)
foreach (ext ${extension_list})
use_DynamoRIO_extension(${name}_test ${ext})
endforeach (ext)
target_compile_definitions(${name}_test PRIVATE -DSHOW_RESULTS)
set(tgts ${tgts} ${name}_test PARENT_SCOPE)
endif ()
endfunction (add_sample_client)
function (add_sample_standalone name source_file_list)
add_executable(${name} ${source_file_list})
configure_DynamoRIO_standalone(${name})
if (NOT DynamoRIO_INTERNAL OR NOT "${CMAKE_GENERATOR}" MATCHES "Ninja")
if (UNIX)
set(FIND_MSG "(set LD_LIBRARY_PATH)")
else (UNIX)
set(FIND_MSG "(set PATH or copy to same directory)")
endif (UNIX)
add_custom_command(TARGET ${name}
POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E echo "Make sure the loader finds the dynamorio library ${FIND_MSG}"
VERBATIM)
endif ()
set(srcs ${srcs} ${source_file_list} PARENT_SCOPE)
set(tgts ${tgts} ${name} PARENT_SCOPE)
endfunction (add_sample_standalone)
configure_DynamoRIO_global(OFF ON)
add_sample_client(bbbuf "bbbuf.c" "drmgr;drreg;drx")
add_sample_client(bbcount "bbcount.c" "drmgr;drreg;drx")
add_sample_client(bbsize "bbsize.c" "drmgr;drx")
if (LINUX)
CHECK_INCLUDE_FILE("libunwind.h" HAVE_LIBUNWIND_H)
if (HAVE_LIBUNWIND_H)
add_sample_client(callstack "callstack.cpp" "drmgr;drwrap;drcallstack;drsyms;droption")
endif ()
endif ()
add_sample_client(div "div.c" "drmgr")
add_sample_client(empty "empty.c" "")
add_sample_client(memtrace_simple "memtrace_simple.c;utils.c" "drmgr;drreg;drutil;drx")
add_sample_client(memval_simple "memval_simple.c;utils.c" "drmgr;drreg;drutil;drx")
add_sample_client(instrace_simple "instrace_simple.c;utils.c" "drmgr;drreg;drx")
add_sample_client(opcode_count "opcode_count.cpp" "drmgr;drreg;drx;droption")
if (X86)
add_sample_client(cbr "cbr.c" "drmgr")
add_sample_client(countcalls "countcalls.c" "drmgr;drreg")
add_sample_client(inc2add "inc2add.c" "drmgr;drreg")
add_sample_client(memtrace_x86_binary "memtrace_x86.c;utils.c"
"drcontainers;drmgr;drreg;drutil;drx")
add_sample_client(memtrace_x86_text "memtrace_x86.c;utils.c"
"drcontainers;drmgr;drreg;drutil;drx")
_DR_append_property_list(TARGET memtrace_x86_text COMPILE_DEFINITIONS "OUTPUT_TEXT")
add_sample_client(instrace_x86_binary "instrace_x86.c;utils.c"
"drcontainers;drmgr;drreg;drx")
add_sample_client(instrace_x86_text "instrace_x86.c;utils.c"
"drcontainers;drmgr;drreg;drx")
_DR_append_property_list(TARGET instrace_x86_text COMPILE_DEFINITIONS "OUTPUT_TEXT")
add_sample_client(prefetch "prefetch.c" "drmgr")
if (NOT WIN32)
add_sample_client(ssljack "ssljack.c" "drmgr;drwrap")
endif (NOT WIN32)
add_sample_client(modxfer "modxfer.c;utils.c" "drmgr;drreg;drx")
add_sample_client(modxfer_app2lib "modxfer_app2lib.c" "drmgr")
add_sample_client(instrcalls "instrcalls.c;utils.c" "drmgr;drsyms;drx")
add_sample_client(cbrtrace "cbrtrace.c;utils.c" "drmgr;drx")
add_sample_client(hot_bbcount "hot_bbcount.c" "drmgr;drreg;drbbdup;drx")
endif (X86)
add_sample_client(wrap "wrap.c" "drmgr;drwrap")
add_sample_client(signal "signal.c" "drmgr")
add_sample_client(syscall "syscall.c" "drmgr")
if (NOT WIN32)
add_sample_client(statecmp "statecmp.c" "drmgr;drstatecmp")
endif (NOT WIN32)
add_sample_client(inline "inline.c" "drmgr;drcontainers")
add_sample_client(inscount "inscount.cpp" "drmgr;droption")
add_sample_client(opcodes "opcodes.c" "drmgr;drreg;drx")
add_sample_client(stl_test "stl_test.cpp" "")
set(srcs ${srcs} "utils.h")
if (WIN32)
add_sample_client(stats "stats.c;utils.c" "drmgr;drreg;drx")
_DR_append_property_string(TARGET wrap LINK_FLAGS "/safeseh:no")
endif ()
add_sample_standalone(tracedump "tracedump.c")
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt" string)
string(REGEX REPLACE "[^\n]*NON-PUBLIC[^\n]*\n" "" string "${string}")
string(REGEX REPLACE "# Strip out everything.*$" "" string "${string}")
string(REGEX REPLACE "\\$\\{(VERSION_NUMBER[^\\}]*)\\}" "@\\1@" string "${string}")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeLists-public.txt.in" "${string}")
set(public_clist "${CMAKE_CURRENT_BINARY_DIR}/CMakeLists.txt")
set_property(GLOBAL PROPERTY DynamoRIO_sample_proj_dir "${CMAKE_CURRENT_BINARY_DIR}")
configure_file("${CMAKE_CURRENT_BINARY_DIR}/CMakeLists-public.txt.in"
"${public_clist}" @ONLY)
foreach (src ${srcs})
configure_file(${src} ${CMAKE_CURRENT_BINARY_DIR}/${src} COPYONLY)
endforeach ()
foreach (tgt ${tgts})
add_dependencies(${tgt} api_headers)
if (WIN32 AND GENERATE_PDBS)
get_target_property(cur_flags ${tgt} LINK_FLAGS)
if (NOT cur_flags)
set(cur_flags "")
endif (NOT cur_flags)
set_target_properties(${tgt} PROPERTIES LINK_FLAGS "${cur_flags} /debug")
endif (WIN32 AND GENERATE_PDBS)
endforeach (tgt)
if (NOT DEFINED INSTALL_PREFIX)
set(INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/../../exports" CACHE PATH
"where to install")
set(CMAKE_INSTALL_PREFIX "${INSTALL_PREFIX}" CACHE INTERNAL
"where to install" FORCE)
endif (NOT DEFINED INSTALL_PREFIX)
set(INSTALL_SAMPLES samples)
if (NOT DEFINED X64)
message(FATAL_ERROR "we assume X64 is inherited from parent scope")
endif (NOT DEFINED X64)
if (X64)
set(INSTALL_SAMPLES_BIN ${INSTALL_SAMPLES}/bin64)
else (X64)
set(INSTALL_SAMPLES_BIN ${INSTALL_SAMPLES}/bin32)
endif (X64)
DR_install(TARGETS ${tgts} DESTINATION ${INSTALL_SAMPLES_BIN})
DR_install(FILES ${srcs} DESTINATION ${INSTALL_SAMPLES})
DR_install(FILES "${public_clist}"
DESTINATION "${INSTALL_SAMPLES}" RENAME CMakeLists.txt)
if (WIN32)
DynamoRIO_get_full_path(DR_TARGET_LOCATION dynamorio "${location_suffix}")
DR_install(FILES "${DR_TARGET_LOCATION}"
DESTINATION "${INSTALL_SAMPLES_BIN}")
endif (WIN32)
DR_install(DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/
DESTINATION ${INSTALL_SAMPLES_BIN}
FILE_PERMISSIONS ${owner_access} OWNER_EXECUTE GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE
FILES_MATCHING
PATTERN "*.debug"
PATTERN "*.pdb"
REGEX ".*.dSYM/.*DWARF/.*"
)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/README "This directory contains sample clients that show how to use the DR API. If this is an official release package that includes DRMF, for samples that use the DRMF API, see ../drmemory/drmf/samples/.\n")
DR_install(FILES ${CMAKE_CURRENT_BINARY_DIR}/README DESTINATION ${INSTALL_SAMPLES})