Mercurial > code
changeset 616:47f003c55e1e
Executable: dedicate new module directory, closes #684
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 21 Aug 2017 11:40:55 +0200 |
parents | e8661a550a12 |
children | 266f32919d0a |
files | CMakeLists.txt misc/CMakeLists.txt misc/executable.cpp modules/executable/CMakeLists.txt modules/executable/executable.cpp modules/executable/executable.hpp modules/executable/test/main.cpp |
diffstat | 7 files changed, 218 insertions(+), 129 deletions(-) [+] |
line wrap: on
line diff
--- 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)
--- 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 <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. -# - -add_executable(test-misc test-all.cpp) -target_link_libraries(test-misc gtest) -
--- 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 <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; -}
--- /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 <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>" +)
--- /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 <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; +}
--- /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 <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
--- /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 <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); +}