Skip to content

Commit

Permalink
loader: include FreeBSD in supported platforms
Browse files Browse the repository at this point in the history
Added FreeBSD in supported platforms in vk_loader_platform.h, and tests,
defined BSD_VISIBLE and included some necessary header files for
Vulkan-Loader to compile on FreeBSD.

v2: did the same in loader/loader.c (Charles Giessen)
v3: did the same in the tests (Charles Giessen)
v4: applied this downstream patch from Jan Beich because there's no /proc
on FreeBSD: https://github.com/freebsd/freebsd-ports/blob/7caf52e37ba2174dc50a7440d63eec70d7d58ebc/graphics/vulkan-loader/files/patch-loader_vk__loader__platform.h (Jan Beich)
v4: rebased to master and modified tests/loader_regression_tests.cpp,
tests/loader_testing_main.cpp
v5: moved the definition of __BSD_VISIBLE 1 (required for alloca to
work) in CMakeLists.txt from header files (Charles Giessen)
v6: replaced ${CMAKE_SYSTEM_NAME} with CMAKE_SYSTEM_NAME in
loader/CMakeLists.txt to match the style of previous checks (Jan Beich)
v7: replaced add_compile_definitions with target_compile_definitions to
prevent the macro from being applied unnecessarily (Charles Giessen)
  • Loading branch information
hikiko authored and charles-lunarg committed Oct 20, 2021
1 parent f524349 commit b355279
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 25 deletions.
3 changes: 3 additions & 0 deletions loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ else()
target_link_libraries(vulkan Vulkan::Headers)
# Used to make alloca() and secure_getenv() available
target_compile_definitions(vulkan PRIVATE _GNU_SOURCE)
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
target_compile_definitions(vulkan PRIVATE __BSD_VISIBLE=1)
endif()
if(APPLE)
find_library(COREFOUNDATION_LIBRARY NAMES CoreFoundation)
target_link_libraries(vulkan "-framework CoreFoundation")
Expand Down
2 changes: 1 addition & 1 deletion loader/allocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void loader_device_heap_free(const struct loader_device *device, void *pMemory);
void *loader_device_heap_realloc(const struct loader_device *device, void *pMemory, size_t orig_size, size_t size,
VkSystemAllocationScope alloc_scope);

#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__)
#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__) || defined(__FreeBSD__)
#define loader_stack_alloc(size) alloca(size)
#elif defined(_WIN32)
#define loader_stack_alloc(size) _alloca(size)
Expand Down
4 changes: 2 additions & 2 deletions loader/get_environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#endif // !defined(VULKAN_NON_CMAKE_BUILD)

// Environment variables
#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__)
#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__) || defined(__FreeBSD__)

bool is_high_integrity() { return geteuid() != getuid() || getegid() != getgid(); }

Expand All @@ -49,7 +49,7 @@ char *loader_getenv(const char *name, const struct loader_instance *inst) {
}

char *loader_secure_getenv(const char *name, const struct loader_instance *inst) {
#if defined(__APPLE__)
#if defined(__APPLE__) || defined(__FreeBSD__)
// Apple does not appear to have a secure getenv implementation.
// The main difference between secure getenv and getenv is that secure getenv
// returns NULL if the process is being run with elevated privileges by a normal user.
Expand Down
2 changes: 1 addition & 1 deletion loader/get_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
char *loader_getenv(const char *name, const struct loader_instance *inst);
void loader_free_getenv(char *val, const struct loader_instance *inst);

#if defined(WIN32) || defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__)
#if defined(WIN32) || defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__) || defined(__FreeBSD__)

bool is_high_integrity();

Expand Down
33 changes: 29 additions & 4 deletions loader/vk_loader_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*/
#pragma once

#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/select.h>
#endif

#if defined(_WIN32)
// WinSock2.h must be included *BEFORE* windows.h
#include <winsock2.h>
Expand All @@ -37,7 +42,7 @@
#include "dlopen_fuchsia.h"
#endif // defined(__Fuchsia__)

#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__)
#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__) || defined(__FreeBSD__)
#include <unistd.h>
// Note: The following file is for dynamic loading:
#include <dlfcn.h>
Expand Down Expand Up @@ -97,7 +102,7 @@
// Override layer information
#define VK_OVERRIDE_LAYER_NAME "VK_LAYER_LUNARG_override"

#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__)
#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__) || defined(__FreeBSD__)
/* Linux-specific common code: */

// VK Library Filenames, Paths, etc.:
Expand Down Expand Up @@ -151,7 +156,7 @@ static inline bool loader_platform_is_path_absolute(const char *path) {

static inline char *loader_platform_dirname(char *path) { return dirname(path); }

#if defined(__linux__)
#if defined(__linux__) || defined(__FreeBSD__)

// find application path + name. Path cannot be longer than 1024, returns NULL if it is greater than that.
static inline char *loader_platform_executable_path(char *buffer, size_t size) {
Expand All @@ -161,7 +166,7 @@ static inline char *loader_platform_executable_path(char *buffer, size_t size) {
buffer[count] = '\0';
return buffer;
}
#elif defined(__APPLE__) // defined(__linux__)
#elif defined(__APPLE__) // defined(__linux__)
#include <libproc.h>
static inline char *loader_platform_executable_path(char *buffer, size_t size) {
pid_t pid = getpid();
Expand All @@ -170,6 +175,26 @@ static inline char *loader_platform_executable_path(char *buffer, size_t size) {
buffer[ret] = '\0';
return buffer;
}
#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/sysctl.h>
static inline char *loader_platform_executable_path(char *buffer, size_t size) {
int mib[] = {
CTL_KERN,
#if defined(__NetBSD__)
KERN_PROC_ARGS,
-1,
KERN_PROC_PATHNAME,
#else
KERN_PROC,
KERN_PROC_PATHNAME,
-1,
#endif
};
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buffer, &size, NULL, 0) < 0)
return NULL;

return buffer;
}
#elif defined(__Fuchsia__)
static inline char *loader_platform_executable_path(char *buffer, size_t size) { return NULL; }
#elif defined(__QNXNTO__)
Expand Down
Binary file modified tests/framework/framework_config.h.in
Binary file not shown.
8 changes: 4 additions & 4 deletions tests/framework/shim/shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ struct PlatformShim {

std::vector<fs::path> icd_paths;

#elif defined(__linux__) || defined(__APPLE__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
bool is_fake_path(fs::path const& path);
fs::path const& get_fake_path(fs::path const& path);

Expand All @@ -152,7 +152,7 @@ extern "C" {
using PFN_get_platform_shim = PlatformShim* (*)();
#define GET_PLATFORM_SHIM_STR "get_platform_shim"

#elif defined(__linux__)
#elif defined(__linux__) || defined(__FreeBSD__)
// statically link on linux
PlatformShim* get_platform_shim();
#endif
Expand All @@ -175,7 +175,7 @@ inline std::vector<std::string> parse_env_var_list(std::string const& var) {
for (size_t i = 0; i < var.size(); i++) {
#if defined(WIN32)
if (var[i] == ';') {
#elif defined(__linux__) || defined(__APPLE__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
if (var[i] == ':') {
#endif
if (len != 0) {
Expand Down Expand Up @@ -431,7 +431,7 @@ inline void PlatformShim::redirect_category(fs::path const& new_path, ManifestCa
}
}

#elif defined(__linux__) || defined(__APPLE__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)

#include <dirent.h>
#include <unistd.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/framework/shim/unix_shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

static PlatformShim platform_shim;
extern "C" {
#if defined(__linux__)
#if defined(__linux__) || defined(__FreeBSD__)
PlatformShim* get_platform_shim() {
platform_shim = PlatformShim();
return &platform_shim;
Expand All @@ -42,7 +42,7 @@ FRAMEWORK_EXPORT PlatformShim* get_platform_shim() {
#endif

// Necessary for MacOS function himming
#if defined(__linux__)
#if defined(__linux__) || defined(__FreeBSD__)
#define OPENDIR_FUNC_NAME opendir
#define ACCESS_FUNC_NAME access
#define FOPEN_FUNC_NAME fopen
Expand Down
2 changes: 1 addition & 1 deletion tests/framework/test_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ PlatformShimWrapper::PlatformShimWrapper(DebugMode debug_mode) noexcept : debug_
auto get_platform_shim_func = shim_library.get_symbol<PFN_get_platform_shim>(GET_PLATFORM_SHIM_STR);
assert(get_platform_shim_func != NULL && "Must be able to get \"platform_shim\"");
platform_shim = get_platform_shim_func();
#elif defined(__linux__)
#elif defined(__linux__) || defined(__FreeBSD__)
platform_shim = get_platform_shim();
#endif
platform_shim->setup_override(debug_mode);
Expand Down
4 changes: 2 additions & 2 deletions tests/framework/test_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ std::string get_env_var(std::string const& name) {
}
return value;
}
#elif defined(__linux__) || defined(__APPLE__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)

bool set_env_var(std::string const& name, std::string const& value) { return setenv(name.c_str(), value.c_str(), 1); }
bool remove_env_var(std::string const& name) { return unsetenv(name.c_str()); }
Expand Down Expand Up @@ -224,7 +224,7 @@ std::string make_native(std::string const& in_path) {
else
out += c;
}
#elif defined(__linux__) || defined(__APPLE__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
for (size_t i = 0; i < in_path.size(); i++) {
if (i + 1 < in_path.size() && in_path[i] == '\\' && in_path[i + 1] == '\\') {
out += '/';
Expand Down
8 changes: 4 additions & 4 deletions tests/framework/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
#include <direct.h>
#include <windows.h>
#include <strsafe.h>
#elif defined(__linux__) || defined(__APPLE__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -113,7 +113,7 @@ bool remove_env_var(std::string const& name);
#define ENV_VAR_BUFFER_SIZE 4096
std::string get_env_var(std::string const& name, bool report_failure = true);

#elif defined(__linux__) || defined(__APPLE__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
bool set_env_var(std::string const& name, std::string const& value);
bool remove_env_var(std::string const& name);
std::string get_env_var(std::string const& name, bool report_failure = true);
Expand Down Expand Up @@ -144,7 +144,7 @@ struct path {
private:
#if defined(WIN32)
static const char path_separator = '\\';
#elif defined(__linux__) || defined(__APPLE__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
static const char path_separator = '/';
#endif

Expand Down Expand Up @@ -251,7 +251,7 @@ static char* loader_platform_get_proc_address_error(const char* name) {
return errorMsg;
}

#elif defined(__linux__) || defined(__APPLE__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)

typedef void* loader_platform_dl_handle;
static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath) {
Expand Down
4 changes: 2 additions & 2 deletions tests/loader_regression_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ TEST(TryLoadWrongBinaries, WrongICD) {
#if _WIN32 || _WIN64
ASSERT_TRUE(log.find("Failed to open dynamic library"));
#endif
#if defined(__linux__)
#if defined(__linux__) || defined(__FreeBSD__)
#if defined(__x86_64__)
ASSERT_TRUE(log.find("wrong ELF class: ELFCLASS32"));
#else
Expand Down Expand Up @@ -757,7 +757,7 @@ TEST_F(EnumeratePhysicalDeviceGroups, TwoCallIncomplete) {
}
}

#if defined(__linux__)
#if defined(__linux__) || defined(__FreeBSD__)
// Make sure the loader reports the correct message based on if USE_UNSAFE_FILE_SEARCH is set or not
TEST(EnvironmentVariables, NonSecureEnvVarLookup) {
SingleICDShim env(TestICDDetails(TEST_ICD_PATH_VERSION_6));
Expand Down
4 changes: 2 additions & 2 deletions tests/loader_testing_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ int main(int argc, char** argv) {
remove_env_var("VK_LOADER_DEBUG");
remove_env_var("VK_LOADER_DISABLE_INST_EXT_FILTER");

#if defined(__linux__)
#if defined(__linux__) || defined(__FreeBSD__)
set_env_var("XDG_CONFIG_HOME", "/etc");
set_env_var("XDG_CONFIG_DIRS", "/etc");
set_env_var("XDG_DATA_HOME", "/etc");
set_env_var("XDG_DATA_DIRS", "/etc");
#endif
#if defined(__linux__) || defined(__APPLE__)
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
set_env_var("HOME", "/home/fake_home");
#endif

Expand Down

0 comments on commit b355279

Please sign in to comment.