lifting-bits/sleigh

Expose a CMake slaspec compilation function

ekilmer opened this issue · 0 comments

This project should expose and install a function to compile an arbitrary slaspec file.

This would be similar in spirit to how the FindProtobuf.cmake file exposes their codegen capabilities in CMake.

This is an essential feature for downstream users to compile their own custom/patched slaspec files.

Fortunately, there is already some rough function-like capability to compile a given sleigh file here

sleigh/CMakeLists.txt

Lines 492 to 539 in 31ec99d

foreach(spec_file ${spec_file_list})
# Get 'mx51'
cmake_path(GET spec_file STEM LAST_ONLY spec_name)
# Get '<ghidra_source_prefix>/Ghidra/Processors/8051/data/languages'
cmake_path(GET spec_file PARENT_PATH spec_dir)
# Get '8051/data/languages'
cmake_path(RELATIVE_PATH spec_dir
BASE_DIRECTORY "${ghidrasource_SOURCE_DIR}/${spec_files_dir_prefix}"
OUTPUT_VARIABLE spec_proc_dir
)
set(spec_build_log "${spec_files_build_log_dir}/${spec_name}_build.log")
# Combine back again for the build directory output like
# '<build_prefix>/Ghidra/Processors/8051/data/languages'
set(spec_out_dir "${spec_files_root_dir}/${spec_proc_dir}")
# '<build_prefix>/Ghidra/Processors/8051/data/languages/mx51.sla'
set(spec_out "${spec_out_dir}/${spec_name}.sla")
# Copy all other files from the slaspec source directory:
# '<ghidra_source_prefix>/Ghidra/Processors/8051/data/languages'
# TODO: This only copies the directory once, so you will need to remove the whole directory if you update any of
# the other files, like '*.cspec' or '*.ldef' files
# CMake only guarantees update monitoring for file listed explicitly
add_custom_command(
OUTPUT "${spec_out_dir}"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${spec_dir}" "${spec_out_dir}"
)
# Compile the sla file
add_custom_command(
OUTPUT "${spec_out}"
DEPENDS "${spec_file}" "${spec_out_dir}" "${spec_files_build_log_dir}"
COMMAND "$<TARGET_FILE:sleigh::sleigh_opt>" ${spec_file} "${spec_out}" > ${spec_build_log} 2>&1
WORKING_DIRECTORY "${spec_dir}"
COMMENT "sleigh: Compiling the ${spec_name} spec file (${spec_build_log})"
BYPRODUCTS ${spec_build_log}
VERBATIM
)
string(REPLACE "." "_" spec_target_name ${spec_name})
set(spec_target "sleigh_spec_${spec_target_name}")
add_custom_target(${spec_target}
DEPENDS ${spec_out}
)
list(APPEND spec_targets ${spec_target})
list(APPEND spec_files ${spec_out})
endforeach()

I think a standalone function should live in a cmake/utils.cmake or something (as long as it's separate from a CMakeLists.txt), which is then installed into the directory alongside the project config file.