From 5dfe2eb389fc45f06523611cd32df55fb53027fc Mon Sep 17 00:00:00 2001 From: implicitfield <114500360+implicitfield@users.noreply.github.com> Date: Fri, 28 Apr 2023 22:55:59 +0400 Subject: [PATCH] Everywhere: Resolve conflicts with LibC and libc++ Since https://reviews.llvm.org/D131441, libc++ must be included before LibC. As clang includes libc++ as one of the system includes, LibC must be included after those, and the only correct way to do that is to install LibC's headers into the sysroot. Targets that don't link with LibC yet require its headers for one reason or another must add install_libc_headers as a dependency to ensure that the correct headers have been (re)installed into the sysroot. LibC/stddef.h has been dropped since the built-in stddef.h receives a higher include priority. In addition, string.h and wchar.h must define __CORRECT_ISO_CPP_STRING_H_PROTO and _LIBCPP_WCHAR_H_HAS_CONST_OVERLOADS respectively in order to tell libc++ to not try to define methods implemented by LibC. --- CMakeLists.txt | 1 - Kernel/CMakeLists.txt | 3 ++- Kernel/Library/StdLib.h | 3 +-- Kernel/Prekernel/CMakeLists.txt | 1 + Userland/DynamicLoader/CMakeLists.txt | 1 + Userland/Libraries/LibC/CMakeLists.txt | 24 ++++++++++++++++++- Userland/Libraries/LibC/stddef.h | 20 ---------------- Userland/Libraries/LibC/string.h | 4 ++++ Userland/Libraries/LibC/wchar.h | 4 ++++ Userland/Libraries/LibCrypt/CMakeLists.txt | 1 + .../Libraries/LibSanitizer/CMakeLists.txt | 2 ++ Userland/Libraries/LibSystem/CMakeLists.txt | 2 ++ Userland/Libraries/LibTest/CMakeLists.txt | 5 ++++ Userland/Libraries/LibTimeZone/CMakeLists.txt | 3 +++ 14 files changed, 49 insertions(+), 25 deletions(-) delete mode 100644 Userland/Libraries/LibC/stddef.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 01808e2d74e..2a9522ec061 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -183,7 +183,6 @@ add_link_options(-Wno-unused-command-line-argument) include_directories(.) include_directories(Userland/Libraries) -include_directories(Userland/Libraries/LibC) include_directories(Userland/Libraries/LibCrypt) include_directories(Userland/Libraries/LibSystem) include_directories(Userland/Services) diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index 1aceb78b85b..ea039070886 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -693,9 +693,10 @@ add_compile_definitions(KERNEL) add_link_options(LINKER:-z,notext) add_library(kernel_heap STATIC ${KERNEL_HEAP_SOURCES}) +add_dependencies(kernel_heap install_libc_headers) add_executable(Kernel ${SOURCES}) -add_dependencies(Kernel generate_EscapeSequenceStateMachine.h generate_version_header) +add_dependencies(Kernel generate_EscapeSequenceStateMachine.h generate_version_header install_libc_headers) if (NOT "${SERENITY_ARCH}" STREQUAL "aarch64") add_custom_command( diff --git a/Kernel/Library/StdLib.h b/Kernel/Library/StdLib.h index ead5b06c60d..d9572d998ec 100644 --- a/Kernel/Library/StdLib.h +++ b/Kernel/Library/StdLib.h @@ -13,6 +13,7 @@ #include #include #include +#include ErrorOr> try_copy_kstring_from_user(Userspace, size_t); ErrorOr copy_time_from_user(timespec const*); @@ -51,8 +52,6 @@ void const* memmem(void const* haystack, size_t, void const* needle, size_t); [[nodiscard]] inline u16 htons(u16 w) { return (w & 0xff) << 8 | ((w >> 8) & 0xff); } } -#define offsetof(type, member) __builtin_offsetof(type, member) - template [[nodiscard]] inline ErrorOr copy_from_user(T* dest, T const* src) { diff --git a/Kernel/Prekernel/CMakeLists.txt b/Kernel/Prekernel/CMakeLists.txt index a5d4cbc6b02..cdff1582a56 100644 --- a/Kernel/Prekernel/CMakeLists.txt +++ b/Kernel/Prekernel/CMakeLists.txt @@ -16,6 +16,7 @@ endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static") add_executable(${PREKERNEL_TARGET} ${SOURCES}) +add_dependencies(${PREKERNEL_TARGET} install_libc_headers) target_compile_options(${PREKERNEL_TARGET} PRIVATE -no-pie -fno-pic -fno-threadsafe-statics) target_link_options(${PREKERNEL_TARGET} PRIVATE LINKER:-T ${CMAKE_CURRENT_SOURCE_DIR}/linker.ld -nostdlib LINKER:--no-pie) diff --git a/Userland/DynamicLoader/CMakeLists.txt b/Userland/DynamicLoader/CMakeLists.txt index 761dfbdc9ec..b61ddad788f 100644 --- a/Userland/DynamicLoader/CMakeLists.txt +++ b/Userland/DynamicLoader/CMakeLists.txt @@ -47,6 +47,7 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") endif() add_executable(Loader.so ${SOURCES}) +add_dependencies(Loader.so install_libc_headers) if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") target_link_libraries(Loader.so PRIVATE gcc) diff --git a/Userland/Libraries/LibC/CMakeLists.txt b/Userland/Libraries/LibC/CMakeLists.txt index a0d388de548..b42fb30d076 100644 --- a/Userland/Libraries/LibC/CMakeLists.txt +++ b/Userland/Libraries/LibC/CMakeLists.txt @@ -76,6 +76,25 @@ set(LIBC_SOURCES wstdio.cpp ) +file(GLOB_RECURSE LIBC_HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" CONFIGURE_DEPENDS "*.h") +list(APPEND LIBC_HEADERS "../LibELF/ELFABI.h" "../LibRegex/RegexDefs.h") + +add_custom_target(install_libc_headers) + +# Copy LibC's headers into the sysroot to satisfy libc++'s include priority requirements. +foreach(RELATIVE_HEADER_PATH IN LISTS LIBC_HEADERS) + get_filename_component(directory ${RELATIVE_HEADER_PATH} DIRECTORY) + string(REPLACE "../" "" subdirectory "${directory}") + file(MAKE_DIRECTORY "${CMAKE_STAGING_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/${subdirectory}") + add_custom_command( + TARGET install_libc_headers + PRE_BUILD + COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${RELATIVE_HEADER_PATH}" "${CMAKE_STAGING_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/${subdirectory}" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + VERBATIM + ) +endforeach() + file(GLOB ELF_SOURCES CONFIGURE_DEPENDS "../LibELF/*.cpp") if ("${SERENITY_ARCH}" STREQUAL "aarch64") @@ -98,12 +117,14 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-warning-option") # Since all native serenity applications use dynamic libraries, prevent coverage on libc.a as well add_library(crt0 STATIC crt0.cpp) +add_dependencies(crt0 install_libc_headers) target_link_libraries(crt0 PRIVATE NoCoverage) add_custom_command( TARGET crt0 COMMAND "${CMAKE_COMMAND}" -E copy $ ${CMAKE_INSTALL_PREFIX}/usr/lib/crt0.o ) add_library(crt0_shared STATIC crt0_shared.cpp) +add_dependencies(crt0_shared install_libc_headers) target_link_libraries(crt0_shared PRIVATE NoCoverage) add_custom_command( TARGET crt0_shared @@ -126,6 +147,7 @@ add_custom_command( set_source_files_properties (ssp_nonshared.cpp PROPERTIES COMPILE_FLAGS "-fno-stack-protector") add_library(ssp_nonshared STATIC ssp_nonshared.cpp) +add_dependencies(ssp_nonshared install_libc_headers) target_link_libraries(ssp_nonshared PRIVATE NoCoverage) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libssp_nonshared.a DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/lib/) @@ -164,7 +186,7 @@ set_property( set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nolibc") serenity_libc(LibC c) -add_dependencies(LibC crti crt0 crt0_shared crtn) +add_dependencies(LibC crti crt0 crt0_shared crtn install_libc_headers) target_link_libraries(LibC PRIVATE LibSystem LibTimeZone) # We mark LibCStatic as a dependency of LibC because this triggers the build of the LibCStatic target diff --git a/Userland/Libraries/LibC/stddef.h b/Userland/Libraries/LibC/stddef.h deleted file mode 100644 index 7d9655c3020..00000000000 --- a/Userland/Libraries/LibC/stddef.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include - -#define offsetof(type, member) __builtin_offsetof(type, member) - -#ifdef __cplusplus -# define NULL nullptr -#else -# define NULL ((void*)0) -#endif - -typedef __PTRDIFF_TYPE__ ptrdiff_t; -typedef __SIZE_TYPE__ size_t; diff --git a/Userland/Libraries/LibC/string.h b/Userland/Libraries/LibC/string.h index 824c2d7c28f..23ec5ef69f9 100644 --- a/Userland/Libraries/LibC/string.h +++ b/Userland/Libraries/LibC/string.h @@ -12,6 +12,10 @@ __BEGIN_DECLS +#ifdef __cplusplus +# define __CORRECT_ISO_CPP_STRING_H_PROTO +#endif + // A few C Standard Libraries include this header in , and hence expect // `strcasecmp` etcetera to be available as part of a include, so let's // do the same here to maintain compatibility diff --git a/Userland/Libraries/LibC/wchar.h b/Userland/Libraries/LibC/wchar.h index 909b3b8aa27..b6052f7c81d 100644 --- a/Userland/Libraries/LibC/wchar.h +++ b/Userland/Libraries/LibC/wchar.h @@ -18,6 +18,10 @@ __BEGIN_DECLS # define WEOF (0xffffffffu) #endif +#ifdef __cplusplus +# define _LIBCPP_WCHAR_H_HAS_CONST_OVERLOADS +#endif + typedef __WINT_TYPE__ wint_t; typedef unsigned long int wctype_t; diff --git a/Userland/Libraries/LibCrypt/CMakeLists.txt b/Userland/Libraries/LibCrypt/CMakeLists.txt index 6ecc98ca1c4..46101c37f6c 100644 --- a/Userland/Libraries/LibCrypt/CMakeLists.txt +++ b/Userland/Libraries/LibCrypt/CMakeLists.txt @@ -3,6 +3,7 @@ # To avoid a circular dependency chain with LibCrypt --> LibCrypto --> LibCore --> LibCrypt # We include the SHA2 implementation from LibCrypto here manually add_library(LibCryptSHA2 OBJECT ../LibCrypto/Hash/SHA2.cpp) +add_dependencies(LibCryptSHA2 install_libc_headers) set_target_properties(LibCryptSHA2 PROPERTIES CXX_VISIBILITY_PRESET hidden) set_target_properties(LibCryptSHA2 PROPERTIES VISIBILITY_INLINES_HIDDEN ON) diff --git a/Userland/Libraries/LibSanitizer/CMakeLists.txt b/Userland/Libraries/LibSanitizer/CMakeLists.txt index 988e8b29710..fe43a55eb67 100644 --- a/Userland/Libraries/LibSanitizer/CMakeLists.txt +++ b/Userland/Libraries/LibSanitizer/CMakeLists.txt @@ -9,6 +9,8 @@ set_source_files_properties(../LibC/ssp_nonshared.cpp PROPERTIES COMPILE_FLAGS " set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib") serenity_libc(LibUBSanitizer ubsan) +add_dependencies(LibUBSanitizer install_libc_headers) add_library(LibUBSanitizerStatic STATIC ${SOURCES}) +add_dependencies(LibUBSanitizerStatic install_libc_headers) target_link_libraries(LibUBSanitizerStatic PRIVATE NoCoverage) diff --git a/Userland/Libraries/LibSystem/CMakeLists.txt b/Userland/Libraries/LibSystem/CMakeLists.txt index a8932887751..95992e7c234 100644 --- a/Userland/Libraries/LibSystem/CMakeLists.txt +++ b/Userland/Libraries/LibSystem/CMakeLists.txt @@ -4,8 +4,10 @@ set(SOURCES set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib") serenity_libc(LibSystem system) +add_dependencies(LibSystem install_libc_headers) target_include_directories(LibSystem PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) add_library(LibSystemStatic STATIC ${SOURCES}) +add_dependencies(LibSystemStatic install_libc_headers) target_include_directories(LibSystemStatic PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(LibSystemStatic PRIVATE NoCoverage) diff --git a/Userland/Libraries/LibTest/CMakeLists.txt b/Userland/Libraries/LibTest/CMakeLists.txt index 0800106eb40..a1ca2eea8f9 100644 --- a/Userland/Libraries/LibTest/CMakeLists.txt +++ b/Userland/Libraries/LibTest/CMakeLists.txt @@ -9,3 +9,8 @@ serenity_lib(LibTest test) add_library(LibTestMain OBJECT TestMain.cpp) add_library(JavaScriptTestRunnerMain OBJECT JavaScriptTestRunnerMain.cpp) + +if (SERENITYOS) + add_dependencies(LibTestMain install_libc_headers) + add_dependencies(JavaScriptTestRunnerMain install_libc_headers) +endif() diff --git a/Userland/Libraries/LibTimeZone/CMakeLists.txt b/Userland/Libraries/LibTimeZone/CMakeLists.txt index c1e9f992ae4..a92f60a8b5b 100644 --- a/Userland/Libraries/LibTimeZone/CMakeLists.txt +++ b/Userland/Libraries/LibTimeZone/CMakeLists.txt @@ -13,3 +13,6 @@ target_compile_definitions(LibTimeZone PRIVATE ENABLE_TIME_ZONE_DATA=$