Skip to content

[libc++][modules] Adds std.compat module. #71438

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

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 40 additions & 1 deletion libcxx/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ set(LIBCXX_MODULE_STD_SOURCES
std/version.inc
)

set(LIBCXX_MODULE_STD_COMPAT_SOURCES
std.compat/cassert.inc
std.compat/cctype.inc
std.compat/cerrno.inc
std.compat/cfenv.inc
std.compat/cfloat.inc
std.compat/cinttypes.inc
std.compat/climits.inc
std.compat/clocale.inc
std.compat/cmath.inc
std.compat/csetjmp.inc
std.compat/csignal.inc
std.compat/cstdarg.inc
std.compat/cstddef.inc
std.compat/cstdint.inc
std.compat/cstdio.inc
std.compat/cstdlib.inc
std.compat/cstring.inc
std.compat/ctime.inc
std.compat/cuchar.inc
std.compat/cwchar.inc
std.compat/cwctype.inc
)

# TODO MODULES the CMakeLists.txt in the install directory is only temporary
# When that is removed the configured file can use the substitution
# LIBCXX_GENERATED_INCLUDE_TARGET_DIR avoiding this set.
Expand Down Expand Up @@ -154,10 +178,25 @@ configure_file(
@ONLY
)

set(LIBCXX_MODULE_STD_COMPAT_INCLUDE_SOURCES)
foreach(file ${LIBCXX_MODULE_STD_COMPAT_SOURCES})
set(
LIBCXX_MODULE_STD_COMPAT_INCLUDE_SOURCES
"${LIBCXX_MODULE_STD_COMPAT_INCLUDE_SOURCES}#include \"${file}\"\n"
)
endforeach()

configure_file(
"std.compat.cppm.in"
"${LIBCXX_GENERATED_MODULE_DIR}/std.compat.cppm"
@ONLY
)

set(_all_modules)
list(APPEND _all_modules "${LIBCXX_GENERATED_MODULE_DIR}/CMakeLists.txt")
list(APPEND _all_modules "${LIBCXX_GENERATED_MODULE_DIR}/std.cppm")
foreach(file ${LIBCXX_MODULE_STD_SOURCES})
list(APPEND _all_modules "${LIBCXX_GENERATED_MODULE_DIR}/std.compat.cppm")
foreach(file ${LIBCXX_MODULE_STD_SOURCES} ${LIBCXX_MODULE_STD_COMPAT_SOURCES})
set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
set(dst "${LIBCXX_GENERATED_MODULE_DIR}/${file}")
add_custom_command(OUTPUT ${dst}
Expand Down
28 changes: 28 additions & 0 deletions libcxx/modules/CMakeLists.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ macro(compile_define_if condition def)
endif()
endmacro()

### STD

add_library(std)
target_sources(std
PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES FILES
Expand All @@ -52,3 +54,29 @@ set_target_properties(std
PROPERTIES
OUTPUT_NAME "c++std"
)

### STD.COMPAT

add_library(std.compat)
target_sources(std.compat
PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES FILES
std.compat.cppm
)

target_include_directories(std.compat SYSTEM PRIVATE @LIBCXX_CONFIGURED_INCLUDE_DIRS@)

if (NOT @LIBCXX_ENABLE_EXCEPTIONS@)
target_compile_options(std.compat PUBLIC -fno-exceptions)
endif()

target_compile_options(std.compat
PUBLIC
-nostdinc++
-Wno-reserved-module-identifier
-Wno-reserved-user-defined-literal
@LIBCXX_COMPILE_FLAGS@
)
set_target_properties(std.compat
PROPERTIES
OUTPUT_NAME "c++std.compat"
)
208 changes: 208 additions & 0 deletions libcxx/modules/std.compat.cppm.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// WARNING, this entire header is generated by
// utils/generate_libcxx_cppm_in.py
// DO NOT MODIFY!

module;

#include <__config>

// The headers of Table 24: C++ library headers [tab:headers.cpp]
// and the headers of Table 25: C++ headers for C library facilities [tab:headers.cpp.c]
#include <algorithm>
#include <any>
#include <array>
Comment on lines +20 to +22
Copy link
Member

Choose a reason for hiding this comment

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

I feel better to not include C++ headers in std.compat.cppm. See the following comments for details.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the feedback. I'll not address this now but in a follow-up PR. I really like to get this code in and unblock some other WIP patches.

#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
# include <atomic>
#endif
#if !defined(_LIBCPP_HAS_NO_THREADS)
# include <barrier>
#endif
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <charconv>
#include <chrono>
#include <cinttypes>
#include <climits>
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <clocale>
#endif
#include <cmath>
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <codecvt>
#endif
#include <compare>
#include <complex>
#include <concepts>
#include <condition_variable>
#include <coroutine>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cuchar>
#if !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
# include <cwchar>
#endif
#if !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
# include <cwctype>
#endif
#include <deque>
#include <exception>
#include <execution>
#include <expected>
#include <filesystem>
#include <format>
#include <forward_list>
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <fstream>
#endif
#include <functional>
#if !defined(_LIBCPP_HAS_NO_THREADS)
# include <future>
#endif
#include <initializer_list>
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <iomanip>
#endif
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <ios>
#endif
#include <iosfwd>
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <iostream>
#endif
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <istream>
#endif
#include <iterator>
#if !defined(_LIBCPP_HAS_NO_THREADS)
# include <latch>
#endif
#include <limits>
#include <list>
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <locale>
#endif
#include <map>
#include <mdspan>
#include <memory>
#include <memory_resource>
#include <mutex>
#include <new>
#include <numbers>
#include <numeric>
#include <optional>
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <ostream>
#endif
#include <print>
#include <queue>
#include <random>
#include <ranges>
#include <ratio>
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <regex>
#endif
#include <scoped_allocator>
#if !defined(_LIBCPP_HAS_NO_THREADS)
# include <semaphore>
#endif
#include <set>
#if !defined(_LIBCPP_HAS_NO_THREADS)
# include <shared_mutex>
#endif
#include <source_location>
#include <span>
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <sstream>
#endif
#include <stack>
#include <stdexcept>
#if !defined(_LIBCPP_HAS_NO_THREADS)
# include <stop_token>
#endif
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <streambuf>
#endif
#include <string>
#include <string_view>
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <strstream>
#endif
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
# include <syncstream>
#endif
#include <system_error>
#if !defined(_LIBCPP_HAS_NO_THREADS)
# include <thread>
#endif
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <variant>
#include <vector>
#include <version>

// *** Headers not yet available ***
#if __has_include(<debugging>)
# error "please update the header information for <debugging> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<debugging>)
#if __has_include(<flat_map>)
# error "please update the header information for <flat_map> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<flat_map>)
#if __has_include(<flat_set>)
# error "please update the header information for <flat_set> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<flat_set>)
#if __has_include(<generator>)
# error "please update the header information for <generator> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<generator>)
#if __has_include(<hazard_pointer>)
# error "please update the header information for <hazard_pointer> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<hazard_pointer>)
#if __has_include(<linalg>)
# error "please update the header information for <linalg> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<linalg>)
#if __has_include(<rcu>)
# error "please update the header information for <rcu> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<rcu>)
#if __has_include(<spanstream>)
# error "please update the header information for <spanstream> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<spanstream>)
#if __has_include(<stacktrace>)
# error "please update the header information for <stacktrace> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<stacktrace>)
#if __has_include(<stdfloat>)
# error "please update the header information for <stdfloat> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<stdfloat>)
#if __has_include(<text_encoding>)
# error "please update the header information for <text_encoding> in headers_not_available in utils/libcxx/header_information.py"
#endif // __has_include(<text_encoding>)

export module std.compat;

@LIBCXX_MODULE_STD_INCLUDE_SOURCES@
@LIBCXX_MODULE_STD_COMPAT_INCLUDE_SOURCES@
Comment on lines +205 to +208
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
export module std.compat;
@LIBCXX_MODULE_STD_INCLUDE_SOURCES@
@LIBCXX_MODULE_STD_COMPAT_INCLUDE_SOURCES@
export module std.compat;
export import std;
@LIBCXX_MODULE_STD_COMPAT_INCLUDE_SOURCES@

I feel this may have better compile-time performance when users write:

import std;
import std.compat;

In this case, the compiler needs to handle the redeclarations in module std and module std.compat. Then it may have the same problem as we discussed before.

While the example itself looks ridiculous, I feel it is highly possible in real worlds:

// a.cppm
export module a;
import std;

// b.cppm
export module b;
import std.compat;

// some_user.cc
import a;
import b;

Then the compiler needs to handle the redeclarations.

12 changes: 12 additions & 0 deletions libcxx/modules/std.compat/cassert.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

export {
// This module exports nothing.
} // export
25 changes: 25 additions & 0 deletions libcxx/modules/std.compat/cctype.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

export {
using ::isalnum;
using ::isalpha;
using ::isblank;
using ::iscntrl;
using ::isdigit;
using ::isgraph;
using ::islower;
using ::isprint;
using ::ispunct;
using ::isspace;
using ::isupper;
using ::isxdigit;
using ::tolower;
using ::toupper;
} // export
12 changes: 12 additions & 0 deletions libcxx/modules/std.compat/cerrno.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

export {
// This module exports nothing.
} // export
29 changes: 29 additions & 0 deletions libcxx/modules/std.compat/cfenv.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

export {
// types
using ::fenv_t;
using ::fexcept_t;

// functions
using ::feclearexcept;
using ::fegetexceptflag;
using ::feraiseexcept;
using ::fesetexceptflag;
using ::fetestexcept;

using ::fegetround;
using ::fesetround;

using ::fegetenv;
using ::feholdexcept;
using ::fesetenv;
using ::feupdateenv;
} // export
Loading