Add most of CMake build

This commit is contained in:
Isaac Hier 2017-11-01 09:52:02 -04:00 committed by Aliaksey Kandratsenka
parent 9e4f72fd1f
commit fa9bedc82c
2 changed files with 237 additions and 23 deletions

View File

@ -1,5 +1,6 @@
project(gperftools VERSION 2.6.1 LANGUAGES CXX)
cmake_minimum_required(VERSION 3.0)
project(gperftools VERSION 2.6.1 LANGUAGES C CXX)
set(tcmalloc_so_version 8.5.4)
set(profiler_so_version 4.14.4)
@ -27,13 +28,21 @@ elseif(host MATCHES ".*-darwin.*")
set(default_build_heap_checker OFF)
endif()
include(CheckCSourceCompiles)
include(CheckFunctionExists)
include(CheckIncludeFile)
include(CheckTypeSize)
include(CMakeDependentOption)
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
include(CheckCXXSymbolExists)
include(CheckFunctionExists)
include(CheckIncludeFileCXX)
include(CheckLibraryExists)
include(CheckTypeSize)
include(CheckVariableExists)
check_c_source_compiles("int main() { return __s390__; }" s390)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(PCFromUContext)
check_cxx_source_compiles("int main() { return __s390__; }" s390)
if(s390)
set(default_enable_libunwind OFF)
set(default_enable_backtrace ON)
@ -42,7 +51,7 @@ else()
set(default_enable_backtrace OFF)
endif()
check_c_source_compiles("int main() { return __PPC64__; }" PPC64)
check_cxx_source_compiles("int main() { return __PPC64__; }" PPC64)
if(PPC64)
set(default_enable_libunwind OFF)
set(default_tcmalloc_pagesize 64)
@ -78,11 +87,11 @@ option(gperftools_enable_libunwind
set(gperftools_tcmalloc_pagesize ${default_tcmalloc_pagesize}
CACHE STRING "Set the tcmalloc internal page size")
set_property(CACHE gperftools_tcmalloc_pagesize PROPERTY STRINGS "8" "32" "64")
if(gperftools_tcmalloc_alignment NOT STREQUAL "8" AND
gperftools_tcmalloc_alignment NOT STREQUAL "32" AND
gperftools_tcmalloc_alignment NOT STREQUAL "64")
if(NOT gperftools_tcmalloc_pagesize STREQUAL "8" AND
NOT gperftools_tcmalloc_pagesize STREQUAL "32" AND
NOT gperftools_tcmalloc_pagesize STREQUAL "64")
message(WARNING
"Invalid gperftools_tcmalloc_pagesize (${gperftools_tcmalloc_pagesize}),"
"Invalid gperftools_tcmalloc_pagesize (${gperftools_tcmalloc_pagesize}), "
"setting to default value (${default_tcmalloc_pagesize})")
set(gperftools_tcmalloc_pagesize ${default_tcmalloc_pagesize})
endif()
@ -90,8 +99,8 @@ endif()
set(gperftools_tcmalloc_alignment ${default_tcmalloc_alignment}
CACHE STRING "Set the tcmalloc allocation alignment")
set_property(CACHE gperftools_tcmalloc_alignment PROPERTY STRINGS "8" "16")
if(gperftools_tcmalloc_alignment NOT STREQUAL "8" AND
gperftools_tcmalloc_alignment NOT STREQUAL "16")
if(NOT gperftools_tcmalloc_alignment STREQUAL "8" AND
NOT gperftools_tcmalloc_alignment STREQUAL "16")
message(WARNING
"Invalid gperftools_tcmalloc_alignment (${gperftools_tcmalloc_alignment}), "
"setting to default value (${default_tcmalloc_alignment})")
@ -103,15 +112,157 @@ endif()
# TODO: Find `objcopy` with `--weaken` support
check_type_size("__int64" __INT64)
check_type_size("__int64" __INT64 LANGUAGE CXX)
set(CMAKE_EXTRA_INCLUDE_FILES "malloc.h")
check_type_size("struct mallinfo" struct_mallinfo)
check_type_size("struct mallinfo" STRUCT_MALLINFO LANGUAGE CXX)
set(CMAKE_EXTRA_INCLUDE_FILES "elf.h")
check_type_size("Elf32_Versym")
check_function_exists("sbrk" HAS_SBRK)
check_function_exists("geteuid" HAS_GETEUID)
check_function_exists("fork" HAS_FORK)
check_include_file("features.h" HAS_FEATURES)
check_include_file("malloc.h" HAS_MALLOC)
check_include_file("glob.h" HAS_GLOB)
check_include_file("execinfo.h" HAS_EXECINFO)
check_type_size("Elf32_Versym" ELF32_VERSYM LANGUAGE CXX)
set(CMAKE_EXTRA_INCLUDE_FILES)
check_function_exists("sbrk" HAVE_SBRK)
check_function_exists("geteuid" HAVE_GETEUID)
check_function_exists("fork" HAVE_FORK)
check_include_file_cxx("features.h" HAVE_FEATURES_H)
check_include_file_cxx("malloc.h" HAVE_MALLOC_H)
check_include_file_cxx("glob.h" HAVE_GLOB_H)
check_include_file_cxx("execinfo.h" HAVE_EXECINFO_H)
check_include_file_cxx("unwind.h" HAVE_UNWIND_H)
check_include_file_cxx("sched.h" HAVE_SCHED_H)
check_include_file_cxx("conflict-signal.h" HAVE_CONFLICT_SIGNAL_H)
check_include_file_cxx("sys/prctl.h" HAVE_SYS_PRCTL_H)
check_include_file_cxx("linux/ptrace.h" HAVE_LINUX_PTRACE_H)
check_include_file_cxx("sys/syscall.h" HAVE_SYS_SYSCALL_H)
check_include_file_cxx("sys/socket.h" HAVE_SYS_SOCKET_H)
check_include_file_cxx("sys/wait.h" HAVE_SYS_WAIT_H)
check_include_file_cxx("poll.h" HAVE_POLL_H)
check_include_file_cxx("fcntl.h" HAVE_FCNTL_H)
check_include_file_cxx("grp.h" HAVE_GRP_H)
check_include_file_cxx("pwd.h" HAVE_PWD_H)
check_include_file_cxx("sys/resource.h" HAVE_SYS_RESOURCE_H)
check_include_file_cxx("valgrind.h" HAVE_VALGRIND_H)
check_include_file_cxx("sys/cdefs.h" HAVE_SYS_CDEFS_H)
check_include_file_cxx("features.h" HAVE_FEATURES_H)
set(CMAKE_REQUIRED_DEFINITIONS -D_XOPEN_SOURCE=600)
check_cxx_symbol_exists("cfree" "stdlib.h;malloc.h" HAVE_CFREE)
set(CMAKE_REQUIRED_DEFINITIONS)
check_symbol_exists("posix_memalign" "stdlib.h;malloc.h" HAVE_POSIX_MEMALIGN)
check_symbol_exists("memalign" "stdlib.h;malloc.h" HAVE_MEMALIGN)
check_symbol_exists("valloc" "stdlib.h;malloc.h" HAVE_VALLOC)
check_symbol_exists("pvalloc" "stdlib.h;malloc.h" HAVE_PVALLOC)
check_symbol_exists("mmap" "stdlib.h;unistd.h;sys/mman.h;sys/param.h" HAVE_MMAP)
check_cxx_source_compiles(
"#include <stdint.h>\nint main() { int32_t v1 = 0; intptr_t v2 = 0; return (&v1 - &v2); }"
INT32_EQUALS_INTPTR)
pc_from_ucontext(pc_field)
set(unwind_libs)
if(gperftools_enable_libunwind)
check_include_file_cxx("libunwind.h" HAVE_LIBUNWIND_H)
find_library(libunwind_location NAMES unwind)
if(libunwind_location)
check_library_exists(unwind backtrace ${libunwind_location} HAVE_LIBUNWIND)
endif()
if(HAVE_LIBUNWIND)
list(APPEND unwind_libs unwind)
set(will_use_libunwind ON)
endif()
endif()
cmake_dependent_option(
gperftools_enable_frame_pointers
"On x86_64 systems, compile with -fno-omit-frame-pointer (see INSTALL)"
OFF
"CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL x86_64"
OFF)
check_cxx_compiler_flag("-Wno-unused-result" HAVE_W_NO_UNUSED_RESULT)
option(gperftools_dynamic_sized_delete_support
"Try to build run-time switch for sized delete operator"
OFF)
if(gperftools_dynamic_sized_delete_support)
set(ENABLE_DYNAMIC_SIZED_DELETE 1)
endif()
option(gperftools_sized_delete "Build sized delete operator" OFF)
if(gperftools_sized_delete)
set(ENABLE_SIZED_DELETE 1)
endif()
check_cxx_compiler_flag("-fsized-deallocation" HAVE_SIZED_DEALLOCATION)
check_cxx_source_compiles("include <unwind.h>\nint main() { &Unwind_Backtrace; return 0; }"
HAVE_UNWIND_BACKTRACE)
if(enable_backtrace)
set(default_emergency_malloc ON)
else()
set(default_emergency_malloc OFF)
endif()
if(will_use_libunwind AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm")
set(default_emergency_malloc ON)
endif()
option(gperftools_emergency_malloc
"Build emergency malloc"
${default_emergency_malloc})
check_cxx_source_compiles(
"int main() { void* sp = __buildin_stack_pointer(); return 0; }"
HAVE_BUILTIN_STACK_POINTER)
check_cxx_source_compiles(
"int main() { return __builtin_expect(main != 0, 1); }"
HAVE_BUILTIN_EXPECT)
check_cxx_source_compiles(
"#include <unistd.h>\nint main() { char** env = __environ; return 0; }"
HAVE___ENVIRON)
if(CMAKE_CXX_COMPILER STREQUAL "GNU" AND
CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.1.2")
message(WARNING "gcc has this bug: http://gcc.gnu.org/ml/gcc-bugs/2006-09/msg02275.html")
elseif(APPLE)
message(WARNING "OSX __thread support is known to call malloc which makes "
"it unsafe to use from malloc replacement")
else()
check_cxx_symbol_exists("__MINGW32__" "" mingw32)
if(mingw32)
message(WARNING "mingw doesn't really support tls")
else()
check_cxx_source_compiles("static __thread int p = 0;" HAVE_TLS)
endif()
endif()
if(need_nanosleep)
check_cxx_source_compiles(
"#include <time.h>\nint main() { static struct timespec ts; nanosleep(&ts, NULL); return 0; }"
nanosleep_ok)
if(NOT nanosleep_ok)
set(CMAKE_REQUIRED_LIBRARIES rt)
check_cxx_source_compiles(
"int main() { static struct timespec ts; nanosleep(&ts, NULL); return 0; }"
nanosleep_ok)
if(nanosleep_ok)
list(APPEND LIBS rt)
else()
message(FATAL_ERROR "cannot find the nanosleep function")
endif()
set(CMAKE_REQUIRED_LIBRARIES)
endif()
endif()
if(EXISTS /usr/sfw/lib/libstdc++.la)
file(READ /usr/sfw/lib/libstdc++.la _ch LIMIT 1)
if(string(LENGTH _ch) EQUAL 0)
# TODO: list(APPEND LIBS "src/solaris")
endif()
endif()
check_variable_exists("program_invocation_name" HAVE_PROGRAM_INVOCATION_NAME)
check_cxx_source_compiles(
"#include <string>\n#include <vector>\nint main() { pthread_t th; pthread_join(th, 0); return 0; }"
HAVE_PTHREAD_DESPITE_ASKING_FOR)

View File

@ -0,0 +1,63 @@
include(CheckCSourceCompiles)
include(CheckIncludeFile)
macro(pc_from_ucontext variable)
set(HAVE_${variable} OFF)
check_include_file_cxx("ucontext.h" HAVE_UCONTEXT_H)
if(EXISTS /etc/redhat-release)
set(redhat7_release_pattern "Red Hat Linux release 7")
file(STRINGS /etc/redhat-release redhat_release_match
LIMIT_COUNT 1
REGEX ${redhat7_release_pattern})
if(redhat_release_match MATCHES ${redhat7_release_pattern})
set(HAVE_SYS_UCONTEXT_H OFF)
else()
check_include_file_cxx("sys/ucontext.h" HAVE_SYS_UCONTEXT_H)
endif()
else()
check_include_file_cxx("sys/ucontext.h" HAVE_SYS_UCONTEXT_H)
endif()
check_include_file_cxx("cygwin/signal.h" HAVE_CYGWIN_SIGNAL_H)
set(pc_fields
"uc_mcontext.gregs[REG_PC]" # Solaris x86 (32 + 64 bit)
"uc_mcontext.gregs[REG_EIP]" # Linux (i386)
"uc_mcontext.gregs[REG_RIP]" # Linux (x86_64)
"uc_mcontext.sc_ip" # Linux (ia64)
"uc_mcontext.pc" # Linux (mips)
"uc_mcontext.uc_regs->gregs[PT_NIP]" # Linux (ppc)
"uc_mcontext.psw.addr" # Linux (s390)
"uc_mcontext.gregs[R15]" # Linux (arm old [untested])
"uc_mcontext.arm_pc" # Linux (arm arch 5)
"uc_mcontext.gp_regs[PT_NIP]" # Suse SLES 11 (ppc64)
"uc_mcontext.mc_eip" # FreeBSD (i386)
"uc_mcontext.mc_rip" # FreeBSD (x86_64 [untested])
"uc_mcontext.__gregs[_REG_EIP]" # NetBSD (i386)
"uc_mcontext.__gregs[_REG_RIP]" # NetBSD (x86_64)
"uc_mcontext->ss.eip" # OS X (i386, <=10.4)
"uc_mcontext->__ss.__eip" # OS X (i386, >=10.5)
"uc_mcontext->ss.rip" # OS X (x86_64)
"uc_mcontext->__ss.__rip" # OS X (>=10.5 [untested])
"uc_mcontext->ss.srr0" # OS X (ppc, ppc64 [untested])
"uc_mcontext->__ss.__srr0") # OS X (>=10.5 [untested])
set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE=1)
set(CMAKE_REQUIRED_QUIET ON)
if(HAVE_CYGWIN_SIGNAL_H)
set(_inc "cygwin/signal.h")
elseif(HAVE_SYS_UCONTEXT_H)
set(_inc "sys/ucontext.h")
elseif(HAVE_UCONTEXT_H)
set(_inc "ucontext.h")
endif()
foreach(pc_field IN LISTS pc_fields)
check_cxx_source_compiles(
"#include <${_inc}>\nint main() { ucontext_t u; return u.${pc_field} == 0; }"
pc_field_found)
if(DEFINED pc_field_found)
set(HAVE_${variable} ON)
set(${variable} ${pc_field})
break()
endif()
endforeach()
endmacro()