Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Commit

Permalink
vcpkg: Support Windows and arm64 (#726)
Browse files Browse the repository at this point in the history
* Fixes and updates for McSema to build on Windows

* Enable C CXX ASM languages from beginning

Workaround for issue after finding Clang using Clang's own CMake
find-scripts

* Support aarch64 and Windows release triplets

* Conditional link to remill_settings for new vcpkg build
  • Loading branch information
ekilmer authored Feb 2, 2021
1 parent bc4d47f commit 36f2660
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 69 deletions.
5 changes: 3 additions & 2 deletions CMakeLists_vcpkg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ find_package(remill COMPONENTS VCPKG_DEPS QUIET)

include(cmake/vcpkg_helper.cmake)

project(mcsema)
project(mcsema C CXX ASM)

include(GNUInstallDirs)
cmake_minimum_required(VERSION 3.14)

Expand Down Expand Up @@ -65,7 +66,7 @@ endif()
#

find_package(remill CONFIG REQUIRED)
list(APPEND PROJECT_LIBRARIES remill)
list(APPEND PROJECT_LIBRARIES remill_settings remill)
get_target_property(REMILL_INCLUDE_LOCATION remill_settings INTERFACE_INCLUDE_DIRECTORIES)

# protobuf
Expand Down
81 changes: 16 additions & 65 deletions cmake/BCCompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,30 @@ set(DEFAULT_BC_COMPILER_FLAGS
-ffreestanding -fno-common -fno-builtin -fno-exceptions -fno-rtti
-fno-asynchronous-unwind-tables -Wno-unneeded-internal-declaration
-Wno-unused-function -Wgnu-inline-cpp-without-extern -std=c++14
-Wno-pass-failed=transform-warning
${EXTRA_BC_SYSROOT}
)

set(CLANG_CXX_EXECUTABLE_NAME "clang++")
set(LLVMLINK_EXECUTABLE_NAME "llvm-link")
find_package(Clang CONFIG REQUIRED)
get_target_property(CLANG_PATH clang LOCATION)
get_target_property(LLVMLINK_PATH llvm-link LOCATION)

if(DEFINED WIN32)
set(CLANG_CXX_EXECUTABLE_NAME "${CLANG_CXX_EXECUTABLE_NAME}.exe")
set(LLVMLINK_EXECUTABLE_NAME "${LLVMLINK_EXECUTABLE_NAME}.exe")
endif()

if(DEFINED ENV{LLVM_INSTALL_PREFIX})
message(STATUS "Setting LLVM_INSTALL_PREFIX from the environment variable...")
set(LLVM_INSTALL_PREFIX $ENV{LLVM_INSTALL_PREFIX})
endif()

if (DEFINED CXX_COMMON_REPOSITORY_ROOT)
if (NOT DEFINED LLVM_INSTALL_PREFIX)
set(LLVM_INSTALL_PREFIX "${CXX_COMMON_REPOSITORY_ROOT}/llvm" CACHE PATH "Path to LLVM install prefix")
endif()
endif()
file(WRITE "${CMAKE_BINARY_DIR}/emitllvm.test.cpp" "int main(int argc, char* argv[]){return 0;}\n\n")

if (DEFINED LLVM_INSTALL_PREFIX)
message(STATUS "LLVM bin dir: ${LLVM_INSTALL_PREFIX}/bin")
# clang path
find_program(CLANG_PATH
NAMES "${CLANG_CXX_EXECUTABLE_NAME}"
HINTS "${LLVM_INSTALL_PREFIX}/bin"
NO_DEFAULT_PATH
)
execute_process(COMMAND "${CLANG_PATH}" "-emit-llvm" "-c" "emitllvm.test.cpp" "-o" "emitllvm.test.cpp.bc"
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
RESULT_VARIABLE AOUT_IS_NOT_BC
OUTPUT_QUIET ERROR_QUIET
)

# llvm-link path
find_program(LLVMLINK_PATH
NAMES "${LLVMLINK_EXECUTABLE_NAME}"
HINTS "${LLVM_INSTALL_PREFIX}/bin"
NO_DEFAULT_PATH
)
if(NOT "${AOUT_IS_NOT_BC}" STREQUAL "0")
message(SEND_ERROR "The following compiler is not suitable to generate bitcode: ${CLANG_PATH}")
else()
# clang path
if("${CMAKE_CXX_COMPILER}" STREQUAL "${CLANG_CXX_EXECUTABLE_NAME}")
set(CLANG_PATH "${CMAKE_CXX_COMPILER}")

else()
find_program(CLANG_PATH
NAMES "${CLANG_CXX_EXECUTABLE_NAME}"
PATHS "/usr/bin" "/usr/local/bin" "${LLVM_INSTALL_PREFIX}/bin" "${LLVM_TOOLS_BINARY_DIR}" "C:/Program Files/LLVM/bin" "C:/Program Files (x86)/LLVM/bin"
)
endif()

# llvm-link path
find_program(LLVMLINK_PATH
NAMES "${LLVMLINK_EXECUTABLE_NAME}"
PATHS "/usr/bin" "/usr/local/bin" "${LLVM_INSTALL_PREFIX}/bin" "${LLVM_TOOLS_BINARY_DIR}" "C:/Program Files/LLVM/bin" "C:/Program Files (x86)/LLVM/bin"
)
endif()

if((NOT "${CLANG_PATH}" MATCHES "CLANG_PATH-NOTFOUND") AND (NOT "${LLVMLINK_PATH}" MATCHES "LLVMLINK_PATH-NOTFOUND"))
file(WRITE "${CMAKE_BINARY_DIR}/emitllvm.test.cpp" "int main(int argc, char* argv[]){return 0;}\n\n")
message(STATUS "The following compiler has been selected to compile the bitcode: ${CLANG_PATH}")
message(STATUS "The following linker has been selected to link the bitcode: ${LLVMLINK_PATH}")

execute_process(COMMAND "${CLANG_PATH}" "-emit-llvm" "-c" "emitllvm.test.cpp" "-o" "emitllvm.test.cpp.bc"
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
RESULT_VARIABLE AOUT_IS_NOT_BC
OUTPUT_QUIET ERROR_QUIET
)

if(NOT "${AOUT_IS_NOT_BC}" STREQUAL "0")
message(SEND_ERROR "The following compiler is not suitable to generate bitcode: ${CLANG_PATH}")
else()
message(STATUS "The following compiler has been selected to compile the bitcode: ${CLANG_PATH}")

set(CMAKE_BC_COMPILER "${CLANG_PATH}" CACHE PATH "Bitcode Compiler")
set(CMAKE_BC_LINKER "${LLVMLINK_PATH}" CACHE PATH "Bitcode Linker")
endif()
set(CMAKE_BC_COMPILER "${CLANG_PATH}" CACHE PATH "Bitcode Compiler")
set(CMAKE_BC_LINKER "${LLVMLINK_PATH}" CACHE PATH "Bitcode Linker")
endif()

#
Expand Down
18 changes: 16 additions & 2 deletions cmake/vcpkg_helper.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,24 @@ if (NOT USE_SYSTEM_DEPENDENCIES)

# Set default triplet to Release VCPKG build unless we can't find it
if (NOT DEFINED VCPKG_TARGET_TRIPLET)
set(_project_arch "x64")
if (UNIX)
execute_process(COMMAND uname -m
OUTPUT_VARIABLE _SYSTEM_ARCH
OUTPUT_STRIP_TRAILING_WHITESPACE)
else()
message(WARNING "No detection of architecture for this platform. Assuming x64")
endif()
if (_SYSTEM_ARCH MATCHES "^[Aa][Aa][Rr][Cc][Hh]64$")
set(_project_arch "arm64")
endif()

if (APPLE)
set(_project_vcpkg_triplet "x64-osx-rel")
set(_project_vcpkg_triplet "${_project_arch}-osx-rel")
elseif(UNIX)
set(_project_vcpkg_triplet "x64-linux-rel")
set(_project_vcpkg_triplet "${_project_arch}-linux-rel")
elseif(WIN32)
set(_project_vcpkg_triplet "${_project_arch}-windows-static-md-rel")
else()
message(FATAL_ERROR "Could not detect default release triplet")
endif()
Expand Down
3 changes: 3 additions & 0 deletions mcsema/Version/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ include("${MCSEMA_SOURCE_DIR}/cmake/git_watcher.cmake")

# Create a library out of the compiled post-configure file.
add_library(McSemaVersion STATIC ${POST_CONFIGURE_FILE})
if (NOT DEFINED ENV{TRAILOFBITS_LIBRARIES})
target_link_libraries(McSemaVersion PRIVATE remill_settings)
endif()
target_include_directories(McSemaVersion PUBLIC ${MCSEMA_SOURCE_DIR})
add_dependencies(McSemaVersion check_git_${PROJECT_NAME})

0 comments on commit 36f2660

Please sign in to comment.