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)
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.
15 # The name of the target to create. Building this target will generate the
16 # requested disassembly.
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.
24 # FUNCTION function-name:
25 # The name of the function to disassemble in the executable.
28 # If provided, the generated target is included in the 'all' target.
30 function(disassemble target)
31 cmake_parse_arguments(ARGS "ALL" # options
32 "EXECUTABLE;FUNCTION" # 1 value args
36 if (NOT ARGS_EXECUTABLE)
37 message(FATAL_ERROR "The `EXECUTABLE` argument must be provided.")
39 if (NOT TARGET ${ARGS_EXECUTABLE})
40 message(FATAL_ERROR "The `EXECUTABLE` argument must be the name of a valid "
41 "executable created with `add_executable`.")
44 if (NOT ARGS_FUNCTION)
45 message(FATAL_ERROR "The `FUNCTION` argument must be provided.")
55 add_custom_target(${target} ${ARGS_ALL}
56 COMMAND ${DISASSEMBLE_lldb} -f $<TARGET_FILE:${ARGS_EXECUTABLE}>
57 -o "disassemble --name ${ARGS_FUNCTION}"
59 DEPENDS ${ARGS_EXECUTABLE}
61 elseif(DISASSEMBLE_gdb)
62 add_custom_target(${target} ${ARGS_ALL}
63 COMMAND ${DISASSEMBLE_gdb} -batch -se $<TARGET_FILE:${ARGS_EXECUTABLE}>
64 -ex "disassemble ${ARGS_FUNCTION}"
65 DEPENDS ${ARGS_EXECUTABLE}
70 find_program(DISASSEMBLE_gdb gdb)
71 find_program(DISASSEMBLE_lldb lldb)