Modernized CMakeLists

This commit is contained in:
Joel Höner 2017-07-02 08:40:41 +02:00
parent 392c36c85f
commit a0f54a45c3
1 changed files with 75 additions and 55 deletions

View File

@ -1,20 +1,19 @@
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 3.1)
include(GenerateExportHeader) include(GenerateExportHeader)
project(Zydis) project(Zydis VERSION 2.0)
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)
option(ZYDIS_BUILD_SHARED_LIBS # =============================================================================================== #
"Build shared libraries rather than static ones" # Overridable options #
OFF) # =============================================================================================== #
option(ZYDIS_FORCE_SHARED_CRT
"Forces shared linkage against the CRT even when building a static library. MSVC only." # Features
OFF)
option(ZYDIS_FEATURE_EVEX option(ZYDIS_FEATURE_EVEX
"Enable EVEX encoded instructions" "Enable support for EVEX instructions"
ON) ON)
option(ZYDIS_FEATURE_MVEX option(ZYDIS_FEATURE_MVEX
"Enable MVEX encoded instructions" "Enable support for MVEX instructions"
ON) ON)
option(ZYDIS_FEATURE_FLAGS option(ZYDIS_FEATURE_FLAGS
"Include information about affected flags" "Include information about affected flags"
@ -22,14 +21,34 @@ option(ZYDIS_FEATURE_FLAGS
option(ZYDIS_FEATURE_CPUID option(ZYDIS_FEATURE_CPUID
"Include information about CPUID feature-flags" "Include information about CPUID feature-flags"
OFF) OFF)
option(ZYDIS_BUILD_EXAMPLES "Build examples" ON) option(ZYDIS_FEATURE_ENCODER
option(ZYDIS_BUILD_TOOLS "Build tools" ON) "Enable instruction encoding functionality"
OFF)
if (NOT CONFIGURED_ONCE) # Build configuration
option(ZYDIS_BUILD_SHARED_LIBS
"Build shared libraries rather than static ones"
OFF)
option(ZYDIS_BUILD_EXAMPLES
"Build examples"
ON)
option(ZYDIS_BUILD_TOOLS
"Build tools"
ON)
option(ZYDIS_DEV_MODE
"Enable developer mode (-Wall, -Werror, ...)"
OFF)
# =============================================================================================== #
# Developer mode #
# =============================================================================================== #
# If in developer mode, hack global compiler flags.
if (ZYDIS_DEV_MODE)
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang") "${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
set(compiler_specific "-std=c99 -pedantic -Wextra -Werror") set(compiler_specific "-pedantic -Wextra -Werror")
elseif (MSVC) elseif (MSVC)
set(compiler_specific "/WX /W4 /TC") set(compiler_specific "/WX /W4 /TC")
endif () endif ()
@ -37,47 +56,12 @@ if (NOT CONFIGURED_ONCE)
CACHE STRING "Flags used by the compiler during all build types." FORCE) CACHE STRING "Flags used by the compiler during all build types." FORCE)
endif () endif ()
# CMake always orders MSVC to build with a shared CRT. Hack CMake variables in order # =============================================================================================== #
# to generate with a statically linked CRT when we build a static library. # Library configuration #
if (("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") AND NOT ZYDIS_FORCE_SHARED_CRT) # =============================================================================================== #
foreach(flag_var
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif ()
endforeach ()
endif ()
# Library add_library("Zydis")
set(headers
"include/Zydis/Decoder.h"
#"include/Zydis/Encoder.h"
"include/Zydis/Defines.h"
"include/Zydis/Formatter.h"
"include/Zydis/InstructionInfo.h"
"include/Zydis/Mnemonic.h"
"include/Zydis/Register.h"
"include/Zydis/Status.h"
"include/Zydis/Types.h"
"include/Zydis/Utils.h"
"include/Zydis/Zydis.h"
"include/Zydis/Internal/InstructionTable.h")
set(sources
"src/Decoder.c"
#"src/Encoder.c"
"src/Formatter.c"
"src/InstructionTable.c"
"src/Mnemonic.c"
"src/Register.c"
"src/Utils.c"
"src/Zydis.c")
if (ZYDIS_BUILD_SHARED_LIBS AND WIN32)
set(sources ${sources} "src/VersionInfo.rc")
endif ()
add_library("Zydis" ${headers} ${sources})
target_include_directories("Zydis" PUBLIC "include/" ${PROJECT_BINARY_DIR}) target_include_directories("Zydis" PUBLIC "include/" ${PROJECT_BINARY_DIR})
target_compile_definitions("Zydis" PRIVATE "_CRT_SECURE_NO_WARNINGS" "ZYDIS_EXPORTS") target_compile_definitions("Zydis" PRIVATE "_CRT_SECURE_NO_WARNINGS" "ZYDIS_EXPORTS")
generate_export_header("Zydis" BASE_NAME "ZYDIS" EXPORT_FILE_NAME "ZydisExportConfig.h") generate_export_header("Zydis" BASE_NAME "ZYDIS" EXPORT_FILE_NAME "ZydisExportConfig.h")
@ -95,7 +79,40 @@ if (ZYDIS_FEATURE_CPUID)
target_compile_definitions("Zydis" PRIVATE "ZYDIS_ENABLE_FEATURE_CPUID") target_compile_definitions("Zydis" PRIVATE "ZYDIS_ENABLE_FEATURE_CPUID")
endif () endif ()
# Examples target_sources("Zydis"
PUBLIC
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Decoder.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Defines.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Formatter.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/InstructionInfo.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Mnemonic.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Register.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Status.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Types.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Utils.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Zydis.h"
PRIVATE
"include/Zydis/Internal/InstructionTable.h"
"src/Decoder.c"
"src/Formatter.c"
"src/InstructionTable.c"
"src/Mnemonic.c"
"src/Register.c"
"src/Utils.c"
"src/Zydis.c")
if (ZYDIS_BUILD_SHARED_LIBS AND WIN32)
target_sources("Zydis" PRIVATE "src/VersionInfo.rc")
endif ()
if (ZYDIS_FEATURE_ENCODER)
target_sources("Zydis" PUBLIC "include/Encoder.h" PRIVATE "src/Encoder.c")
endif ()
# =============================================================================================== #
# Examples #
# =============================================================================================== #
if (ZYDIS_BUILD_EXAMPLES) if (ZYDIS_BUILD_EXAMPLES)
add_executable("FormatterHooks" add_executable("FormatterHooks"
"examples/FormatterHooks.c" "examples/FormatterHooks.c"
@ -105,7 +122,10 @@ if (ZYDIS_BUILD_EXAMPLES)
target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS") target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS")
endif () endif ()
# Tools # =============================================================================================== #
# Tools #
# =============================================================================================== #
if (ZYDIS_BUILD_TOOLS) if (ZYDIS_BUILD_TOOLS)
add_executable("ZydisDisasm" "tools/ZydisDisasm.c") add_executable("ZydisDisasm" "tools/ZydisDisasm.c")
target_link_libraries("ZydisDisasm" "Zydis") target_link_libraries("ZydisDisasm" "Zydis")