Support alternative namespaces in GFLAGS_NAMESPACE CMake variable.
authorAndreas Schuh <andreas.schuh.84@gmail.com>
Fri, 2 May 2014 13:56:58 +0000 (14:56 +0100)
committerAndreas Schuh <andreas.schuh.84@gmail.com>
Fri, 2 May 2014 13:56:58 +0000 (14:56 +0100)
The first element in the GFLAGS_NAMESPACE list is used as primary/default namespace. The symbols are then imported from this primary namespace into each of the other alternative namespaces with the using keyword. This is in particular used to maintain backwards compatibility with previous gflags library versions that used the "google" namespace instead of the new default "gflags" namespace.

CMakeLists.txt
cmake/utils.cmake
src/gflags.h.in
src/gflags_ns.h.in [new file with mode: 0644]

index ca2c1df..1e55bb0 100644 (file)
@@ -26,15 +26,19 @@ set (PACKAGE_SOVERSION "${PACKAGE_VERSION_MAJOR}")
 
 # ----------------------------------------------------------------------------
 # options
-set (GFLAGS_NAMESPACE   "${PACKAGE_NAME}" CACHE STRING "C++ namespace identifier of gflags library.")
-set (GFLAGS_INCLUDE_DIR "${PACKAGE_NAME}" CACHE STRING "Include subdirectory of gflags header files.")
-
-if (IS_ABSOLUTE GFLAGS_INCLUDE_DIR)
-  message (FATAL_ERROR "GFLAGS_INCLUDE_DIR must be a path relative to CMAKE_INSTALL_PREFIX/include")
-endif ()
-if (GFLAGS_INCLUDE_DIR MATCHES "^\\.\\.[/\\]")
-  message (FATAL_ERROR "GFLAGS_INCLUDE_DIR must not start with parent directory reference (../)")
+set (GFLAGS_NAMESPACE "${PACKAGE_NAME};google" CACHE STRING "C++ namespace identifier(s) of gflags library.")
+set (GFLAGS_NAMESPACE_SECONDARY "${GFLAGS_NAMESPACE}")
+list (REMOVE_DUPLICATES GFLAGS_NAMESPACE_SECONDARY)
+if (NOT GFLAGS_NAMESPACE_SECONDARY)
+  message (FATAL_ERROR "GFLAGS_NAMESPACE must be set to one (or more) valid C++ namespace identifier(s separated by semicolon \";\").")
 endif ()
+foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
+  if (NOT ns MATCHES "^[a-zA-Z][a-zA-Z0-9_]*$")
+    message (FATAL_ERROR "GFLAGS_NAMESPACE contains invalid namespace identifier: ${ns}")
+  endif ()
+endforeach ()
+list (GET       GFLAGS_NAMESPACE_SECONDARY 0 GFLAGS_NAMESPACE)
+list (REMOVE_AT GFLAGS_NAMESPACE_SECONDARY 0)
 
 option (BUILD_SHARED_LIBS          "Request build of shared libraries."                                       OFF)
 option (BUILD_STATIC_LIBS          "Request build of static libraries (default if BUILD_SHARED_LIBS is OFF)." OFF)
@@ -48,7 +52,6 @@ option (INSTALL_HEADERS            "Request packaging of headers and other devel
 mark_as_advanced (CLEAR CMAKE_INSTALL_PREFIX)
 mark_as_advanced (CMAKE_CONFIGURATION_TYPES
                   GFLAGS_NAMESPACE
-                  GFLAGS_INCLUDE_DIR
                   BUILD_STATIC_LIBS
                   BUILD_NC_TESTS
                   INSTALL_HEADERS)
@@ -69,6 +72,17 @@ if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CXX_FLAGS)
   set_property (CACHE CMAKE_BUILD_TYPE PROPERTY VALUE Release)
 endif ()
 
+if (NOT GFLAGS_INCLUDE_DIR)
+  set (GFLAGS_INCLUDE_DIR "${GFLAGS_NAMESPACE}")
+else ()
+  if (IS_ABSOLUTE GFLAGS_INCLUDE_DIR)
+    message (FATAL_ERROR "GFLAGS_INCLUDE_DIR must be a path relative to CMAKE_INSTALL_PREFIX/include")
+  endif ()
+  if (GFLAGS_INCLUDE_DIR MATCHES "^\\.\\.[/\\]")
+    message (FATAL_ERROR "GFLAGS_INCLUDE_DIR must not start with parent directory reference (../)")
+  endif ()
+endif ()
+
 # ----------------------------------------------------------------------------
 # system checks
 include (CheckTypeSize)
@@ -183,6 +197,19 @@ set (PUBLIC_HDRS
   "gflags_completions.h"
 )
 
+if (GFLAGS_NAMESPACE_SECONDARY)
+  set (INCLUDE_GFLAGS_NS_H "// Import gflags library symbols into alternative/deprecated namespace(s)")
+  foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
+    string (TOUPPER "${ns}" NS)
+    set (gflags_ns_h "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/gflags_${ns}.h")
+    configure_file ("${PROJECT_SOURCE_DIR}/src/gflags_ns.h.in" "${gflags_ns_h}" @ONLY)
+    list (APPEND PUBLIC_HDRS "${gflags_ns_h}")
+    set (INCLUDE_GFLAGS_NS_H "${INCLUDE_GFLAGS_NS_H}\n#include \"gflags_${ns}.h\"")
+  endforeach ()
+else ()
+  set (INCLUDE_GFLAGS_NS_H)
+endif ()
+
 set (PRIVATE_HDRS
   "config.h"
   "util.h"
index 97c9a66..9cef463 100644 (file)
@@ -46,7 +46,9 @@ endfunction ()
 function (configure_headers out)
   set (tmp)
   foreach (src IN LISTS ARGN)
-    if (EXISTS "${PROJECT_SOURCE_DIR}/src/${src}.in")
+    if (IS_ABSOLUTE "${src}")
+      list (APPEND tmp "${src}")
+    elseif (EXISTS "${PROJECT_SOURCE_DIR}/src/${src}.in")
       configure_file ("${PROJECT_SOURCE_DIR}/src/${src}.in" "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/${src}" @ONLY)
       list (APPEND tmp "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/${src}")
     else ()
index 9ee7738..0324d39 100644 (file)
@@ -565,4 +565,8 @@ inline clstring* dont_pass0toDEFINE_string(char *stringspot,
 
 #endif  // SWIG
 
+
+@INCLUDE_GFLAGS_NS_H@
+
+
 #endif  // GFLAGS_GFLAGS_H_
diff --git a/src/gflags_ns.h.in b/src/gflags_ns.h.in
new file mode 100644 (file)
index 0000000..1bb2038
--- /dev/null
@@ -0,0 +1,101 @@
+// Copyright (c) 2014, Andreas Schuh
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//     * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// -----------------------------------------------------------------------------
+// Imports the gflags library symbols into an alternative/deprecated namespace.
+
+#ifndef GFLAGS_GFLAGS_H_
+#  error The internal header gflags_@ns@.h may only be included by gflags.h
+#endif
+
+#ifndef GFLAGS_@NS@_H_
+#define GFLAGS_@NS@_H_
+
+
+namespace @ns@ {
+
+
+using GFLAGS_NAMESPACE::int32;
+using GFLAGS_NAMESPACE::uint32;
+using GFLAGS_NAMESPACE::int64;
+using GFLAGS_NAMESPACE::uint64;
+
+using GFLAGS_NAMESPACE::RegisterFlagValidator;
+using GFLAGS_NAMESPACE::CommandLineFlagInfo;
+using GFLAGS_NAMESPACE::GetAllFlags;
+using GFLAGS_NAMESPACE::ShowUsageWithFlags;
+using GFLAGS_NAMESPACE::ShowUsageWithFlagsRestrict;
+using GFLAGS_NAMESPACE::DescribeOneFlag;
+using GFLAGS_NAMESPACE::SetArgv;
+using GFLAGS_NAMESPACE::GetArgvs;
+using GFLAGS_NAMESPACE::GetArgv;
+using GFLAGS_NAMESPACE::GetArgv0;
+using GFLAGS_NAMESPACE::GetArgvSum;
+using GFLAGS_NAMESPACE::ProgramInvocationName;
+using GFLAGS_NAMESPACE::ProgramInvocationShortName;
+using GFLAGS_NAMESPACE::ProgramUsage;
+using GFLAGS_NAMESPACE::VersionString;
+using GFLAGS_NAMESPACE::GetCommandLineOption;
+using GFLAGS_NAMESPACE::GetCommandLineFlagInfo;
+using GFLAGS_NAMESPACE::GetCommandLineFlagInfoOrDie;
+using GFLAGS_NAMESPACE::FlagSettingMode;
+using GFLAGS_NAMESPACE::SET_FLAGS_VALUE;
+using GFLAGS_NAMESPACE::SET_FLAG_IF_DEFAULT;
+using GFLAGS_NAMESPACE::SET_FLAGS_DEFAULT;
+using GFLAGS_NAMESPACE::SetCommandLineOption;
+using GFLAGS_NAMESPACE::SetCommandLineOptionWithMode;
+using GFLAGS_NAMESPACE::FlagSaver;
+using GFLAGS_NAMESPACE::CommandlineFlagsIntoString;
+using GFLAGS_NAMESPACE::ReadFlagsFromString;
+using GFLAGS_NAMESPACE::AppendFlagsIntoFile;
+using GFLAGS_NAMESPACE::ReadFromFlagsFile;
+using GFLAGS_NAMESPACE::BoolFromEnv;
+using GFLAGS_NAMESPACE::Int32FromEnv;
+using GFLAGS_NAMESPACE::Int64FromEnv;
+using GFLAGS_NAMESPACE::Uint64FromEnv;
+using GFLAGS_NAMESPACE::DoubleFromEnv;
+using GFLAGS_NAMESPACE::StringFromEnv;
+using GFLAGS_NAMESPACE::SetUsageMessage;
+using GFLAGS_NAMESPACE::SetVersionString;
+using GFLAGS_NAMESPACE::ParseCommandLineNonHelpFlags;
+using GFLAGS_NAMESPACE::HandleCommandLineHelpFlags;
+using GFLAGS_NAMESPACE::AllowCommandLineReparsing;
+using GFLAGS_NAMESPACE::ReparseCommandLineNonHelpFlags;
+using GFLAGS_NAMESPACE::ShutDownCommandLineFlags;
+using GFLAGS_NAMESPACE::FlagRegisterer;
+
+#ifndef SWIG
+using GFLAGS_NAMESPACE::ParseCommandLineFlags;
+#endif
+
+
+} // namespace @ns@
+
+
+#endif  // GFLAGS_@NS@_H_