- 
                Notifications
    You must be signed in to change notification settings 
- Fork 15k
[libcxx][modules] Fix missing includes for windows #158781
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
base: main
Are you sure you want to change the base?
Conversation
| @llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-libcxx Author: Matt (matts1) ChangesPreviously, I was getting the following error when attempting to compile libc++ on windows with modules enabled. Full diff: https://github.com/llvm/llvm-project/pull/158781.diff 1 Files Affected: 
 diff --git a/libcxx/include/__new/align_val_t.h b/libcxx/include/__new/align_val_t.h
index 03ab7cb143a2b..36fc9f3365062 100644
--- a/libcxx/include/__new/align_val_t.h
+++ b/libcxx/include/__new/align_val_t.h
@@ -16,14 +16,19 @@
 #  pragma GCC system_header
 #endif
 
+#if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+#if defined(_LIBCPP_ABI_VCRUNTIME)
+// std::align_val_t is defined in vcruntime_new.h
+#include <vcruntime_new.h>
+#else
 _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
-#if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION && !defined(_LIBCPP_ABI_VCRUNTIME)
 #  ifndef _LIBCPP_CXX03_LANG
 enum class align_val_t : size_t {};
 #  else
 enum align_val_t { __zero = 0, __max = (size_t)-1 };
 #  endif
-#endif
 _LIBCPP_END_UNVERSIONED_NAMESPACE_STD
+#endif
+#endif
 
 #endif // _LIBCPP___NEW_ALIGN_VAL_T_H
 | 
86f27e3    to
    7abcb82      
    Compare
  
    | ✅ With the latest revision this PR passed the C/C++ code formatter. | 
7abcb82    to
    453c945      
    Compare
  
    | #150182 is related. Do you have any idea why did the error only happen with modules? Can we add a test case or unblock an existing one? | 
| I don't know for sure, but my suspicion would be that with a non-modules build,  Adding a testcase is probably not feasible because last I checked, libc++ wasn't set up to compile the whole of std as a clang module, since compiling std as a clang module requires the underlying platform to be a clang module. It only works for me because I've gone to the effort of generating modulemaps for the target platform for all the sysroots we use in chromium. | 
| @matts1 Is there any reason not to have these modulemaps upstream? I assume you'd want to maintain them in the future and having them upstream would make it significantly less likely that we regress that configuration. | 
| Unfortunately, that's more complex than I'd like. Modulemaps aren't specific to a platform, so much as they're specific to a (platform, sysroot version) pair. This works for us because we only support one version of a sysroot at a time, and the sysroot version is stored in version control, so we just update the sysroot version and the modulemaps at the same time, and we have a script to generate the updated modulemap. The real solution here is to have have the sysroots provide their own modulemap files similar to how libc++ provides a module.modulemap. Apple already does this and provides modulemap files for their sysroot, but they're currently the only platform to do so. We could, of course, do something similar in libc++, but there's a lot of obstacles: 
 | 
453c945    to
    641ffd0      
    Compare
  
    Previously, I was getting the following error when attempting to compile
libc++ on windows with modules enabled.
```
While building module 'std':
In file included from <module-includes>:1:
In file included from gen/third_party/libc++/src/include/algorithm:1865:
In file included from gen/third_party/libc++/src/include/__algorithm/inplace_merge.h:28:
In file included from gen/third_party/libc++/src/include/__memory/unique_temporary_buffer.h:17:
In file included from gen/third_party/libc++/src/include/__memory/allocator.h:19:
gen/third_party/libc++/src/include/__new/allocate.h(40,73): error: 
      declaration of 'align_val_t' must be imported from module
      'sys_stage1.sysroot_vcruntime_new_h' before it is required
   40 |     return static_cast<_Tp*>(__builtin_operator_new(__size, static_cast<align_val...
      |                                                                         ^
../../third_party/depot_tools/win_toolchain/vs_files/e4305f407e/VC/Tools/MSVC/14.44.35207/include/vcruntime_new.h(27,33): note: 
      declaration here is not visible
   27 |     _VCRT_EXPORT_STD enum class align_val_t : size_t {};
      |                                 ^
```
I also received a similar error for `std::bad_alloc`.
We have an include chain `__new/exceptions.h` => `__exception/exception.h` => `vcruntime_exception.h` => `vcruntime_new.h`. This means that it works fine with non-modules because `vcrunmite_exception.h` and `vcruntime_new.h` were both in the transitive includes.
However, this fails with modules because `__new/exceptions.h` no longer exports symbols from
`__exception/exception.h`, and thus `std::bad_alloc` and `std::align_val_t` are no longer visible.
    641ffd0    to
    b48066f      
    Compare
  
    | FWIW, I'm certainly not opposed to adding sysroot modulemaps, but it's something that I can't personally contribute development time to (though I could contribute my expertise and reviews). In the current state this is blocking us from being able to use modules on windows, so I'd really like to submit this without an associated test, since adding a test for it would take setting up a very large amount of infra. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change itself looks good to me.
Can we get a +1 from a libc++ maintainer?
| @ldionne ping? | 
Previously, I was getting the following error when attempting to compile libc++ on windows with modules enabled.