summaryrefslogtreecommitdiff
path: root/slicer/tool/slicer.cpp
blob: a256bda41c2b9c5e697163a60da4816ea95c0dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <boost/algorithm/string/split.hpp>
#include <boost/program_options.hpp>
#include <compileTimeFormatter.h>
#include <tool/parser.h>
#include <unistd.h>

namespace po = boost::program_options;
using namespace AdHoc::literals;

static std::string
defaultPostProcessor()
{
	constexpr std::array<const std::pair<std::string_view, std::string_view>, 1> pps {{
			{"clang-format", "-i"},
	}};
	const std::string_view path {getenv("PATH")};
	const auto pathBegin = make_split_iterator(path, first_finder(":", boost::is_equal()));
	for (const auto & [cmd, opts] : pps) {
		for (auto p = pathBegin; p != decltype(pathBegin) {}; ++p) {
			if (std::filesystem::exists(std::filesystem::path(p->begin(), p->end()) / cmd)) {
				return "%? %?"_fmt(cmd, opts);
			}
		}
	}
	return "";
}

int
main(int argc, char ** argv)
{
	Slicer::Slicer slicer;
	std::string post;

	po::options_description opts("Slicer options");
	// clang-format off
	opts.add_options()
		("help,h", "Show this help message")
		("include,I", po::value(&slicer.includes), "Add include directory to search path")
		// NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.VirtualCall)
		("headerPrefix", po::value(&slicer.headerPrefix)->default_value(slicer.headerPrefix), "Prefix path for Slicer C++ #includes")
		("post,p", po::value(&post)->default_value(defaultPostProcessor()), "Post-process command")
		("slice,i", po::value(&slicer.slicePath), "Input ICE Slice file")
		("cpp,o", po::value(&slicer.cppPath), "Output C++ file");
	// clang-format on

	po::positional_options_description p;
	p.add("slice", 1);
	p.add("cpp", 1);

	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(opts).positional(p).run(), vm);
	po::notify(vm);

	if (vm.count("help") || slicer.slicePath.empty() || slicer.cppPath.empty()) {
		// LCOV_EXCL_START
		std::cout << opts << std::endl;
		return 1;
		// LCOV_EXCL_STOP
	}
	slicer.Execute();

	if (!post.empty()) {
		return system("%? %?"_fmt(post, slicer.cppPath).c_str());
	}

	return 0;
}