Disassemble.cmake 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright Louis Dionne 2016
  2. # Copyright Zach Laine 2016
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  5. #
  6. #
  7. # This CMake module provides a way to get the disassembly of a function within
  8. # an executable created with `add_executable`. The module provides a `disassemble`
  9. # function that creates a target which, when built, outputs the disassembly of
  10. # the given function within an executable to standard output.
  11. #
  12. # Parameters
  13. # ----------
  14. # target:
  15. # The name of the target to create. Building this target will generate the
  16. # requested disassembly.
  17. #
  18. # EXECUTABLE executable:
  19. # The name of an executable to disassemble. This must be the name of a valid
  20. # executable that was created with `add_executable`. The disassembly target
  21. # thus created will be made dependent on the executable, so that it is built
  22. # automatically when the disassembly is requested.
  23. #
  24. # FUNCTION function-name:
  25. # The name of the function to disassemble in the executable.
  26. #
  27. # [ALL]:
  28. # If provided, the generated target is included in the 'all' target.
  29. #
  30. function(disassemble target)
  31. cmake_parse_arguments(ARGS "ALL" # options
  32. "EXECUTABLE;FUNCTION" # 1 value args
  33. "" # multivalued args
  34. ${ARGN})
  35. if (NOT ARGS_EXECUTABLE)
  36. message(FATAL_ERROR "The `EXECUTABLE` argument must be provided.")
  37. endif()
  38. if (NOT TARGET ${ARGS_EXECUTABLE})
  39. message(FATAL_ERROR "The `EXECUTABLE` argument must be the name of a valid "
  40. "executable created with `add_executable`.")
  41. endif()
  42. if (NOT ARGS_FUNCTION)
  43. message(FATAL_ERROR "The `FUNCTION` argument must be provided.")
  44. endif()
  45. if (ARGS_ALL)
  46. set(ARGS_ALL "ALL")
  47. else()
  48. set(ARGS_ALL "")
  49. endif()
  50. if (DISASSEMBLE_lldb)
  51. add_custom_target(${target} ${ARGS_ALL}
  52. COMMAND ${DISASSEMBLE_lldb} -f $<TARGET_FILE:${ARGS_EXECUTABLE}>
  53. -o "disassemble --name ${ARGS_FUNCTION}"
  54. -o quit
  55. DEPENDS ${ARGS_EXECUTABLE}
  56. )
  57. elseif(DISASSEMBLE_gdb)
  58. add_custom_target(${target} ${ARGS_ALL}
  59. COMMAND ${DISASSEMBLE_gdb} -batch -se $<TARGET_FILE:${ARGS_EXECUTABLE}>
  60. -ex "disassemble ${ARGS_FUNCTION}"
  61. DEPENDS ${ARGS_EXECUTABLE}
  62. )
  63. endif()
  64. endfunction()
  65. find_program(DISASSEMBLE_gdb gdb)
  66. find_program(DISASSEMBLE_lldb lldb)