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.
This commit is contained in:
implicitfield 2023-04-28 22:55:59 +04:00 committed by Andreas Kling
parent 58c4e266e9
commit 5dfe2eb389
14 changed files with 49 additions and 25 deletions

View File

@ -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)

View File

@ -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(

View File

@ -13,6 +13,7 @@
#include <AK/Userspace.h>
#include <Kernel/Library/KString.h>
#include <Kernel/UnixTypes.h>
#include <stddef.h>
ErrorOr<NonnullOwnPtr<Kernel::KString>> try_copy_kstring_from_user(Userspace<char const*>, size_t);
ErrorOr<Duration> 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<typename T>
[[nodiscard]] inline ErrorOr<void> copy_from_user(T* dest, T const* src)
{

View File

@ -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)

View File

@ -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)

View File

@ -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 $<TARGET_OBJECTS:crt0> ${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

View File

@ -1,20 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <sys/cdefs.h>
#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;

View File

@ -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 <string.h>, and hence expect
// `strcasecmp` etcetera to be available as part of a <string.h> include, so let's
// do the same here to maintain compatibility

View File

@ -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;

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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()

View File

@ -13,3 +13,6 @@ target_compile_definitions(LibTimeZone PRIVATE ENABLE_TIME_ZONE_DATA=$<BOOL:${EN
# NOTE: These objects are used by the DynamicLoader, which is always built without coverage instrumentation.
# We could allow them to be instrumented for coverage if DynamicLoader built its own copy
target_link_libraries(LibTimeZone PRIVATE NoCoverage)
if (SERENITYOS)
add_dependencies(LibTimeZone install_libc_headers)
endif()