Browse Source

Add CMake options to disable custom compiler flags

* SET_WARNING_FLAGS for warning options (don't affect binaries)
* SET_OPTIMIZATION_FLAGS for compiler & linker optimization options

Also, re-enable some warnings that were disabled and instead suppress
them for the individual occurences.
pull/2/head
Daniel Scharrer 14 years ago
parent
commit
a54b579cc3
  1. 76
      CMakeLists.txt
  2. 4
      README.md
  3. 21
      cmake/BuildType.cmake
  4. 28
      cmake/CompileCheck.cmake
  5. 2
      src/cli/main.cpp
  6. 2
      src/setup/file.cpp
  7. 2
      src/setup/type.cpp
  8. 2
      src/stream/block.cpp
  9. 9
      src/util/enum.hpp
  10. 12
      src/util/flags.hpp

76
CMakeLists.txt

@ -7,15 +7,16 @@ cmake_minimum_required(VERSION 2.8)
option(USE_LZMA "Build lzma decompression support." ON)
option(DEBUG_EXTRA "Expensive debug options" OFF)
option(SET_WARNING_FLAGS "Adjust compiler warning flags" ON)
option(SET_OPTIMIZATION_FLAGS "Adjust compiler optimization flags" ON)
set(MAN_DIR "share/man" CACHE STRING "Install location for man pages (relative to prefix).")
set(MAN_DIR "share/man" CACHE STRING "Install path for man pages (relative to prefix).")
mark_as_advanced(MAN_DIR)
# Helper scrips
# For custom cmake modules.
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") # For custom cmake modules
include(CompileCheck)
include(VersionString)
include(CheckSymbolExists)
@ -27,14 +28,16 @@ include(TestBigEndian)
# Find required libraries
# Force re-checking libraries if the compiler or compiler flags change.
# Force re-checking libraries if the compiler or compiler flags change
if((NOT LAST_CMAKE_CXX_FLAGS STREQUAL CMAKE_CXX_FLAGS)
OR (NOT LAST_CMAKE_CXX_COMPILER STREQUAL CMAKE_CXX_COMPILER))
force_recheck_library(LZMA)
force_recheck_library(Boost)
unset(Boost_INCLUDE_DIR CACHE)
set(LAST_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE INTERNAL "The last C++ compiler flags.")
set(LAST_CMAKE_CXX_COMPILER "${CMAKE_CXX_COMPILER}" CACHE INTERNAL "The last C++ compiler.")
set(LAST_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE INTERNAL
"The last C++ compiler flags")
set(LAST_CMAKE_CXX_COMPILER "${CMAKE_CXX_COMPILER}" CACHE INTERNAL
"The last C++ compiler")
endif()
unset(LIBRARIES)
@ -73,44 +76,53 @@ check_link_library(iconv iconv_LIBRARIES)
if(${Boost_VERSION} LESS 104800)
# Older Boost versions don't work with C++11
add_cxxflag("-std=c++03")
else()
add_cxxflag("-std=c++11")
endif()
add_cxxflag("-Wall")
add_cxxflag("-Wextra")
add_cxxflag("-Wformat=2")
add_cxxflag("-Wundef")
add_cxxflag("-Wpointer-arith")
add_cxxflag("-Wcast-qual")
add_cxxflag("-Woverloaded-virtual")
add_cxxflag("-Wlogical-op")
add_cxxflag("-Wliteral-conversion")
add_cxxflag("-Wshift-overflow")
add_cxxflag("-Woverflow")
add_cxxflag("-Wbool-conversions")
add_cxxflag("-Wconversion")
add_cxxflag("-Wsign-conversion")
add_cxxflag("-Wmissing-declarations")
add_cxxflag("-Wredundant-decls")
if(SET_WARNING_FLAGS)
# GCC (and compatible)
add_cxxflag("-Wall")
add_cxxflag("-Wextra")
add_cxxflag("-Wformat=2")
add_cxxflag("-Wundef")
add_cxxflag("-Wpointer-arith")
add_cxxflag("-Wcast-qual")
add_cxxflag("-Woverloaded-virtual")
add_cxxflag("-Wlogical-op")
add_cxxflag("-Woverflow")
add_cxxflag("-Wconversion")
add_cxxflag("-Wsign-conversion")
add_cxxflag("-Wmissing-declarations")
add_cxxflag("-Wredundant-decls")
# clang
add_cxxflag("-Wliteral-conversion")
add_cxxflag("-Wshift-overflow")
add_cxxflag("-Wbool-conversions")
# icc
if(NOT DEBUG_EXTRA)
add_cxxflag("-wd1418") # 'external function definition with no prior declaration'
endif()
endif()
if(DEBUG_EXTRA)
add_cxxflag("-ftrapv") # to add checks for (undefined) signed integer overflow
add_cxxflag("-fbounds-checking")
add_cxxflag("-fcatch-undefined-behavior")
add_cxxflag("-Wstrict-aliasing=1")
else()
# -Wuninitialized causes too many false positives - thanks very much, gcc
add_cxxflag("-Wno-uninitialized")
# (clang only) Conflicts with using const variables for configuration.
add_cxxflag("-Wno-constant-logical-operand")
add_cxxflag("-Wno-unneeded-internal-declaration")
add_cxxflag("-Wno-unused-function")
endif()
# Link as few libraries as possible
# This is much easier than trying to decide which libraries are needed for each system
add_ldflag("-Wl,--as-needed")
if(SET_OPTIMIZATION_FLAGS)
# Link as few libraries as possible
# This is much easier than trying to decide which libraries are needed for each system
# Specifically, then need for libboost_system depends on the Boost version
add_ldflag("-Wl,--as-needed")
endif()
# Check for optional functionality and system configuration

4
README.md

@ -43,7 +43,9 @@ Build options:
* `USE_LZMA` (default: `ON`): Use *liblzma* if available.
* `CMAKE_BUILD_TYPE` (default: `Release`): Set to `Debug` to enable debug output.
* `CMAKE_INSTALL_PREFIX` (default: `/usr/local` on UNIX): Where to install innoextract.
* `DEBUG_EXTRA` (default: `OFF`): Expensive debug options
* `SET_WARNING_FLAGS` (default: `ON`): Adjust compiler warning flags. This should not affect the produced binaries but is useful to catch potential problems.
* `SET_OPTIMIZATION_FLAGS` (default: `ON`): Adjust compiler optimization flags. For non-debug builds the only thing this does is instruct the linker to only link against libraries that are actually needed.
* `DEBUG_EXTRA` (default: `OFF`): Expensive debug options.
* `MAN_DIR` (default: `share/man`): Install location for man pages (relative to prefix).
Set options by passing `-D<option>=<value>` to cmake.

21
cmake/BuildType.cmake

@ -10,14 +10,21 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DDEBUG)
set(DEBUG 1)
check_compiler_flag(RESULT "-g3")
if(NOT RESULT STREQUAL "")
string(REGEX REPLACE "-g(|[0-9]|gdb)" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
if(SET_OPTIMIZATION_FLAGS)
# set debug symbol level to -g3
check_compiler_flag(RESULT "-g3")
if(NOT RESULT STREQUAL "")
string(REGEX REPLACE "-g(|[0-9]|gdb)" "" CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${RESULT}")
endif()
# disable optimizations
check_compiler_flag(RESULT "-O0")
string(REGEX REPLACE "-O[0-9]" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${RESULT}")
endif()
check_compiler_flag(RESULT "-O0")
string(REGEX REPLACE "-O[0-9]" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${RESULT}")
endif()

28
cmake/CompileCheck.cmake

@ -30,9 +30,11 @@ function(check_compiler_flag RESULT FLAG)
set(compile_test_file "${CMAKE_CURRENT_BINARY_DIR}/compile_flag_test.cpp")
file(WRITE ${compile_test_file} "__attribute__((const)) int main(){ return 0; }\n")
try_compile(CHECK_COMPILER_FLAG ${CMAKE_BINARY_DIR} ${compile_test_file} COMPILE_DEFINITIONS "${FLAG}" OUTPUT_VARIABLE ERRORLOG)
try_compile(CHECK_COMPILER_FLAG ${CMAKE_BINARY_DIR} ${compile_test_file}
COMPILE_DEFINITIONS "${FLAG}" OUTPUT_VARIABLE ERRORLOG)
string(REGEX MATCH "warning:" HAS_WARNING "${ERRORLOG}")
string(REGEX MATCH "warning:|command line warning|unrecognized option"
HAS_WARNING "${ERRORLOG}")
if(NOT CHECK_COMPILER_FLAG)
message(STATUS "Checking compiler flag: ${FLAG} - unsupported")
@ -69,18 +71,19 @@ function(add_ldflag FLAG)
endfunction(add_ldflag)
function(try_link_library LIBRARY_NAME LIBRARY_FILE ERROR_VAR)
# See if we can link a simple program with the library using the configured c++ compiler.
# See if we can link a simple program with the library using the configured c++ compiler
set(link_test_file "${CMAKE_CURRENT_BINARY_DIR}/link_test.cpp")
file(WRITE ${link_test_file} "int main(){}\n")
if(CMAKE_THREAD_LIBS_INIT)
list(APPEND LIBRARY_FILE "${CMAKE_THREAD_LIBS_INIT}")
endif()
try_compile(CHECK_${LIBRARY_NAME}_LINK "${CMAKE_BINARY_DIR}" "${link_test_file}" CMAKE_FLAGS "-DLINK_LIBRARIES=${LIBRARY_FILE}" OUTPUT_VARIABLE ERRORLOG)
try_compile(CHECK_${LIBRARY_NAME}_LINK "${CMAKE_BINARY_DIR}" "${link_test_file}"
CMAKE_FLAGS "-DLINK_LIBRARIES=${LIBRARY_FILE}" OUTPUT_VARIABLE ERRORLOG)
set(${ERROR_VAR} "${ERRORLOG}" PARENT_SCOPE)
endfunction(try_link_library)
##############################################################################
# Check that a a library actually works for the current configuration.
# Check that a a library actually works for the current configuration
function(check_link_library LIBRARY_NAME LIBRARY_VARIABLE)
set(lib_current "${${LIBRARY_VARIABLE}}")
@ -102,7 +105,7 @@ function(check_link_library LIBRARY_NAME LIBRARY_VARIABLE)
message(STATUS "Checking ${LIBRARY_NAME}: ${lib_current}")
endif()
# Check if we can link to the full path found by find_package.
# Check if we can link to the full path found by find_package
try_link_library(${LIBRARY_NAME} "${lib_current}" ERRORLOG1)
if(CHECK_${LIBRARY_NAME}_LINK)
@ -110,8 +113,9 @@ function(check_link_library LIBRARY_NAME LIBRARY_VARIABLE)
return()
endif()
# Check if the linker is smarter than cmake and try to link with only the library name.
string(REGEX REPLACE "(^|;)[^;]*/lib([^;/]*)\\.so" "\\1-l\\2" LIBRARY_FILE "${lib_current}")
# Check if the linker is smarter than cmake and try to link with only the library name
string(REGEX REPLACE "(^|;)[^;]*/lib([^;/]*)\\.so" "\\1-l\\2"
LIBRARY_FILE "${lib_current}")
if(NOT LIBRARY_FILE STREQUAL lib_current)
@ -126,11 +130,15 @@ function(check_link_library LIBRARY_NAME LIBRARY_VARIABLE)
endif()
# Force cmake to search again, as the cached library doesn't work.
# Force cmake to search again, as the cached library doesn't work
unset(FIND_PACKAGE_MESSAGE_DETAILS_${ARGV2} CACHE)
unset(FIND_PACKAGE_MESSAGE_DETAILS_${LIBRARY_NAME} CACHE)
message(FATAL_ERROR "\n${ERRORLOG1}\n\n${ERRORLOG2}\n\n!! No suitable version of ${LIBRARY_NAME} found.\n Maybe you don't have the right (32 vs.64 bit) architecture installed?\n\n Tried ${lib_current} and ${LIBRARY_FILE}\n Using compiler ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS}\n\n\n")
message(FATAL_ERROR "\n${ERRORLOG1}\n\n${ERRORLOG2}\n\n"
"!! No suitable version of ${LIBRARY_NAME} found.\n"
" Maybe you don't have the right (32 vs.64 bit) architecture installed?\n\n"
" Tried ${lib_current} and ${LIBRARY_FILE}\n"
" Using compiler ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS}\n\n\n")
endfunction(check_link_library)

2
src/cli/main.cpp

@ -207,7 +207,7 @@ static void process_file(const fs::path & file, const options & o) {
BOOST_FOREACH(const Chunks::value_type & chunk, chunks) {
debug("[starting " << chunk.first.compression << " chunk @ " << chunk.first.first_slice
debug("[starting " << chunk.first.compression << " chunk @ slice " << chunk.first.first_slice
<< " + " << print_hex(offsets.data_offset) << " + " << print_hex(chunk.first.offset)
<< ']');

2
src/setup/file.cpp

@ -72,7 +72,7 @@ namespace setup {
void file_entry::load(std::istream & is, const version & version) {
(void)enum_names<file_copy_mode>::names;
USE_ENUM_NAMES(file_copy_mode)
options = 0;

2
src/setup/type.cpp

@ -53,6 +53,8 @@ namespace setup {
void type_entry::load(std::istream & is, const version & version) {
USE_FLAG_NAMES(setup::type_flags)
is >> encoded_string(name, version.codepage());
is >> encoded_string(description, version.codepage());
if(version >= INNO_VERSION(4, 0, 1)) {

2
src/stream/block.cpp

@ -154,7 +154,7 @@ namespace stream {
block_reader::pointer block_reader::get(std::istream & base, const setup::version & version) {
(void)enum_names<block_compression>::name;
USE_ENUM_NAMES(block_compression)
uint32_t expected_checksum = load_number<uint32_t>(base);
crypto::crc32 actual_checksum;

9
src/util/enum.hpp

@ -68,6 +68,15 @@ struct enum_names {
const size_t enum_names<get_enum<Enum>::type>::count \
= ARRAY_SIZE(enum_names<get_enum<Enum>::type>::names);
#define USE_ENUM_NAMES(Enum) \
(void)enum_names<get_enum<Enum>::type>::count; \
(void)enum_names<get_enum<Enum>::type>::name; \
(void)enum_names<get_enum<Enum>::type>::names;
#define USE_FLAG_NAMES(Flags) \
USE_FLAGS_OVERLOADS(Flags) \
USE_ENUM_NAMES(Flags)
template <class Enum>
typename boost::enable_if_c<enum_names<Enum>::named, std::ostream &>::type
operator<<(std::ostream & os, Enum value) {

12
src/util/flags.hpp

@ -148,6 +148,11 @@ public:
};
template <typename Enum, size_t Bits>
inline flags<Enum, Bits> operator|(Enum a, flags<Enum, Bits> b) {
return b | a;
}
#define DECLARE_ENUM_SIZE(Enum, Size) \
template <> \
struct enum_size<Enum> { \
@ -165,9 +170,6 @@ public:
inline Flagname operator|(Flagname::enum_type a, Flagname::enum_type b) { \
return Flagname(a) | b; \
} \
inline Flagname operator|(Flagname::enum_type a, Flagname b) { \
return b | a; \
} \
inline Flagname operator~(Flagname::enum_type a) { \
return ~Flagname(a); \
}
@ -197,4 +199,8 @@ public:
DECLARE_ENUM_SIZE(FLAGS_ENUM(Flagname), FLAGS_ENUM_END(Flagname)) \
DECLARE_FLAGS_OPERATORS(Flagname)
#define USE_FLAGS_OVERLOADS(Flagname) \
(void)(~Flagname::enum_type(0)); \
(void)(Flagname::enum_type(0) | Flagname::enum_type(0));
#endif // INNOEXTRACT_UTIL_FLAGS_HPP

Loading…
Cancel
Save