Fix host build compatibility with GCC

Fix compilation errors when building host binaries (such as magiskboot)
using GCC on Linux:
- Define _GNU_SOURCE in base and boot build.rs for glibc extensions.
- Add missing <cstdarg>, <cstring>, and <memory> standard headers.
- Provide a fallback definition for __printflike on glibc.
- Implement portable strscpy without relying on BSD strlcpy.
- Add unwrap_ref in bootimg.hpp to allow binding references to packed
  struct members under GCC.
- Remove cxx_impl_annotations in codegen.rs to prevent always_inline
  from being emitted on header declarations without function bodies.

Assisted-by: Gemini 3.8 Flash
This commit is contained in:
topjohnwu
2026-10-03 06:35:05 +00:00
parent 66bcdaf4e0
commit a70a7e67b0
7 changed files with 31 additions and 3 deletions
+9 -1
View File
@@ -107,7 +107,15 @@ int ssprintf(char *dest, size_t size, const char *fmt, ...) {
#undef strlcpy
size_t strscpy(char *dest, const char *src, size_t size) {
return std::min(strlcpy(dest, src, size), size - 1);
if (size == 0) return 0;
size_t len = strlen(src);
if (len >= size) {
memcpy(dest, src, size - 1);
dest[size - 1] = '\0';
return size - 1;
}
memcpy(dest, src, len + 1);
return len;
}
#undef vsnprintf
+1
View File
@@ -12,6 +12,7 @@ fn main() {
build
.cpp(true)
.std("c++23")
.define("_GNU_SOURCE", None)
.include("include")
.include("../include")
.include("../../out/generated")
+7
View File
@@ -5,10 +5,17 @@
#include <dirent.h>
#include <fcntl.h>
#include <pthread.h>
#include <cstdarg>
#include <cstring>
#include <memory>
#include <functional>
#include <rust/cxx.h>
#ifndef __printflike
#define __printflike(a, b) __attribute__((__format__(__printf__, a, b)))
#endif
void LOGD(const char *fmt, ...) __printflike(1, 2);
void LOGI(const char *fmt, ...) __printflike(1, 2);
void LOGW(const char *fmt, ...) __printflike(1, 2);
+12 -1
View File
@@ -444,11 +444,22 @@ dyn_img_hdr *clone() const override { \
return p; \
};
template <typename T, typename U>
static T unwrap_ref(U *v) {
if constexpr (std::is_reference_v<T>) {
return *reinterpret_cast<std::remove_reference_t<T>*>(v);
} else {
return reinterpret_cast<T>(v);
}
}
#define __impl_val(name, hdr_name) \
decltype(std::declval<const dyn_img_hdr>().name()) name() const override { return hdr_name->name; }
#define __impl_var(name, hdr_name) \
decltype(std::declval<dyn_img_hdr>().name()) name() override { return hdr_name->name; } \
decltype(std::declval<dyn_img_hdr>().name()) name() override { \
return unwrap_ref<decltype(std::declval<dyn_img_hdr>().name())>(&hdr_name->name); \
} \
__impl_val(name, hdr_name)
#define impl_cls(ver) __impl_cls(dyn_img_##ver, boot_img_hdr_##ver)
+1
View File
@@ -45,6 +45,7 @@ fn main() {
build
.cpp(true)
.std("c++23")
.define("_GNU_SOURCE", None)
.include("../include")
.include("../base/include")
.include("../external/cxx-rs/include")
+1
View File
@@ -1,5 +1,6 @@
#pragma once
#include <cstring>
#include <base.hpp>
#define HEADER_FILE "header"
-1
View File
@@ -39,7 +39,6 @@ fn write_if_diff<P: AsRef<Path>>(path: P, bytes: &[u8]) -> io::Result<()> {
pub fn gen_cxx_binding(name: &str) {
println!("cargo:rerun-if-changed=lib.rs");
let mut opt = Opt::default();
opt.cxx_impl_annotations = Some("[[gnu::always_inline]]".to_string());
opt.include.push(Include {
path: "rust/cxx.h".to_string(),
kind: IncludeKind::Bracketed,