summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-07-11 22:48:30 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-07-11 22:48:30 +0800
commit3ad39e9878310e5317ee73c9b353c1e2c1c5c4ee (patch)
tree4bd5a49ef60ad1e371c0984cd46838368c5ef83a /scripts
parent48ef95c24e9ac3d11200b61af617a01b10b507d1 (diff)
downloadlume-3ad39e9878310e5317ee73c9b353c1e2c1c5c4ee.tar.gz
lume-3ad39e9878310e5317ee73c9b353c1e2c1c5c4ee.tar.xz
lib: establish Zircon libc++ and KTL foundation
Diffstat (limited to 'scripts')
-rw-r--r--scripts/Kbuild.include5
-rw-r--r--scripts/build/environments/kernel.mk2
-rwxr-xr-xscripts/build/probe-toolchain.sh24
-rw-r--r--scripts/build/probes/libc_headers.c10
-rw-r--r--scripts/build/probes/libc_headers.cc12
-rw-r--r--scripts/build/probes/libcxx.cc16
-rw-r--r--scripts/build/toolchain.mk45
7 files changed, 103 insertions, 11 deletions
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 7ccc03d..611499d 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -131,7 +131,10 @@ if_changed = @scripts/build/run-if-changed.sh \
quiet_cmd_toolchain_probe = PROBE toolchain
cmd_toolchain_probe = scripts/build/probe-toolchain.sh $@ \
$(CC) $(CXX) $(LD) $(COMPILER_FAMILY) $(TARGET_TRIPLE) \
- '$(GCC_TOOLCHAIN)' '$(GCC_SYSROOT)'
+ '$(GCC_TOOLCHAIN)' '$(GCC_SYSROOT)' \
+ '$(KTL_INCLUDE)' '$(KTL_AFTER_INCLUDE)' \
+ '$(KERNEL_LIBC_INCLUDE)' '$(KERNEL_LIBC_AFTER_INCLUDE)' \
+ '$(LIBCXX_INCLUDE)' '$(COMPILER_RESOURCE_INCLUDE)'
quiet_cmd_phys_cc_o_c = PHYSCC $(PRIVATE_SOURCE)
cmd_phys_cc_o_c = $(CC) $(PHYS_CFLAGS) -MMD -MP -c $(PRIVATE_SOURCE) -o $@
diff --git a/scripts/build/environments/kernel.mk b/scripts/build/environments/kernel.mk
index 088cdf0..1d6c8cf 100644
--- a/scripts/build/environments/kernel.mk
+++ b/scripts/build/environments/kernel.mk
@@ -14,7 +14,7 @@ CFLAGS := $(WARNINGS) $(KERNEL_OPT_FLAGS) \
$(TOOLCHAIN_CFLAGS) $(KCPPFLAGS) $(INCLUDES) \
-I$(BUILD)/generated
-CFLAGS_LIBC := $(CFLAGS) -std=gnu23 -Iinclude/libc-shim \
+CFLAGS_LIBC := -Iinclude/libc-shim $(CFLAGS) -std=gnu23 \
-Wno-parentheses -Wno-sign-compare
CXXFLAGS := $(CFLAGS) $(TOOLCHAIN_CXXFLAGS) -std=c++23 \
diff --git a/scripts/build/probe-toolchain.sh b/scripts/build/probe-toolchain.sh
index 2f1f600..218a821 100755
--- a/scripts/build/probe-toolchain.sh
+++ b/scripts/build/probe-toolchain.sh
@@ -10,6 +10,12 @@ family=$5
target=$6
gcc_toolchain=$7
sysroot=$8
+ktl_include=$9
+ktl_after_include=${10}
+libc_include=${11}
+libc_after_include=${12}
+libcxx_include=${13}
+compiler_resource_include=${14}
set --
if [ "$family" = clang ]; then
@@ -28,6 +34,22 @@ probe() {
-fsyntax-only - >/dev/null 2>&1
}
+probe_libcxx() {
+ printf '%s\n' \
+ '#include <ktl/array.h>' \
+ '#include <ktl/atomic.h>' \
+ '#include <ktl/optional.h>' \
+ '#include <ktl/span.h>' \
+ '#include <ktl/string_view.h>' \
+ 'constexpr ktl::array<int, 2> values{1, 2};' \
+ 'static_assert(values[1] == 2);' | \
+ "$cxx" "$@" -std=c++23 -ffreestanding -fno-exceptions -fno-rtti \
+ -nostdinc -I include -I "$ktl_include" -I "$libc_include" \
+ -isystem "$compiler_resource_include" \
+ -isystem "$libcxx_include" -idirafter "$ktl_after_include" \
+ -idirafter "$libc_after_include" -fsyntax-only -x c++ - >/dev/null 2>&1
+}
+
tmp="$output.tmp.$$"
mkdir -p "$(dirname "$output")"
{
@@ -37,6 +59,7 @@ mkdir -p "$(dirname "$output")"
echo "target=$target"
printf 'c23='; if probe c gnu23 "$@"; then echo yes; else echo no; fi
printf 'cxx23='; if probe c++ c++23 "$@"; then echo yes; else echo no; fi
+ printf 'libcxx-headers='; if probe_libcxx "$@"; then echo yes; else echo no; fi
printf 'fno-pie='; if probe c++ c++23 "$@" -fno-pie; then echo yes; else echo no; fi
printf 'mcmodel-medany='; if probe c++ c++23 "$@" -mcmodel=medany; then echo yes; else echo no; fi
} > "$tmp"
@@ -44,5 +67,6 @@ mv -f "$tmp" "$output"
grep -q '^c23=yes$' "$output"
grep -q '^cxx23=yes$' "$output"
+grep -q '^libcxx-headers=yes$' "$output"
grep -q '^fno-pie=yes$' "$output"
grep -q '^mcmodel-medany=yes$' "$output"
diff --git a/scripts/build/probes/libc_headers.c b/scripts/build/probes/libc_headers.c
new file mode 100644
index 0000000..e4bdd20
--- /dev/null
+++ b/scripts/build/probes/libc_headers.c
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <assert.h>
+#include <inttypes.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+_Static_assert(sizeof(uintptr_t) == sizeof(void*), "uintptr_t must hold a pointer");
diff --git a/scripts/build/probes/libc_headers.cc b/scripts/build/probes/libc_headers.cc
new file mode 100644
index 0000000..1085cd3
--- /dev/null
+++ b/scripts/build/probes/libc_headers.cc
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <assert.h>
+#include <inttypes.h>
+#include <multi-file.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string-file.h>
+#include <string.h>
+
+static_assert(sizeof(uintptr_t) == sizeof(void*));
diff --git a/scripts/build/probes/libcxx.cc b/scripts/build/probes/libcxx.cc
new file mode 100644
index 0000000..8237151
--- /dev/null
+++ b/scripts/build/probes/libcxx.cc
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <array>
+#include <atomic>
+#include <optional>
+#include <span>
+#include <string_view>
+#include <type_traits>
+
+constexpr std::array<int, 3> kValues{1, 2, 3};
+static_assert(kValues[2] == 3);
+static_assert(std::is_same_v<std::span<const int>::element_type, const int>);
+static_assert(std::string_view{"libc++"}.size() == 6);
+
+using Optional = std::optional<unsigned long>;
+using Atomic = std::atomic<unsigned long>;
diff --git a/scripts/build/toolchain.mk b/scripts/build/toolchain.mk
index 49d4f0a..d244981 100644
--- a/scripts/build/toolchain.mk
+++ b/scripts/build/toolchain.mk
@@ -10,10 +10,30 @@ LLVM ?= 0
LLVM_VERSION ?= 20
LD_TYPE ?= bfd
LLVM_BINUTILS ?= 0
-CXX_STDLIB ?= libstdc++
+CXX_STDLIB ?= libc++
CXX_ABI ?= lume
COMPILER_RUNTIME ?= compiler-default
+# One pinned libc++ header snapshot is shared by GCC and Clang. Platform C
+# declarations and libc++ configuration come from the Zircon libc/KTL layer,
+# not from a Linux or Fuchsia userland __config_site.
+LIBCXX_ROOT ?= third_party/libcxx
+LIBCXX_INCLUDE := $(LIBCXX_ROOT)/include/c++/v1
+KTL_INCLUDE := lib/ktl/include
+KTL_AFTER_INCLUDE := lib/ktl/include-after
+KERNEL_LIBC_INCLUDE := lib/libc/include
+KERNEL_LIBC_AFTER_INCLUDE := lib/libc/include-after
+
+ifeq ($(wildcard $(LIBCXX_INCLUDE)/__config),)
+$(error missing pinned libc++ headers under '$(LIBCXX_INCLUDE)')
+endif
+ifeq ($(wildcard $(KTL_INCLUDE)/__config_site),)
+$(error missing KTL libc++ configuration under '$(KTL_INCLUDE)')
+endif
+ifneq ($(CXX_STDLIB),libc++)
+$(error unsupported CXX_STDLIB '$(CXX_STDLIB)' (LumeOS uses its pinned libc++ headers))
+endif
+
GCC_TOOLCHAIN ?=
CROSS_COMPILE ?=
@@ -64,29 +84,36 @@ CC := $(CLANG)
CXX := $(CLANGXX)
COMPILER_FAMILY := clang
TOOLCHAIN_CFLAGS := $(CLANG_TARGET_FLAGS)
-ifeq ($(CXX_STDLIB),libstdc++)
-TOOLCHAIN_CXXFLAGS := -stdlib=libstdc++
-else
-$(error unsupported CXX_STDLIB '$(CXX_STDLIB)' for the current imported runtime)
-endif
+TOOLCHAIN_CXXFLAGS :=
+COMPILER_RESOURCE_DIR := $(strip $(shell $(CXX) $(CLANG_TARGET_FLAGS) -print-resource-dir 2>/dev/null))
+COMPILER_RESOURCE_INCLUDE := $(COMPILER_RESOURCE_DIR)/include
ifeq ($(LD_TYPE),lld)
LD := $(LLD)
LINKER_FAMILY := lld
else
-ifneq ($(CXX_STDLIB),libstdc++)
-$(error GCC configuration currently requires CXX_STDLIB=libstdc++)
-endif
LD := $(CROSS_COMPILE)ld
LINKER_FAMILY := bfd
endif
+
else
CC := $(CROSS_COMPILE)gcc
CXX := $(CROSS_COMPILE)g++
LD := $(CROSS_COMPILE)ld
COMPILER_FAMILY := gcc
LINKER_FAMILY := bfd
+COMPILER_RESOURCE_INCLUDE := $(strip $(shell $(CXX) -print-file-name=include 2>/dev/null))
endif
+ifeq ($(wildcard $(COMPILER_RESOURCE_INCLUDE)/stddef.h),)
+$(error missing compiler resource headers under '$(COMPILER_RESOURCE_INCLUDE)')
+endif
+
+# Kernel and phys code must not observe the Linux target sysroot. Supply only
+# compiler intrinsic headers explicitly; the target graph provides the kernel
+# libc and KTL layers, followed by the pinned libc++ headers for C++.
+TOOLCHAIN_CFLAGS += -nostdinc -isystem $(COMPILER_RESOURCE_INCLUDE)
+TOOLCHAIN_CXXFLAGS += -isystem $(LIBCXX_INCLUDE)
+
ifeq ($(LLVM_BINUTILS),1)
AR := llvm-ar$(CLANG_SUFFIX)
NM := llvm-nm$(CLANG_SUFFIX)