Skip to content

Commit

Permalink
compat: do not use std::source_location if it is broken
Browse files Browse the repository at this point in the history
in a recent clang change, __builtin_source_location was added.
see https://reviews.llvm.org/rGd61487490022 . but a bug in clang
frontend pratically prevents us from using it. see
llvm/llvm-project#48230. so before this
issue is fixed, we have to prevent clang from using the builtin
source location implementation.

in this change, a new library is added for introducing the macro
of "SEASTAR_BROKEN_SOURCE_LOCATION", if it is defined we'd have to
use the homebrew implementation of source_location even the header
file of <source_location> or <experimental/source_location> can be
found by the C++ compiler.

please note, -std=c++20 is not added to CMAKE_REQUIRED_FLAGS when
performing the test, because we want to ensure that we are using
the same CXX_FLAGS as the ones when compiling std-compat.hh.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
  • Loading branch information
tchaikov committed May 19, 2022
1 parent 9cc802e commit 4b93aef
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ target_link_libraries (seastar
cryptopp::cryptopp
fmt::fmt
lz4::lz4
SourceLocation::source_location
PRIVATE
${CMAKE_DL_LIBS}
GnuTLS::gnutls
Expand Down Expand Up @@ -1238,6 +1239,7 @@ if (Seastar_INSTALL)
${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindGnuTLS.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindLinuxMembarrier.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindSanitizers.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindSourceLocation.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindStdAtomic.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Findc-ares.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Findcryptopp.cmake
Expand Down
40 changes: 40 additions & 0 deletions cmake/FindSourceLocation.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# This file is open source software, licensed to you under the terms
# of the Apache License, Version 2.0 (the "License"). See the NOTICE file
# distributed with this work for additional information regarding copyright
# ownership. 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.
#

#
# Copyright (C) 2022 Kefu Chai ( tchaikov@gmail.com )
#

include (CheckCXXSourceCompiles)
include(CMakePushCheckState)

file (READ ${CMAKE_CURRENT_LIST_DIR}/code_tests/Source_location_test.cc _source_location_test_code)
cmake_push_check_state ()
set(CMAKE_CXX_STANDARD ${Seastar_CXX_STANDARD})
# set(CMAKE_REQUIRED_FLAGS "-std=${Seastar_CXX_STANDARD}")
check_cxx_source_compiles ("${_source_location_test_code}" CxxSourceLocation_SUPPORTED)
cmake_pop_check_state ()

if (NOT (TARGET SourceLocation::source_location))
add_library (SourceLocation::source_location INTERFACE IMPORTED)
if (NOT CxxSourceLocation_SUPPORTED)
set_target_properties (SourceLocation::source_location
PROPERTIES
INTERFACE_COMPILE_DEFINITIONS SEASTAR_BROKEN_SOURCE_LOCATION)
endif ()
endif ()
1 change: 1 addition & 0 deletions cmake/SeastarDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ macro (seastar_find_dependencies)
GnuTLS
LinuxMembarrier
Sanitizers
SourceLocation
StdAtomic
hwloc
lksctp-tools # No version information published.
Expand Down
26 changes: 26 additions & 0 deletions cmake/code_tests/Source_location_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if __has_include(<source_location>)
#include <source_location>
#endif

#ifdef __cpp_lib_source_location
using source_location = std::source_location;
#elif __has_include(<experimental/source_location>)
#include <experimental/source_location>
using source_location = std::experimental::source_location;
#endif

#if defined(__cpp_lib_source_location) || defined(__cpp_lib_experimental_source_location)
struct format_info {
format_info(source_location loc = source_location::current()) noexcept
: loc(loc)
{ }
source_location loc;
};
#else
struct format_info { };
#endif

int main()
{
format_info fi;
}
4 changes: 2 additions & 2 deletions include/seastar/util/std-compat.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ namespace std::pmr {
#include <source_location>
#endif

#ifdef __cpp_lib_source_location
#if defined(__cpp_lib_source_location) && !defined(SEASTAR_BROKEN_SOURCE_LOCATION)
namespace seastar::compat {
using source_location = std::source_location;
}
#elif __has_include(<experimental/source_location>)
#elif __has_include(<experimental/source_location>) && !defined(SEASTAR_BROKEN_SOURCE_LOCATION)
#include <experimental/source_location>
namespace seastar::compat {
using source_location = std::experimental::source_location;
Expand Down

0 comments on commit 4b93aef

Please sign in to comment.