Files
lifting-bits-sleigh/example/CMakeLists.txt
T
Eric Kilmer 74838a39ba CMake refactor (#105)
* Major CMake refactor

* CI fixes

* Fix CMake errors

* Install graphviz on Windows

* More consistent CMake build types

* Fix compiler defines for Windows

* Fix more bugs

* Add standalone tool building support

* Fix typo

* Update README

* Install sleighLift before trying to smoketest it

* Test coverage preset

The compiler option '-fkeep-inline-functions' originally included with
the coverage build type causes errors with the message "undefined
reference to 'vtable for OpAction*'" for all OpAction subclasses.

This StackOverflow answer might help in resolving this?
https://stackoverflow.com/a/57504289

* Build documentation by default with CI presets

* Use COMPONENT for install commands

This allows for someone to specify which components they want to install
if they only want or need a subset

* Support cross compiling with host system sleigh compiler

* Various fixes found during review

* Move "_WINDOWS" define to CMakeLists.txt file

* Rename ToB 'tools' directory to 'extra-tools'

* Separate projects for sleigh tools

Library is still top-level CMakeLists.txt

More options

* CMake find_package(Git)

* Fix bad merge for patch paths

* Fix missing libconfig.h header install

* Fix issues with installation of headers

* More refactoring

* Refactor sleighexample into its own project/directory

* Refactor specfiles building into separate project/directory

* Check for termios.h header and set define if found

* Update READMEs

* Better consistency with install rules

* Build sleighLift as a standalone project with added source

* More changes to CMake subprojects

* Also add "Specs" component to sleigh installation config

* Try to be better at bootstrapping subprojects (hopefully this doesn't
  bite us, but if it does, I'm happy to remove it and say "not
  supported")

* Address some code review

* Add project sleigh_tool for tool subdirectory

Could make it easier to build just the tools

* Add help text to name the tool executable name

* Add license notice to CMake files

* Fix/Remove too specific CPack packaging configuration

* Consistent capitalization of Sleigh

Co-authored-by: Alex Cameron <asc@tetsuo.sh>
2022-08-22 15:41:53 +10:00

116 lines
3.4 KiB
CMake

#
# Copyright (c) 2022-present, Trail of Bits, Inc.
# All rights reserved.
#
# This source code is licensed in accordance with the terms specified in
# the LICENSE file found in the root directory of this source tree.
#
cmake_minimum_required(VERSION 3.18)
include(../cmake/prelude.cmake)
# We include this because the source code for this example is in upstream
include(../src/setup-ghidra-source.cmake)
project(sleigh_example
VERSION "${ghidra_version}"
DESCRIPTION "Sleigh example"
HOMEPAGE_URL "https://github.com/lifting-bits/sleigh"
LANGUAGES CXX
)
include(../cmake/project-is-top-level.cmake)
if(NOT TARGET sleigh::sla)
find_package(sleigh)
if(NOT sleigh_FOUND)
message(WARNING "Could not find sleigh library, building from source")
# Exclude from all because there are no install rules for the example
# See sleigh-lift for example with install rules
add_subdirectory(.. sleigh EXCLUDE_FROM_ALL)
endif()
endif()
add_executable(sleigh_example
"${library_root}/sleighexample.cc"
)
target_compile_features(sleigh_example PRIVATE cxx_std_11)
sleigh_add_optional_defines(sleigh_example PRIVATE)
target_link_libraries(sleigh_example PRIVATE
sleigh::sla
)
set_target_properties(sleigh_example PROPERTIES
OUTPUT_NAME sleighexample
)
#
# Compile our required sleigh spec file
#
# Get the native machine's sleigh compiler or use the one we're about to build
# if not cross compiling
if(CMAKE_CROSSCOMPILING)
find_program(
SLEIGH_EXECUTABLE sleigh
DOC "Path to host system sleigh compiler"
REQUIRED
)
set(sleigh_compiler "${SLEIGH_EXECUTABLE}")
else()
# Try to find/acquire or bootstrap the sleigh spec compiler
# Logic is repeated in sleighspecs/CMakeLists.txt
if(NOT TARGET sleigh::sleigh)
find_package(sleigh QUIET)
if(NOT sleigh_FOUND OR NOT TARGET sleigh::sleigh)
find_program(
SLEIGH_EXECUTABLE sleigh
DOC "Path to host system sleigh compiler"
)
if(NOT SLEIGH_EXECUTABLE)
message(WARNING "Could not find sleigh compiler, building from source")
set(saved_skip_install_rules "${CMAKE_SKIP_INSTALL_RULES}")
set(CMAKE_SKIP_INSTALL_RULES TRUE)
add_subdirectory(../tools/spec-compiler spec-compiler EXCLUDE_FROM_ALL)
set(CMAKE_SKIP_INSTALL_RULES "${saved_skip_install_rules}")
endif()
endif()
endif()
endif()
if(SLEIGH_EXECUTABLE)
set(sleigh_compiler "${SLEIGH_EXECUTABLE}")
else()
set(sleigh_compiler "$<TARGET_FILE:sleigh::sleigh>")
endif()
# Compile the sla file
include(../cmake/modules/sleighCompile.cmake)
sleigh_compile(
TARGET sleigh_example_sleigh_spec_x86
COMPILER "${sleigh_compiler}"
SLASPEC "${ghidrasource_SOURCE_DIR}/Ghidra/Processors/x86/data/languages/x86.slaspec"
LOG_FILE "${CMAKE_CURRENT_BINARY_DIR}/specfiles/x86.sla.log"
OUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/specfiles/x86.sla"
)
set_property(
TARGET sleigh_example_sleigh_spec_x86
PROPERTY EXCLUDE_FROM_ALL FALSE
)
#
# Run the example
#
add_custom_target(sleigh_example_runner)
set(example_actions disassemble pcode emulate)
foreach(action ${example_actions})
add_custom_target(sleigh_example_${action}
COMMAND sleigh_example ${action}
COMMENT "Running example ${action}"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
add_dependencies(sleigh_example_${action} sleigh_example_sleigh_spec_x86)
add_dependencies(sleigh_example_runner sleigh_example_${action})
endforeach()