1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
if(__add_lief_compiler_flags)
return()
endif()
set(__add_lief_compiler_flags ON)
function(append value)
foreach(variable ${ARGN})
set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
endforeach(variable)
endfunction()
function(append_if condition value)
if (${condition})
foreach(variable ${ARGN})
set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
endforeach(variable)
endif()
endfunction()
macro(ADD_FLAG_IF_SUPPORTED flag name)
CHECK_C_COMPILER_FLAG("${flag}" "C_SUPPORTS_${name}")
CHECK_CXX_COMPILER_FLAG("${flag}" "CXX_SUPPORTS_${name}")
if (C_SUPPORTS_${name})
target_compile_options(LIB_LIEF_STATIC PRIVATE ${flag})
target_compile_options(LIB_LIEF_SHARED PRIVATE ${flag})
endif()
if (CXX_SUPPORTS_${name})
target_compile_options(LIB_LIEF_STATIC PRIVATE ${flag})
target_compile_options(LIB_LIEF_SHARED PRIVATE ${flag})
endif()
endmacro()
if (MSVC)
add_definitions(-DNOMINMAX)
target_compile_options(LIB_LIEF_STATIC PUBLIC /FIiso646.h)
target_compile_options(LIB_LIEF_SHARED PUBLIC /FIiso646.h)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
target_compile_options(LIB_LIEF_STATIC PUBLIC /MTd)
else()
target_compile_options(LIB_LIEF_STATIC PUBLIC /MT)
endif()
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if (UNIX)
if (LIEF_FORCE32)
target_compile_options(LIB_LIEF_STATIC PRIVATE -m32)
target_compile_options(LIB_LIEF_SHARED PRIVATE -m32)
set_property(TARGET LIB_LIEF_STATIC LIB_LIEF_SHARED PROPERTY LINK_FLAGS -m32)
endif()
endif()
endif()
if (NOT MSVC)
ADD_FLAG_IF_SUPPORTED("-Wall" WALL)
ADD_FLAG_IF_SUPPORTED("-Wextra" WEXTRA)
ADD_FLAG_IF_SUPPORTED("-Wpedantic" WPEDANTIC)
ADD_FLAG_IF_SUPPORTED("-fno-stack-protector" NO_STACK_PROTECTOR)
ADD_FLAG_IF_SUPPORTED("-fomit-frame-pointer" OMIT_FRAME_POINTER)
ADD_FLAG_IF_SUPPORTED("-fno-strict-aliasing" NO_STRICT_ALIASING)
ADD_FLAG_IF_SUPPORTED("-fexceptions" EXCEPTION)
ADD_FLAG_IF_SUPPORTED("-fvisibility=hidden" VISIBILITY)
ADD_FLAG_IF_SUPPORTED("-Wno-expansion-to-defined" NO_EXPANSION_TO_DEFINED)
endif()
#ADD_FLAG_IF_SUPPORTED("-Wduplicated-cond" HAS_DUPLICATED_COND)
#ADD_FLAG_IF_SUPPORTED("-Wduplicated-branches" HAS_DUPLICATED_BRANCHES)
#ADD_FLAG_IF_SUPPORTED("-Wlogical-op" HAS_LOGICAL_OP)
#ADD_FLAG_IF_SUPPORTED("-Wshadow" HAS_SHADOW)
# =========================================
# MSVC FLAGS
# This part is inspired from LLVM:
# https://github.com/llvm-mirror/llvm/blob/a86576d8771c89502d239f0b85a1a6992020aa47/cmake/modules/HandleLLVMOptions.cmake
# =========================================
set(msvc_warning_flags
# Disabled warnings.
-wd4141 # Suppress ''modifier' : used more than once' (because of __forceinline combined with inline)
-wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
-wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored'
-wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
-wd4258 # Suppress ''var' : definition from the for loop is ignored; the definition from the enclosing scope is used'
-wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'
-wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception'
-wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized'
-wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized'
-wd4355 # Suppress ''this' : used in base member initializer list'
-wd4456 # Suppress 'declaration of 'var' hides local variable'
-wd4457 # Suppress 'declaration of 'var' hides function parameter'
-wd4458 # Suppress 'declaration of 'var' hides class member'
-wd4459 # Suppress 'declaration of 'var' hides global declaration'
-wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated'
-wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible'
-wd4722 # Suppress 'function' : destructor never returns, potential memory leak
-wd4800 # Suppress ''type' : forcing value to bool 'true' or 'false' (performance warning)'
-wd4100 # Suppress 'unreferenced formal parameter'
-wd4127 # Suppress 'conditional expression is constant'
-wd4512 # Suppress 'assignment operator could not be generated'
-wd4505 # Suppress 'unreferenced local function has been removed'
-wd4610 # Suppress '<class> can never be instantiated'
-wd4510 # Suppress 'default constructor could not be generated'
-wd4702 # Suppress 'unreachable code'
-wd4245 # Suppress 'signed/unsigned mismatch'
-wd4706 # Suppress 'assignment within conditional expression'
-wd4310 # Suppress 'cast truncates constant value'
-wd4701 # Suppress 'potentially uninitialized local variable'
-wd4703 # Suppress 'potentially uninitialized local pointer variable'
-wd4389 # Suppress 'signed/unsigned mismatch'
-wd4611 # Suppress 'interaction between '_setjmp' and C++ object destruction is non-portable'
-wd4805 # Suppress 'unsafe mix of type <type> and type <type> in operation'
-wd4204 # Suppress 'nonstandard extension used : non-constant aggregate initializer'
-wd4577 # Suppress 'noexcept used with no exception handling mode specified; termination on exception is not guaranteed'
-wd4091 # Suppress 'typedef: ignored on left of '' when no variable is declared'
# C4592 is disabled because of false positives in Visual Studio 2015
# Update 1. Re-evaluate the usefulness of this diagnostic with Update 2.
-wd4592 # Suppress ''var': symbol will be dynamically initialized (implementation limitation)
-wd4319 # Suppress ''operator' : zero extending 'type' to 'type' of greater size'
-wd4710 # Suppress 'function not inlined'
-wd4820 # Suppress 'XXX bytes padding added after data member'
-wd4514 # Suppress 'unreferenced inline function has been removed'
# Ideally, we'd like this warning to be enabled, but MSVC 2013 doesn't
# support the 'aligned' attribute in the way that clang sources requires (for
# any code that uses the LLVM_ALIGNAS macro), so this is must be disabled to
# avoid unwanted alignment warnings.
# When we switch to requiring a version of MSVC that supports the 'alignas'
# specifier (MSVC 2015?) this warning can be re-enabled.
-wd4324 # Suppress 'structure was padded due to __declspec(align())'
# Promoted warnings.
-w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
# Promoted warnings to errors.
-we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
)
if (MSVC)
set(msvc_warning_flags "/W4 ${msvc_warning_flags}")
string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
#foreach(flag ${msvc_warning_flags})
# target_compile_options(LIB_LIEF_STATIC PRIVATE ${flag})
# target_compile_options(LIB_LIEF_SHARED PRIVATE ${flag})
#endforeach(flag)
endif()
# Speed up MSVC build
if (MSVC_IDE)
set(LIEF_COMPILER_JOBS "0" CACHE STRING
"Number of parallel compiler jobs. 0 means use all processors. Default is 0.")
if (NOT LIEF_COMPILER_JOBS STREQUAL "1")
if(LIEF_COMPILER_JOBS STREQUAL "0")
message(STATUS "Number of parallel compiler jobs set to /MP")
add_definitions(/MP)
else()
message(STATUS "Number of parallel compiler jobs set to /MP${LIEF_COMPILER_JOBS}")
add_definitions(/MP${LIEF_COMPILER_JOBS})
endif()
else()
message(STATUS "Parallel compilation disabled")
endif()
endif()
|