More work on the CMake build.

* CMakeLists.txt: Adjust for the changes listed below.
* build/SerfVersion.cmake: Renamed from build/version.cmake.
* build/SerfChecks.cmake: Renamed from build/feature.cmake.
* build/APRCommon.cmake: New; common code for FindAPR and FindAPRUTIL.
* build/FindAPR.cmake: Renamed from build/FindApr.cmake.
   Expose version, extract common code, fix variable names, support APR 2.x.
* build/FindAPRUTIL.cmake: Renamed from build/FindApu.cmake.
   Other changes as for FindAPR.cmake.


git-svn-id: https://svn.apache.org/repos/asf/serf/trunk@1834227 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CMakeLists.txt b/CMakeLists.txt
index bbde0af..d913984 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -20,8 +20,7 @@
 cmake_minimum_required(VERSION 3.0.2)
 
 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/build")
-include(version)
-include(feature)
+include(SerfVersion)
 
 project("Serf" VERSION ${SERF_VERSION} LANGUAGES C)
 message(WARNING
@@ -44,12 +43,13 @@
 endif(WINDOWS)
 
 
-find_package(openssl)
-find_package(zlib)
-find_package(apr)
-find_package(apu)
+find_package(OpenSSL)
+find_package(ZLib)
+find_package(APR)
+find_package(APRUtil)
 
 # Feature tests
+include(SerfChecks)
 CheckNotFunction("BIO_set_init" "SERF_NO_SSL_BIO_WRAPPERS" ${OPENSSL_LIBRARIES})
 CheckNotFunction("X509_STORE_get0_param" "SERF_NO_SSL_X509_STORE_WRAPPERS" ${OPENSSL_LIBRARIES})
 CheckNotFunction("X509_get0_notBefore" "SERF_NO_SSL_X509_GET0_NOTBEFORE" ${OPENSSL_LIBRARIES})
@@ -64,23 +64,23 @@
 CheckHeader("openssl/applink.c" "SERF_HAVE_OPENSSL_APPLINK_C" ${OPENSSL_INCLUDE_DIR})
 CheckType("OSSL_HANDSHAKE_STATE" "openssl/ssl.h" "SERF_HAVE_OSSL_HANDSHAKE_STATE" ${OPENSSL_INCLUDE_DIR})
 
-set(DEPENDENCY_INCLUDE_DIRS
+set(DEPENDENCY_INCLUDES
     ${OPENSSL_INCLUDE_DIR}
     ${ZLIB_INCLUDE_DIRS}
     ${APR_INCLUDES}
-    ${APU_INCLUDES}
+    ${APRUTIL_INCLUDES}
 )
-list(REMOVE_DUPLICATES DEPENDENCY_INCLUDE_DIRS)
+list(REMOVE_DUPLICATES DEPENDENCY_INCLUDES)
 
 set(DEPENDENCY_LIBRARIES
     ${OPENSSL_LIBRARIES}
     ${ZLIB_LIBRARIES}
-    ${APR_LIBS}
-    ${APU_LIBS}
+    ${APR_LIBRARIES}
+    ${APRUTIL_LIBRARIES}
 )
 list(REMOVE_DUPLICATES DEPENDENCY_LIBRARIES)
 
-include_directories(BEFORE SYSTEM ${DEPENDENCY_INCLUDE_DIRS})
+include_directories(BEFORE SYSTEM ${DEPENDENCY_INCLUDES})
 include_directories(${CMAKE_SOURCE_DIR})
 add_library(SerfStatic STATIC ${SOURCES})
 add_library(SerfShared SHARED ${SOURCES} ${SHARED_SOURCES})
diff --git a/build/APRCommon.cmake b/build/APRCommon.cmake
new file mode 100644
index 0000000..4c5609f
--- /dev/null
+++ b/build/APRCommon.cmake
@@ -0,0 +1,48 @@
+# ===================================================================
+#   Licensed to the Apache Software Foundation (ASF) under one
+#   or more contributor license agreements.  See the NOTICE file
+#   distributed with this work for additional information
+#   regarding copyright ownership.  The ASF licenses this file
+#   to you under the Apache License, Version 2.0 (the
+#   "License"); you may not use this file except in compliance
+#   with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing,
+#   software distributed under the License is distributed on an
+#   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#   KIND, either express or implied.  See the License for the
+#   specific language governing permissions and limitations
+#   under the License.
+# ===================================================================
+
+function(_apru_config _program _varname _separate _regexp)
+  execute_process(COMMAND ${_program} ${ARGN}
+                  OUTPUT_VARIABLE _apru_output
+                  RESULT_VARIABLE _apru_failed)
+
+  if(_apru_failed)
+    message(FATAL_ERROR "${_program} ${ARGN} failed")
+  else()
+
+    # Join multi-line outupt
+    string(REGEX REPLACE "[\r\n]"       ""  _apru_output "${_apru_output}")
+
+    # Optionally apply the regular expression filter
+    if(NOT ${_regexp} STREQUAL "")
+      string(REGEX REPLACE "${_regexp}" " " _apru_output "${_apru_output}")
+    endif()
+
+    # Remove leading and trailing spaces
+    string(REGEX REPLACE "^ +"          ""  _apru_output "${_apru_output}")
+    string(REGEX REPLACE " +$"          ""  _apru_output "${_apru_output}")
+
+    # Optionally split the result into an argument list
+    if(${_separate})
+      separate_arguments(_apru_output)
+    endif()
+
+    set(${_varname} "${_apru_output}" PARENT_SCOPE)
+  endif()
+endfunction(_apru_config)
diff --git a/build/FindAPR.cmake b/build/FindAPR.cmake
new file mode 100644
index 0000000..3df36f2
--- /dev/null
+++ b/build/FindAPR.cmake
@@ -0,0 +1,61 @@
+# ===================================================================
+#   Licensed to the Apache Software Foundation (ASF) under one
+#   or more contributor license agreements.  See the NOTICE file
+#   distributed with this work for additional information
+#   regarding copyright ownership.  The ASF licenses this file
+#   to you under the Apache License, Version 2.0 (the
+#   "License"); you may not use this file except in compliance
+#   with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing,
+#   software distributed under the License is distributed on an
+#   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#   KIND, either express or implied.  See the License for the
+#   specific language governing permissions and limitations
+#   under the License.
+# ===================================================================
+
+# This module defines
+# APR_INCLUDES, where to find apr.h, etc.
+# APR_LIBRARIES, linker switches to use with ld to link against APR
+# APR_EXTRALIBS, additional libraries to link against
+# APR_CFLAGS, the flags to use to compile
+# APR_FOUND, set to TRUE if found, FALSE otherwise
+# APR_VERSION, the version of APR that was found
+# APR_CONTAINS_APRUTIL, set to TRUE if the APR major version is 2 or greater.
+
+set(APR_FOUND FALSE)
+
+if(DEFINED APR_ROOT)
+  find_program(APR_CONFIG_EXECUTABLE NAMES apr-2-config apr-1-config
+               PATHS "${APR_ROOT}/bin" NO_DEFAULT_PATH)
+else()
+  find_program(APR_CONFIG_EXECUTABLE NAMES apr-2-config apr-1-config)
+endif()
+mark_as_advanced(APR_CONFIG_EXECUTABLE)
+
+include(APRCommon)
+macro(_apr_invoke _varname _separate _regexp)
+  _apru_config("${APR_CONFIG_EXECUTABLE}" "${_varname}" "${_separate}" "${_regexp}" ${ARGN})
+endmacro(_apr_invoke)
+
+_apr_invoke(APR_CFLAGS    FALSE "(^| )-(g|O)[^ ]*" --cppflags --cflags)
+_apr_invoke(APR_INCLUDES  TRUE  "(^| )-I"          --includes)
+_apr_invoke(APR_LIBRARIES TRUE  ""                 --link-ld)
+_apr_invoke(APR_EXTRALIBS TRUE  "(^| )-l"          --libs)
+_apr_invoke(APR_VERSION   TRUE  ""                 --version)
+
+string(REGEX REPLACE "^([0-9]+)\\..*$" "\\1" _apr_major "${APR_VERSION}")
+if(_apr_major GREATER 2)
+  set(APR_CONTAINS_APRUTIL TRUE)
+else()
+  set(APR_CONTAINS_APRUTIL FALSE)
+endif()
+unset(_apr_major)
+
+INCLUDE(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(APR
+                                  REQUIRED_VARS APR_LIBRARIES APR_INCLUDES
+                                  VERSION_VAR APR_VERSION)
diff --git a/build/FindAPRUTIL.cmake b/build/FindAPRUTIL.cmake
new file mode 100644
index 0000000..bd20b45
--- /dev/null
+++ b/build/FindAPRUTIL.cmake
@@ -0,0 +1,68 @@
+# ===================================================================
+#   Licensed to the Apache Software Foundation (ASF) under one
+#   or more contributor license agreements.  See the NOTICE file
+#   distributed with this work for additional information
+#   regarding copyright ownership.  The ASF licenses this file
+#   to you under the Apache License, Version 2.0 (the
+#   "License"); you may not use this file except in compliance
+#   with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing,
+#   software distributed under the License is distributed on an
+#   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#   KIND, either express or implied.  See the License for the
+#   specific language governing permissions and limitations
+#   under the License.
+# ===================================================================
+
+# This module defines
+# APRUTIL_INCLUDES, where to find apu.h, etc.
+# APRUTIL_LIBRARIES, linker switches to use with ld to link against apr-util
+# APRUTIL_EXTRALIBS, additional libraries to link against
+# APRUTIL_LDFLAGS, additional linker flags that must be used
+# APRUTIL_FOUND, set to TRUE if found, FALSE otherwise
+# APRUTIL_VERSION, set to the version of apr-util found
+
+if(NOT APR_FOUND)
+  find_package(APR)
+endif()
+
+if(APR_CONTAINS_APRUTIL)
+
+  set(APRUTIL_FOUND TRUE)
+  set(APRUTIL_INCLUDES ${APR_INCLUDES})
+  set(APRUTIL_LIBRARIES ${APR_LIBRARIES})
+  set(APRUTIL_EXTRALIBS ${APR_EXTRALIBS})
+  set(APRUTIL_VERSION ${APR_VERSION})
+
+else(APR_CONTAINS_APRUTIL)
+
+  set(APRUTIL_FOUND FALSE)
+
+  if(DEFINED APRUTIL_ROOT)
+    find_program(APRUTIL_CONFIG_EXECUTABLE apu-1-config
+                 PATHS "${APRUTIL_ROOT}/bin" NO_DEFAULT_PATH)
+  else()
+    find_program(APRUTIL_CONFIG_EXECUTABLE apu-1-config)
+  endif()
+  mark_as_advanced(APRUTIL_CONFIG_EXECUTABLE)
+
+  include(APRCommon)
+  macro(_apu_invoke _varname _separate _regexp)
+    _apru_config("${APRUTIL_CONFIG_EXECUTABLE}" "${_varname}" "${_separate}" "${_regexp}" ${ARGN})
+  endmacro(_apu_invoke)
+
+  _apu_invoke(APRUTIL_INCLUDES  TRUE  "(^| )-I" --includes)
+  _apu_invoke(APRUTIL_EXTRALIBS TRUE  "(^| )-l" --libs)
+  _apu_invoke(APRUTIL_LIBRARIES TRUE  ""        --link-ld)
+  _apu_invoke(APRUTIL_LDFLAGS   FALSE ""        --ldflags)
+  _apu_invoke(APRUTIL_VERSION   TRUE  ""        --version)
+
+  INCLUDE(FindPackageHandleStandardArgs)
+  FIND_PACKAGE_HANDLE_STANDARD_ARGS(APRUTIL
+                                    REQUIRED_VARS APRUTIL_LIBRARIES APRUTIL_INCLUDES
+                                    VERSION_VAR APRUTIL_VERSION)
+
+endif(APR_CONTAINS_APRUTIL)
diff --git a/build/FindApr.cmake b/build/FindApr.cmake
deleted file mode 100644
index 6aa93ae..0000000
--- a/build/FindApr.cmake
+++ /dev/null
@@ -1,51 +0,0 @@
-# Source: http://svn.trolocsis.com/repos/projects/templates/apr/build/FindAPR.cmake
-# Locate APR include paths and libraries
-
-# This module defines
-# APR_INCLUDES, where to find apr.h, etc.
-# APR_LIBS, linker switches to use with ld to link against APR
-# APR_EXTRALIBS, additional libraries to link against
-# APR_CFLAGS, the flags to use to compile
-# APR_FOUND, set to TRUE if found, FALSE otherwise
-# APR_VERSION, the version of APR that was found
-
-set(APR_FOUND FALSE)
-
-find_program(APR_CONFIG_EXECUTABLE apr-1-config)
-mark_as_advanced(APR_CONFIG_EXECUTABLE)
-
-macro(_apr_invoke _varname _separate _regexp)
-    execute_process(
-        COMMAND ${APR_CONFIG_EXECUTABLE} ${ARGN}
-        OUTPUT_VARIABLE _apr_output
-        RESULT_VARIABLE _apr_failed
-    )
-
-    if(_apr_failed)
-        message(FATAL_ERROR "${APR_CONFIG_EXECUTABLE} ${ARGN} failed")
-    else()
-        string(REGEX REPLACE "[\r\n]"  "" _apr_output "${_apr_output}")
-        string(REGEX REPLACE " +$"     "" _apr_output "${_apr_output}")
-
-        if(NOT ${_regexp} STREQUAL "")
-            string(REGEX REPLACE "${_regexp}" " " _apr_output "${_apr_output}")
-        endif()
-
-        # XXX: We don't want to invoke separate_arguments() for APR_CFLAGS;
-        # just leave as-is
-        if(${_separate})
-            separate_arguments(_apr_output)
-        endif()
-
-        set(${_varname} "${_apr_output}")
-    endif()
-endmacro(_apr_invoke)
-
-_apr_invoke(APR_CFLAGS    FALSE ""        --cppflags --cflags)
-_apr_invoke(APR_INCLUDES  TRUE  "(^| )-I" --includes)
-_apr_invoke(APR_LIBS      TRUE  ""        --link-ld)
-_apr_invoke(APR_EXTRALIBS TRUE  "(^| )-l" --libs)
-_apr_invoke(APR_VERSION   TRUE  ""        --version)
-
-INCLUDE(FindPackageHandleStandardArgs)
-FIND_PACKAGE_HANDLE_STANDARD_ARGS(APR DEFAULT_MSG APR_INCLUDES APR_LIBS APR_VERSION)
diff --git a/build/FindApu.cmake b/build/FindApu.cmake
deleted file mode 100644
index 632544b..0000000
--- a/build/FindApu.cmake
+++ /dev/null
@@ -1,51 +0,0 @@
-# Locate apr-util include paths and libraries. Based on findapr.cmake;
-# simple modifications to apply to apr-util instead.
-
-# This module defines
-# APU_INCLUDES, where to find apu.h, etc.
-# APU_LIBS, linker switches to use with ld to link against apr-util
-# APU_EXTRALIBS, additional libraries to link against
-# APU_LDFLAGS, additional linker flags that must be used
-# APU_FOUND, set to TRUE if found, FALSE otherwise
-# APU_VERSION, set to the version of apr-util found
-
-set(APU_FOUND FALSE)
-
-find_program(APU_CONFIG_EXECUTABLE apu-1-config)
-mark_as_advanced(APU_CONFIG_EXECUTABLE)
-
-macro(_apu_invoke _varname _separate _regexp)
-    execute_process(
-        COMMAND ${APU_CONFIG_EXECUTABLE} ${ARGN}
-        OUTPUT_VARIABLE _apu_output
-        RESULT_VARIABLE _apu_failed
-    )
-
-    if(_apu_failed)
-        message(FATAL_ERROR "${APU_CONFIG_EXECUTABLE} ${ARGN} failed")
-    else()
-        string(REGEX REPLACE "[\r\n]"  "" _apu_output "${_apu_output}")
-        string(REGEX REPLACE " +$"     "" _apu_output "${_apu_output}")
-
-        if(NOT ${_regexp} STREQUAL "")
-            string(REGEX REPLACE "${_regexp}" " " _apu_output "${_apu_output}")
-        endif()
-
-        # XXX: We don't want to invoke separate_arguments() for APU_LDFLAGS;
-        # just leave as-is
-        if(${_separate})
-            separate_arguments(_apu_output)
-        endif()
-
-        set(${_varname} "${_apu_output}")
-    endif()
-endmacro(_apu_invoke)
-
-_apu_invoke(APU_INCLUDES  TRUE  "(^| )-I" --includes)
-_apu_invoke(APU_EXTRALIBS TRUE  "(^| )-l" --libs)
-_apu_invoke(APU_LIBS      TRUE  ""        --link-ld)
-_apu_invoke(APU_LDFLAGS   FALSE ""        --ldflags)
-_apu_invoke(APU_VERSION   TRUE  ""        --version)
-
-INCLUDE(FindPackageHandleStandardArgs)
-FIND_PACKAGE_HANDLE_STANDARD_ARGS(APU DEFAULT_MSG APU_INCLUDES APU_LIBS APU_VERSION)
diff --git a/build/feature.cmake b/build/SerfChecks.cmake
similarity index 100%
rename from build/feature.cmake
rename to build/SerfChecks.cmake
diff --git a/build/version.cmake b/build/SerfVersion.cmake
similarity index 100%
rename from build/version.cmake
rename to build/SerfVersion.cmake