summaryrefslogtreecommitdiff
path: root/project2/ice/sliceCompile.cpp
blob: 0544aeae057f976c729161cd84aea290566d303d (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
68
69
70
71
72
73
74
75
76
77
78
79
#include <pch.hpp>
#include "sliceCompile.h"
#include <scopeExit.h>
#include <mutex>

std::mutex slicePreprocessor;

SliceCompile::SliceCompile(const boost::filesystem::path & slice, const IceCompile::Deps & dep) :
	IceCompile(slice, dep)
{
}

unsigned int
SliceCompile::build(const boost::filesystem::path & in, FILE * out) const
{
	std::vector<std::string> cppArgs;
	std::lock_guard<std::mutex> lock(slicePreprocessor);
	Slice::PreprocessorPtr icecpp = Slice::Preprocessor::create("slice2project2", in.string(), cppArgs);
	FILE * cppHandle = icecpp->preprocess(false);

	if (cppHandle == NULL) {
		throw std::runtime_error("preprocess failed");
	}

	Slice::UnitPtr u = Slice::Unit::createUnit(false, false, false, false);
	AdHoc::ScopeExit uDestroy(boost::bind(&Slice::Unit::destroy, u.get()));

	int parseStatus = u->parse(in.string(), cppHandle, false);

	if (!icecpp->close()) {
		throw std::runtime_error("preprocess close failed");
	}

	if (parseStatus == EXIT_FAILURE) {
		throw std::runtime_error("unit parse failed");
	}

	if (out) {
		fprintf(out, "#include <%s>\n", (in.filename().replace_extension(".h")).string().c_str());
		Headers(out);
		fprintf(out, "\n");
	}
	return Body(out, u);
}

unsigned int
SliceCompile::Build(const boost::filesystem::path & in, const boost::filesystem::path & outpath) const
{
	FILE * out = fopen(outpath.string().c_str(), "w");
	if (!out) {
		throw std::runtime_error("failed to open target");
	}

	auto components = build(in, out);

	if (fclose(out)) {
		throw std::runtime_error("failed to close target");
	}

	return components;
}

unsigned int
SliceCompile::Count(const boost::filesystem::path & in) const
{
	return build(in, NULL);
}

void
SliceCompile::Headers(FILE *) const
{
}

boost::filesystem::path
SliceCompile::InputPath() const
{
	return slice;
}