Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix std::chrono/date conflict for mac builds with C++20 #22138

Merged
merged 7 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions include/onnxruntime/core/common/logging/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,43 @@

using Timestamp = std::chrono::time_point<std::chrono::system_clock>;

// TODO: When other compilers support std::chrono::operator<<, update this.
// TODO: Check support for other compilers' version before enable C++20 for other compilers.
// Xcode added support for C++20's std::chrono::operator<< in SDK version 14.4.
#if __cplusplus >= 202002L && __MAC_OS_X_VERSION_MAX_ALLOWED >= 140400L
// C++20 has operator<< in std::chrono for Timestamp type but mac builds need additional checks
// to ensure usage is valid.
// TODO: As we enable C++20 on other platforms we may need similar checks.

Check warning on line 63 in include/onnxruntime/core/common/logging/logging.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Missing username in TODO; it should look like "// TODO(my_username): Stuff." [readability/todo] [2] Raw Output: include/onnxruntime/core/common/logging/logging.h:63: Missing username in TODO; it should look like "// TODO(my_username): Stuff." [readability/todo] [2]
// define a temporary value to determine whether to use the std::chrono or date implementation.
#define TEMP_USE_CXX20_STD_CHRONO __cplusplus >= 202002L

// Apply constraints for mac builds
#if __APPLE__
#include <TargetConditionals.h>

// Catalyst check must be first as it has both TARGET_OS_MACCATALYST and TARGET_OS_MAC set
#if TARGET_OS_MACCATALYST
// maccatalyst requires version 16.3
#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 160300)
#undef TEMP_USE_CXX20_STD_CHRONO
#endif

#elif TARGET_OS_MAC
// Xcode added support for C++20's std::chrono::operator<< in SDK version 14.4,
// but the target macOS version must also be >= 13.3 for it to be used.
#if (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED < 140400) || \
(defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 130300)
#undef TEMP_USE_CXX20_STD_CHRONO
#endif

#endif

#endif // __APPLE__

#if TEMP_USE_CXX20_STD_CHRONO
namespace timestamp_ns = std::chrono;
#else
namespace timestamp_ns = ::date;
#endif

#undef _USE_CXX20_STD_CHRONO

#ifndef NDEBUG
ORT_ATTRIBUTE_UNUSED static bool vlog_enabled = true; // Set directly based on your needs.
#else
Expand Down
3 changes: 2 additions & 1 deletion onnxruntime/core/common/logging/sinks/ostream_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void OStreamSink::SendImpl(const Timestamp& timestamp, const std::string& logger
}
#endif

msg << timestamp << " [" << message.SeverityPrefix() << ":" << message.Category() << ":" << logger_id << ", "
timestamp_ns::operator<<(msg, timestamp); // handle ambiguity with C++20 where date and std::chrono have operator<<
msg << " [" << message.SeverityPrefix() << ":" << message.Category() << ":" << logger_id << ", "
<< message.Location().ToString() << "] " << message.Message();

#ifndef ORT_MINIMAL_BUILD
Expand Down
4 changes: 3 additions & 1 deletion onnxruntime/core/platform/apple/logging/apple_log_sink.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
void AppleLogSink::SendImpl(const Timestamp& timestamp, const std::string& logger_id, const Capture& message) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file still includes the third-party "date.h" header file, even when timestamp_ns refers to std::chrono

using timestamp_ns::operator<<;
std::ostringstream msg;
msg << timestamp << " [" << message.SeverityPrefix() << ":" << message.Category() << ":" << logger_id << ", "

timestamp_ns::operator<<(msg, timestamp); // handle ambiguity with C++20 where date and std::chrono have operator<<
msg << " [" << message.SeverityPrefix() << ":" << message.Category() << ":" << logger_id << ", "
<< message.Location().ToString() << "] " << message.Message();
NSLog(@"%s", msg.str().c_str());
}
Expand Down
Loading