From 6ca4c8cb1d2a990b02032f686ecae7a1985ab5f3 Mon Sep 17 00:00:00 2001 From: gwquk <47087722+gwquk@users.noreply.github.com> Date: Thu, 31 Jan 2019 06:29:04 +0000 Subject: [PATCH] Add build and run support for FreeBSD OS, and potentially other BSD OSes (#476) --- README.md | 48 ++++++++++++++++++++++++++++++++++++++- include/retdec/utils/os.h | 13 +++++++---- src/utils/memory.cpp | 33 +++++++++++++++++++++++++-- 3 files changed, 87 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0332182b1..3ae87002f 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ For more information, check out our ## Installation and Use -Currently, we support Windows (7 or later), Linux, and macOS. An installed version of RetDec requires approximately 4 GB of free disk space. +Currently, we support Windows (7 or later), Linux, macOS, and (experimental) FreeBSD. An installed version of RetDec requires approximately 4 GB of free disk space. ### Windows @@ -91,6 +91,21 @@ Currently, we support Windows (7 or later), Linux, and macOS. An installed versi For more information, run `retdec-decompiler.py` with `--help`. +### FreeBSD + +1. There are currently no pre-built "ports" packages for FreeBSD. You will have to build and install the decompiler by yourself. The process is described below. + +2. After you have built the decompiler, you may need to install the following packages and execute the following command: +* `sudo pkg install python37 bison autotools` +* `sudo ln -s /usr/local/bin/python3.7 /usr/local/bin/python3` + +3. Now, you are all set to run the decompiler. To decompile a binary file named `test.exe`, run + + ``` + $RETDEC_INSTALL_DIR/bin/retdec-decompiler.py test.exe + ``` + For more information, run `retdec-decompiler.py` with `--help`. + ## Build and Installation This section describes a local build and installation of RetDec. Instructions for Docker are given in the next section. @@ -154,6 +169,21 @@ Packages should be preferably installed via [Homebrew](https://brew.sh). * [autotools](https://en.wikipedia.org/wiki/GNU_Build_System) ([autoconf](https://www.gnu.org/software/autoconf/autoconf.html), [automake](https://www.gnu.org/software/automake/), and [libtool](https://www.gnu.org/software/libtool/)) * Optional: [Doxygen](http://www.stack.nl/~dimitri/doxygen/) and [Graphviz](http://www.graphviz.org/) for generating API documentation +#### FreeBSD + +Packages should be installed via FreeBSDs pre-compiled package repository using the `pkg` command OR built from scratch using the `ports` database method. + +* Full "pkg" tool instructions [handbook pkg method](https://www.freebsd.org/doc/handbook/pkgng-intro.html) + * `pkg install cmake python37 bison git autotools` +OR +* Full "ports" instructions [handbook ports method](https://www.freebsd.org/doc/handbook/ports-using.html) + * `portsnap fetch` + * `portsnap extract` +* For example "cmake" would be + * `whereis cmake` + * `cd /usr/ports/devel/cmake` + * `make install clean` + ### Process Note: Although RetDec now supports a system-wide installation ([#94](https://github.com/avast-tl/retdec/issues/94)), unless you use your distribution's package manager to install it, we recommend installing RetDec locally into a designated directory. The reason for this is that uninstallation will be easier as you will only need to remove a single directory. To perform a local installation, run `cmake` with the `-DCMAKE_INSTALL_PREFIX=` parameter, where `` is directory into which RetDec will be installed (e.g. `$HOME/projects/retdec-install` on Linux and macOS, and `C:\projects\retdec-install` on Windows). @@ -186,6 +216,22 @@ Note: Although RetDec now supports a system-wide installation ([#94](https://git * `cmake .. -DCMAKE_INSTALL_PREFIX=` * `make -jN` (`N` is the number of CPU cores to use for parallel build) * `make install` +* FreeBSD: + * `sudo pkg install git cmake` + * `git clone https://github.com/avast-tl/retdec` + * `cd retdec` + * `mkdir build && cd build` + * ```sh + # FreeBSD (and other BSDs) do need cmake, python3, bison, git, autotools. Flex and perl are pre-installed in the OS but check versions. + # Later versions may be available for each of the packages. + # See what is installed: + sudo pkg info cmake python37 bison autotools + # Install/upgrade them + sudo pkg install cmake python37 bison autotools + ``` + * `cmake .. -DCMAKE_INSTALL_PREFIX=` + * `make -jN` (`N` is the number of CPU cores to use for parallel build) + * `make install` You have to pass the following parameters to `cmake`: * `-DCMAKE_INSTALL_PREFIX=` to set the installation path to ``. Quote the path if you are using backslashes on Windows (e.g. `-DCMAKE_INSTALL_PREFIX="C:\retdec"`). diff --git a/include/retdec/utils/os.h b/include/retdec/utils/os.h index 5514a7770..e5c00c52f 100644 --- a/include/retdec/utils/os.h +++ b/include/retdec/utils/os.h @@ -11,14 +11,19 @@ // Windows, macOS, and Linux. #if defined(__WIN) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) #define OS_WINDOWS -#elif defined(__APPLE__) - #define OS_MACOS #else - #define OS_LINUX + #include + #if defined(__APPLE__) + #define OS_MACOS + #elif defined(BSD) + #define OS_BSD + #else + #define OS_LINUX + #endif #endif // It is also useful to know whether the operating system is POSIX compliant. -#if defined(OS_MACOS) || defined(OS_LINUX) +#if defined(OS_MACOS) || defined(OS_LINUX) || defined(OS_BSD) #define OS_POSIX #endif diff --git a/src/utils/memory.cpp b/src/utils/memory.cpp index c7019ed51..d3637a047 100644 --- a/src/utils/memory.cpp +++ b/src/utils/memory.cpp @@ -6,10 +6,11 @@ #include "retdec/utils/memory.h" #include "retdec/utils/os.h" +#include #ifdef OS_WINDOWS #include -#elif defined(OS_MACOS) +#elif defined(OS_MACOS) || defined(OS_BSD) #include #include #else @@ -32,7 +33,7 @@ namespace { */ bool limitSystemMemoryOnPOSIX(std::size_t limit) { struct rlimit rl = { - .rlim_cur = limit, // Soft limit. + .rlim_cur = static_cast(limit), // Soft limit. .rlim_max = RLIM_INFINITY // Hard limit (ceiling for rlim_cur). }; auto rc = setrlimit(RLIMIT_AS, &rl); @@ -119,6 +120,30 @@ bool limitSystemMemoryOnMacOS(std::size_t limit) { return limitSystemMemoryOnPOSIX(limit); } +#elif defined(OS_BSD) + +/** +* @brief Implementation of @c getTotalSystemMemory() on *BSD. +* +* AKA FreeBSD, DragonFly, NetBSD, OpenBSD, TrueOS, PCBSD +*/ +std::size_t getTotalSystemMemoryOnBSD() { + int what[] = { CTL_HW, HW_PHYSMEM }; + std::size_t value = 0; + std::size_t length = sizeof(value); + auto rc = sysctl(what, 2, &value, &length, nullptr, 0); + return rc != -1 ? value : 0; +} + +/** +* @brief Implementation of @c limitSystemMemory() on *BSD. +* +* AKA FreeBSD, DragonFly, NetBSD, OpenBSD, TrueOS, PCBSD +*/ +bool limitSystemMemoryOnBSD(std::size_t limit) { + return limitSystemMemoryOnPOSIX(limit); +} + #else /* @@ -151,6 +176,8 @@ std::size_t getTotalSystemMemory() { return getTotalSystemMemoryOnWindows(); #elif defined(OS_MACOS) return getTotalSystemMemoryOnMacOS(); +#elif defined(OS_BSD) + return getTotalSystemMemoryOnBSD(); #else return getTotalSystemMemoryOnLinux(); #endif @@ -175,6 +202,8 @@ bool limitSystemMemory(std::size_t limit) { return limitSystemMemoryOnWindows(limit); #elif defined(OS_MACOS) return limitSystemMemoryOnMacOS(limit); +#elif defined(OS_BSD) + return limitSystemMemoryOnBSD(limit); #else return limitSystemMemoryOnLinux(limit); #endif