From 2fb135e10ac1c9fe83a111c71aa88c800be2f9f4 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 17 Dec 2017 12:40:21 -0800 Subject: [PATCH] Support FreeBSD This commit fixes issue on cquery's side. Its dependencies loguru and sparsepp should also be ported to FreeBSD: * https://github.com/emilk/loguru/pull/46 * sysinfo(2) called by sparsepp https://github.com/greg7mdp/sparsepp/blob/master/sparsepp/spp_memory.h#L32 --- src/{platform_linux.cc => platform_posix.cc} | 35 ++++++++++++-------- wscript | 12 +++++-- 2 files changed, 32 insertions(+), 15 deletions(-) rename src/{platform_linux.cc => platform_posix.cc} (94%) diff --git a/src/platform_linux.cc b/src/platform_posix.cc similarity index 94% rename from src/platform_linux.cc rename to src/platform_posix.cc index f2d7dd9d..6be1097d 100644 --- a/src/platform_linux.cc +++ b/src/platform_posix.cc @@ -1,4 +1,4 @@ -#if defined(__linux__) || defined(__APPLE__) +#if defined(__unix__) || defined(__APPLE__) #include "platform.h" #include "utils.h" @@ -32,12 +32,12 @@ #include #include -#ifndef __APPLE__ -#include -#endif - -#if defined(__linux__) -#include +#if defined(__FreeBSD__) +# include // MAXPATHLEN +# include // sysctl +#elif defined(__linux__) +# include +# include #endif namespace { @@ -216,11 +216,7 @@ extern "C" int _NSGetExecutablePath(char* buf,uint32_t* bufsize); // See https://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from std::string GetExecutablePath() { -#ifndef __APPLE__ - char buffer[PATH_MAX+1] = {0}; - readlink("/proc/self/exe", buffer, PATH_MAX); - return std::string(buffer); -#else +#ifdef __APPLE__ uint32_t size = 0; _NSGetExecutablePath(nullptr, &size); char *buffer = new char[size]; @@ -228,6 +224,19 @@ std::string GetExecutablePath() { std::string result(buffer); delete[] buffer; return result; +#elif defined(__FreeBSD__) + static const int name[] = { + CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1, + }; + char path[MAXPATHLEN]; + size_t len = sizeof(path); + path[0] = '\0'; + (void)sysctl(name, 4, path, &len, NULL, 0); + return std::string(path); +#else + char buffer[PATH_MAX] = {0}; + readlink("/proc/self/exe", buffer, PATH_MAX); + return std::string(buffer); #endif } @@ -256,7 +265,7 @@ bool TryMakeDirectory(const std::string& absolute_path) { void SetCurrentThreadName(const std::string& thread_name) { loguru::set_thread_name(thread_name.c_str()); -#ifndef __APPLE__ +#ifdef __linux__ prctl(PR_SET_NAME, thread_name.c_str(), 0, 0, 0); #endif } diff --git a/wscript b/wscript index 9b93d495..e6bc2215 100644 --- a/wscript +++ b/wscript @@ -79,7 +79,7 @@ def options(opt): grp.add_option('--use-system-clang', dest='use_system_clang', default=False, action='store_true', help='enable use of clang from the system') grp.add_option('--bundled-clang', dest='bundled_clang', default='4.0.0', - help='bundled clang version, downloaded from http://releases.llvm.org/ , e.g. 4.0.0 5.0.0') + help='bundled clang version, downloaded from https://releases.llvm.org/ , e.g. 4.0.0 5.0.0') grp.add_option('--llvm-config', dest='llvm_config', default='llvm-config', help='specify path to llvm-config for automatic configuration [default: %default]') grp.add_option('--clang-prefix', dest='clang_prefix', default='', @@ -178,6 +178,8 @@ def configure(ctx): CLANG_TARBALL_EXT = '.tar.xz' if sys.platform == 'darwin': CLANG_TARBALL_NAME = 'clang+llvm-$version-x86_64-apple-darwin' + elif sys.platform.startswith('freebsd'): + CLANG_TARBALL_NAME = 'clang+llvm-$version-amd64-unknown-freebsd10' elif sys.platform.startswith('linux'): # These executable depend on libtinfo.so.5 CLANG_TARBALL_NAME = 'clang+llvm-$version-x86_64-linux-gnu-ubuntu-14.04' @@ -185,7 +187,7 @@ def configure(ctx): CLANG_TARBALL_NAME = 'LLVM-$version-win64' CLANG_TARBALL_EXT = '.exe' else: - sys.stderr.write('ERROR: Unknown platform {0}\n'.format(sys.platform)) + sys.stderr.write('ERROR: releases.llvm.org does not provide pre-built binaries for your platform {0}\n'.format(sys.platform)) sys.exit(1) CLANG_TARBALL_NAME = string.Template(CLANG_TARBALL_NAME).substitute(version=ctx.options.bundled_clang) @@ -227,6 +229,12 @@ def build(bld): lib.append('rt') lib.append('pthread') lib.append('dl') + elif sys.platform.startswith('freebsd'): + # loguru::stacktrace_as_stdstring calls backtrace_symbols + lib.append('execinfo') + + lib.append('pthread') + lib.append('thr') elif sys.platform == 'darwin': lib.append('pthread')