1
0
Fork 0

Precompiled headers

This commit is contained in:
Pabloader 2026-05-23 16:34:14 +00:00
parent 1637d4ed1b
commit 135fb6efd1
4 changed files with 73 additions and 91 deletions

6
build/assets/pch/pch.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

17
build/assets/pch/pch.hpp Normal file
View File

@ -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>

View File

@ -107,22 +107,44 @@ async function saveFlags(output: string, flags: string[]): Promise<void> {
await Bun.write(output + '.flags', flags.join('\0')); 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( async function compileLibGroup(
cc: CompilerWithFlags, cc: CompilerWithFlags,
flags: string[], flags: string[],
sources: string[], sources: string[],
std: string[], std: string[],
objDir: string, objDir: string,
pch: string | null,
): Promise<string[]> { ): Promise<string[]> {
if (sources.length === 0) return []; if (sources.length === 0) return [];
await fs.mkdir(objDir, { recursive: true }); await fs.mkdir(objDir, { recursive: true });
const objPaths = sources.map(src => path.resolve(objDir, path.basename(src) + '.o')); 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) => { await Promise.all(sources.map(async (src, i) => {
if (await needsRebuild(objPaths[i], [src], compilationFlags)) { if (await needsRebuild(objPaths[i], [src, ...inputs], compilationFlags)) {
const result = await $`${cc.cc} -c ${flags} ${std} -o ${objPaths[i]} ${src}`; const result = await $`${cc.cc} -c ${flags} ${std} ${pchArgs} -o ${objPaths[i]} ${src}`;
if (result.exitCode !== 0) throw new Error(`Compile failed: ${src}`); if (result.exitCode !== 0) throw new Error(`Compile failed: ${src}`);
await saveFlags(objPaths[i], compilationFlags); 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 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([ const [, libCObjs, libCppObjs] = await Promise.all([
(async () => { (async () => {
const compilationFlags = [...flags, ...std]; const compilationFlags = [...flags, ...std, ...inputPchArgs];
if (!await needsRebuild(inputObjPath, [args.path], compilationFlags)) return; const extraInputs = inputPch ? [inputPch] : [];
const result = await $`${cc.cc} -c ${flags} ${std} -o ${inputObjPath} ${args.path}`; 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'); if (result.exitCode !== 0) throw new Error('Compile failed, check output');
await saveFlags(inputObjPath, compilationFlags); await saveFlags(inputObjPath, compilationFlags);
})(), })(),
compileLibGroup(cc, flags, libCFiles, STD_C, libCObjDir), compileLibGroup(cc, flags, libCFiles, STD_C, libCObjDir, pchC),
compileLibGroup(cc, flags, libCppFiles, STD_CPP, libCppObjDir), compileLibGroup(cc, flags, libCppFiles, STD_CPP, libCppObjDir, pchCpp),
]); ]);
const allObjs = [inputObjPath, ...libCObjs, ...libCppObjs]; const allObjs = [inputObjPath, ...libCObjs, ...libCppObjs];

View File

@ -1,94 +1,19 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <iostream>
#include <js.h> #include <js.h>
#include <stdlib.h> #include <print>
#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;
}
JS_EXPORT void awoo() { JS_EXPORT void awoo() {
bit a{1, "a"}; std::print("arc4 = {}\nrand = {}\n", arc4random(), rand());
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;
} }
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; return a + 1;
} }