From 70b9ceba72a228c76caf22e487b5bd91056e4894 Mon Sep 17 00:00:00 2001 From: Nobutaka Mantani <2285962+nmantani@users.noreply.github.com> Date: Fri, 22 Mar 2024 05:31:17 +0900 Subject: [PATCH] build: Added support patches for FreeBSD (#1584) This pull request fixes build on FreeBSD. The changes are conditioned with `#if defined(__FreeBSD__)` preprocessor macro and they should not affect build for other operating systems. --------- Co-authored-by: Nik Co-authored-by: iTrooz --- cmake/build_helpers.cmake | 3 +++ lib/libimhex/source/api/imhex_api.cpp | 6 ++++- lib/libimhex/source/helpers/fs.cpp | 4 ++++ .../providers/process_memory_provider.hpp | 2 +- plugins/builtin/source/content/providers.cpp | 2 +- .../content/providers/disk_provider.cpp | 23 ++++++++++++++++--- .../providers/process_memory_provider.cpp | 2 +- 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake index 882bc30da..b1ba20949 100644 --- a/cmake/build_helpers.cmake +++ b/cmake/build_helpers.cmake @@ -70,6 +70,9 @@ macro(detectOS) add_compile_definitions(OS_WEB) elseif (UNIX AND NOT APPLE) add_compile_definitions(OS_LINUX) + if (BSD AND BSD STREQUAL "FreeBSD") + add_compile_definitions(OS_FREEBSD) + endif() include(GNUInstallDirs) if(IMHEX_PLUGINS_IN_SHARE) diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 94c9d2faf..1e3e9fb6f 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -658,7 +658,11 @@ namespace hex { #if defined(OS_WINDOWS) return "Windows"; #elif defined(OS_LINUX) - return "Linux"; + #if defined(OS_FREEBSD) + return "FreeBSD"; + #else + return "Linux"; + #endif #elif defined(OS_MACOS) return "macOS"; #elif defined(OS_WEB) diff --git a/lib/libimhex/source/helpers/fs.cpp b/lib/libimhex/source/helpers/fs.cpp index 7a4364cf0..2027fa771 100644 --- a/lib/libimhex/source/helpers/fs.cpp +++ b/lib/libimhex/source/helpers/fs.cpp @@ -13,7 +13,11 @@ #include #elif defined(OS_LINUX) || defined(OS_WEB) #include +# if defined(OS_FREEBSD) + #include +# else #include +# endif #endif #if defined(OS_WEB) diff --git a/plugins/builtin/include/content/providers/process_memory_provider.hpp b/plugins/builtin/include/content/providers/process_memory_provider.hpp index 0869a86ce..53c024dcc 100644 --- a/plugins/builtin/include/content/providers/process_memory_provider.hpp +++ b/plugins/builtin/include/content/providers/process_memory_provider.hpp @@ -1,6 +1,6 @@ #pragma once -#if defined(OS_WINDOWS) || defined (OS_LINUX) +#if defined(OS_WINDOWS) || (defined(OS_LINUX) && !defined(OS_FREEBSD)) #include #include diff --git a/plugins/builtin/source/content/providers.cpp b/plugins/builtin/source/content/providers.cpp index 8a59ad955..2307ab189 100644 --- a/plugins/builtin/source/content/providers.cpp +++ b/plugins/builtin/source/content/providers.cpp @@ -37,7 +37,7 @@ namespace hex::plugin::builtin { ContentRegistry::Provider::add(false); ContentRegistry::Provider::add(false); - #if defined(OS_WINDOWS) ||defined (OS_LINUX) + #if defined(OS_WINDOWS) || (defined(OS_LINUX) && !defined(OS_FREEBSD)) ContentRegistry::Provider::add(); #endif diff --git a/plugins/builtin/source/content/providers/disk_provider.cpp b/plugins/builtin/source/content/providers/disk_provider.cpp index db94a75f6..2920fab49 100644 --- a/plugins/builtin/source/content/providers/disk_provider.cpp +++ b/plugins/builtin/source/content/providers/disk_provider.cpp @@ -28,7 +28,9 @@ #elif defined(OS_LINUX) #include #include - #include + #if !defined(OS_FREEBSD) + #include + #endif #include #include #include @@ -41,8 +43,11 @@ #include #endif -#if defined(OS_LINUX) -#define lseek lseek64 +#if defined(OS_LINUX) && !defined(OS_FREEBSD) + #define lseek lseek64 +#elif defined(OS_FREEBSD) + #include + #define DEFAULT_SECTOR_SIZE 512 #endif namespace hex::plugin::builtin { @@ -83,6 +88,12 @@ namespace hex::plugin::builtin { return -1; return 0; } + #elif defined(OS_FREEBSD) && defined(DIOCGSECTORSIZE) + int blkdev_get_sector_size(int fd, int *sector_size) { + if (ioctl(fd, DIOCGSECTORSIZE, sector_size) < 0) + return -1; + return 0; + } #else int blkdev_get_sector_size(int fd, int *sector_size) { (void)fd; @@ -97,6 +108,12 @@ namespace hex::plugin::builtin { return -1; return 0; } + #elif defined(OS_FREEBSD) && defined(DIOCGMEDIASIZE) + int blkdev_get_size(int fd, u64 *bytes) { + if (ioctl(fd, DIOCGMEDIASIZE, bytes) < 0) + return -1; + return 0; + } #else int blkdev_get_size(int fd, u64 *bytes) { struct stat st; diff --git a/plugins/builtin/source/content/providers/process_memory_provider.cpp b/plugins/builtin/source/content/providers/process_memory_provider.cpp index 8cee45d2f..a532181d2 100644 --- a/plugins/builtin/source/content/providers/process_memory_provider.cpp +++ b/plugins/builtin/source/content/providers/process_memory_provider.cpp @@ -1,4 +1,4 @@ -#if defined(OS_WINDOWS) || defined (OS_LINUX) +#if defined(OS_WINDOWS) || (defined(OS_LINUX) && !defined(OS_FREEBSD)) #include