From c926ad7e417bfbb6ce8b7a2f55ab9075daee4d7f Mon Sep 17 00:00:00 2001
From: Torsten Maehne <Torsten.Maehne@gmx.de>
Date: Mon, 4 Nov 2013 11:25:23 +0100
Subject: [PATCH] Fix CMake PortGroup, which modification due to ticket #40648
 broke other ports.

Unfortunately, the CMake man pages and
<http://www.cmake.org/cmake/help/v2.8.12/cmake.html> do not mention at
all that the environment variables CFLAGS, CXXFLAGS, and LDFLAGS get
honored by CMake upon its first call in a new build directory. The
book "Mastering CMake" (4th edition) by Ken Martin and Bill Hoffmann
mentions it only briefly in Section 2.7 "Specifying the Compiler to
CMake" and the end of Appendix A.

Therefore, the previously proposed modification of the CMake PortGroup
to honor configure.optflags and configure.cppflags proposed in
MacPorts ticket #40648 can be simplified as other language-specific
flags were already forwarded correctly to CMake via the above
environment variables.

This should also fix the observed compilation problems of other ports
using CMake to configure their sources for the build phase.
---
 ports/_resources/port1.0/group/cmake-1.0.tcl | 88 ++++++++++------------------
 1 file changed, 32 insertions(+), 56 deletions(-)

diff --git a/ports/_resources/port1.0/group/cmake-1.0.tcl b/ports/_resources/port1.0/group/cmake-1.0.tcl
index 085a820..6571141 100644
--- a/ports/_resources/port1.0/group/cmake-1.0.tcl
+++ b/ports/_resources/port1.0/group/cmake-1.0.tcl
@@ -56,54 +56,16 @@ configure.args      -DCMAKE_VERBOSE_MAKEFILE=ON \
                     -DCMAKE_FIND_FRAMEWORK=LAST \
                     -Wno-dev
 
-# Handle configure.cppflags, configure.optflags, configure.cflags,
-# configure.cxxflags, configure.ldflags by setting the equivalent CMAKE_*_FLAGS.
-#
-# Be aware that a CMake script can always override these flags when it runs, as
-# they are frequently set internally in function of other CMake build variables!
+# CMake honors set environment variables CFLAGS, CXXFLAGS, and LDFLAGS when it
+# is first run in a build directory to initialize CMAKE_C_FLAGS,
+# CMAKE_CXX_FLAGS, CMAKE_[EXE|SHARED|MODULE]_LINKER_FLAGS. However, be aware
+# that a CMake script can always override these flags when it runs, as they
+# are frequently set internally in function of other CMake build variables!
 #
 # Attention: If you want to be sure that no compiler flags are passed via
-# configure.args, you have to set manually configure.optflags to "", as it is by
-# default "-O2" and added to all language-specific flags. If you want to turn off
-# optimization, explicitly set configue.optflags to "-O0".
-if {${configure.cppflags} != ""} {
-    # Add the preprocessor flags to the C/C++ compiler flags as CMake does not
-    # honor separately CPPFLAGS (it uses usually add_definitions() for that).
-    # We use the compiler flags for all build types, as they are usually empty.
-    # Cf. also to CMake upstream ticket #12928 "CMake silently ignores CPPFLAGS"
-    # <http://www.cmake.org/Bug/view.php?id=12928>.
-    configure.args-append -DCMAKE_C_FLAGS="${configure.cppflags}"
-    configure.args-append -DCMAKE_CXX_FLAGS="${configure.cppflags}"
-}
-if {${configure.cflags} != ""} {
-    # The configure.cflags contain configure.optflags by default. Therefore, we
-    # set the Release flags, which would otherwise overrule the optimization
-    # flags, as they are set by default to "-O3 -NDEBUG". Therefore, be sure
-    # to add "-NDEBUG" to the configure.cflags if you want to turn off
-    # assertions in release builds!
-    configure.args-append -DCMAKE_C_FLAGS_RELEASE="${configure.cflags}"
-}
-set cxx_stdlibflags {}
-if {[info exists configure.cxx_stdlib] &&
-    ${configure.cxx_stdlib} ne {} &&
-    [string match *clang* ${configure.cxx}]} {
-    set cxx_stdlibflags -stdlib=${configure.cxx_stdlib}
-}
-if {${configure.cxxflags} != "" || ${cxx_stdlibflags} != ""} {
-    # The configure.cxxflags contain configure.optflags by default. Therefore,
-    # we set the Release flags, which would otherwise overrule the optimization
-    # flags, as they are set by default to "-O3 -NDEBUG". Therefore, be sure
-    # to add "-NDEBUG" to the configure.cflags if you want to turn off
-    # assertions in release builds!
-    configure.args-append -DCMAKE_CXX_FLAGS_RELEASE="${configure.cxxflags} ${cxx_stdlibflags}"
-}
-if {${configure.ldflags} != ""} {
-    # CMake supports individual linker flags for executables, modules, and dlls.
-    # By default, they are empty.
-    configure.args-append -DCMAKE_EXE_LINKER_FLAGS="${configure.ldflags}"
-    configure.args-append -DCMAKE_SHARED_LINKER_FLAGS="${configure.ldflags}"
-    configure.args-append -DCMAKE_MODULE_LINKER_FLAGS="${configure.ldflags}"
-}
+# configure.args, you have to set manually configure.optflags to "", as it is
+# by default "-O2" and added to all language-specific flags. If you want to
+# turn off optimization, explicitly set configue.optflags to "-O0".
 
 # TODO: Handle configure.objcflags (cf. to CMake upstream ticket #4756
 #       "CMake needs an Objective-C equivalent of CMAKE_CXX_FLAGS"
@@ -114,8 +76,30 @@ if {${configure.ldflags} != ""} {
 
 # TODO: Handle the Java-specific configure.classpath variable.
 
-platform darwin {
-    pre-configure {
+pre-configure {
+    # The environment variable CPPFLAGS is not considered by CMake.
+    # (CMake upstream ticket #12928 "CMake silently ignores CPPFLAGS"
+    # <http://www.cmake.org/Bug/view.php?id=12928>).
+    # Thus, we have to add them manually to the CFLAGS and CXXFLAGS in the
+    # pre-configure phase.
+    if {${configure.cppflags} != ""} {
+        configure.cflags-append ${configure.cppflags}
+        configure.cxxflags-append ${configure.cppflags}
+    }
+
+    # In addition, CMake provides build-type-specific flags for
+    # Release (-O3 -DNDEBUG), Debug (-g), MinSizeRel (-Os -DNDEBUG), and
+    # RelWithDebInfo (-O2 -g -DNDEBUG). If the configure.optflags have been
+    # set (-O2 by default), we have to remove the optimization flags from the
+    # from the concerned Release build type so that configure.optflags
+    # gets honored (Debug used by the +debug variant does not set
+    # optimization flags by default).
+    if {${configure.optflags} != ""} {
+        configure.args-append -DCMAKE_C_FLAGS_RELEASE="-NDEBUG" \
+                              -DCMAKE_CXX_FLAGS_RELEASE="-NDEBUG"
+    }
+
+    platform darwin {
         if {[variant_exists universal] && [variant_isset universal]} {
             if {[info exists universal_archs_supported]} {
                 global merger_configure_args
@@ -144,14 +128,6 @@ configure.universal_args-delete --disable-dependency-tracking
 variant debug description "Enable debug binaries" {
     configure.args-delete   -DCMAKE_BUILD_TYPE=Release
     configure.args-append   -DCMAKE_BUILD_TYPE=Debug
-    # Consider the configure.cflags and configure.cxxflags for Debug builds.
-    # Attention, they contain configure.optflags by default!
-    if {${configure.cflags} != ""} {
-        configure.args-append -DCMAKE_C_FLAGS_DEBUG="-g ${configure.cflags}"
-    }
-    if {${configure.cxxflags} != "" || ${cxx_stdlibflags} != ""} {
-        configure.args-append -DCMAKE_CXX_FLAGS_DEBUG="-g ${configure.cxxflags} ${cxx_stdlibflags}"
-    }
 }
 
 # cmake doesn't like --enable-debug, so in case a portfile sets
-- 
1.8.4.1

