From 49c7ee068327af7e1dc71cdd37afb61f3132097f Mon Sep 17 00:00:00 2001 From: Yuri Date: Thu, 21 May 2020 07:34:00 -0700 Subject: [PATCH] FreeBSD compatibility patches. --- halide.cmake | 1 + src/JITModule.cpp | 11 +++++++++++ src/LLVM_Runtime_Linker.cpp | 16 +++++++++++++--- src/Target.cpp | 4 ++++ src/Target.h | 1 + src/Util.cpp | 21 ++++++++++++++++++++- 6 files changed, 50 insertions(+), 4 deletions(-) diff --git a/halide.cmake b/halide.cmake index b6e68cf8ac21..07f8e72b932e 100644 --- a/halide.cmake +++ b/halide.cmake @@ -148,6 +148,7 @@ function(halide_library_from_generator BASENAME) "${FEATURE}" STREQUAL "linux" OR "${FEATURE}" STREQUAL "osx" OR "${FEATURE}" STREQUAL "windows" OR + "${FEATURE}" STREQUAL "freebsd" OR "${FEATURE}" STREQUAL "ios" OR "${FEATURE}" STREQUAL "android") message(FATAL_ERROR "HALIDE_TARGET_FEATURES may not the Arch/OS/Bits string '${FEATURE}'; use HALIDE_TARGET instead.") diff --git a/src/JITModule.cpp b/src/JITModule.cpp index c9961779100a..4449c5be32aa 100644 --- a/src/JITModule.cpp +++ b/src/JITModule.cpp @@ -109,6 +109,17 @@ void load_opengl() { llvm::sys::DynamicLibrary::LoadLibraryPermanently("/System/Library/Frameworks/OpenGL.framework/OpenGL", &error); user_assert(error.empty()) << "Could not find OpenGL.framework\n"; } +#elif defined(__FreeBSD__) + if (have_symbol("glXGetCurrentContext") && have_symbol("glDeleteTextures")) { + debug(1) << "OpenGL support code already linked in...\n"; + } else { + debug(1) << "Looking for OpenGL support code...\n"; + string error; + llvm::sys::DynamicLibrary::LoadLibraryPermanently("libGL.so.1", &error); + user_assert(error.empty()) << "Could not find libGL.so\n"; + llvm::sys::DynamicLibrary::LoadLibraryPermanently("libX11.so", &error); + user_assert(error.empty()) << "Could not find libX11.so\n"; + } #else internal_error << "JIT support for OpenGL on anything other than linux or OS X not yet implemented\n"; #endif diff --git a/src/LLVM_Runtime_Linker.cpp b/src/LLVM_Runtime_Linker.cpp index d50c534a3e93..e6df129b7b52 100644 --- a/src/LLVM_Runtime_Linker.cpp +++ b/src/LLVM_Runtime_Linker.cpp @@ -401,6 +401,9 @@ llvm::Triple get_triple_for_target(const Target &target) { if (target.os == Target::Linux) { triple.setOS(llvm::Triple::Linux); triple.setEnvironment(llvm::Triple::GNU); + } else if (target.os == Target::FreeBSD) { + triple.setOS(llvm::Triple::FreeBSD); + triple.setEnvironment(llvm::Triple::GNU); } else if (target.os == Target::OSX) { triple.setVendor(llvm::Triple::Apple); triple.setOS(llvm::Triple::MacOSX); @@ -447,6 +450,9 @@ llvm::Triple get_triple_for_target(const Target &target) { } else if (target.os == Target::Linux) { triple.setOS(llvm::Triple::Linux); triple.setEnvironment(llvm::Triple::GNUEABIHF); + } else if (target.os == Target::FreeBSD) { + triple.setOS(llvm::Triple::FreeBSD); + triple.setEnvironment(llvm::Triple::GNUEABIHF); } else if (target.os == Target::Fuchsia) { triple.setOS(llvm::Triple::Fuchsia); } else { @@ -508,6 +514,10 @@ llvm::Triple get_triple_for_target(const Target &target) { triple.setOS(llvm::Triple::Linux); // TODO: Check what options there are here. triple.setEnvironment(llvm::Triple::GNUEABIHF); + } else if (target.os == Target::FreeBSD) { + triple.setOS(llvm::Triple::FreeBSD); + // TODO: Check what options there are here. + triple.setEnvironment(llvm::Triple::GNUEABIHF); } else if (target.os == Target::NoOS) { // for baremetal environment } else { @@ -772,7 +782,7 @@ std::unique_ptr get_initial_module_for_target(Target t, llvm::LLVM if (module_type != ModuleGPU) { if (module_type != ModuleJITInlined && module_type != ModuleAOTNoRuntime) { // OS-dependent modules - if (t.os == Target::Linux) { + if (t.os == Target::Linux || t.os == Target::FreeBSD) { modules.push_back(get_initmod_posix_allocator(c, bits_64, debug)); modules.push_back(get_initmod_posix_error_handler(c, bits_64, debug)); modules.push_back(get_initmod_posix_print(c, bits_64, debug)); @@ -1085,7 +1095,7 @@ std::unique_ptr get_initial_module_for_target(Target t, llvm::LLVM } if (t.has_feature(Target::OpenGL)) { modules.push_back(get_initmod_opengl(c, bits_64, debug)); - if (t.os == Target::Linux) { + if (t.os == Target::Linux || t.os == Target::FreeBSD) { if (t.has_feature(Target::EGL)) { modules.push_back(get_initmod_opengl_egl_context(c, bits_64, debug)); } else { @@ -1104,7 +1114,7 @@ std::unique_ptr get_initial_module_for_target(Target t, llvm::LLVM if (t.os == Target::Android) { // Only platform that supports OpenGL Compute for now. modules.push_back(get_initmod_opengl_egl_context(c, bits_64, debug)); - } else if (t.os == Target::Linux) { + } else if (t.os == Target::Linux || t.os == Target::FreeBSD) { if (t.has_feature(Target::EGL)) { modules.push_back(get_initmod_opengl_egl_context(c, bits_64, debug)); } else { diff --git a/src/Target.cpp b/src/Target.cpp index 2031dc022175..098a89357e55 100644 --- a/src/Target.cpp +++ b/src/Target.cpp @@ -60,6 +60,9 @@ static void cpuid(int info[4], int infoType, int extra) { Target calculate_host_target() { Target::OS os = Target::OSUnknown; +#if defined(__FreeBSD__) + os = Target::FreeBSD; +#endif #ifdef __linux__ os = Target::Linux; #endif @@ -256,6 +259,7 @@ Target::Feature get_host_cuda_capability(Target t) { const std::map os_name_map = { {"os_unknown", Target::OSUnknown}, + {"freebsd", Target::FreeBSD}, {"linux", Target::Linux}, {"windows", Target::Windows}, {"osx", Target::OSX}, diff --git a/src/Target.h b/src/Target.h index 182a27fa9418..e07faa9d0dfa 100644 --- a/src/Target.h +++ b/src/Target.h @@ -22,6 +22,7 @@ struct Target { * Corresponds to os_name_map in Target.cpp. */ enum OS { OSUnknown = 0, + FreeBSD, Linux, Windows, OSX, diff --git a/src/Util.cpp b/src/Util.cpp index 8453acff2bd6..ff6fa4438162 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -20,6 +20,12 @@ #include #include +#if defined(__FreeBSD__) +#define CAN_GET_RUNNING_PROGRAM_NAME +#include +#include +#include // For PATH_MAX +#endif #ifdef __linux__ #define CAN_GET_RUNNING_PROGRAM_NAME #include // For PATH_MAX @@ -79,11 +85,24 @@ string running_program_name() { #else string program_name; char path[PATH_MAX] = {0}; - uint32_t size = sizeof(path); #if defined(__linux__) + size_t size = sizeof(path); ssize_t len = ::readlink("/proc/self/exe", path, size - 1); #elif defined(__APPLE__) + size_t size = sizeof(path); ssize_t len = ::_NSGetExecutablePath(path, &size); +#elif defined(__FreeBSD__) + int mib[4]; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PATHNAME; + mib[3] = -1; + + size_t len = sizeof(path); + if (sysctl(mib, 4, path, &len, NULL, 0) != 0) { + path[0] = '\0'; + } + #endif if (len != -1) { #if defined(__linux__)