Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Add installation support and fix cmake files #168

Merged
merged 21 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
913cd40
feat: refactor + add install support for cmake
madebr Mar 11, 2020
4cebaaf
docs: fix enumeration of SENTRY_BACKEND options
madebr Mar 11, 2020
31ebf40
fix: redefinitions of NOMINMAX/WIN32_LEAN_AND_MEAN/_CRT_SECURE_NO_WAR…
madebr Mar 11, 2020
77f721a
fix: calling convention of worker thread should be __stdcall on MSVC
madebr Mar 11, 2020
6f79e7d
fix: address comments from review
madebr Mar 11, 2020
a462434
fix: re-enable hidden visibility
madebr Mar 11, 2020
f3a9e7d
ttt
madebr Mar 11, 2020
c405210
fix: lowercase windows includes to allow mingw on linux
madebr Mar 11, 2020
ed48d41
fix: pass WINVER and NTDDI_VERSION to set minimum required windows ve…
madebr Mar 11, 2020
fb50a85
fix: add CURL_COMPILE_DEFINITIONS + crashpad_handler dependency
madebr Mar 12, 2020
b3b7a26
fix: disable CURL on azure pipeline CI + fix pytest
madebr Mar 12, 2020
85ac23c
fix: move WITH_LIBUNWINDSTACK and WITH_LIBBACKTRACE to top of file
madebr Mar 12, 2020
2939fa5
fix: fix include_directory of libunwindstack
madebr Mar 12, 2020
64639e6
fix: get_target_property values can be NONFOUND which causes errors
madebr Mar 12, 2020
2c0a779
fix: set output directory for android on azure pipelines
madebr Mar 12, 2020
0181afd
Merge remote-tracking branch 'origin/master' into cmake
madebr Mar 12, 2020
ae29729
fix: breakpad backend
madebr Mar 12, 2020
1b303ac
fix: breakpad and crashpad tests were wrong
madebr Mar 12, 2020
52321a4
fix: -E flag is only needed for tests + remove PREPEND
madebr Mar 12, 2020
6fa3d6d
fix: disable CURL_SUPPORT in unit tests
madebr Mar 12, 2020
baa6a22
fix: require c++11 for crashpad
madebr Mar 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ charset = utf-8

[Makefile]
indent_style = tab

[{CMakeLists.txt, *.cmake*}]
indent_style = tab
243 changes: 138 additions & 105 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
cmake_minimum_required (VERSION 3.10)
project (Sentry-Native LANGUAGES C CXX ASM)
project(Sentry-Native LANGUAGES C CXX ASM)

include(GNUInstallDirs)
set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/sentry")

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(LINUX TRUE)
Expand All @@ -11,7 +14,19 @@ else()
set(SENTRY_DEFAULT_BACKEND "inproc")
endif()

OPTION(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" ON)
option(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" ON)

if("${CMAKE_SOURCE_DIR}" STREQUAL "${PROJECT_SOURCE_DIR}")
set(SENTRY_MAIN_PROJECT ON)
endif()

option(SENTRY_ENABLE_INSTALL "Enable sentry installation" "${SENTRY_MAIN_PROJECT}")

function(sentry_install)
if(SENTRY_ENABLE_INSTALL)
install(${ARGN})
endif()
endfunction()

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo")
Expand All @@ -30,7 +45,7 @@ else()
message(FATAL_ERROR "SENTRY_BACKEND must be one of 'none', 'inproc' or 'crashpad'")
endif()

if(BACKEND_CRASHPAD AND NOT APPLE AND NOT WIN32)
if(BACKEND_CRASHPAD AND NOT (APPLE OR WIN32))
message(FATAL_ERROR "The Crashpad backend is currently only supported on macOS and Windows")
endif()
if(BACKEND_INPROC AND WIN32)
Expand All @@ -51,58 +66,87 @@ if(APPLE)
if(DSYMUTIL_PROGRAM)
foreach(lang C CXX)
foreach(var LINK_EXECUTABLE CREATE_SHARED_LIBRARY)
set(CMAKE_${lang}_${var} "${CMAKE_${lang}_${var}}" "${DSYMUTIL_PROGRAM} <TARGET>")
#set(CMAKE_${lang}_${var} "${CMAKE_${lang}_${var}}" "${DSYMUTIL_PROGRAM} <TARGET>")
madebr marked this conversation as resolved.
Show resolved Hide resolved
endforeach()
endforeach()
endif()
endif()


OPTION(WITH_ASAN_OPTION "Build sentry-native with address sanitizer" OFF)
option(WITH_ASAN_OPTION "Build sentry-native with address sanitizer" OFF)
if(WITH_ASAN_OPTION)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer")
link_libraries("-fsanitize=address")
add_compile_options(-g -fsanitize=address -fno-omit-frame-pointer)
link_libraries(-fsanitize=address)
endif()

OPTION(WITH_TSAN_OPTION "Build sentry-native with thread sanitizer" OFF)
option(WITH_TSAN_OPTION "Build sentry-native with thread sanitizer" OFF)
if(WITH_TSAN_OPTION)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fsanitize=thread -fno-omit-frame-pointer")
link_libraries("-fsanitize=thread")
add_compile_options(-g -fsanitize=thread -fno-omit-frame-pointer)
link_libraries(-fsanitize=thread)
endif()

option(SENTRY_CURL_SUPPORT "Buils sentry-native with curl support" ON)

# builtin `list(TRANSFORM)` (https://cmake.org/cmake/help/latest/command/list.html#transform)
# was only added in cmake 3.12, but we target 3.10, so we define a custom
# functions which can prepend a prefix to all list elements.
# copied from https://stackoverflow.com/a/27630120
function(PREPEND var prefix)
set(listVar "")
foreach(f ${ARGN})
list(APPEND listVar "${prefix}/${f}")
endforeach(f)
set(${var} "${listVar}" PARENT_SCOPE)
endfunction()

# ===== sentry library =====

add_library(sentry "${PROJECT_SOURCE_DIR}/vendor/mpack.c")
add_library(sentry::sentry ALIAS sentry)
add_subdirectory(src)
set_target_properties(sentry PROPERTIES PUBLIC_HEADER "include/sentry.h")

# https://gitlab.kitware.com/cmake/cmake/issues/18393
if(BUILD_SHARED_LIBS)
add_definitions(-DSENTRY_BUILD_SHARED)
else()
add_definitions(-DSENTRY_BUILD_STATIC)
if(APPLE)
sentry_install(FILES "$<TARGET_FILE:sentry>.dSYM" DESTINATION "${CMAKE_INSTALL_LIBDIR}")
elseif(MSVC)
sentry_install(FILES "$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:$<TARGET_PDB_FILE:sentry>>"
DESTINATION "${CMAKE_INSTALL_BINDIR}")
endif()
endif()

FIND_PACKAGE(CURL)
if(CURL_FOUND)
madebr marked this conversation as resolved.
Show resolved Hide resolved
set(WITH_CURL TRUE)
add_definitions(-DSENTRY_WITH_LIBCURL_TRANSPORT)
include_directories(${CURL_INCLUDE_DIR})
set(LINK_LIBRARIES ${LINK_LIBRARIES} ${CURL_LIBRARIES})
if(BUILD_SHARED_LIBS)
target_compile_definitions(sentry PRIVATE SENTRY_BUILD_SHARED)
else()
target_compile_definitions(sentry PRIVATE SENTRY_BUILD_STATIC)
endif()
if(WIN32)
add_definitions(-DSENTRY_WITH_WINHTTP_TRANSPORT)
set(LINK_LIBRARIES ${LINK_LIBRARIES} "winhttp.lib")

if(SENTRY_CURL_SUPPORT)
find_package(CURL REQUIRED)
target_compile_definitions(sentry PUBLIC SENTRY_WITH_LIBCURL_TRANSPORT)
target_include_directories(sentry PRIVATE ${CURL_INCLUDE_DIR})
target_link_libraries(sentry PRIVATE $<BUILD_INTERFACE:${CURL_LIBRARIES}>)
endif()

if(BACKEND_CRASHPAD)
set(CMAKE_CXX_STANDARD 14)
else()
set(CMAKE_CXX_STANDARD 11)
if(WIN32)
target_compile_definitions(sentry PUBLIC SENTRY_WITH_WINHTTP_TRANSPORT)
target_link_libraries(sentry PRIVATE winhttp)
endif()

#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration -Werror=incompatible-function-pointer-types -Wall -fvisibility=hidden")
if(NOT WIN32)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fvisibility=hidden")
if(MSVC)
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /safeseh")
endif()
else()
#set_property(TARGET sentry PROPERTY C_VISIBILITY_PRESET hidden)
target_compile_options(sentry PUBLIC $<BUILD_INTERFACE:-Wall>)
endif()

if(BACKEND_CRASHPAD AND WIN32)
if(BACKEND_CRASHPAD AND MSVC)
# Disable duplicate-define warnings, as the crashpad build defines all the
# "slim windows.h" defines, which sentry_boot also defines.
add_definitions(/wd4005)
target_compile_options(sentry PUBLIC $<BUILD_INTERFACE:/wd4005>)
madebr marked this conversation as resolved.
Show resolved Hide resolved
endif()

if(ANDROID)
Expand All @@ -111,107 +155,96 @@ elseif(NOT WIN32)
set(WITH_LIBBACKTRACE TRUE)
endif()

include_directories("include")
include_directories("src")

file(GLOB_RECURSE SENTRY_NATIVE_TEST_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/tests/unit/*.c
target_include_directories(sentry PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)

if(LINUX)
set(LINK_LIBRARIES ${LINK_LIBRARIES} "pthread" "dl")
target_link_libraries(sentry PRIVATE pthread dl)
endif()

if(WITH_LIBUNWINDSTACK)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/external/libunwindstack-ndk/include)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/libunwindstack-ndk/cmake)
set(LINK_LIBRARIES ${LINK_LIBRARIES} "unwindstack")
target_include_directories(sentry PRIVATE
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/external/libunwindstack-ndk/include)>")
add_subdirectory(${PROJECT_SOURCE_DIR}/external/libunwindstack-ndk/cmake)
target_link_libraries(sentry PRIVATE unwindstack)
if(NOT BUILD_SHARED_LIBS)
sentry_install(TARGETS unwindstack EXPORT sentry
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
endif()
endif()

if(WIN32)
set(LINK_LIBRARIES ${LINK_LIBRARIES} "dbghelp.lib" "pathcch.lib")
target_link_libraries(sentry PRIVATE dbghelp pathcch)
endif()

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR})
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR})
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR})
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR})
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR})

# builtin `list(TRANSFORM)` (https://cmake.org/cmake/help/latest/command/list.html#transform)
# was only added in cmake 3.12, but we target 3.10, so we define a custom
# functions which can prepend a prefix to all list elements.
# copied from https://stackoverflow.com/a/27630120
FUNCTION(PREPEND var prefix)
SET(listVar "")
FOREACH(f ${ARGN})
LIST(APPEND listVar "${prefix}/${f}")
ENDFOREACH(f)
SET(${var} "${listVar}" PARENT_SCOPE)
ENDFUNCTION(PREPEND)

if(BACKEND_CRASHPAD)
add_subdirectory(external/crashpad EXCLUDE_FROM_ALL)
include_directories(external/crashpad external/crashpad/third_party/mini_chromium/mini_chromium)
add_definitions(-DSENTRY_WITH_CRASHPAD_BACKEND)
set(LINK_LIBRARIES ${LINK_LIBRARIES} crashpad_client crashpad_util)
install(TARGETS "crashpad_handler")
madebr marked this conversation as resolved.
Show resolved Hide resolved
target_include_directories(sentry PRIVATE
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/external/crashpad>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/external/crashpad/third_party/mini_chromium/mini_chromium>"
)
target_compile_definitions(sentry PRIVATE SENTRY_WITH_CRASHPAD_BACKEND)
target_link_libraries(sentry PRIVATE crashpad_client crashpad_util)
if(NOT BUILD_SHARED_LIBS)
sentry_install(TARGETS crashpad_client crashpad_util crashpad_compat getopt mini_chromium zlib EXPORT sentry
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
endif()
elseif(BACKEND_INPROC)
add_definitions(-DSENTRY_WITH_INPROC_BACKEND)
target_compile_definitions(sentry PRIVATE SENTRY_WITH_INPROC_BACKEND)
endif()

# ===== sentry library =====

add_library("sentry" "${CMAKE_CURRENT_SOURCE_DIR}/vendor/mpack.c")
add_subdirectory(src)

set_target_properties("sentry" PROPERTIES PUBLIC_HEADER "include/sentry.h")
target_link_libraries("sentry" ${LINK_LIBRARIES})
install(TARGETS "sentry"
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
PUBLIC_HEADER DESTINATION include)

# https://gitlab.kitware.com/cmake/cmake/issues/18393
if(BUILD_SHARED_LIBS)
if(APPLE)
install(FILES "$<TARGET_FILE:sentry>.dSYM" DESTINATION lib)
elseif(WIN32)
install(FILES $<TARGET_PDB_FILE:sentry> DESTINATION lib)
endif()
endif()
include(CMakePackageConfigHelpers)
configure_package_config_file(sentry-config.cmake.in sentry-config.cmake
INSTALL_DESTINATION "${CMAKE_INSTALL_CMAKEDIR}")

if(BACKEND_CRASHPAD)
add_dependencies("sentry" "crashpad_handler")
endif()
sentry_install(TARGETS sentry EXPORT sentry
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
sentry_install(EXPORT sentry NAMESPACE sentry:: FILE sentry-targets.cmake
DESTINATION "${CMAKE_INSTALL_CMAKEDIR}")
sentry_install(FILES "${PROJECT_BINARY_DIR}/sentry-config.cmake"
DESTINATION "${CMAKE_INSTALL_CMAKEDIR}")

# ===== tests =====

# compile tests separately and pass an extra preprocessor define so we can
# switch some internal modes for the unittests.

get_target_property(SENTRY_NATIVE_ALL_SOURCES sentry SOURCES)
add_executable("sentry_test_unit" EXCLUDE_FROM_ALL
${SENTRY_NATIVE_ALL_SOURCES}
${SENTRY_NATIVE_TEST_SOURCES}
)
get_target_property(SENTRY_DEFS sentry COMPILE_DEFINITIONS)
target_compile_definitions("sentry_test_unit" PUBLIC SENTRY_UNITTEST ${SENTRY_DEFS})
target_link_libraries("sentry_test_unit" ${LINK_LIBRARIES})
get_target_property(SENTRY_COMPILE_DEFINITIONS sentry COMPILE_DEFINITIONS)
get_target_property(SENTRY_INTERFACE_INCLUDE_DIRECTORIES sentry INTERFACE_INCLUDE_DIRECTORIES)
get_target_property(SENTRY_INCLUDE_DIRECTORIES sentry INCLUDE_DIRECTORIES)
get_target_property(SENTRY_LINK_LIBRARIES sentry LINK_LIBRARIES)

add_executable(sentry_test_unit EXCLUDE_FROM_ALL ${SENTRY_NATIVE_ALL_SOURCES})
madebr marked this conversation as resolved.
Show resolved Hide resolved
add_subdirectory(tests/unit)

target_compile_definitions(sentry_test_unit PRIVATE ${SENTRY_COMPILE_DEFINITIONS})
target_include_directories(sentry_test_unit PRIVATE ${SENTRY_INTERFACE_INCLUDE_DIRECTORIES} ${SENTRY_INCLUDE_DIRECTORIES})
target_link_libraries(sentry_test_unit PRIVATE ${SENTRY_LINK_LIBRARIES})

target_compile_definitions(sentry_test_unit PRIVATE SENTRY_UNITTEST)

enable_testing()
add_test(NAME sentry_test_unit COMMAND sentry_test_unit)

# to fix some issues with tests (dladdr can't find functions otherwise)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
target_link_libraries("sentry_test_unit" "-Wl,--build-id=sha1,-E")
endif()

if(BACKEND_CRASHPAD)
add_dependencies("sentry_test_unit" "crashpad_handler")
target_link_libraries(sentry_test_unit PRIVATE -Wl,--build-id=sha1,-E)
endif()

# ===== example, also used as integration test =====

add_executable("sentry_example" EXCLUDE_FROM_ALL "./examples/example.c")
target_link_libraries("sentry_example" "sentry")
add_executable(sentry_example EXCLUDE_FROM_ALL examples/example.c)
target_link_libraries(sentry_example sentry)
add_test(NAME sentry_example COMMAND sentry_example)
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ using `cmake -D BUILD_SHARED_LIBS=OFF ..`.
`OFF` will build `sentry` as a static library instead.
- `SENTRY_BACKEND` (Default: depending on platform):
Sentry can use different backends depending on platform.
**crashpad**: This uses the out-of-process crashpad handler. It is currently
- **crashpad**: This uses the out-of-process crashpad handler. It is currently
only supported on Windows and macOS, and used as the default there.
**inproc**: A small in-process handler which is supported on all platforms
- **inproc**: A small in-process handler which is supported on all platforms
except Windows, and is used as default on Linux and Android.
**none**: This builds `sentry-native` without a backend, so it does not handle
- **none**: This builds `sentry-native` without a backend, so it does not handle
crashes at all. It is primarily used for tests.

### Build Targets
Expand Down
14 changes: 14 additions & 0 deletions sentry-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(SENTRY_BACKEND @SENTRY_BACKEND@)
set(SENTRY_CURL_SUPPORT @SENTRY_CURL_SUPPORT@)

include("${CMAKE_CURRENT_LIST_DIR}/sentry-targets.cmake")

if(SENTRY_CURL_SUPPORT AND NOT @BUILD_SHARED_LIBS@)
find_package(CURL REQUIRED)
set_property(TARGET sentry::sentry APPEND
PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CURL_INCLUDE_DIRS})
set_property(TARGET sentry::sentry APPEND
PROPERTY INTERFACE_LINK_LIBRARIES ${CURL_LIBRARIES})
endif()

@PACKAGE_INIT@
Loading