-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCompiler.cpp
More file actions
60 lines (46 loc) · 1.63 KB
/
Compiler.cpp
File metadata and controls
60 lines (46 loc) · 1.63 KB
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
#include "Compiler.h"
#include "segappend.h"
#include <filesystem>
#include <iostream>
#include <array>
namespace charon {
int compile(const char *executable, const char *sourceFile,
const char *outputFile) {
auto f = std::fopen(sourceFile, "r");
if (!f) {
std::cout << "Failed to open file: " << sourceFile << std::endl;
return 1;
}
auto source = std::string{};
auto buf = std::array<char, 1024>{};
while (auto n = std::fread(buf.data(), 1, buf.size(), f)) {
source.append(buf.data(), n);
}
std::fclose(f);
std::filesystem::copy_file(executable, outputFile,
std::filesystem::copy_options::overwrite_existing);
auto codesign = std::string{"codesign --remove-signature "} + outputFile;
auto code = std::system(codesign.c_str());
auto hbc = std::fopen(sourceFile, "r");
if (!hbc) {
std::cout << "Failed to open file: " << sourceFile << std::endl;
return 1;
}
auto hbcSize = std::filesystem::file_size(sourceFile);
auto hbcData = malloc(hbcSize + 8);
auto hbcStart = (uint8_t *)hbcData + 8;
std::fread(hbcStart, 1, hbcSize, hbc);
std::fclose(hbc);
*((size_t *)hbcData) = hbcSize;
auto status = segappend_create_segment(outputFile, "__charon_start", hbcData,
hbcSize + 8, outputFile);
if (status != segappend_ok) {
std::cout << "Failed to append segment: " << status << std::endl;
return 1;
}
codesign = std::string{"codesign -fs - "} + outputFile;
code = std::system(codesign.c_str());
std::cout << "Compiled " << sourceFile << " to " << outputFile << std::endl;
return 0;
}
} // namespace charon