changeset 640:7e40ed196bb0

executable: use boost::dll::program_location
author David Demelier <markand@malikania.fr>
date Mon, 30 Apr 2018 17:26:41 +0200
parents a03f61a31cb1
children 3d8fae8e4447
files cpp/CMakeLists.txt cpp/executable/CMakeLists.txt cpp/executable/executable.cpp cpp/executable/executable.hpp cpp/executable/test/main.cpp
diffstat 5 files changed, 0 insertions(+), 218 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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 <markand@malikania.fr>
-#
-# 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="$<TARGET_FILE:test-executable>"
-)
--- 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 <markand@malikania.fr>
- *
- * 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 <string>
-#include <stdexcept>
-
-#if defined(__linux__)
-#   include <unistd.h>
-
-#   include <cerrno>
-#   include <climits>
-#   include <cstring>
-#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
-#   if defined(__NetBSD__)
-#       include <sys/param.h>
-#       include <unistd.h>
-#   else
-#       include <sys/types.h>
-#   endif
-
-#   if defined(__OpenBSD__)
-#       include <unistd.h>
-#   endif
-
-#   include <sys/sysctl.h>
-
-#   include <cerrno>
-#   include <climits>
-#   include <cstddef>
-#   include <cstdlib>
-#   include <cstring>
-#elif defined(__APPLE__)
-#   include <cerrno>
-#   include <cstring>
-#   include <libproc.h>
-#   include <unistd.h>
-#elif defined(_WIN32)
-#   include <Windows.h>
-#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<char**>(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;
-}
--- 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 <markand@malikania.fr>
- *
- * 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 <string>
-
-/**
- * Get executable path.
- *
- * \return the string to the executable path
- * \throw std::exception on any system error
- */
-std::string executable_path();
-
-#endif // !EXECUTABLE_HPP
--- 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 <markand@malikania.fr>
- *
- * 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 <boost/filesystem.hpp>
-#include <boost/test/unit_test.hpp>
-
-#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);
-}