Skip to content

Commit

Permalink
rust: fix build errors (esp-rs/embuild#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
delan committed Nov 30, 2024
1 parent ea25238 commit 1cc7118
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
3 changes: 0 additions & 3 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[build]
target = "thumbv6m-none-eabi"

[unstable]
build-std = ["core", "panic_abort"]
build-std-features = ["panic_immediate_abort"]
27 changes: 21 additions & 6 deletions platformio.cargo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init_props(self, env):
self.__cargo_ran = False

self.__rust_lib = env.GetProjectOption("rust_lib")
self.__rust_target = env.GetProjectOption("rust_target")
self.__rust_target = env.GetProjectOption("rust_target", default = None)

self.__rust_bindgen_enabled = env.GetProjectOption("rust_bindgen_enabled", default = "false").lower() == "true"
self.__rust_bindgen_extra_clang_args = env.GetProjectOption("rust_bindgen_extra_clang_args", default = "")
Expand Down Expand Up @@ -66,25 +66,40 @@ def __run_cargo(self, source, target, env):
env["ENV"]["CARGO_PIO_BUILD_LINK_FLAGS"] = env.subst("$LINKFLAGS")
env["ENV"]["CARGO_PIO_BUILD_LINK"] = env.subst("$LINK")
env["ENV"]["CARGO_PIO_BUILD_LINKCOM"] = env.subst("$LINKCOM")
env["ENV"]["CARGO_PIO_BUILD_MCU"] = board_mcu
if board_mcu is not None:
env["ENV"]["CARGO_PIO_BUILD_MCU"] = board_mcu

if self.__rust_bindgen_enabled:
env["ENV"]["CARGO_PIO_BUILD_BINDGEN_RUN"] = "True"
env["ENV"]["CARGO_PIO_BUILD_BINDGEN_EXTRA_CLANG_ARGS"] = self.__rust_bindgen_extra_clang_args

env["ENV"]["CARGO_PIO_BUILD_PIO_PLATFORM_DIR"] = env.PioPlatform().get_dir()[0]
env["ENV"]["CARGO_PIO_BUILD_PIO_FRAMEWORK_DIR"] = env.PioPlatform().get_package_dir(env.PioPlatform().frameworks[env.GetProjectOption("framework")[0]]["package"])
pio_platform_dir = env.PioPlatform().get_dir()[0]
if pio_platform_dir is not None:
env["ENV"]["CARGO_PIO_BUILD_PIO_PLATFORM_DIR"] = pio_platform_dir
framework = env.GetProjectOption("framework")
if framework:
pio_framework_dir = env.PioPlatform().get_package_dir(env.PioPlatform().frameworks[framework[0]]["package"])
if pio_framework_dir is not None:
env["ENV"]["CARGO_PIO_BUILD_PIO_FRAMEWORK_DIR"] = pio_framework_dir
if self.__rust_target is not None:
cargo_target_option = f"--target {self.__rust_target}"
else:
cargo_target_option = ""

self.__cargo_ran = True
result = env.Execute(f"cargo build {'--release' if self.__cargo_profile == 'release' else ''} --lib --target {self.__rust_target} {self.__cargo_options}")
result = env.Execute(f"cargo build {'--release' if self.__cargo_profile == 'release' else ''} --lib {cargo_target_option} {self.__cargo_options}")

print("<<< CARGO")

return result

def __link_cargo(self, source, target, env):
env.Prepend(LINKFLAGS = ["-Wl,--allow-multiple-definition"]) # A hack to workaround this issue with Rust's compiler intrinsics: https://github.com/rust-lang/compiler-builtins/issues/353
env.Prepend(LIBPATH = [env.subst(os.path.join(self.__cargo_target_dir, self.__rust_target, self.__cargo_profile))])
if self.__rust_target is not None:
cargo_profile_path = os.path.join(self.__cargo_target_dir, self.__rust_target, self.__cargo_profile)
else:
cargo_profile_path = os.path.join(self.__cargo_target_dir, self.__cargo_profile)
env.Prepend(LIBPATH = [env.subst(cargo_profile_path)])
env.Prepend(LIBS = [self.__rust_lib])

Cargo().run(env)
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
[env]
extra_scripts = pre:platformio.git.py, pre:platformio.patch.py, platformio.cargo.py
rust_lib = usb3sun
rust_target = thumbv6m-none-eabi

[env:pico]
rust_target = thumbv6m-none-eabi
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = pico
framework = arduino
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Remove if STD is supported for your platform and you plan to use it
#![no_std]
#![cfg_attr(target_os = "none", no_std)]

// Remove if STD is supported for your platform and you plan to use it
#[cfg(target_os = "none")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
Expand Down

0 comments on commit 1cc7118

Please sign in to comment.