devel/adb: new port

This commit is contained in:
c0dev0id
2026-07-08 11:08:05 +02:00
parent a91ecb0f1d
commit 8813f7e720
28 changed files with 786 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
Index: vendor/CMakeLists.adb.txt
--- vendor/CMakeLists.adb.txt.orig
+++ vendor/CMakeLists.adb.txt
@@ -43,6 +43,9 @@ set(libadb_SOURCES
if(APPLE)
list(APPEND libadb_SOURCES
adb/client/usb_osx.cpp)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
+ list(APPEND libadb_SOURCES
+ adb/client/usb_openbsd.cpp)
else()
list(APPEND libadb_SOURCES
adb/client/usb_linux.cpp)
@@ -129,7 +132,7 @@ set(libcutils_SOURCES
core/libcutils/strlcpy.c
core/libcutils/trace-host.cpp)
-if (NOT APPLE AND NOT WIN32)
+if (NOT APPLE AND NOT WIN32 AND NOT (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD"))
list(APPEND libcutils_SOURCES
core/libcutils/canned_fs_config.cpp
core/libcutils/fs_config.cpp)

View File

@@ -0,0 +1,31 @@
Index: vendor/CMakeLists.fastboot.txt
--- vendor/CMakeLists.fastboot.txt.orig
+++ vendor/CMakeLists.fastboot.txt
@@ -119,6 +119,9 @@ set(fastboot_SOURCES
if(APPLE)
list(APPEND fastboot_SOURCES
core/fastboot/usb_osx.cpp)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
+ list(APPEND fastboot_SOURCES
+ core/fastboot/usb_libusb.cpp)
else()
list(APPEND fastboot_SOURCES
core/fastboot/usb_linux.cpp)
@@ -136,10 +139,16 @@ target_compile_definitions(fastboot PRIVATE
target_link_libraries(fastboot
libsparse libzip libcutils liblog liblp libutil
libbase libext4 libselinux libsepol libdiagnoseusb crypto
- z PkgConfig::libpcre2-8 Threads::Threads dl)
+ z PkgConfig::libpcre2-8 Threads::Threads)
+if(NOT CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
+ target_link_libraries(fastboot dl)
+endif()
if(APPLE)
target_link_libraries(fastboot
"-framework CoreFoundation"
"-framework IOKit")
+endif()
+if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
+ target_link_libraries(fastboot PkgConfig::libusb-1.0)
endif()

View File

@@ -0,0 +1,12 @@
Index: vendor/CMakeLists.mke2fs.txt
--- vendor/CMakeLists.mke2fs.txt.orig
+++ vendor/CMakeLists.mke2fs.txt
@@ -130,7 +130,7 @@ target_include_directories("${ANDROID_MKE2FS_NAME}" PR
e2fsprogs/lib)
# fails to build due to https://android-review.googlesource.com/c/platform/system/core/+/2760169
-if(NOT APPLE)
+if(NOT APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
add_executable(e2fsdroid
e2fsprogs/contrib/android/e2fsdroid.c
e2fsprogs/contrib/android/basefs_allocator.c

View File

@@ -0,0 +1,50 @@
Index: vendor/CMakeLists.txt
--- vendor/CMakeLists.txt.orig
+++ vendor/CMakeLists.txt
@@ -16,6 +16,17 @@ if(APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_DARWIN_C_SOURCE -D__DARWIN_C_LEVEL=__DARWIN_C_FULL")
endif()
+if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_BSD_SOURCE")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_BSD_SOURCE")
+ add_compile_definitions(off64_t=off_t lseek64=lseek ftruncate64=ftruncate
+ pread64=pread pwrite64=pwrite mmap64=mmap)
+ include_directories(BEFORE
+ ${CMAKE_CURRENT_SOURCE_DIR}/compat
+ ${CMAKE_CURRENT_SOURCE_DIR}/e2fsprogs/lib
+ ${CMAKE_CURRENT_SOURCE_DIR}/e2fsprogs/lib/ext2fs)
+endif()
+
# Different versions of clang require a different set of flags for -ftrivial-auto-var-init
# Simplify this contruct once old clang version support is dropped
include(CheckCXXCompilerFlag)
@@ -105,7 +116,9 @@ include(CMakeLists.libandroidfw.txt)
include(CMakeLists.adb.txt)
include(CMakeLists.sparse.txt)
include(CMakeLists.fastboot.txt)
+if(NOT CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
include(CMakeLists.f2fstools.txt)
+endif()
include(CMakeLists.mke2fs.txt)
include(CMakeLists.partition.txt)
include(CMakeLists.mkbootimg.txt)
@@ -124,13 +137,15 @@ install(TARGETS
lpflash
lpmake
lpunpack
- make_f2fs
- sload_f2fs
simg2img
ext2simg
DESTINATION bin)
-if(NOT APPLE)
+if(NOT CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
+ install(TARGETS make_f2fs sload_f2fs DESTINATION bin)
+endif()
+
+if(NOT APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
install(TARGETS
e2fsdroid
DESTINATION bin)

View File

@@ -0,0 +1,63 @@
Index: vendor/adb/client/usb_libusb.cpp
--- vendor/adb/client/usb_libusb.cpp.orig
+++ vendor/adb/client/usb_libusb.cpp
@@ -26,6 +26,7 @@
#include <unistd.h>
#endif
+#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
@@ -1042,7 +1043,50 @@ void usb_init() {
LIBUSB_CLASS_PER_INTERFACE, hotplug_callback, nullptr, nullptr);
if (rc != LIBUSB_SUCCESS) {
- LOG(FATAL) << "failed to register libusb hotplug callback";
+ LOG(WARNING) << "libusb hotplug not supported (" << libusb_error_name(rc)
+ << "), falling back to polling";
+ // Spawn a polling thread to detect USB devices without hotplug.
+ std::thread([]() {
+ adb_thread_setname("libusb poll");
+ std::vector<libusb_device*> known;
+ for (;;) {
+ libusb_device** list = nullptr;
+ ssize_t n = libusb_get_device_list(nullptr, &list);
+ if (n >= 0) {
+ for (ssize_t i = 0; i < n; ++i) {
+ libusb_device* dev = list[i];
+ if (std::find(known.begin(), known.end(), dev) == known.end()) {
+ libusb_ref_device(dev);
+ device_connected(dev);
+ known.push_back(dev);
+ }
+ }
+ for (auto it = known.begin(); it != known.end(); ) {
+ bool present = false;
+ for (ssize_t i = 0; i < n; ++i) {
+ if (list[i] == *it) { present = true; break; }
+ }
+ if (!present) {
+ device_disconnected(*it);
+ libusb_unref_device(*it);
+ it = known.erase(it);
+ } else {
+ ++it;
+ }
+ }
+ libusb_free_device_list(list, 1);
+ adb_notify_device_scan_complete();
+ }
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ }
+ }).detach();
+ std::thread([]() {
+ adb_thread_setname("libusb");
+ while (true) {
+ libusb_handle_events(nullptr);
+ }
+ }).detach();
+ return;
}
// Spawn a thread for libusb_handle_events.

View File

@@ -0,0 +1,25 @@
Index: vendor/adb/sysdeps.h
--- vendor/adb/sysdeps.h.orig
+++ vendor/adb/sysdeps.h
@@ -409,6 +409,9 @@ size_t ParseCompleteUTF8(const char* first, const char
#include <netinet/tcp.h>
#include <poll.h>
#include <pthread.h>
+#ifdef __OpenBSD__
+#include <pthread_np.h>
+#endif
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
@@ -652,6 +655,11 @@ inline int adb_socket_get_local_port(borrowed_fd fd) {
static inline int adb_thread_setname(const std::string& name) {
#ifdef __APPLE__
return pthread_setname_np(name.c_str());
+#elif defined(__OpenBSD__)
+ char buf[16];
+ strlcpy(buf, name.c_str(), sizeof(buf));
+ pthread_set_name_np(pthread_self(), buf);
+ return 0;
#else
// Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
// glibc doesn't have strlcpy, so we have to fake it.

View File

@@ -0,0 +1,15 @@
Index: vendor/core/libutils/include/utils/Condition.h
--- vendor/core/libutils/include/utils/Condition.h.orig
+++ vendor/core/libutils/include/utils/Condition.h
@@ -103,9 +103,11 @@ inline Condition::Condition(int type) {
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
#endif
+#ifndef __OpenBSD__
if (type == SHARED) {
pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
}
+#endif
pthread_cond_init(&mCond, &attr);
pthread_condattr_destroy(&attr);

View File

@@ -0,0 +1,13 @@
Index: vendor/core/libutils/include/utils/Mutex.h
--- vendor/core/libutils/include/utils/Mutex.h.orig
+++ vendor/core/libutils/include/utils/Mutex.h
@@ -169,7 +169,9 @@ inline Mutex::Mutex(int type, __attribute__((unused))
if (type == SHARED) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
+#ifndef __OpenBSD__
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+#endif
pthread_mutex_init(&mMutex, &attr);
pthread_mutexattr_destroy(&attr);
} else {

View File

@@ -0,0 +1,9 @@
Index: vendor/e2fsprogs/lib/config.h
--- vendor/e2fsprogs/lib/config.h.orig
+++ vendor/e2fsprogs/lib/config.h
@@ -1,4 +1,4 @@
-#ifndef __APPLE__
+#if !defined(__APPLE__) && !defined(__OpenBSD__)
#define HAVE_MALLOC_H 1
#endif

View File

@@ -0,0 +1,15 @@
protobuf >= 22 returns absl::string_view from Descriptor::full_name();
wrap in std::string() so operator+ resolves.
Index: vendor/extras/libjsonpb/parse/jsonpb.cpp
--- vendor/extras/libjsonpb/parse/jsonpb.cpp.orig
+++ vendor/extras/libjsonpb/parse/jsonpb.cpp
@@ -33,7 +33,7 @@ using google::protobuf::util::TypeResolver;
static constexpr char kTypeUrlPrefix[] = "type.googleapis.com";
std::string GetTypeUrl(const Message& message) {
- return std::string(kTypeUrlPrefix) + "/" + message.GetDescriptor()->full_name();
+ return std::string(kTypeUrlPrefix) + "/" + std::string(message.GetDescriptor()->full_name());
}
ErrorOr<std::string> MessageToJsonString(const Message& message) {

View File

@@ -0,0 +1,15 @@
Index: vendor/extras/partition_tools/lpdump.cc
--- vendor/extras/partition_tools/lpdump.cc.orig
+++ vendor/extras/partition_tools/lpdump.cc
@@ -16,10 +16,10 @@
#include <getopt.h>
#include <inttypes.h>
+#include <sys/types.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
-#include <sys/types.h>
#include <sysexits.h>
#include <unistd.h>

View File

@@ -0,0 +1,62 @@
Index: vendor/libbase/file.cpp
--- vendor/libbase/file.cpp.orig
+++ vendor/libbase/file.cpp
@@ -26,6 +26,9 @@
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
+#ifdef __OpenBSD__
+#include <sys/sysctl.h>
+#endif
#include <unistd.h>
#include <memory>
@@ -508,6 +511,48 @@ std::string GetExecutablePath() {
return path;
#elif defined(__EMSCRIPTEN__)
abort();
+#elif defined(__OpenBSD__)
+ // Try /proc/curproc/file if procfs is mounted.
+ {
+ std::string path;
+ if (android::base::Readlink("/proc/curproc/file", &path) && !path.empty()) {
+ return path;
+ }
+ }
+ // Fall back to sysctl(KERN_PROC_ARGV) to recover argv[0] and resolve it.
+ {
+ int mib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV};
+ size_t len = 0;
+ if (sysctl(mib, 4, nullptr, &len, nullptr, 0) != -1 && len > 0) {
+ std::vector<char> buf(len);
+ if (sysctl(mib, 4, buf.data(), &len, nullptr, 0) != -1) {
+ char* argv0 = reinterpret_cast<char**>(buf.data())[0];
+ if (argv0 != nullptr) {
+ if (argv0[0] == '/') {
+ return argv0; // Already absolute.
+ }
+ // Search PATH directories for argv0.
+ if (const char* path_env = getenv("PATH")) {
+ const char* p = path_env;
+ while (*p) {
+ const char* q = strchr(p, ':');
+ size_t dirlen = q ? (size_t)(q - p) : strlen(p);
+ std::string full(p, dirlen);
+ full += '/';
+ full += argv0;
+ char resolved[PATH_MAX];
+ if (realpath(full.c_str(), resolved) != nullptr) {
+ return resolved;
+ }
+ p += dirlen;
+ if (*p == ':') ++p;
+ }
+ }
+ }
+ }
+ }
+ }
+ return "";
#else
#error unknown OS
#endif

View File

@@ -0,0 +1,15 @@
Index: vendor/libbase/include/android-base/endian.h
--- vendor/libbase/include/android-base/endian.h.orig
+++ vendor/libbase/include/android-base/endian.h
@@ -25,6 +25,11 @@
#include <sys/endian.h>
+#elif defined(__OpenBSD__)
+#include <sys/endian.h>
+#define htonq(x) htobe64(x)
+#define ntohq(x) be64toh(x)
+
#elif defined(__GLIBC__) || defined(ANDROID_HOST_MUSL) || defined(__linux__)
/* glibc and musl's <endian.h> are like bionic's <sys/endian.h>. */

View File

@@ -0,0 +1,13 @@
Index: vendor/libbase/include/android-base/off64_t.h
--- vendor/libbase/include/android-base/off64_t.h.orig
+++ vendor/libbase/include/android-base/off64_t.h
@@ -16,7 +16,7 @@
#pragma once
-#if defined(__APPLE__)
-/** Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */
+#if defined(__APPLE__) || defined(__OpenBSD__)
+/** Mac OS and OpenBSD have always had a 64-bit off_t, so they don't have off64_t. */
typedef off_t off64_t;
#endif

View File

@@ -0,0 +1,12 @@
Index: vendor/libbase/logging.cpp
--- vendor/libbase/logging.cpp.orig
+++ vendor/libbase/logging.cpp
@@ -62,7 +62,7 @@ namespace android {
namespace base {
// BSD-based systems like Android/macOS have getprogname(). Others need us to provide one.
-#if !defined(__APPLE__) && !defined(__BIONIC__)
+#if !defined(__APPLE__) && !defined(__BIONIC__) && !defined(__OpenBSD__)
static const char* getprogname() {
#ifdef _WIN32
static bool first = true;

View File

@@ -0,0 +1,12 @@
Index: vendor/libbase/threads.cpp
--- vendor/libbase/threads.cpp.orig
+++ vendor/libbase/threads.cpp
@@ -37,6 +37,8 @@ uint64_t GetThreadId() {
uint64_t tid;
pthread_threadid_np(NULL, &tid);
return tid;
+#elif defined(__OpenBSD__)
+ return (uint64_t)getthrid();
#elif defined(__linux__)
return syscall(__NR_gettid);
#elif defined(_WIN32)

View File

@@ -0,0 +1,12 @@
Index: vendor/logging/liblog/logger_write.cpp
--- vendor/logging/liblog/logger_write.cpp.orig
+++ vendor/logging/liblog/logger_write.cpp
@@ -120,7 +120,7 @@ void __android_log_close() {
}
// BSD-based systems like Android/macOS have getprogname(). Others need us to provide one.
-#if !defined(__APPLE__) && !defined(__BIONIC__)
+#if !defined(__APPLE__) && !defined(__BIONIC__) && !defined(__OpenBSD__)
static const char* getprogname() {
#ifdef _WIN32
static bool first = true;

View File

@@ -0,0 +1,13 @@
Index: vendor/selinux/libselinux/src/label_file.h
--- vendor/selinux/libselinux/src/label_file.h.orig
+++ vendor/selinux/libselinux/src/label_file.h
@@ -6,7 +6,9 @@
#include <string.h>
#include <sys/stat.h>
+#ifndef __OpenBSD__
#include <sys/xattr.h>
+#endif
/*
* regex.h/c were introduced to hold all dependencies on the regular

View File

@@ -0,0 +1,29 @@
Index: vendor/selinux/libsepol/src/private.h
--- vendor/selinux/libsepol/src/private.h.orig
+++ vendor/selinux/libsepol/src/private.h
@@ -5,7 +5,7 @@
#include <sepol/policydb/policydb.h>
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__OpenBSD__)
#include <sys/types.h>
#include <machine/endian.h>
#else
@@ -13,9 +13,15 @@
#include <endian.h>
#endif
+#ifdef __OpenBSD__
+#define bswap_16 bswap16
+#define bswap_32 bswap32
+#define bswap_64 bswap64
+#endif
+
#include <errno.h>
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__OpenBSD__)
#define __BYTE_ORDER BYTE_ORDER
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#endif