Skip to content

Instantly share code, notes, and snippets.

@Decision2016
Forked from mrexodia/IntelPIN.cmake
Created May 9, 2023 15:15
Show Gist options
  • Save Decision2016/e8a37f3bc7ff9e9bcf313e650bcd54ca to your computer and use it in GitHub Desktop.
Save Decision2016/e8a37f3bc7ff9e9bcf313e650bcd54ca to your computer and use it in GitHub Desktop.
IntelPIN.cmake

IntelPIN.cmake

Use Intel Pin easily from CMake. Review the Intel Pin license before using. This snippet is licensed under BSL-1.0. You also need CPM.cmake to use this.

Example CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(icount)

# Enable solution folder support
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Include Intel PIN targets (this should use find_package but this is easier sometimes)
include(cmake/CPM.cmake)
include(cmake/IntelPIN.cmake)

# Instruction counter example
add_pintool(icount
    ${PIN_DIR}/source/tools/InstLibExamples/icount.cpp
)
# Website: https://software.intel.com/content/www/us/en/develop/articles/pin-a-binary-instrumentation-tool-downloads.html
# License: https://software.intel.com/sites/landingpage/pintool/pinlicense.txt
# This snippet: https://gist.github.com/mrexodia/f61fead0108603d04b2ca0ab045e0952
# TODO: lunix support
# Thanks to Francesco for showing me this method
CPMAddPackage(
NAME IntelPIN
VERSION 3.18
URL https://software.intel.com/sites/landingpage/pintool/downloads/pin-3.18-98332-gaebd7b1e6-msvc-windows.zip
DOWNLOAD_ONLY ON
)
if(IntelPIN_ADDED)
# Automatically detect the subfolder in the zip
file(GLOB PIN_DIR LIST_DIRECTORIES true ${IntelPIN_SOURCE_DIR}/pin-*)
# Loosely based on ${PIN_DIR}/source/tools/Config/makefile.win.config
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(PIN_EXE "${PIN_DIR}/intel64/bin/pin${CMAKE_EXECUTABLE_SUFFIX}")
else()
set(PIN_EXE "${PIN_DIR}/ia32/bin/pin${CMAKE_EXECUTABLE_SUFFIX}")
endif()
string(REGEX REPLACE "/" "\\\\" PIN_EXE ${PIN_EXE})
add_library(IntelPIN INTERFACE)
target_include_directories(IntelPIN INTERFACE
${PIN_DIR}/source/include/pin
${PIN_DIR}/source/include/pin/gen
${PIN_DIR}/extras/components/include
${PIN_DIR}/extras/stlport/include
${PIN_DIR}/extras
${PIN_DIR}/extras/libstdc++/include
${PIN_DIR}/extras/crt/include
${PIN_DIR}/extras/crt
${PIN_DIR}/extras/crt/include/kernel/uapi
${PIN_DIR}/extras/crt/include/kernel/uapi/asm-x86
)
target_link_libraries(IntelPIN INTERFACE
pin
xed
pinvm
pincrt
)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
target_link_options(IntelPIN INTERFACE /NODEFAULTLIB /EXPORT:main /BASE:0xC5000000 /ENTRY:Ptrace_DllMainCRTStartup /IGNORE:4210 /IGNORE:4281)
else()
target_link_options(IntelPIN INTERFACE /NODEFAULTLIB /EXPORT:main /BASE:0x55000000 /ENTRY:Ptrace_DllMainCRTStartup@12 /IGNORE:4210 /IGNORE:4281 /SAFESEH:NO)
endif()
target_compile_options(IntelPIN INTERFACE /GR- /GS- /EHs- /EHa- /fp:strict /Oi- /FIinclude/msvc_compat.h /wd5208)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
target_include_directories(IntelPIN INTERFACE
${PIN_DIR}/extras/xed-intel64/include/xed
${PIN_DIR}/extras/crt/include/arch-x86_64
)
target_link_directories(IntelPIN INTERFACE
${PIN_DIR}/intel64/lib
${PIN_DIR}/intel64/lib-ext
${PIN_DIR}/intel64/runtime/pincrt
${PIN_DIR}/extras/xed-intel64/lib
)
target_compile_definitions(IntelPIN INTERFACE
TARGET_IA32E
HOST_IA32E
TARGET_WINDOWS
__PIN__=1
PIN_CRT=1
__LP64__
_WINDOWS_H_PATH_=../um # dirty hack
)
target_link_libraries(IntelPIN INTERFACE
ntdll-64
kernel32
${PIN_DIR}/intel64/runtime/pincrt/crtbeginS.obj
)
else()
target_include_directories(IntelPIN INTERFACE
${PIN_DIR}/extras/xed-ia32/include/xed
${PIN_DIR}/extras/crt/include/arch-x86
)
target_link_directories(IntelPIN INTERFACE
${PIN_DIR}/ia32/lib
${PIN_DIR}/ia32/lib-ext
${PIN_DIR}/ia32/runtime/pincrt
${PIN_DIR}/extras/xed-ia32/lib
)
target_compile_definitions(IntelPIN INTERFACE
TARGET_IA32
HOST_IA32
TARGET_WINDOWS
__PIN__=1
PIN_CRT=1
__i386__
_WINDOWS_H_PATH_=../um # dirty hack
)
target_link_libraries(IntelPIN INTERFACE
ntdll-32
kernel32
${PIN_DIR}/ia32/runtime/pincrt/crtbeginS.obj
)
endif()
# Create a static library InstLib that is used in a lot of example pintools
file(GLOB InstLib_SOURCES
"${PIN_DIR}/source/tools/InstLib/*.cpp"
"${PIN_DIR}/source/tools/InstLib/*.H"
)
add_library(InstLib STATIC EXCLUDE_FROM_ALL ${InstLib_SOURCES})
target_include_directories(InstLib PUBLIC "${PIN_DIR}/source/tools/InstLib")
target_link_libraries(InstLib PUBLIC IntelPIN)
function(add_pintool target)
add_library(${target} SHARED ${ARGN})
target_link_libraries(${target} PRIVATE IntelPIN)
endfunction()
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment