# HG changeset patch # User David Demelier # Date 1525102001 -7200 # Node ID 7e40ed196bb0c062b500d541d6ef7959816ebf5c # Parent a03f61a31cb14a1f3cd935f12d04a65f92dbf800 executable: use boost::dll::program_location diff -r a03f61a31cb1 -r 7e40ed196bb0 cpp/CMakeLists.txt --- a/cpp/CMakeLists.txt Mon Apr 30 17:06:02 2018 +0200 +++ b/cpp/CMakeLists.txt Mon Apr 30 17:26:41 2018 +0200 @@ -16,7 +16,6 @@ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # -add_subdirectory(executable) add_subdirectory(is_boolean) add_subdirectory(is_number) add_subdirectory(join) diff -r a03f61a31cb1 -r 7e40ed196bb0 cpp/executable/CMakeLists.txt --- a/cpp/executable/CMakeLists.txt Mon Apr 30 17:06:02 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -# -# CMakeLists.txt -- code building for common code -# -# Copyright (c) 2016-2018 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 a03f61a31cb1 -r 7e40ed196bb0 cpp/executable/executable.cpp --- a/cpp/executable/executable.cpp Mon Apr 30 17:06:02 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,126 +0,0 @@ -/* - * executable.cpp -- get executable path - * - * Copyright (c) 2016-2018 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 a03f61a31cb1 -r 7e40ed196bb0 cpp/executable/executable.hpp --- a/cpp/executable/executable.hpp Mon Apr 30 17:06:02 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -/* - * executable.hpp -- get executable path - * - * Copyright (c) 2016-2018 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 a03f61a31cb1 -r 7e40ed196bb0 cpp/executable/test/main.cpp --- a/cpp/executable/test/main.cpp Mon Apr 30 17:06:02 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -/* - * main.cpp -- test executable_path function - * - * Copyright (c) 2016-2018 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); -}