Hi,

A small heads up for a CMake change I'll be committing in a short while,
the patch is attached.

I've been fiddling with the way CMake defines the compiler flags and
have a patch to change their implementation.

At the moment the flags are determined on the first run and stored in
CMAKE_CXX_FLAGS, CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_RELEASE[*].
This was actuall not a good idea, since CMake combines CMAKE_CXX_FLAGS
with the CMAKE_CXX_FLAGS_<BUILD_TYPE>. The problem with this setup is
when the compiler is changed the flags are reset to their defaults and
the user made changes to the flags are lost. The second issue with the
old implementation is that the ENABLE_STRICT_COMPILATION CMake option
must be directly set; changing its value after the initial generation
had no effect. 

The change will only set CMAKE_CXX_FLAGS, based on the environment
variable $CXXFLAGS, this is stored in its own variable, named
CXX_FLAGS_USER, in CMake and can be changed later. This flag is only
initialised on the first run. It is also based on the default flags we
use for Wesnoth and the _current_ state of the ENABLE_STRICT_COMPILATION
option. For the transition from the old to the new system the old
CMAKE_CXX_FLAGS are used as initial value for the CXX_FLAGS_USER. This
means no flags are lost, but some extra flags are added, will CMake to
cause a recompilation. So it's advised to regenerate the project files
or change the CXX_FLAGS_USER after CMake as updated the flags.

Since several people seem to be interested in the extra compiler flags I
use I'll add a configuration option ENABLE_PENDANTIC_COMPILATION. This
option will be disabled by default. I only tested these flags with
GCC-4.3 and GCC-4.4, before committing them I'll also run them along
GCC-4.6, GCC-4.7, Clang 3.1 and the current GCC snapshot.

[*] Note I removed the CFlags since all C based code has been removed,
with the removal of the poolallocator.

-- 
Regards,
Mark de Wever aka Mordante/SkeletonCrew
Index: INSTALL
===================================================================
--- INSTALL     (revision 54608)
+++ INSTALL     (working copy)
@@ -277,3 +277,23 @@
 
 Debug builds:
 Set CMAKE_BUILD_TYPE to "debug"
+
+Compiler flags:
+CMake determines the compiler flags by combining CMAKE_CXX_FLAGS and
+CMAKE_CXX_FLAGS_<CMAKE_BUILD_TYPE>. If no CMAKE_BUILD_TYPE is specified only 
the
+CMAKE_CXX_FLAGS are used.
+
+The CMAKE_CXX_FLAGS are controlled by CMake and should not be set by the user.
+They are generated by the following parts:
+- CXX_FLAGS_PROJECT The default flags for all programs in the Wesnoth. These
+  flags are determined by the Wesnoth developers.
+- CXX_FLAGS_STRICT_COMPILATION The flags used for strict compilation. Whether
+  these flags are used depends on the configuration option
+  ENABLE_STRICT_COMPILATION.  When this option is changed the CMAKE_CXX_FLAGS
+  will be changed to reflect the change. (Starting from Wesnoth 1.11.0 the flag
+  can be changed after the initial generation.) What flags are set when 
choosing
+  this option is determined by the Wesnoth developers.
+- CXX_FLAGS_USER These flags set when configuring Wesnoth. The initial value of
+  this variable depends on the CXXFLAGS in the environment or defined by
+  -DCXX_FLAGS_USER. These flags are stored and can be changed later by running
+  ccmake and changing the value of CXX_FLAGS_USER.
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt      (revision 54608)
+++ CMakeLists.txt      (working copy)
@@ -117,6 +117,8 @@
 # Handle options (set paths/definitions/etc...)
 #
 
+##### Set the compiler flags.
+
 # This macro checks whether a compiler supports a compiler flag.
 #
 # If the flag is supported the flag will be added to the target compiler flags.
@@ -144,77 +146,124 @@
        endif(${variable})
 endmacro(check_compiler_has_flag)
 
-# If UNIX Assume Clang.
-if(CMAKE_COMPILER_IS_GNUCXX OR UNIX)
-       # Set our own default flags at first run.
-       if(NOT CONFIGURED)
 
-               if(ENABLE_STRICT_COMPILATION)
-                       # The current unit test code breaks strict aliasing 
with g++ 4.4.
-                       set(STRICT_FLAGS "-Werror -Wno-strict-aliasing")
+### Set the environment compiler flags.
 
-                       # This flag is/will be added in gcc-4.8 and fails with 
BOOST_STATIC_ASSERT
-                       check_compiler_has_flag(
-                                       STRICT_FLAGS
-                                       "-Wunused-local-typedefs"
-                                       HAS_COMPILER_FLAG_WUNUSED_LOCAL_TYPEDEFS
-                                       "-Wno-unused-local-typedefs")
+if(CONFIGURED)
+       # The CONFIGURED flag was replaced when trunk `was' 1.11, before the 
release of 1.11.0
+       message("Builed files depending on 'CONFIGURED' found, please 
regenerate your build files.")
+       set(CXX_FLAGS_USER
+               "${CMAKE_CXX_FLAGS}"
+               CACHE
+               STRING
+               "The CXXFLAGS environment variable used for the initial 
generation."
+               FORCE
+       )
+       unset(CONFIGURED CACHE)
+endif(CONFIGURED)
 
-                       # This flag is/will be added in gcc-4.8 and fails with 
png in C++11 mode
-                       check_compiler_has_flag(
-                                       STRICT_FLAGS
-                                       "-Wliteral-suffix"
-                                       HAS_COMPILER_FLAG_WLITERAL_SUFFIX
-                                       "-Wno-literal-suffix")
+if(NOT DEFINED CXX_FLAGS_USER)
 
-                       # This flag is needed for Clang to silence its 
complains about
-                       # unused command line arguments.
-                       check_compiler_has_flag(
-                                       STRICT_FLAGS
-                                       "-Qunused-arguments"
-                                       HAS_COMPILER_FLAG_QUNUSED_ARGUMENTS)
+       message(STATUS "Environment compiler flags set to »${CXX_FLAGS_USER}«")
+       set(CXX_FLAGS_USER
+               "$ENV{CXXFLAGS}"
+               CACHE
+               STRING
+               "The CXXFLAGS environment variable used for the initial 
generation."
+               FORCE
+       )
 
-                       # Silences Clang warnings about declaring a class a 
class first and
-                       # a struct later.
-                       check_compiler_has_flag(
-                                       STRICT_FLAGS
-                                       "-Wmismatched-tags"
-                                       HAS_COMPILER_FLAG_WMISMATCHED_TAGS
-                                       "-Wno-mismatched-tags")
+endif(NOT DEFINED CXX_FLAGS_USER)
 
-                       if(NOT CMAKE_COMPILER_IS_GNUCXX)
-                               # Silences warnings about overloaded virtuals.
-                               # (GCC doesn't complaing Clang does.)
-                               check_compiler_has_flag(
-                                               STRICT_FLAGS
-                                               "-Woverloaded-virtual"
-                                               
HAS_COMPILER_FLAG_WOVERLOADED_VIRTUAL
-                                               "-Wno-overloaded-virtual")
-                       endif(NOT CMAKE_COMPILER_IS_GNUCXX)
 
-               else(ENABLE_STRICT_COMPILATION)
-                       set(STRICT_FLAGS "")
-               endif(ENABLE_STRICT_COMPILATION)
+### Set default Wesnoth project compiler flags
 
-               # Strict compilation for C files is disabled until somebody 
wants to clean them.
-               set(CMAKE_C_FLAGS "-O2 -W -Wall -ansi $ENV{CFLAGS}"
-                               CACHE STRING "Flags used by the C compiler 
during normal builds." FORCE)
-               set(CMAKE_C_FLAGS_DEBUG "-O0 -DDEBUG -ggdb3 -W -Wall -ansi 
$ENV{CFLAGS}"
-                               CACHE STRING "Flags used by the C compiler 
during debug builds." FORCE)
-               set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG -W -Wall -ansi 
$ENV{CFLAGS} -Wno-unused"
-                               CACHE STRING "Flags used by the C compiler 
during release builds." FORCE)
+set(CXX_FLAGS_PROJECT)
+check_compiler_has_flag(CXX_FLAGS_PROJECT "-std=c++98" HAS_COMPILER_FLAG_STD)
+check_compiler_has_flag(CXX_FLAGS_PROJECT "-W" HAS_COMPILER_FLAG_W)
+check_compiler_has_flag(CXX_FLAGS_PROJECT "-Wall" HAS_COMPILER_FLAG_WALL)
 
-               set(CMAKE_CXX_FLAGS "-O2 -W -Wall -std=c++98 ${STRICT_FLAGS} 
$ENV{CXXFLAGS}"
-                               CACHE STRING "Flags used by the CXX compiler 
during normal builds." FORCE)
-               set(CMAKE_CXX_FLAGS_DEBUG "-O0 -DDEBUG -ggdb3 -W -Wall 
-std=c++98 ${STRICT_FLAGS} $ENV{CXXFLAGS}"
-                               CACHE STRING "Flags used by the CXX compiler 
during debug builds." FORCE)
-               set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -W -Wall -std=c++98 
${STRICT_FLAGS} $ENV{CXXFLAGS} -Wno-unused"
-                               CACHE STRING "Flags used by the CXX compiler 
during release builds." FORCE)
 
-       endif(NOT CONFIGURED)
+### Set strict compilern flags.
 
-endif(CMAKE_COMPILER_IS_GNUCXX OR UNIX)
+set(CXX_FLAGS_STRICT_COMPILATION)
+check_compiler_has_flag(
+       CXX_FLAGS_STRICT_COMPILATION
+       "-Werror"
+       HAS_COMPILER_FLAG_WERROR
+)
 
+# The current unit test code breaks strict aliasing with g++ 4.4.
+check_compiler_has_flag(
+       CXX_FLAGS_STRICT_COMPILATION
+       "-Wstrict-aliasing"
+       HAS_COMPILER_FLAG_WERROR_STRICT_ALIASING
+       "-Wno-strict-aliasing"
+)
+
+# This flag is/will be added in gcc-4.8 and fails with BOOST_STATIC_ASSERT
+check_compiler_has_flag(
+       CXX_FLAGS_STRICT_COMPILATION
+       "-Wunused-local-typedefs"
+       HAS_COMPILER_FLAG_WUNUSED_LOCAL_TYPEDEFS
+       "-Wno-unused-local-typedefs"
+)
+
+# This flag is/will be added in gcc-4.8 and fails with png in C++11 mode
+check_compiler_has_flag(
+       CXX_FLAGS_STRICT_COMPILATION
+       "-Wliteral-suffix"
+       HAS_COMPILER_FLAG_WLITERAL_SUFFIX
+       "-Wno-literal-suffix"
+)
+
+# This removes a lot of warnings from Clang regarding unused -I arguments
+check_compiler_has_flag(
+       CXX_FLAGS_STRICT_COMPILATION
+       "-Qunused-arguments"
+       HAS_COMPILER_FLAG_QUNUSED_ARGUMENTS
+)
+
+# Silences Clang warnings about declaring a class a class first and
+# a struct later.
+check_compiler_has_flag(
+       CXX_FLAGS_STRICT_COMPILATION
+       "-Wmismatched-tags"
+       HAS_COMPILER_FLAG_WMISMATCHED_TAGS
+       "-Wno-mismatched-tags"
+)
+
+if(NOT CMAKE_COMPILER_IS_GNUCXX)
+       # Silences warnings about overloaded virtuals.
+       # (GCC doesn't complain Clang does.)
+       check_compiler_has_flag(
+               CXX_FLAGS_STRICT_COMPILATION
+               "-Woverloaded-virtual"
+               HAS_COMPILER_FLAG_WOVERLOADED_VIRTUAL
+               "-Wno-overloaded-virtual"
+       )
+endif(NOT CMAKE_COMPILER_IS_GNUCXX)
+
+
+### Set the final compiler flags.
+
+set(COMPILER_FLAGS "${CXX_FLAGS_PROJECT}")
+if(ENABLE_STRICT_COMPILATION)
+       set(COMPILER_FLAGS "${COMPILER_FLAGS} ${CXX_FLAGS_STRICT_COMPILATION}")
+endif(ENABLE_STRICT_COMPILATION)
+set(COMPILER_FLAGS "${COMPILER_FLAGS} ${CXX_FLAGS_USER}")
+
+if(NOT "${CMAKE_CXX_FLAGS}" STREQUAL "${COMPILER_FLAGS}")
+       message(STATUS "CMake compiler flags set to »${COMPILER_FLAGS}«") 
+       set(CMAKE_CXX_FLAGS
+               "${COMPILER_FLAGS}"
+               CACHE
+               STRING
+               "Global flags used by the CXX compiler during all builds."
+               FORCE
+       )
+endif(NOT "${CMAKE_CXX_FLAGS}" STREQUAL "${COMPILER_FLAGS}")
+
 if(UNIX AND NOT CMAKE_COMPILER_IS_GNUCXX)
        # Assume the compiler is the clang compiler.
        set(CMAKE_EXE_LINKER_FLAGS "-lstdc++ -lm ${CMAKE_EXE_LINKER_FLAGS}")
@@ -406,5 +455,3 @@
 include(CPack)
 set(CPACK_GENERATOR "TGZ")
 set(CPACK_SOURCE_GENERATOR "TGZ")
-
-set(CONFIGURED ON CACHE INTERNAL "")
Index: RELEASE_NOTES
===================================================================
--- RELEASE_NOTES       (revision 54608)
+++ RELEASE_NOTES       (working copy)
@@ -33,6 +33,10 @@
 The Clang compiler now also defaults to strict compilation when building with 
CMake. Also the default flags have changed to the ones used for GCC. This has 
only be tested with Clang 3.1, please file bug reports if it breaks older 
versions of Clang.
 [/section]
 
+[section="CMake compiler flags"]
+The part to determine the CMake compiler flags has been rewritten and now has 
better support for compiler specific enabling of targets. Therefore it is 
recommended to regenerate your CMake build.
+[/section]
+
 [section="Another Change"]
 [/section]
 
_______________________________________________
Wesnoth-dev mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-dev

Reply via email to