Precompiled headers
This commit is contained in:
parent
1637d4ed1b
commit
135fb6efd1
|
|
@ -0,0 +1,6 @@
|
|||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
|
@ -107,22 +107,44 @@ async function saveFlags(output: string, flags: string[]): Promise<void> {
|
|||
await Bun.write(output + '.flags', flags.join('\0'));
|
||||
}
|
||||
|
||||
async function buildPch(
|
||||
cc: CompilerWithFlags,
|
||||
flags: string[],
|
||||
std: string[],
|
||||
header: string,
|
||||
outDir: string,
|
||||
): Promise<string> {
|
||||
await fs.mkdir(outDir, { recursive: true });
|
||||
const out = path.resolve(outDir, path.basename(header) + '.pch');
|
||||
const lang = std === STD_CPP ? 'c++-header' : 'c-header';
|
||||
const compilationFlags = [...flags, ...std, '-x', lang];
|
||||
if (await needsRebuild(out, [header], compilationFlags)) {
|
||||
const r = await $`${cc.cc} -x ${lang} ${flags} ${std} -o ${out} ${header}`;
|
||||
if (r.exitCode !== 0) throw new Error(`PCH build failed: ${header}`);
|
||||
await saveFlags(out, compilationFlags);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
async function compileLibGroup(
|
||||
cc: CompilerWithFlags,
|
||||
flags: string[],
|
||||
sources: string[],
|
||||
std: string[],
|
||||
objDir: string,
|
||||
pch: string | null,
|
||||
): Promise<string[]> {
|
||||
if (sources.length === 0) return [];
|
||||
|
||||
await fs.mkdir(objDir, { recursive: true });
|
||||
const objPaths = sources.map(src => path.resolve(objDir, path.basename(src) + '.o'));
|
||||
|
||||
const compilationFlags = [...flags, ...std];
|
||||
const pchArgs = pch ? ['-include-pch', pch] : [];
|
||||
const compilationFlags = [...flags, ...std, ...pchArgs];
|
||||
const inputs = pch ? [pch] : [];
|
||||
await Promise.all(sources.map(async (src, i) => {
|
||||
if (await needsRebuild(objPaths[i], [src], compilationFlags)) {
|
||||
const result = await $`${cc.cc} -c ${flags} ${std} -o ${objPaths[i]} ${src}`;
|
||||
if (await needsRebuild(objPaths[i], [src, ...inputs], compilationFlags)) {
|
||||
const result = await $`${cc.cc} -c ${flags} ${std} ${pchArgs} -o ${objPaths[i]} ${src}`;
|
||||
if (result.exitCode !== 0) throw new Error(`Compile failed: ${src}`);
|
||||
await saveFlags(objPaths[i], compilationFlags);
|
||||
}
|
||||
|
|
@ -341,16 +363,28 @@ const wasmPlugin = ({ production }: WasmLoaderConfig = {}): BunPlugin => {
|
|||
|
||||
const std = args.path.endsWith('.cpp') ? STD_CPP : STD_C;
|
||||
|
||||
const pchDir = path.resolve(distDir, 'pch');
|
||||
const needCpp = libCppFiles.length > 0 || args.path.endsWith('.cpp');
|
||||
const needC = libCFiles.length > 0 || args.path.endsWith('.c');
|
||||
const [pchC, pchCpp] = await Promise.all([
|
||||
needC ? buildPch(cc, flags, STD_C, path.resolve(buildAssets, 'pch', 'pch.h'), pchDir) : Promise.resolve(null),
|
||||
needCpp ? buildPch(cc, flags, STD_CPP, path.resolve(buildAssets, 'pch', 'pch.hpp'), pchDir) : Promise.resolve(null),
|
||||
]);
|
||||
|
||||
const inputPch = args.path.endsWith('.cpp') ? pchCpp : pchC;
|
||||
const inputPchArgs = inputPch ? ['-include-pch', inputPch] : [];
|
||||
|
||||
const [, libCObjs, libCppObjs] = await Promise.all([
|
||||
(async () => {
|
||||
const compilationFlags = [...flags, ...std];
|
||||
if (!await needsRebuild(inputObjPath, [args.path], compilationFlags)) return;
|
||||
const result = await $`${cc.cc} -c ${flags} ${std} -o ${inputObjPath} ${args.path}`;
|
||||
const compilationFlags = [...flags, ...std, ...inputPchArgs];
|
||||
const extraInputs = inputPch ? [inputPch] : [];
|
||||
if (!await needsRebuild(inputObjPath, [args.path, ...extraInputs], compilationFlags)) return;
|
||||
const result = await $`${cc.cc} -c ${flags} ${std} ${inputPchArgs} -o ${inputObjPath} ${args.path}`;
|
||||
if (result.exitCode !== 0) throw new Error('Compile failed, check output');
|
||||
await saveFlags(inputObjPath, compilationFlags);
|
||||
})(),
|
||||
compileLibGroup(cc, flags, libCFiles, STD_C, libCObjDir),
|
||||
compileLibGroup(cc, flags, libCppFiles, STD_CPP, libCppObjDir),
|
||||
compileLibGroup(cc, flags, libCFiles, STD_C, libCObjDir, pchC),
|
||||
compileLibGroup(cc, flags, libCppFiles, STD_CPP, libCppObjDir, pchCpp),
|
||||
]);
|
||||
|
||||
const allObjs = [inputObjPath, ...libCObjs, ...libCppObjs];
|
||||
|
|
|
|||
|
|
@ -1,94 +1,19 @@
|
|||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <js.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
class bit {
|
||||
private:
|
||||
static constexpr bit nor(const bit& a, const bit& b);
|
||||
|
||||
public:
|
||||
bit() = default;
|
||||
bit(bool n) : n(n), composition(n ? "1" : "0") {
|
||||
}
|
||||
bit(bool n, std::string_view composition) : n(n), composition(composition) {
|
||||
}
|
||||
bit(const bit& other) : bit(other.n, other.composition) {
|
||||
}
|
||||
|
||||
constexpr bit operator!() const {
|
||||
const bit& a = *this;
|
||||
return nor(a, a);
|
||||
}
|
||||
|
||||
constexpr bit operator|(const bit& b) const {
|
||||
const bit& a = *this;
|
||||
return !nor(a, b);
|
||||
}
|
||||
|
||||
constexpr bit operator&(const bit& b) const {
|
||||
const bit& a = *this;
|
||||
return nor(!a, !b);
|
||||
}
|
||||
|
||||
constexpr bit operator^(const bit& b) const {
|
||||
const bit& a = *this;
|
||||
|
||||
return (!a & b) | (a & !b);
|
||||
}
|
||||
|
||||
constexpr std::pair<bit, bit> operator+(const bit& b) const {
|
||||
const bit& a = *this;
|
||||
|
||||
return {
|
||||
a ^ b,
|
||||
a & b,
|
||||
};
|
||||
}
|
||||
|
||||
operator bool() const {
|
||||
return n;
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& stream, const bit& val) {
|
||||
return stream << val.composition;
|
||||
}
|
||||
|
||||
friend std::istream& operator>>(std::istream& stream, bit& val) {
|
||||
return stream >> val.n;
|
||||
}
|
||||
|
||||
private:
|
||||
bool n = 0;
|
||||
std::string composition = "";
|
||||
};
|
||||
|
||||
constexpr bit bit::nor(const bit& a, const bit& b) {
|
||||
bit result{
|
||||
!(a.n || b.n),
|
||||
"(" + a.composition + " NOR " + b.composition + ")",
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
#include <print>
|
||||
|
||||
JS_EXPORT void awoo() {
|
||||
bit a{1, "a"};
|
||||
bit b{1, "b"};
|
||||
|
||||
auto [sum, carry] = a + b;
|
||||
|
||||
std::cout << "sum = " << sum << "\ncarry = " << carry << std::endl;
|
||||
|
||||
std::cout << "rand = " << rand() << "\narc4 = " << arc4random() << std::endl;
|
||||
std::print("arc4 = {}\nrand = {}\n", arc4random(), rand());
|
||||
}
|
||||
|
||||
auto autofunc(auto a) requires std::is_integral_v<decltype(a)> {
|
||||
template <typename T>
|
||||
concept addable = requires(T a) {
|
||||
{ a + 1 };
|
||||
};
|
||||
|
||||
auto autofunc(addable auto a) {
|
||||
return a + 1;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue