blob: 0677dcda9115ee1dd11762c034267085eadd3a48 [file] [log] [blame]
David Benjaminf07ba172016-04-28 14:38:20 -04001# Copyright (c) 2014, Pavel Rojtberg
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#
7# 1. Redistributions of source code must retain the above copyright notice,
8# this list of conditions and the following disclaimer.
9#
10# 2. Redistributions in binary form must reproduce the above copyright notice,
11# this list of conditions and the following disclaimer in the documentation
12# and/or other materials provided with the distribution.
13#
14# 3. Neither the name of the copyright holder nor the names of its
15# contributors may be used to endorse or promote products derived from this
16# software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29
30# ------------------------------------------------------------------------------
31# Usage:
32# 1. place AndroidNdkGdb.cmake somewhere inside ${CMAKE_MODULE_PATH}
33# 2. inside your project add
34#
35# include(AndroidNdkGdb)
36# android_ndk_gdb_enable()
37# # for each target
38# add_library(MyLibrary ...)
39# android_ndk_gdb_debuggable(MyLibrary)
40
41
42# add gdbserver and general gdb configuration to project
43# also create a mininal NDK skeleton so ndk-gdb finds the paths
44#
45# the optional parameter defines the path to the android project.
46# uses PROJECT_SOURCE_DIR by default.
47macro(android_ndk_gdb_enable)
48 if(ANDROID)
49 # create custom target that depends on the real target so it gets executed afterwards
50 add_custom_target(NDK_GDB ALL)
51
52 if(${ARGC})
53 set(ANDROID_PROJECT_DIR ${ARGV0})
54 else()
55 set(ANDROID_PROJECT_DIR ${PROJECT_SOURCE_DIR})
56 endif()
57
58 set(NDK_GDB_SOLIB_PATH ${ANDROID_PROJECT_DIR}/obj/local/${ANDROID_NDK_ABI_NAME}/)
59 file(MAKE_DIRECTORY ${NDK_GDB_SOLIB_PATH})
60
61 # 1. generate essential Android Makefiles
62 file(MAKE_DIRECTORY ${ANDROID_PROJECT_DIR}/jni)
63 if(NOT EXISTS ${ANDROID_PROJECT_DIR}/jni/Android.mk)
64 file(WRITE ${ANDROID_PROJECT_DIR}/jni/Android.mk "APP_ABI := ${ANDROID_NDK_ABI_NAME}\n")
65 endif()
66 if(NOT EXISTS ${ANDROID_PROJECT_DIR}/jni/Application.mk)
67 file(WRITE ${ANDROID_PROJECT_DIR}/jni/Application.mk "APP_ABI := ${ANDROID_NDK_ABI_NAME}\n")
68 endif()
69
70 # 2. generate gdb.setup
71 get_directory_property(PROJECT_INCLUDES DIRECTORY ${PROJECT_SOURCE_DIR} INCLUDE_DIRECTORIES)
72 string(REGEX REPLACE ";" " " PROJECT_INCLUDES "${PROJECT_INCLUDES}")
73 file(WRITE ${LIBRARY_OUTPUT_PATH}/gdb.setup "set solib-search-path ${NDK_GDB_SOLIB_PATH}\n")
74 file(APPEND ${LIBRARY_OUTPUT_PATH}/gdb.setup "directory ${PROJECT_INCLUDES}\n")
75
76 # 3. copy gdbserver executable
77 file(COPY ${ANDROID_NDK}/prebuilt/android-${ANDROID_ARCH_NAME}/gdbserver/gdbserver DESTINATION ${LIBRARY_OUTPUT_PATH})
78 endif()
79endmacro()
80
81# register a target for remote debugging
82# copies the debug version to NDK_GDB_SOLIB_PATH then strips symbols of original
83macro(android_ndk_gdb_debuggable TARGET_NAME)
84 if(ANDROID)
85 get_property(TARGET_LOCATION TARGET ${TARGET_NAME} PROPERTY LOCATION)
86
87 # create custom target that depends on the real target so it gets executed afterwards
88 add_dependencies(NDK_GDB ${TARGET_NAME})
89
90 # 4. copy lib to obj
91 add_custom_command(TARGET NDK_GDB POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TARGET_LOCATION} ${NDK_GDB_SOLIB_PATH})
92
93 # 5. strip symbols
94 add_custom_command(TARGET NDK_GDB POST_BUILD COMMAND ${CMAKE_STRIP} ${TARGET_LOCATION})
95 endif()
96endmacro()