-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCMakeLists.txt
100 lines (76 loc) · 3.33 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
cmake_minimum_required( VERSION 3.9 )
PROJECT( gdb_python_api )
set( CMAKE_CXX_STANDARD 17 )
# Create a compilation database (compile_commands.json) for the use of Clang tools
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
# always use -fno-omit-frame-pointer because stack printing won't work otherwise ATM
add_compile_options( "-fno-omit-frame-pointer" )
set( CMAKE_CXX_FLAGS "-Wall -Wextra -Werror" )
# for relaxing debug, make it:
set( CMAKE_CXX_FLAGS_DEBUG "-g -Og" )
if( NOT CMAKE_BUILD_TYPE )
# for the purposes of gdb integration this is the right choice
set( CMAKE_BUILD_TYPE Debug )
endif()
# if using Clang, also use libc++
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_definitions(-stdlib=libc++)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
endif()
#
# Code we will be operating on
#
# stack dump demonstration code
add_executable( ll examples/lotsa_locals.cpp )
# navigation in templated code demo
add_executable( swl examples/stl_with_lambda.cpp )
target_compile_options( swl PRIVATE -std=c++17 )
# code with a leak
add_executable( leak examples/leaker2.cpp )
# using std::sort
add_executable( srs examples/sort_random_sequence.cpp )
#
# libClang stuff
#
# libClang via the C API
add_executable( le libclang_test.cpp )
find_package( Boost 1.65 REQUIRED )
# libClang via Python
# In Ubuntu python-clang package is only in Python 2.7 search paths, while gdb uses Python 3
# You can get it in python3 as well by:
# sudo apt-get install python3-pip
# sudo pip3 install clang
if( LLVM_ROOT )
# using a custom build of LLVM. We need to do two things:
# 1) pick up the appropriate libclang.so
set( CLANG_LIB_DIR ${LLVM_ROOT}/build/lib )
target_link_libraries( le PUBLIC ${CLANG_LIB_DIR}/libclang.so )
target_include_directories( le PUBLIC ${LLVM_ROOT}/tools/clang/include )
# 2) ensure analysis is done with the path to stddef.h (compiler-supplied) added
target_compile_definitions( le PUBLIC "-DLLVM_ROOT=${LLVM_ROOT}" )
else()
# Look for an installed version
# Works for Ubuntu, others...?
find_file( INDEX_PATH "clang-c/Index.h"
HINTS /usr/lib/llvm-5.0/include /usr/lib/llvm-4.0/include /usr/lib/llvm-3.8/include )
if( NOT INDEX_PATH )
message( FATAL_ERROR "cannot find libClang headers" )
endif()
get_filename_component( CLANG_INCLUDE_DIR ${INDEX_PATH} DIRECTORY ) # remove "Index.h"
get_filename_component( CLANG_INCLUDE_DIR ${CLANG_INCLUDE_DIR} DIRECTORY ) # remove "clang-c"
target_include_directories( le PUBLIC ${CLANG_INCLUDE_DIR} )
get_filename_component( LLVM_BASE_DIR ${CLANG_INCLUDE_DIR} DIRECTORY ) # remove "include"
set( CLANG_LIB_DIR ${LLVM_BASE_DIR}/lib )
target_link_libraries( le PUBLIC ${CLANG_LIB_DIR}/libclang.so )
endif()
target_link_libraries( le PRIVATE Boost::boost -lstdc++fs )
# some "tests" that just exercise the libClang users enough to show they are working
# (and to provide usage examples)
enable_testing()
add_test( NAME libclang_via_python
COMMAND ../libclang_test.py ${CMAKE_CURRENT_SOURCE_DIR}/stl_with_lambda.cpp ./compile_commands.json )
add_test( NAME libclang_via_c
COMMAND ./le ${CMAKE_CURRENT_SOURCE_DIR}/stl_with_lambda.cpp ./compile_commands.json )
# Python scripts aren't linked against dynamic libraries so we specify them this way:
set_tests_properties( libclang_via_python PROPERTIES
ENVIRONMENT "LD_LIBRARY_PATH=${CLANG_LIB_DIR}" )