From 2dd6f858c03ae1b7f07fc7ee97cba940163488ca Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Date: Tue, 30 Aug 2022 20:30:07 +0000 Subject: [PATCH] build: (fixes #730) add compiler workaround for ostream& operator<<(ostream&, nullptr_t) --- .../ostream-operator-nullptr.h | 15 ++++++ .../ns3-compiler-workarounds.cmake | 50 +++++++++++++++++++ build-support/macros-and-definitions.cmake | 9 ++++ 3 files changed, 74 insertions(+) create mode 100644 build-support/compiler-workarounds/ostream-operator-nullptr.h create mode 100644 build-support/custom-modules/ns3-compiler-workarounds.cmake diff --git a/build-support/compiler-workarounds/ostream-operator-nullptr.h b/build-support/compiler-workarounds/ostream-operator-nullptr.h new file mode 100644 index 000000000..075cc3699 --- /dev/null +++ b/build-support/compiler-workarounds/ostream-operator-nullptr.h @@ -0,0 +1,15 @@ +#ifndef OSTREAM_OPERATOR_NULLPTR_H +#define OSTREAM_OPERATOR_NULLPTR_H + +#include +#include + +namespace std +{ +inline std::ostream &operator<< (std::ostream &os, std::nullptr_t ptr) +{ + return os << "nullptr"; //whatever you want nullptr to show up as in the console +} +} + +#endif //OSTREAM_OPERATOR_NULLPTR_H diff --git a/build-support/custom-modules/ns3-compiler-workarounds.cmake b/build-support/custom-modules/ns3-compiler-workarounds.cmake new file mode 100644 index 000000000..6f5e96ecc --- /dev/null +++ b/build-support/custom-modules/ns3-compiler-workarounds.cmake @@ -0,0 +1,50 @@ +# Copyright (c) 2022 Universidade de Brasília +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License version 2 as published by the Free +# Software Foundation; +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# +# Author: Gabriel Ferreira + +include(CheckCXXSourceCompiles) + +# Clang 6, 7 and 8 shipped with incomplete C++17 features and do not handle +# ostream& operator<<(ostream& os, nullptr_t ptr) +# https://gitlab.com/nsnam/ns-3-dev/-/issues/730 +check_cxx_source_compiles( + " + #include + #include + inline std::ostream& operator << (std::ostream& os, std::nullptr_t ptr) + { + return os << \"nullptr\"; //whatever you want nullptr to show up as in the console + } + int main() + { + std::ostream os(NULL); + os << std::nullptr_t(); + return 0; + } + " + MISSING_OSTREAM_NULLPTR_OPERATOR +) + +if(${MISSING_OSTREAM_NULLPTR_OPERATOR}) + message( + ${HIGHLIGHTED_STATUS} + "Using compiler workaround: compiling in \"ostream& operator<<(ostream&, nullptr_t)\"" + ) + add_definitions( + -include + ${CMAKE_CURRENT_SOURCE_DIR}/build-support/compiler-workarounds/ostream-operator-nullptr.h + ) +endif() diff --git a/build-support/macros-and-definitions.cmake b/build-support/macros-and-definitions.cmake index 313149994..a37f9425b 100644 --- a/build-support/macros-and-definitions.cmake +++ b/build-support/macros-and-definitions.cmake @@ -495,6 +495,15 @@ macro(process_options) ) endif() + # Honor CMAKE_CXX_STANDARD in check_cxx_source_compiles + # https://cmake.org/cmake/help/latest/policy/CMP0067.html + cmake_policy(SET CMP0066 NEW) + cmake_policy(SET CMP0067 NEW) + + # After setting the correct CXX version, we can proceed to check for compiler + # workarounds + include(build-support/custom-modules/ns3-compiler-workarounds.cmake) + if(${NS3_DES_METRICS}) add_definitions(-DENABLE_DES_METRICS) endif()