# HG changeset patch # User David Demelier # Date 1503308455 -7200 # Node ID 47f003c55e1e8d105a60300a08f16a0838c18fa4 # Parent e8661a550a12396264ea0caa81c84dd4b0fcdf4b Executable: dedicate new module directory, closes #684 diff -r e8661a550a12 -r 47f003c55e1e CMakeLists.txt --- a/CMakeLists.txt Mon Aug 21 11:31:22 2017 +0200 +++ b/CMakeLists.txt Mon Aug 21 11:40:55 2017 +0200 @@ -49,5 +49,6 @@ ) endif () +add_subdirectory(modules/executable) add_subdirectory(modules/join) add_subdirectory(modules/js) diff -r e8661a550a12 -r 47f003c55e1e misc/CMakeLists.txt --- a/misc/CMakeLists.txt Mon Aug 21 11:31:22 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ -# -# CMakeLists.txt -- code building for common code -# -# Copyright (c) 2013-2016 David Demelier -# -# Permission to use, copy, modify, and/or distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# - -add_executable(test-misc test-all.cpp) -target_link_libraries(test-misc gtest) - diff -r e8661a550a12 -r 47f003c55e1e misc/executable.cpp --- a/misc/executable.cpp Mon Aug 21 11:31:22 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +0,0 @@ -#include -#include - -#if defined(__linux__) -# include - -# include -# include -# include -#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) -# if defined(__NetBSD__) -# include -# include -# else -# include -# endif - -# if defined(__OpenBSD__) -# include -# endif - -# include - -# include -# include -# include -# include -# include -#elif defined(__APPLE__) -# include -# include -# include -# include -#elif defined(_WIN32) -# include -#endif - -std::string executable_path() -{ - std::string result; - -#if defined(__linux__) - char path[PATH_MAX + 1] = {0}; - - if (readlink("/proc/self/exe", path, sizeof (path) - 1) < 0) - throw std::runtime_error(std::strerror(errno)); - - result = path; -#elif defined(__FreeBSD__) || defined(__DragonFly__) - int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; - char path[PATH_MAX + 1] = {0}; - size_t size = PATH_MAX; - - if (sysctl(mib, 4, path, &size, nullptr, 0) < 0) - throw std::runtime_error(std::strerror(errno)); - - result = path; -#elif defined(__APPLE__) - char path[PROC_PIDPATHINFO_MAXSIZE + 1] = {0}; - - if ((proc_pidpath(getpid(), path, sizeof (path) - 1) == 0) - throw std::runtime_error(std::strerror(errno)); - - result = path; -#elif defined(_WIN32) - char path[PATH_MAX + 1] = {0}; - - if (GetModuleFileNameA(nullptr, path, sizeof (path) - 1) == 0) - throw std::runtime_error("GetModuleFileName error"); - - result = path; -#elif defined(__NetBSD__) - char path[4096 + 1] = {0}; - -# if defined(KERN_PROC_PATHNAME) - int mib[] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME }; - int size = sizeof (path) - 1; - - if (sysctl(mib, 4, path, &size, nullptr, 0) < 0) - throw std::runtime_error(std::strerror(errno)); -# else - if (readlink("/proc/curproc/exe", path, sizeof (path) - 1) < 0) - throw std::runtime_error(std::strerror(errno)); -# endif - - result = path; -#elif defined(__OpenBSD__) - char **paths, path[PATH_MAX + 1] = {0}; - int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV }; - size_t length = 0; - - if (sysctl(mib, 4, 0, &length, 0, 0) < 0) - throw std::runtime_error(std::strerror(errno)); - if (!(paths = static_cast(std::malloc(length)))) - throw std::runtime_error(std::strerror(errno)); - if (sysctl(mib, 4, paths, &length, 0, 0) < 0) { - std::free(paths); - throw std::runtime_error(std::strerror(errno)); - } - - realpath(paths[0], path); - result = path; - - std::free(paths); -#endif - - return result; -} diff -r e8661a550a12 -r 47f003c55e1e modules/executable/CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/executable/CMakeLists.txt Mon Aug 21 11:40:55 2017 +0200 @@ -0,0 +1,26 @@ +# +# CMakeLists.txt -- code building for common code +# +# Copyright (c) 2013-2017 David Demelier +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +find_package(Boost REQUIRED COMPONENTS filesystem system) + +code_define_module( + NAME executable + SOURCES executable.cpp executable.hpp + LIBRARIES Boost::filesystem Boost::system + FLAGS TARGET_FILE="$" +) diff -r e8661a550a12 -r 47f003c55e1e modules/executable/executable.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/executable/executable.cpp Mon Aug 21 11:40:55 2017 +0200 @@ -0,0 +1,126 @@ +/* + * executable.cpp -- get executable path + * + * Copyright (c) 2016 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#if defined(__linux__) +# include + +# include +# include +# include +#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) +# if defined(__NetBSD__) +# include +# include +# else +# include +# endif + +# if defined(__OpenBSD__) +# include +# endif + +# include + +# include +# include +# include +# include +# include +#elif defined(__APPLE__) +# include +# include +# include +# include +#elif defined(_WIN32) +# include +#endif + +std::string executable_path() +{ + std::string result; + +#if defined(__linux__) + char path[PATH_MAX + 1] = {0}; + + if (readlink("/proc/self/exe", path, sizeof (path) - 1) < 0) + throw std::runtime_error(std::strerror(errno)); + + result = path; +#elif defined(__FreeBSD__) || defined(__DragonFly__) + int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + char path[PATH_MAX + 1] = {0}; + size_t size = PATH_MAX; + + if (sysctl(mib, 4, path, &size, nullptr, 0) < 0) + throw std::runtime_error(std::strerror(errno)); + + result = path; +#elif defined(__APPLE__) + char path[PROC_PIDPATHINFO_MAXSIZE + 1] = {0}; + + if ((proc_pidpath(getpid(), path, sizeof (path) - 1) == 0) + throw std::runtime_error(std::strerror(errno)); + + result = path; +#elif defined(_WIN32) + char path[PATH_MAX + 1] = {0}; + + if (GetModuleFileNameA(nullptr, path, sizeof (path) - 1) == 0) + throw std::runtime_error("GetModuleFileName error"); + + result = path; +#elif defined(__NetBSD__) + char path[4096 + 1] = {0}; + +# if defined(KERN_PROC_PATHNAME) + int mib[] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME }; + int size = sizeof (path) - 1; + + if (sysctl(mib, 4, path, &size, nullptr, 0) < 0) + throw std::runtime_error(std::strerror(errno)); +# else + if (readlink("/proc/curproc/exe", path, sizeof (path) - 1) < 0) + throw std::runtime_error(std::strerror(errno)); +# endif + + result = path; +#elif defined(__OpenBSD__) + char **paths, path[PATH_MAX + 1] = {0}; + int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV }; + size_t length = 0; + + if (sysctl(mib, 4, 0, &length, 0, 0) < 0) + throw std::runtime_error(std::strerror(errno)); + if (!(paths = static_cast(std::malloc(length)))) + throw std::runtime_error(std::strerror(errno)); + if (sysctl(mib, 4, paths, &length, 0, 0) < 0) { + std::free(paths); + throw std::runtime_error(std::strerror(errno)); + } + + realpath(paths[0], path); + result = path; + + std::free(paths); +#endif + + return result; +} diff -r e8661a550a12 -r 47f003c55e1e modules/executable/executable.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/executable/executable.hpp Mon Aug 21 11:40:55 2017 +0200 @@ -0,0 +1,32 @@ +/* + * executable.hpp -- get executable path + * + * Copyright (c) 2016 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef EXECUTABLE_HPP +#define EXECUTABLE_HPP + +#include + +/** + * Get executable path. + * + * \return the string to the executable path + * \throw std::exception on any system error + */ +std::string executable_path(); + +#endif // !EXECUTABLE_HPP diff -r e8661a550a12 -r 47f003c55e1e modules/executable/test/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/executable/test/main.cpp Mon Aug 21 11:40:55 2017 +0200 @@ -0,0 +1,33 @@ +/* + * test-executable.cpp -- test executable_path function + * + * Copyright (c) 2013-2016 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#define BOOST_TEST_MODULE "Executable path" +#include +#include + +#include "executable.hpp" + +namespace fs = boost::filesystem; + +BOOST_AUTO_TEST_CASE(normal) +{ + fs::path expected(TARGET_FILE); + fs::path result(executable_path()); + + BOOST_REQUIRE_EQUAL(expected, result); +}