From 135fb6efd13ec024ce7c5f2a6707c68147474ac8 Mon Sep 17 00:00:00 2001 From: Pabloader Date: Sat, 23 May 2026 16:34:14 +0000 Subject: [PATCH] Precompiled headers --- build/assets/pch/pch.h | 6 +++ build/assets/pch/pch.hpp | 17 +++++++ build/plugins/wasmPlugin.ts | 50 ++++++++++++++++--- src/games/playground/awoo.cpp | 91 +++-------------------------------- 4 files changed, 73 insertions(+), 91 deletions(-) create mode 100644 build/assets/pch/pch.h create mode 100644 build/assets/pch/pch.hpp diff --git a/build/assets/pch/pch.h b/build/assets/pch/pch.h new file mode 100644 index 0000000..38fca4e --- /dev/null +++ b/build/assets/pch/pch.h @@ -0,0 +1,6 @@ +#pragma once +#include +#include +#include +#include +#include diff --git a/build/assets/pch/pch.hpp b/build/assets/pch/pch.hpp new file mode 100644 index 0000000..2013e9e --- /dev/null +++ b/build/assets/pch/pch.hpp @@ -0,0 +1,17 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include diff --git a/build/plugins/wasmPlugin.ts b/build/plugins/wasmPlugin.ts index 45db69a..003bdec 100644 --- a/build/plugins/wasmPlugin.ts +++ b/build/plugins/wasmPlugin.ts @@ -107,22 +107,44 @@ async function saveFlags(output: string, flags: string[]): Promise { await Bun.write(output + '.flags', flags.join('\0')); } +async function buildPch( + cc: CompilerWithFlags, + flags: string[], + std: string[], + header: string, + outDir: string, +): Promise { + 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 { 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]; diff --git a/src/games/playground/awoo.cpp b/src/games/playground/awoo.cpp index a92ff29..5b22a6c 100644 --- a/src/games/playground/awoo.cpp +++ b/src/games/playground/awoo.cpp @@ -1,94 +1,19 @@ #include #include #include -#include #include -#include -#include -#include -#include - -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 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 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 { +template +concept addable = requires(T a) { + { a + 1 }; +}; + +auto autofunc(addable auto a) { return a + 1; }