dnl ===================================================================
dnl   Licensed to the Apache Software Foundation (ASF) under one
dnl   or more contributor license agreements.  See the NOTICE file
dnl   distributed with this work for additional information
dnl   regarding copyright ownership.  The ASF licenses this file
dnl   to you under the Apache License, Version 2.0 (the
dnl   "License"); you may not use this file except in compliance
dnl   with the License.  You may obtain a copy of the License at
dnl
dnl     http://www.apache.org/licenses/LICENSE-2.0
dnl
dnl   Unless required by applicable law or agreed to in writing,
dnl   software distributed under the License is distributed on an
dnl   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
dnl   KIND, either express or implied.  See the License for the
dnl   specific language governing permissions and limitations
dnl   under the License.
dnl ===================================================================
dnl
dnl SVN_CFLAGS_ADD_IFELSE(option, success, failure)
dnl
dnl   Check if the C compiler accepts $option. If it does, prepend it
dnl   to CFLAGS and execute $success; otherwise execute $failure.
dnl
dnl SVN_CXXFLAGS_ADD_IFELSE(option, success, failure)
dnl
dnl   Like SVN_CFLAGS_ADD_IFELSE, but for C++ and CXXFLAGS.
dnl
dnl SVN_PROG_CC: Customized replacement for AC_PROG_CC
dnl SVN_PROG_CXX: Customized replacement for AC_PROG_CXX

AC_DEFUN([_SVN_XXFLAGS_ADD_IFELSE],
[
  _svn_xxflags__save="[$][$3]"
  AC_LANG_PUSH([$1])
  AC_MSG_CHECKING([if [$][$2] accepts $5])
  [$3]="$5 [$][$3]"
  dnl Compiling is not enough: Solaris cc accepts invalid options at
  dnl compile-time and just warns, assuming they are for the linker,
  dnl but they cause errors at link-time.
  AC_LINK_IFELSE([AC_LANG_SOURCE([$4])],[
    AC_MSG_RESULT([yes])
    $6
  ],[
    AC_MSG_RESULT([no])
    [$3]="$_svn_xxflags__save"
    $7
  ])
  AC_LANG_POP([$1])
])

AC_DEFUN([SVN_CFLAGS_ADD_IFELSE],
  [_SVN_XXFLAGS_ADD_IFELSE([C],[CC],[CFLAGS],[[int main(void){return 0;}]],[$1],[$2],[$3])])

AC_DEFUN([SVN_CXXFLAGS_ADD_IFELSE],
  [_SVN_XXFLAGS_ADD_IFELSE([C++],[CXX],[CXXFLAGS],[[int main(){}]],[$1],[$2],[$3])])


AC_DEFUN([SVN_CC_MODE_SETUP],
[
  CFLAGS_KEEP="$CFLAGS"
  CFLAGS=""

  if test "$GCC" = "yes"; then
    dnl Find flags to force C90 mode
                  dnl gcc and clang
    SVN_CFLAGS_ADD_IFELSE([-std=c90],[],[
      SVN_CFLAGS_ADD_IFELSE([-std=c89],[],[
        SVN_CFLAGS_ADD_IFELSE([-ansi])
      ])
    ])
  fi

  dnl check for snprintf so that we can use it instead of sprintf.
  AC_CHECK_FUNCS(snprintf)

  CMODEFLAGS="$CFLAGS"
  CFLAGS=""
  SVN_DOT_CLANGD_CC([$CMODEFLAGS])

  if test "$GCC" = "yes"; then
    dnl Find flags to silence all warnings
    SVN_CFLAGS_ADD_IFELSE([-w],[SVN_DOT_CLANGD_CC([-w])])
  fi

  CNOWARNFLAGS="$CFLAGS"
  CFLAGS="$CFLAGS_KEEP"

  AC_SUBST(CMODEFLAGS)
  AC_SUBST(CNOWARNFLAGS)
  AC_SUBST(CMAINTAINERFLAGS)
  AC_SUBST(CUSERFLAGS)

  if test "$GCC" = "yes"; then
    dnl Tell clang to not accept unknown warning flags
    SVN_CFLAGS_ADD_IFELSE([-Werror=unknown-warning-option],[
      SVN_DOT_CLANGD_CC([-Werror=unknown-warning-option])
    ])
  fi
])


AC_DEFUN([SVN_CXX_MODE_SETUP],
[
  CXXFLAGS_KEEP="$CXXFLAGS"
  CXXFLAGS=""

  if test "$GXX" = "yes"; then
    dnl Find flags to force C++ mode
                  dnl g++ and clang++
    if test "$cxx_language_level" = "any"; then
      SVN_CXXFLAGS_ADD_IFELSE([-std=c++23],[],[
        SVN_CXXFLAGS_ADD_IFELSE([-std=c++20],[],[
          SVN_CXXFLAGS_ADD_IFELSE([-std=c++17],[],[
            SVN_CXXFLAGS_ADD_IFELSE([-std=c++11])
          ])
        ])
      ])
    else
      SVN_CXXFLAGS_ADD_IFELSE([-std=$cxx_language_level],[],[
        AC_MSG_ERROR([$CXX does not accept -std=$cxx_language_level (see option --enable-c++)])
      ])
      dnl We require at least C++11
      AC_MSG_CHECKING([if '$CXX $CXXFLAGS' supports at least a C++11])
      AC_LANG_PUSH([C++])
      AC_COMPILE_IFELSE([
        AC_LANG_SOURCE([
          #if !defined(__cplusplus) || __cplusplus < 201103
          #error "Not C++11"
          #endif
        ])
      ],[AC_MSG_RESULT([yes])],[
        AC_MSG_RESULT([no])
        AC_MSG_ERROR([JavHL and C++ bindings require at least C++11])
      ])
      AC_LANG_POP([C++])
    fi
  fi

  CXXMODEFLAGS="$CXXFLAGS"
  CXXFLAGS=""
  SVN_DOT_CLANGD_CXX([$CXXMODEFLAGS])

  if test "$GXX" = "yes"; then
    dnl Find flags to silence all warnings
    SVN_CXXFLAGS_ADD_IFELSE([-w],[SVN_DOT_CLANGD_CXX([-w])])
  fi

  CXXNOWARNFLAGS="$CXXFLAGS"
  CXXFLAGS="$CXXFLAGS_KEEP"

  AC_SUBST(CXXMODEFLAGS)
  AC_SUBST(CXXNOWARNFLAGS)
  AC_SUBST(CXXMAINTAINERFLAGS)
  AC_SUBST(CXXUSERFLAGS)

  if test "$GXX" = "yes"; then
    dnl Tell clang++ to not accept unknown warning flags
    SVN_CXXFLAGS_ADD_IFELSE([-Werror=unknown-warning-option],[
      SVN_DOT_CLANGD_CXX([-Werror=unknown-warning-option])
    ])
  fi
])
