Skip to content
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

Add support for std #1069

Open
wants to merge 12 commits into
base: theseus_main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ github_pages/doc/

# library lock files
/libs/**/Cargo.lock

# TODO: Remove once std is upstreamed
.cargo/
Copy link
Member

Choose a reason for hiding this comment

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

what is this, something that the build of rust generates?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we set the specific path to our custom rustc installation in .cargo/config.toml.

1 change: 1 addition & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ports/rust
kevinaboos marked this conversation as resolved.
Show resolved Hide resolved
tsoutsman marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ default-members = [


exclude = [
"shim",
"applications/test_std",

## Exclude the build directories
"build",
"target",
Expand Down
78 changes: 65 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ debug ?= none
net ?= none
merge_sections ?= yes
bootloader ?= grub
std ?= no

ifeq ($(std),yes)
export override FEATURES+=--features std
endif

## aarch64 only supports booting via UEFI
ifeq ($(ARCH),aarch64)
Expand All @@ -46,6 +51,7 @@ else
$(error Error:unsupported option "boot_spec=$(boot_spec)". Options are 'bios' or 'uefi')
endif

HOST_TRIPLE = $(shell rustc -vV | sed -n 's|host: ||p')
## test for Windows Subsystem for Linux (Linux on Windows)
IS_WSL = $(shell grep -is 'microsoft' /proc/version)

Expand All @@ -68,6 +74,7 @@ THESEUS_CARGO := $(ROOT_DIR)/tools/theseus_cargo
THESEUS_CARGO_BIN := $(THESEUS_CARGO)/bin/theseus_cargo
EXTRA_FILES := $(ROOT_DIR)/extra_files
LIMINE_DIR := $(ROOT_DIR)/limine-prebuilt
RUST_SOURCE := $(ROOT_DIR)/ports/rust
tsoutsman marked this conversation as resolved.
Show resolved Hide resolved


### Set up tool names/locations for cross-compiling on a Mac OS / macOS host (Darwin).
Expand All @@ -77,6 +84,7 @@ ifeq ($(UNAME),Darwin)
## macOS uses a different unmounting utility
UNMOUNT = diskutil unmount
USB_DRIVES = $(shell diskutil list external | grep -s "/dev/" | awk '{print $$1}')
HEAD = ghead
else
## Handle building for aarch64 on x86_64 Linux/WSL
ifeq ($(ARCH),aarch64)
Expand All @@ -85,6 +93,7 @@ else
## Just use normal umount on Linux/WSL
UNMOUNT = umount
USB_DRIVES = $(shell lsblk -O | grep -i usb | awk '{print $$2}' | grep --color=never '[^0-9]$$')
HEAD = head
endif

### Handle multiple bootloader options and ensure the corresponding tools are installed.
Expand Down Expand Up @@ -157,7 +166,6 @@ APP_CRATE_NAMES += $(EXTRA_APP_CRATE_NAMES)
check-usb \
clean clean-doc clean-old-build \
orun orun_pause run run_pause iso build cargo copy_kernel $(bootloader) extra_files \
libtheseus \
simd_personality_sse build_sse simd_personality_avx build_avx \
gdb gdb_aarch64 \
clippy doc docs view-doc view-docs book view-book
Expand Down Expand Up @@ -250,7 +258,6 @@ build: $(nano_core_binary)
-a ./applications \
--kernel-prefix $(KERNEL_PREFIX) \
--app-prefix $(APP_PREFIX) \
-e "$(EXTRA_APP_CRATE_NAMES) libtheseus"

## Third, perform partial linking on each object file, which shrinks their size
## and most importantly, accelerates their loading and linking at runtime.
Expand Down Expand Up @@ -313,10 +320,42 @@ endif
### end of "build" target ###
#############################



## This target invokes the actual Rust build process via `cargo`.
cargo:
ifeq ($(std),yes)
if [ ! -d $(RUST_SOURCE) ] ; then \
git clone https://github.com/theseus-os/rust.git --branch theseus-std-5 $(RUST_SOURCE); \
git -c $(RUST_SOURCE) checkout 3167b29; \
git -c $(RUST_SOURCE) sumbodule update --init; \
fi

## Cache std/Cargo.toml
cp $(RUST_SOURCE)/library/std/Cargo.toml $(ROOT_DIR)/std-cargo.toml
## Remove the last line of std/Cargo.toml
@$(HEAD) -n -1 $(RUST_SOURCE)/library/std/Cargo.toml > $(ROOT_DIR)/temp-std-cargo.toml
@mv $(ROOT_DIR)/temp-std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml
## Add the correct dependency path, since the dependency path in std/Cargo.toml assumes that std is
## being built using -Zbuild-std.
@echo "theseus-shim = { path = \"../../../../shim\", features = ['rustc-dep-of-std'] }" >> $(RUST_SOURCE)/library/std/Cargo.toml

## Build the compiler
@cd $(RUST_SOURCE) && RUSTFLAGS="" CARGOFLAGS="" ./x.py build library --stage 1
@cd $(ROOT_DIR)

## Remove the last line of std/Cargo.toml
@$(HEAD) -n -1 $(RUST_SOURCE)/library/std/Cargo.toml > $(ROOT_DIR)/temp-std-cargo.toml
@mv $(ROOT_DIR)/temp-std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml
## Restore previous std/Cargo.toml
mv $(ROOT_DIR)/std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml

## Add .cargo/config.toml
@mkdir -p $(ROOT_DIR)/.cargo
@echo "[build]" > $(ROOT_DIR)/.cargo/config.toml
@echo "rustc = \"$(RUST_SOURCE)/build/$(HOST_TRIPLE)/stage1/bin/rustc\"" >> $(ROOT_DIR)/.cargo/config.toml
tsoutsman marked this conversation as resolved.
Show resolved Hide resolved
else
@rm -f $(ROOT_DIR)/.cargo/config.toml
endif

@mkdir -p $(BUILD_DIR)
@mkdir -p $(NANO_CORE_BUILD_DIR)
@mkdir -p $(OBJECT_FILES_BUILD_DIR)
Expand Down Expand Up @@ -470,14 +509,6 @@ c_test:



### Demo/test target for building libtheseus
libtheseus: theseus_cargo $(ROOT_DIR)/libtheseus/Cargo.* $(ROOT_DIR)/libtheseus/src/*
@( \
cd $(ROOT_DIR)/libtheseus && \
$(THESEUS_CARGO_BIN) --input $(DEPS_BUILD_DIR) build; \
)


### This target builds the `theseus_cargo` tool as a dedicated binary.
theseus_cargo: $(wildcard $(THESEUS_CARGO)/Cargo.*) $(wildcard$(THESEUS_CARGO)/src/*)
@echo -e "\n=================== Building the theseus_cargo tool ==================="
Expand All @@ -489,6 +520,26 @@ theseus_cargo: $(wildcard $(THESEUS_CARGO)/Cargo.*) $(wildcard$(THESEUS_CARGO)/
clean:
@rm -rf $(BUILD_DIR)
cargo clean

clean-std: clean
@rm -rf $(ROOT_DIR)/.cargo
## Cache std/Cargo.toml
cp $(RUST_SOURCE)/library/std/Cargo.toml $(ROOT_DIR)/std-cargo.toml
## Remove the last line of std/Cargo.toml
@$(HEAD) -n -1 $(RUST_SOURCE)/library/std/Cargo.toml > $(ROOT_DIR)/temp-std-cargo.toml
@mv $(ROOT_DIR)/temp-std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml
## Add the correct dependency path, since the dependency path in std/Cargo.toml assumes that std is
## being built using -Zbuild-std.
@echo "theseus-shim = { path = \"../../../../shim\", features = ['rustc-dep-of-std'] }" >> $(RUST_SOURCE)/library/std/Cargo.toml

@cd $(RUST_SOURCE) && RUSTFLAGS="" CARGOFLAGS="" ./x.py clean
@cd $(ROOT_DIR)

## Remove the last line of std/Cargo.toml
@$(HEAD) -n -1 $(RUST_SOURCE)/library/std/Cargo.toml > $(ROOT_DIR)/temp-std-cargo.toml
@mv $(ROOT_DIR)/temp-std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml
## Restore previous std/Cargo.toml
mv $(ROOT_DIR)/std-cargo.toml $(RUST_SOURCE)/library/std/Cargo.toml


### Removes only the old files that were copied into the build directory from a previous build.
Expand Down Expand Up @@ -616,6 +667,7 @@ clippy : export override FEATURES := $(subst --workspace,,$(FEATURES))
endif
clippy : export override RUSTFLAGS = $(patsubst %,--cfg %, $(THESEUS_CONFIG))
clippy:
@rm -f $(ROOT_DIR)/.cargo/config.toml
RUST_TARGET_PATH='$(CFG_DIR)' RUSTFLAGS='$(RUSTFLAGS)' \
cargo clippy \
$(BUILD_STD_CARGOFLAGS) $(FEATURES) \
Expand All @@ -629,7 +681,7 @@ RUSTDOC_OUT_FILE := $(RUSTDOC_OUT)/___Theseus_Crates___/index.html

## Builds Theseus's source-level documentation for all Rust crates except applications.
## The entire project is built as normal using the `cargo doc` command (`rustdoc` under the hood).
docs: doc
docs: doc std
doc : export override RUSTDOCFLAGS += -A rustdoc::private_intra_doc_links
doc : export override RUSTFLAGS=
doc : export override CARGOFLAGS=
Expand Down
7 changes: 7 additions & 0 deletions applications/test_std/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions applications/test_std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "test_std"
version = "0.1.0"
edition = "2021"

[dependencies]
7 changes: 7 additions & 0 deletions applications/test_std/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::{string::String, vec::Vec};

pub fn main(_: Vec<String>) -> isize {
println!("Hello, world!");
println!("Task ID: {}", std::process::id());
0
}
2 changes: 1 addition & 1 deletion cfg/Config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ endif
## Also ensure that core memory functions (e.g., memcpy) are included in the build and not name-mangled.
## We keep these flags separate from the regular CARGOFLAGS for purposes of easily creating a sysroot directory.
BUILD_STD_CARGOFLAGS += -Z unstable-options
BUILD_STD_CARGOFLAGS += -Z build-std=core,alloc
BUILD_STD_CARGOFLAGS += -Z build-std
Copy link
Member

Choose a reason for hiding this comment

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

is this no longer needed? Or is this just a change that's left over from experimenting with building std?

If we only need this for "normal" builds of Theseus (without the new std lib) but we cannot use it for builds of the new std lib, then we can change it elsewhere, e.g., as an export override on one of the non-std make targets

BUILD_STD_CARGOFLAGS += -Z build-std-features=compiler-builtins-mem


Expand Down
2 changes: 1 addition & 1 deletion kernel/environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Environment {
/// Changes the current working directory.
#[doc(alias("change"))]
pub fn chdir(&mut self, path: &Path) -> Result<()> {
let new_dir = self.working_dir.lock().get(path.as_ref());
let new_dir = path.get(&self.working_dir);
match new_dir {
Some(FileOrDir::Dir(dir_ref)) => {
self.working_dir = dir_ref;
Expand Down
16 changes: 16 additions & 0 deletions kernel/libtheseus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "libtheseus"
version = "0.1.0"
authors = ["Klim Tsoutsman <klim@tsoutsman.com>"]
description = "Implementation of shim functions for std linkage boundary"
edition = "2021"

[dependencies]
app_io = { path = "../app_io" }
core2 = { version = "0.4.0", default-features = false, features = ["alloc", "nightly"] }
environment = { path = "../environment" }
path = { path = "../path" }
random = { path = "../random" }
theseus_ffi = { path = "../../libs/theseus_ffi" }
task = { path = "../task" }
thread_local_macro = { path = "../thread_local_macro" }
Loading