Skip to content

Instantly share code, notes, and snippets.

@Manu343726
Created September 5, 2015 21:33
Show Gist options
  • Select an option

  • Save Manu343726/64c0a75c089ad96d22cb to your computer and use it in GitHub Desktop.

Select an option

Save Manu343726/64c0a75c089ad96d22cb to your computer and use it in GitHub Desktop.
Generating assembly listings with CMake
## Variables:
#
# OUTPUT_DIRECTORY: Directory where the assemblu file will be placed (For example "/home/manu/listings")
# ASSEMBLY_LISTING_FILE: Assembly listing filename (For example "foo.s")
# SOURCE_FILENAME: Name of the sourcefile being compiled (See bellow). For example "foo", from foo.cpp.
# TARGET: Target being compiled (Mostly an executable target). For example "foo".
if(MSVC)
# Trust me, Microsoft docs suck.
target_compile_options(${TARGET} "/Fa${OUTPUT_PATH}/${ASSEMBLY_LISTING_FILE}" /FA)
else()
# The trick is that makefiles generator defines a [sourcefile].s target for each sourcefile of a target to generate the listing
# of that file. We hook a command after build to invoke that target and copy the result file to our ourput directory:
add_custom_command(TARGET ${TARGET}
POST_BUILD
COMMAND make ARGS ${SOURCE_FILENAME}.s
COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_BINARY_DIR}/CMakeFiles/${TARGET}.dir/${SOURCE_FILENAME}.cpp.s"
"${OUTPUT_PATH}/${ASSEMBLY_LISTING_FILE}"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endif()
@2catycm
Copy link

2catycm commented Mar 31, 2022

how to use it ? do I need to set these variable first?

OUTPUT_DIRECTORY: Directory where the assemblu file will be placed (For example "/home/manu/listings")

ASSEMBLY_LISTING_FILE: Assembly listing filename (For example "foo.s")

SOURCE_FILENAME: Name of the sourcefile being compiled (See bellow). For example "foo", from foo.cpp.

TARGET: Target being compiled (Mostly an executable target). For example "foo".

@Manu343726
Copy link
Author

Yes, these are the input variables of the routine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment