AndroidNdkGdb.cmake 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # 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. # Usage:
  31. # 1. place AndroidNdkGdb.cmake somewhere inside ${CMAKE_MODULE_PATH}
  32. # 2. inside your project add
  33. #
  34. # include(AndroidNdkGdb)
  35. # android_ndk_gdb_enable()
  36. # # for each target
  37. # add_library(MyLibrary ...)
  38. # android_ndk_gdb_debuggable(MyLibrary)
  39. # add gdbserver and general gdb configuration to project
  40. # also create a mininal NDK skeleton so ndk-gdb finds the paths
  41. #
  42. # the optional parameter defines the path to the android project.
  43. # uses PROJECT_BINARY_DIR by default.
  44. macro(android_ndk_gdb_enable)
  45. if(ANDROID)
  46. # create custom target that depends on the real target so it gets executed afterwards
  47. add_custom_target(NDK_GDB ALL)
  48. if(${ARGC})
  49. set(ANDROID_PROJECT_DIR ${ARGV0})
  50. else()
  51. set(ANDROID_PROJECT_DIR ${PROJECT_BINARY_DIR})
  52. endif()
  53. set(NDK_GDB_SOLIB_PATH ${ANDROID_PROJECT_DIR}/obj/local/${ANDROID_NDK_ABI_NAME}/)
  54. file(MAKE_DIRECTORY ${NDK_GDB_SOLIB_PATH})
  55. # 1. generate essential Android Makefiles
  56. file(MAKE_DIRECTORY ${ANDROID_PROJECT_DIR}/jni)
  57. if(NOT EXISTS ${ANDROID_PROJECT_DIR}/jni/Android.mk)
  58. file(WRITE ${ANDROID_PROJECT_DIR}/jni/Android.mk "APP_ABI := ${ANDROID_NDK_ABI_NAME}\n")
  59. endif()
  60. if(NOT EXISTS ${ANDROID_PROJECT_DIR}/jni/Application.mk)
  61. file(WRITE ${ANDROID_PROJECT_DIR}/jni/Application.mk "APP_ABI := ${ANDROID_NDK_ABI_NAME}\n")
  62. endif()
  63. # 2. generate gdb.setup
  64. get_directory_property(PROJECT_INCLUDES DIRECTORY ${PROJECT_SOURCE_DIR} INCLUDE_DIRECTORIES)
  65. string(REGEX REPLACE ";" " " PROJECT_INCLUDES "${PROJECT_INCLUDES}")
  66. file(WRITE ${LIBRARY_OUTPUT_PATH}/gdb.setup "set solib-search-path ${NDK_GDB_SOLIB_PATH}\n")
  67. file(APPEND ${LIBRARY_OUTPUT_PATH}/gdb.setup "directory ${PROJECT_INCLUDES}\n")
  68. # 3. copy gdbserver executable
  69. file(COPY ${ANDROID_NDK}/prebuilt/android-${ANDROID_ARCH_NAME}/gdbserver/gdbserver DESTINATION ${LIBRARY_OUTPUT_PATH})
  70. file(RENAME ${LIBRARY_OUTPUT_PATH}/gdbserver ${LIBRARY_OUTPUT_PATH}/gdbserver.so)
  71. endif()
  72. endmacro()
  73. # register a target for remote debugging
  74. # copies the debug version to NDK_GDB_SOLIB_PATH then strips symbols of original
  75. macro(android_ndk_gdb_debuggable TARGET_NAME)
  76. if(ANDROID)
  77. # create custom target that depends on the real target so it gets executed afterwards
  78. add_dependencies(NDK_GDB ${TARGET_NAME})
  79. # 4. copy lib to obj
  80. add_custom_command(TARGET NDK_GDB POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${TARGET_NAME}> ${NDK_GDB_SOLIB_PATH})
  81. # 5. strip symbols
  82. add_custom_command(TARGET NDK_GDB POST_BUILD COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${TARGET_NAME}>)
  83. endif()
  84. endmacro()