localPackages.biu: add exec

This commit is contained in:
陈浩南 2024-06-09 17:45:49 +08:00
parent 74b6155013
commit 354f7d428e
5 changed files with 52 additions and 9 deletions

View File

@ -184,12 +184,9 @@
{
biu = pkgs.mkShell
{
packages = with pkgs; [ pkg-config cmake ninja clang-tools_17 ];
buildInputs =
(with pkgs; [ fmt boost magic-enum libbacktrace eigen range-v3 ])
++ (with pkgs.localPackages; [ concurrencpp tgbot-cpp nameof ]);
# hardeningDisable = [ "all" ];
# NIX_DEBUG = "1";
inputsFrom = with pkgs.localPackages; [ biu ];
buildInputs = [ pkgs.clang-tools_17 ];
CMAKE_EXPORT_COMPILE_COMMANDS = "1";
};
hpcstat = pkgs.mkShell
{

3
local/pkgs/biu/.clangd Normal file
View File

@ -0,0 +1,3 @@
CompileFlags:
Add: [ -Wall, -Wextra, -std=c++23 ]
Compiler: gcc

View File

@ -7,6 +7,11 @@ std::size_t hash(auto&&... objs);
void unused(auto&&...);
// block forever
void block_forever();
detail_::ExecResult exec
(
std::filesystem::path program, std::vector<std::string> args, std::optional<std::string> stdin,
std::map<std::string, std::string> extra_env
);
using int128_t = ...;
using uint128_t = ...;

View File

@ -1,5 +1,6 @@
# pragma once
# include <regex>
# include <filesystem>
# include <fmt/format.h>
# include <magic_enum_all.hpp>
@ -62,5 +63,12 @@ namespace biu
}
template <typename T, typename Fallback = void> using FallbackIfNoTypeDeclared
= typename detail_::FallbackIfNoTypeDeclaredHelper<T, Fallback>::Type;
namespace detail_ { struct ExecResult { int exit_code; std::string stdout, stderr; }; }
detail_::ExecResult exec
(
std::filesystem::path program, std::vector<std::string> args, std::optional<std::string> stdin,
std::map<std::string, std::string> extra_env
);
}
}

View File

@ -1,14 +1,44 @@
# include <future>
# include <utility>
# include <biu.hpp>
# include <boost/process.hpp>
namespace biu
{
std::regex literals::operator""_re(const char* str, std::size_t len)
{ return std::regex{str, len}; }
void common::block_forever()
namespace common
{
std::promise<void>().get_future().wait();
std::unreachable();
void block_forever() { std::promise<void>().get_future().wait(); std::unreachable(); }
detail_::ExecResult exec
(
std::filesystem::path program, std::vector<std::string> args, std::optional<std::string> stdin,
std::map<std::string, std::string> extra_env
)
{
namespace bp = boost::process;
bp::ipstream stdout, stderr;
bp::opstream input;
std::unique_ptr<bp::child> process;
bp::environment env = boost::this_process::environment();
for (const auto& [key, value] : extra_env) env[key] = value;
if (stdin)
{
process = std::make_unique<bp::child>
(program.string(), bp::args(args), bp::std_out > stdout, bp::std_err > stderr, bp::std_in < input, env);
input << *stdin;
input.pipe().close();
}
else process = std::make_unique<bp::child>
(program.string(), bp::args(args), bp::std_out > stdout, bp::std_err > stderr, bp::std_in < bp::null, env);
process->wait();
return
{
process->exit_code(),
std::string{std::istreambuf_iterator<char>{stdout.rdbuf()}, {}},
std::string{std::istreambuf_iterator<char>{stderr.rdbuf()}, {}}
};
}
}
}