Skip to content
This repository has been archived by the owner on Aug 4, 2024. It is now read-only.

Commit

Permalink
chore: compat: remove unnecessary hardcoded platform check for some s…
Browse files Browse the repository at this point in the history
…tubs

Signed-off-by: Ookiineko <[email protected]>
  • Loading branch information
Ookiineko committed Apr 7, 2024
1 parent 15f31a3 commit c727748
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 95 deletions.
37 changes: 36 additions & 1 deletion CMakeLists.compat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ elseif (EMSCRIPTEN)
endif()

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sALLOW_MEMORY_GROWTH -Wl,-u,ntohs -Wl,-u,htons -Wl,-u,htonl -Wl,--wrap=close")

# exists in musl but not in rust libc crate
set(RUSTFLAGS "${RUSTFLAGS} --cfg mbb_stubs_SYS_dup3")
else()
message(WARNING "
New platform: ${CMAKE_SYSTEM_NAME}
Expand All @@ -43,14 +46,36 @@ endif()
add_compile_definitions(_FILE_OFFSET_BITS=64 _LARGEFILE64_SOURCE)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE")

list(APPEND CMAKE_REQUIRED_INCLUDES "sys/types.h")
include(CheckTypeSize)
check_type_size(off64_t SIZEOF_off64_t)
if(NOT HAVE_off64_t)
add_compile_definitions(off64_t=off_t)
endif()

include(CheckSymbolExists)
check_symbol_exists(lseek64 "unistd.h" LSEEK64_EXISTS)
if(NOT LSEEK64_EXISTS)
add_compile_definitions(off64_t=off_t)
add_compile_definitions(lseek64=lseek)

set(RUSTFLAGS "${RUSTFLAGS} --cfg mbb_stubs_lseek64")
endif()

check_symbol_exists(lseek64 "unistd.h" FTRUNCATE64_EXISTS)
if(NOT FTRUNCATE64_EXISTS)
add_compile_definitions(ftruncate64=ftruncate)
endif()

check_symbol_exists(O_PATH "fcntl.h" HAVE_O_PATH)
if(NOT HAVE_O_PATH)
set(RUSTFLAGS "${RUSTFLAGS} --cfg mbb_stubs_O_PATH")
endif()

check_symbol_exists(SYS_dup3 "sys/syscall.h" HAVE_SYS_dup3)
if(NOT HAVE_SYS_dup3)
set(RUSTFLAGS "${RUSTFLAGS} --cfg mbb_stubs_SYS_dup3")
endif()

if (CMAKE_SYSTEM_NAME MATCHES "Android")
message(STATUS "Fortify enabled")
add_compile_options(-D_FORTIFY_SOURCE=2)
Expand All @@ -62,3 +87,13 @@ else()

include(CMakeLists.android.txt)
endif()

check_symbol_exists(sendfile "sys/sendfile.h" HAVE_sendfile)
if (NOT HAVE_sendfile)
set(RUSTFLAGS "${RUSTFLAGS} --cfg mbb_stubs_sendfile")
endif()

check_symbol_exists(__errno "errno.h" HAVE___errno)
if(NOT HAVE___errno)
set(RUSTFLAGS "${RUSTFLAGS} --cfg mbb_stubs___errno")
endif()
32 changes: 31 additions & 1 deletion CMakeLists.stub.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ add_library(platform_stubs STATIC
libc-compat/placeholder.c)
target_include_directories(platform_stubs PUBLIC libc-compat/include)

if ((NOT CMAKE_SYSTEM_NAME STREQUAL "Linux") AND (NOT CMAKE_SYSTEM_NAME STREQUAL "Android"))
if (NOT HAVE_sendfile)
message(STATUS "Using generic sendfile replacement")
target_sources(platform_stubs PRIVATE
libc-compat/non-linux/sendfile_fallback.c)
Expand All @@ -24,6 +24,36 @@ if (NOT HAVE_STRLCPY)
libc-compat/libbsd/str.c)
endif()

if(NOT HAVE___errno)
target_sources(platform_stubs PRIVATE
libc-compat/common/__errno.c)

# e.g Linux and Emscripten
check_symbol_exists(__errno_location "errno.h" HAVE___errno_location)
if (HAVE___errno_location)
set_source_files_properties(libc-compat/common/__errno.c PROPERTIES
COMPILE_FLAGS -DHAVE___errno_location)
else()
# e.g macOS
check_symbol_exists(__error "errno.h" HAVE___error)
if (HAVE___error)
set_source_files_properties(libc-compat/common/__errno.c PROPERTIES
COMPILE_FLAGS -DHAVE___error)
else()
# e.g MinGW
check_symbol_exists(_errno "errno.h" HAVE__errno)
if (HAVE__errno)
set_source_files_properties(libc-compat/common/__errno.c PROPERTIES
COMPILE_FLAGS -DHAVE__errno)
else()
message(FATAL_ERROR "Don't know errno implementation on this platform")
endif()
endif()
endif()

message(STATUS "Using __errno alias")
endif()

if (WIN32)
message(STATUS "Using win32 libc-compat")

Expand Down
13 changes: 13 additions & 0 deletions libc-compat/common/__errno.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <errno.h>

int *__errno (void) {
#ifdef HAVE___errno_location
return __errno_location();
#elif defined(HAVE___error)
return __error();
#elif defined(HAVE__errno)
return _errno();
#else
# error "Unreachable"
#endif
}
5 changes: 5 additions & 0 deletions libc-compat/rs/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
#[macro_use]
mod stubs;

pub use stubs::*;

#[macro_use]
mod overrides;
5 changes: 5 additions & 0 deletions libc-compat/rs/common/overrides.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
macro_rules! post_defs_overrides {
() => {
add_sendfile_stubs_if_needed!();
}
}
15 changes: 1 addition & 14 deletions libc-compat/rs/common/stubs/errno.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
// android (bionic libc) errno

#[cfg(any(target_os = "linux", target_os = "emscripten"))]
#[cfg(mbb_stubs___errno)]
extern "C" {
#[link_name = "__errno_location"]
pub fn __errno() -> *mut crate::c_int;
}

#[cfg(target_vendor = "apple")]
extern "C" {
#[link_name = "__error"]
pub fn __errno() -> *mut crate::c_int;
}

#[cfg(target_os = "windows")]
extern "cdecl" {
#[link_name = "_errno"]
pub fn __errno() -> *mut crate::c_int;
}
1 change: 1 addition & 0 deletions libc-compat/rs/common/stubs/fcntl.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// for platform those doesn't have O_PATH

#[cfg(mbb_stubs_O_PATH)]
pub const O_PATH: crate::c_int = 0; // no-op
1 change: 1 addition & 0 deletions libc-compat/rs/common/stubs/lfs64.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// just aliases

#[cfg(mbb_stubs_lseek64)]
extern "C" {
#[link_name = "lseek"]
pub fn lseek64(fd: crate::c_int, offset: crate::off_t, whence: crate::c_int) -> crate::off_t;
Expand Down
21 changes: 1 addition & 20 deletions libc-compat/rs/common/stubs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
#[cfg(not(any(target_os = "android", target_os = "cygwin")))]
mod errno;

#[cfg(not(any(target_os = "android", target_os = "cygwin")))]
pub use errno::*;

#[cfg(any(target_vendor = "apple", target_os = "windows"))]
mod fcntl;

#[cfg(any(target_vendor = "apple", target_os = "windows"))]
pub use fcntl::*;

#[cfg(any(target_os = "cygwin", target_vendor = "apple"))]
mod lfs64;

#[cfg(any(target_os = "cygwin", target_vendor = "apple"))]
pub use lfs64::*;

#[cfg(any(target_os = "cygwin", target_vendor = "apple",
target_os = "emscripten", target_os = "windows"))]
mod sc_num;

#[cfg(any(target_os = "cygwin", target_vendor = "apple",
target_os = "emscripten", target_os = "windows"))]
pub use sc_num::*;

#[cfg(any(target_os = "cygwin", target_os = "emscripten",
target_os = "windows"))]
#[macro_use]
mod sendfile;

#[cfg(any(target_os = "cygwin", target_os = "emscripten",
target_os = "windows"))]
pub use sendfile::*;
1 change: 1 addition & 0 deletions libc-compat/rs/common/stubs/sc_num.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// no actually used, just for passing build

#[cfg(mbb_stubs_SYS_dup3)]
pub const SYS_dup3: crate::c_int = 0; // stub
50 changes: 29 additions & 21 deletions libc-compat/rs/common/stubs/sendfile.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
// defined in libc-compat/non-linux/sendfile_fallback.c

extern "C" {
#[link_name = "__sendfile_stub"]
fn real_sendfile(
out_fd: crate::c_int,
in_fd: crate::c_int,
count: crate::size_t
);
}
macro_rules! add_sendfile_stubs_if_needed {
() => {
cfg_if::cfg_if! {
if #[cfg(mbb_stubs_sendfile)] {
extern "C" {
#[link_name = "__sendfile_stub"]
fn real_sendfile(
out_fd: crate::c_int,
in_fd: crate::c_int,
count: crate::size_t
);
}

f! {
pub fn sendfile(
out_fd: crate::c_int,
in_fd: crate::c_int,
offset: *mut crate::off_t,
count: crate::size_t
) -> crate::ssize_t {
if offset.is_null() {
real_sendfile(out_fd, in_fd, count);
0
} else {
panic!("unreachable code")
f! {
pub fn sendfile(
out_fd: crate::c_int,
in_fd: crate::c_int,
offset: *mut crate::off_t,
count: crate::size_t
) -> crate::ssize_t {
if offset.is_null() {
real_sendfile(out_fd, in_fd, count);
0
} else {
panic!("unreachable code")
}
}
}
}
}
}
};
}
29 changes: 0 additions & 29 deletions libc-compat/rs/darwin/defs.rs

This file was deleted.

3 changes: 0 additions & 3 deletions libc-compat/rs/darwin/mod.rs

This file was deleted.

11 changes: 5 additions & 6 deletions libc-compat/rs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
mod macros;

cfg_if::cfg_if! {
if #[cfg(target_vendor = "apple")] {
// on macos, we need to shadow some stuffs

mod darwin;
pub use darwin::*;
} else if #[cfg(target_os = "windows")] {
if #[cfg(target_os = "windows")] {
// windows has some missing defs,
// we also need to shadow add some extra stuffs

Expand All @@ -28,5 +23,9 @@ cfg_if::cfg_if! {
}
}

#[macro_use]
mod common;

post_defs_overrides!();

pub use common::*;

0 comments on commit c727748

Please sign in to comment.