|
| 1 | +# Copyright (c) 2020 Trail of Bits, Inc. |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU Affero General Public License as |
| 5 | +# published by the Free Software Foundation, either version 3 of the |
| 6 | +# License, or (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU Affero General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU Affero General Public License |
| 14 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +# Find remill first because its config has useful dependency-finding info that |
| 17 | +# needs to be found before the CMake `project` declaration |
| 18 | +find_package(remill COMPONENTS VCPKG_DEPS QUIET) |
| 19 | + |
| 20 | +include(cmake/vcpkg_helper.cmake) |
| 21 | + |
| 22 | +project(mcsema) |
| 23 | +include(GNUInstallDirs) |
| 24 | +cmake_minimum_required(VERSION 3.14) |
| 25 | + |
| 26 | + |
| 27 | +include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/settings.cmake") |
| 28 | +include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/utils.cmake") |
| 29 | +include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/BCCompiler.cmake") |
| 30 | +include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/ccache.cmake") |
| 31 | + |
| 32 | +configureCcache() |
| 33 | +FindAndSelectClangCompiler() |
| 34 | + |
| 35 | +set(MCSEMA_SOURCE_DIR "${PROJECT_SOURCE_DIR}") |
| 36 | + |
| 37 | +option(MCSEMA_ENABLE_RUNTIME "Should runtimes for re-compilation of bitcode be produced?" ON) |
| 38 | + |
| 39 | +find_package(Python2 QUIET) |
| 40 | +if(Python2_FOUND) |
| 41 | + option(MCSEMA_INSTALL_PYTHON2_LIBS "Install Python 2 libraries" ON) |
| 42 | +else() |
| 43 | + option(MCSEMA_INSTALL_PYTHON2_LIBS "Install Python 2 libraries") |
| 44 | +endif() |
| 45 | + |
| 46 | +find_package(Python3 QUIET) |
| 47 | +if(Python3_FOUND) |
| 48 | + option(MCSEMA_INSTALL_PYTHON3_LIBS "Install Python 3 libraries" ON) |
| 49 | +else() |
| 50 | + option(MCSEMA_INSTALL_PYTHON3_LIBS "Install Python 3 libraries") |
| 51 | +endif() |
| 52 | + |
| 53 | +# warnings and compiler settings |
| 54 | +if(NOT DEFINED WIN32) |
| 55 | + set(PROJECT_CXXFLAGS |
| 56 | + ${GLOBAL_CXXFLAGS} |
| 57 | + -Wconversion |
| 58 | + -pedantic |
| 59 | + -Wno-unreachable-code-return |
| 60 | + ) |
| 61 | +endif() |
| 62 | + |
| 63 | +# |
| 64 | +# libraries |
| 65 | +# |
| 66 | + |
| 67 | +find_package(remill CONFIG REQUIRED) |
| 68 | +list(APPEND PROJECT_LIBRARIES remill) |
| 69 | +get_target_property(REMILL_INCLUDE_LOCATION remill_settings INTERFACE_INCLUDE_DIRECTORIES) |
| 70 | + |
| 71 | +# protobuf |
| 72 | +# Compatibility since we use older protobuf CMake functions |
| 73 | +set(protobuf_MODULE_COMPATIBLE ON CACHE BOOL "" FORCE) |
| 74 | +find_package(Protobuf CONFIG REQUIRED) |
| 75 | +list(APPEND PROJECT_LIBRARIES ${Protobuf_LIBRARIES}) |
| 76 | +list(APPEND PROJECT_INCLUDEDIRECTORIES ${Protobuf_INCLUDE_DIR}) |
| 77 | +list(APPEND PROJECT_DEFINITIONS "GOOGLE_PROTOBUF_NO_RTTI") |
| 78 | + |
| 79 | +# |
| 80 | +# protobuf file generation |
| 81 | +# |
| 82 | + |
| 83 | +# this function can't be told where to store the output files! we have to add the whole binary directory |
| 84 | +# to the include directories (or change it and lose compatibility with the system libraries) |
| 85 | +protobuf_generate_cpp(PROJECT_PROTOBUFSOURCEFILES |
| 86 | + PROJECT_PROTOBUFHEADERFILES "${CMAKE_CURRENT_SOURCE_DIR}/mcsema/CFG/CFG.proto" |
| 87 | +) |
| 88 | + |
| 89 | +list(APPEND PROJECT_INCLUDEDIRECTORIES ${CMAKE_CURRENT_BINARY_DIR}) |
| 90 | + |
| 91 | +protobuf_generate_python(PROJECT_PROTOBUFPYTHONMODULE |
| 92 | + "${CMAKE_CURRENT_SOURCE_DIR}/mcsema/CFG/CFG.proto" |
| 93 | +) |
| 94 | + |
| 95 | +add_custom_target(protobuf_python_module_ida |
| 96 | + DEPENDS ${PROJECT_PROTOBUFPYTHONMODULE} |
| 97 | +) |
| 98 | + |
| 99 | +# disable -Werror on these file since they have been generated |
| 100 | +set_source_files_properties(${PROJECT_PROTOBUFSOURCEFILES} PROPERTIES |
| 101 | + COMPILE_FLAGS "-Wno-sign-conversion -Wno-shorten-64-to-32 -Wno-conversion" |
| 102 | +) |
| 103 | + |
| 104 | +set_source_files_properties(${PROJECT_PROTOBUFHEADERFILES} PROPERTIES |
| 105 | + COMPILE_FLAGS "-Wno-sign-conversion -Wno-shorten-64-to-32 -Wno-conversion" |
| 106 | +) |
| 107 | + |
| 108 | +# |
| 109 | +# target settings |
| 110 | +# |
| 111 | + |
| 112 | +set(MCSEMA_LIFT mcsema-lift-${REMILL_LLVM_VERSION}) |
| 113 | + |
| 114 | +# for version information |
| 115 | +add_subdirectory(mcsema/Version) |
| 116 | + |
| 117 | +add_executable(${MCSEMA_LIFT} |
| 118 | + ${PROJECT_PROTOBUFSOURCEFILES} |
| 119 | + |
| 120 | + mcsema/Arch/Arch.cpp |
| 121 | + |
| 122 | + mcsema/CFG/CFG.cpp |
| 123 | + |
| 124 | + mcsema/BC/Callback.cpp |
| 125 | + mcsema/BC/External.cpp |
| 126 | + mcsema/BC/Function.cpp |
| 127 | + mcsema/BC/Instruction.cpp |
| 128 | + mcsema/BC/Legacy.cpp |
| 129 | + mcsema/BC/Lift.cpp |
| 130 | + mcsema/BC/Optimize.cpp |
| 131 | + mcsema/BC/Segment.cpp |
| 132 | + mcsema/BC/Util.cpp |
| 133 | + |
| 134 | + tools/mcsema_lift/Lift.cpp |
| 135 | +) |
| 136 | + |
| 137 | + |
| 138 | +# Copy mcsema-disass in |
| 139 | +add_custom_command( |
| 140 | + TARGET protobuf_python_module_ida POST_BUILD |
| 141 | + DEPENDS {PROJECT_PROTOBUFPYTHONMODULE} |
| 142 | + COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_PROTOBUFPYTHONMODULE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/mcsema_disass/ida7 |
| 143 | +) |
| 144 | + |
| 145 | +# this is needed for the #include directives with absolutes paths to work correctly; it must |
| 146 | +# also be set to PUBLIC since mcsema-lift includes some files directly |
| 147 | +list(APPEND PROJECT_INCLUDEDIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}) |
| 148 | + |
| 149 | +add_dependencies(${MCSEMA_LIFT} |
| 150 | + protobuf_python_module_ida) |
| 151 | + |
| 152 | +# |
| 153 | +# libraries |
| 154 | +# |
| 155 | + |
| 156 | +# anvill |
| 157 | +find_package(anvill CONFIG REQUIRED) |
| 158 | +list(APPEND PROJECT_LIBRARIES anvill-${REMILL_LLVM_VERSION}) |
| 159 | + |
| 160 | +# mcsema-disass |
| 161 | + |
| 162 | +set(MCSEMA_PYTHON_SOURCES |
| 163 | + tools/setup.py |
| 164 | + tools/mcsema_disass/__init__.py |
| 165 | + tools/mcsema_disass/__main__.py |
| 166 | + tools/mcsema_disass/ida7/__init__.py |
| 167 | + tools/mcsema_disass/ida7/anvill_compat.py |
| 168 | + tools/mcsema_disass/ida7/arm_util.py |
| 169 | + tools/mcsema_disass/ida7/CFG_pb2.py |
| 170 | + tools/mcsema_disass/ida7/disass.py |
| 171 | + tools/mcsema_disass/ida7/exception.py |
| 172 | + tools/mcsema_disass/ida7/flow.py |
| 173 | + tools/mcsema_disass/ida7/get_cfg.py |
| 174 | + tools/mcsema_disass/ida7/refs.py |
| 175 | + tools/mcsema_disass/ida7/segment.py |
| 176 | + tools/mcsema_disass/ida7/table.py |
| 177 | + tools/mcsema_disass/ida7/util.py |
| 178 | + tools/mcsema_disass/ida7/x86_util.py |
| 179 | +) |
| 180 | + |
| 181 | +if(MCSEMA_INSTALL_PYTHON2_LIBS) |
| 182 | + add_custom_target(build_mcsema_disass_python2 |
| 183 | + DEPENDS ${MCSEMA_PYTHON_SOURCES}) |
| 184 | + |
| 185 | + add_custom_command( |
| 186 | + TARGET build_mcsema_disass_python2 POST_BUILD |
| 187 | + COMMAND which python2 && python2 setup.py build --force |
| 188 | + COMMENT "Building McSema Python 2 mcsema-disass" |
| 189 | + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/tools") |
| 190 | + |
| 191 | + add_dependencies(${MCSEMA_LIFT} |
| 192 | + build_mcsema_disass_python2) |
| 193 | + |
| 194 | + add_dependencies(build_mcsema_disass_python2 |
| 195 | + protobuf_python_module_ida) |
| 196 | +endif() |
| 197 | + |
| 198 | +if(MCSEMA_INSTALL_PYTHON3_LIBS) |
| 199 | + add_custom_target(build_mcsema_disass_python3 |
| 200 | + DEPENDS ${MCSEMA_PYTHON_SOURCES}) |
| 201 | + |
| 202 | + add_custom_command( |
| 203 | + TARGET build_mcsema_disass_python3 POST_BUILD |
| 204 | + COMMAND which python3 && python3 setup.py build --force |
| 205 | + COMMENT "Building McSema Python 3 mcsema-disass" |
| 206 | + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/tools") |
| 207 | + |
| 208 | + add_dependencies(${MCSEMA_LIFT} |
| 209 | + build_mcsema_disass_python3) |
| 210 | + |
| 211 | + add_dependencies(build_mcsema_disass_python3 |
| 212 | + protobuf_python_module_ida) |
| 213 | +endif() |
| 214 | + |
| 215 | +# |
| 216 | +# target settings |
| 217 | +# |
| 218 | + |
| 219 | +target_link_libraries(${MCSEMA_LIFT} PRIVATE ${PROJECT_LIBRARIES} McSemaVersion) |
| 220 | +target_include_directories(${MCSEMA_LIFT} SYSTEM PUBLIC ${PROJECT_INCLUDEDIRECTORIES}) |
| 221 | +target_compile_definitions(${MCSEMA_LIFT} PUBLIC ${PROJECT_DEFINITIONS}) |
| 222 | +target_compile_options(${MCSEMA_LIFT} PRIVATE ${PROJECT_CXXFLAGS}) |
| 223 | + |
| 224 | +if("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "x86_64" AND MCSEMA_ENABLE_RUNTIME) |
| 225 | + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/mcsema/Arch/X86/Runtime) |
| 226 | +endif() |
| 227 | + |
| 228 | +#TODO(artem): this may need some logic to select only ABIs compatible with current os/arch |
| 229 | +#TODO(kumarak): Disable the ABI libraries build till we don't have script to automate the generation of `ABI_libc.h`. Use pre-build library to test the --abi_libraries flag |
| 230 | +function(GetABILibraryList) |
| 231 | + file(GLOB abi_library_list RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/mcsema/OS" "${CMAKE_CURRENT_SOURCE_DIR}/mcsema/OS/*") |
| 232 | + set(GetABILibraryList_Output ${abi_library_list} PARENT_SCOPE) |
| 233 | +endfunction() |
| 234 | + |
| 235 | +if(NOT MCSEMA_ABI_LIBRARY_LIST_INITIALIZED) |
| 236 | + GetABILibraryList() |
| 237 | + set(MCSEMA_DISABLED_ABI_LIBRARIES ${GetABILibraryList_Output} CACHE |
| 238 | + STRING "A semicolon-separated list of disabled ABI libraries" |
| 239 | + ) |
| 240 | + |
| 241 | + set(MCSEMA_ABI_LIBRARY_LIST_INITIALIZED ON CACHE INTERNAL |
| 242 | + "Whether the abi library list has been initialized or not" |
| 243 | + ) |
| 244 | +endif() |
| 245 | + |
| 246 | +message(STATUS "Disabled ABI libraries: ${MCSEMA_DISABLED_ABI_LIBRARIES}") |
| 247 | +message(STATUS "You can change this setting with -DMCSEMA_DISABLED_ABI_LIBRARIES:STRING=\"Name1;Name2\"") |
| 248 | + |
| 249 | +GetABILibraryList() |
| 250 | +foreach(abi_library_name ${GetABILibraryList_Output}) |
| 251 | + set(abi_library_path "mcsema/OS/${abi_library_name}") |
| 252 | + |
| 253 | + list(FIND MCSEMA_DISABLED_ABI_LIBRARIES "${abi_library_name}" abi_lib_index) |
| 254 | + if(NOT ${abi_lib_index} EQUAL -1) |
| 255 | + message(STATUS "Skipping ABI library: ${abi_library_path}") |
| 256 | + continue() |
| 257 | + endif() |
| 258 | + |
| 259 | + message(STATUS "Adding ABI library: ${abi_library_path}") |
| 260 | + add_subdirectory("${abi_library_path}") |
| 261 | +endforeach() |
| 262 | + |
| 263 | +if(DEFINED WIN32) |
| 264 | + set(install_folder "${CMAKE_INSTALL_PREFIX}/mcsema") |
| 265 | +else() |
| 266 | + set(install_folder "${CMAKE_INSTALL_PREFIX}") |
| 267 | +endif() |
| 268 | + |
| 269 | +install( |
| 270 | + TARGETS ${MCSEMA_LIFT} |
| 271 | + RUNTIME DESTINATION "${install_folder}/bin" |
| 272 | + LIBRARY DESTINATION "${install_folder}/lib" |
| 273 | +) |
| 274 | + |
| 275 | +# target for dyninst frontend |
| 276 | +if (DEFINED BUILD_MCSEMA_DYNINST_DISASS) |
| 277 | + if(${BUILD_MCSEMA_DYNINST_DISASS} EQUAL 1) |
| 278 | + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/mcsema_disass/dyninst) |
| 279 | + endif() |
| 280 | +endif() |
| 281 | + |
| 282 | +set(python_package_installer "${CMAKE_CURRENT_SOURCE_DIR}/tools/setup_launcher") |
| 283 | +if(DEFINED WIN32) |
| 284 | + string(REPLACE "/" "\\" python_package_install_path "${install_folder}") |
| 285 | + set(python_package_installer2 "${python_package_installer}.bat") |
| 286 | + set(python_package_installer3 "${python_package_installer}.bat") |
| 287 | + set(optional_interpreter "cmd.exe /C") |
| 288 | +else() |
| 289 | + set(python_package_install_path "${install_folder}") |
| 290 | + set(python_package_installer2 "${python_package_installer}_py2.sh") |
| 291 | + set(python_package_installer3 "${python_package_installer}_py3.sh") |
| 292 | +endif() |
| 293 | + |
| 294 | +if(MCSEMA_INSTALL_PYTHON2_LIBS) |
| 295 | + install(CODE |
| 296 | + "execute_process(COMMAND ${optional_interpreter} \"${python_package_installer2}\" \"${python_package_install_path}\"\nWORKING_DIRECTORY \"${PROJECT_SOURCE_DIR}/tools\"\nRESULT_VARIABLE exit_code2)\n if(NOT exit_code2 EQUAL 0)\n message(FATAL_ERROR \"Failed to install the Python 2 package\")\n endif()") |
| 297 | +endif() |
| 298 | + |
| 299 | +if(MCSEMA_INSTALL_PYTHON3_LIBS) |
| 300 | + install(CODE |
| 301 | + "execute_process(COMMAND ${optional_interpreter} \"${python_package_installer3}\" \"${python_package_install_path}\"\nWORKING_DIRECTORY \"${PROJECT_SOURCE_DIR}/tools\"\nRESULT_VARIABLE exit_code3)\n if(NOT exit_code3 EQUAL 0)\n message(FATAL_ERROR \"Failed to install the Python 3 package\")\n endif()") |
| 302 | +endif() |
| 303 | + |
| 304 | +add_subdirectory(examples) |
| 305 | + |
0 commit comments