--- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.22.1) -include(ExternalProject) + project(libmem LANGUAGES @@ -23,13 +23,23 @@ option(LIBMEM_DEEP_TESTS OFF ) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(targets_export_name libmem-target) + +if (MSVC AND NOT(BUILD_SHARED_LIBS)) + add_definitions(/DLM_EXPORT) +elseif(NOT(MSVC) AND BUILD_SHARED_LIBS) + add_definitions(-DLM_EXPORT) +endif() +if(0) option(LIBMEM_BUILD_STATIC "Build a static library" OFF ) - +endif() set(LIBMEM_ARCH ${CMAKE_SYSTEM_PROCESSOR} CACHE STRING "Force a specific architecture") - +if(0) message(STATUS "[*] Architecture: ${LIBMEM_ARCH}" ) @@ -65,13 +75,17 @@ add_library(capstone STATIC IMPORTED) set_target_properties(capstone PROPERTIES IMPORTED_LOCATION ${CAPSTONE_IMPORT_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}capstone${CMAKE_STATIC_LIBRARY_SUFFIX}) add_library(keystone STATIC IMPORTED) set_target_properties(keystone PROPERTIES IMPORTED_LOCATION ${KEYSTONE_IMPORT_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}keystone${CMAKE_STATIC_LIBRARY_SUFFIX}) +endif() +find_package(PkgConfig) +pkg_check_modules(keystone REQUIRED keystone) +find_package(capstone CONFIG REQUIRED) # End of external dependencies - set(LIBMEM_DIR "${PROJECT_SOURCE_DIR}") +set(LLVM_DEM_DIR "${LIBMEM_DIR}/external/llvm") set(LIBMEM_INC "${LIBMEM_DIR}/include") set(INTERNAL_DIR "${LIBMEM_DIR}/internal") set(COMMON_DIR "${LIBMEM_DIR}/src/common") - +file(GLOB_RECURSE LLVM_DEM_SRC "${LLVM_DEM_DIR}/lib/*.cpp") if(${CMAKE_SYSTEM_NAME} STREQUAL Windows OR ${CMAKE_SYSTEM_NAME} STREQUAL CYGWIN) file(GLOB_RECURSE LIBMEM_SRC "${LIBMEM_DIR}/src/win/*.c" "${LIBMEM_DIR}/src/common/*.c" "${LIBMEM_DIR}/src/common/*.cpp" "${INTERNAL_DIR}/winutils/*.c" "${INTERNAL_DIR}/demangler/*.cpp") elseif(${CMAKE_SYSTEM_NAME} STREQUAL Linux) @@ -89,16 +103,13 @@ elseif(${CMAKE_SYSTEM_NAME} STREQUAL FreeBSD) endif() file(GLOB LIBMEM_SRC ${LIBMEM_ARCH_SRC} "${LIBMEM_DIR}/src/freebsd/*.c" "${LIBMEM_DIR}/src/freebsd/ptrace/*.c" "${LIBMEM_DIR}/src/common/*.c" "${LIBMEM_DIR}/src/common/*.cpp" "${INTERNAL_DIR}/posixutils/*.c" "${INTERNAL_DIR}/elfutils/*.c" "${INTERNAL_DIR}/demangler/*.cpp") endif() -set(LIBMEM_DEPS - capstone - keystone - llvm -) -if (LIBMEM_BUILD_STATIC) +file(GLOB_RECURSE LIBMEM_PUBLIC_HEADERS "${LIBMEM_INC}/libmem/*.h" "${LIBMEM_INC}/libmem/*.hpp") + +if (0) add_library(libmem STATIC ${LIBMEM_SRC}) else() - add_library(libmem SHARED ${LIBMEM_SRC}) + add_library(libmem ${LIBMEM_SRC} ${LLVM_DEM_SRC}) endif() target_include_directories(libmem PRIVATE "${LIBMEM_DIR}/src" "${INTERNAL_DIR}" "${COMMON_DIR}") @@ -109,17 +120,16 @@ include_directories(${PROJECT_SOURCE_DIR} ${LLVM_INC} ) -if (LIBMEM_BUILD_TESTS) +if (0) set(TESTS_DIR "${PROJECT_SOURCE_DIR}/tests") add_subdirectory(${TESTS_DIR}) endif() -set_target_properties(libmem PROPERTIES POSITION_INDEPENDENT_CODE True INCLUDES ${LIBMEM_INC}) +set_target_properties(libmem PROPERTIES POSITION_INDEPENDENT_CODE True PUBLIC_HEADER "${LIBMEM_PUBLIC_HEADERS}") +target_compile_features(libmem PUBLIC cxx_std_17) +target_compile_definitions(libmem PUBLIC LM_EXPORT) +target_include_directories(libmem PRIVATE "${LLVM_DEM_DIR}/include" "${LIBMEM_DIR}/src" "${INTERNAL_DIR}" "${COMMON_DIR}" "${LIBMEM_INC}" INTERFACE $) target_compile_definitions(libmem PUBLIC LM_EXPORT) -add_dependencies(libmem - capstone-engine - keystone-engine -) if(${CMAKE_SYSTEM_NAME} STREQUAL Windows OR ${CMAKE_SYSTEM_NAME} STREQUAL CYGWIN) set(LIBMEM_DEPS @@ -151,8 +161,8 @@ else() message(FATAL_ERROR "[!] Unsupported platform") endif() -target_link_libraries(libmem ${LIBMEM_DEPS}) -if(LIBMEM_BUILD_STATIC) +target_link_libraries(libmem PRIVATE ${LIBMEM_DEPS} ${keystone_LINK_LIBRARIES} capstone::capstone) +if(0) # Create a bundled static library containing all dependencies (to mimic the shared library behavior) set_target_properties(libmem PROPERTIES OUTPUT_NAME "libmem_partial") set(libmem_bundle_files "$") @@ -193,7 +203,7 @@ if(LIBMEM_BUILD_STATIC) endif() endif() -if(${CMAKE_SYSTEM_NAME} STREQUAL Windows OR ${CMAKE_SYSTEM_NAME} STREQUAL CYGWIN) +if(0) if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL CYGWIN) cmake_path(SET CMAKE_INSTALL_PREFIX "$ENV{ProgramFiles}") else() @@ -202,14 +212,44 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL Windows OR ${CMAKE_SYSTEM_NAME} STREQUAL CYGWIN endif() set(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/libmem") execute_process(COMMAND mkdir "${CMAKE_INSTALL_PREFIX}") -else() +elseif(0) set(CMAKE_INSTALL_PREFIX "/usr") endif() - install(TARGETS libmem - LIBRARY DESTINATION lib) + EXPORT ${targets_export_name} + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + PUBLIC_HEADER DESTINATION include/libmem + RUNTIME DESTINATION bin +) -install(TARGETS libmem - RUNTIME DESTINATION lib) +# Install the export file, which allows users to find the target +install( + EXPORT ${targets_export_name} + NAMESPACE libmem:: + FILE ${targets_export_name}.cmake + DESTINATION share/libmem +) -install(DIRECTORY ${LIBMEM_INC}/libmem DESTINATION include) +# Config and version files for the package +include(CMakePackageConfigHelpers) + +configure_package_config_file( + ${CMAKE_CURRENT_SOURCE_DIR}/libmem-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/libmem-config.cmake + INSTALL_DESTINATION share/libmem +) + +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/libmem-config-version.cmake" + VERSION 5.0.2 + COMPATIBILITY SameMajorVersion +) + +# Install the config and version files +install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/libmem-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/libmem-config-version.cmake + DESTINATION share/libmem +)