Skip to content

Instantly share code, notes, and snippets.

@kassane
Created August 10, 2024 17:21
Show Gist options
  • Save kassane/7d8f498a64f908b4d72bc2f019b46059 to your computer and use it in GitHub Desktop.
Save kassane/7d8f498a64f908b4d72bc2f019b46059 to your computer and use it in GitHub Desktop.
Build c3c (v0.62.x) compiler using `build.zig` (bootstrap)
//! Zig v0.13.0 or master
//! Recommended `-Doptimize=ReleaseSmall` or `-Doptimize=ReleaseFast` (disabling sanitizer)
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// -Duse_tb
const use_tb = b.option(bool, "use_tb", "Enable the tild backend") orelse false;
const c3c = b.addExecutable(.{
.name = "c3c",
.target = target,
.optimize = optimize,
});
c3c.addIncludePath(b.path("wrapper/include"));
c3c.addIncludePath(b.path("dependencies/miniz"));
c3c.addIncludePath(b.path("src"));
// espressif-LLVM/18.0.2
// https://github.com/kassane/zig-espressif-bootstrap/releases/download/0.14.0-xtensa-dev/llvm-libs+zstd+zlib-x86_64-linux-musl-baseline.tar.xz
c3c.addLibraryPath(.{
.cwd_relative = "../zig-espressif-bootstrap/out/x86_64-linux-musl-baseline/lib",
});
c3c.addIncludePath(.{
.cwd_relative = "../zig-espressif-bootstrap/out/x86_64-linux-musl-baseline/include",
});
c3c.addCSourceFiles(.{
.root = b.path("src"),
.files = src,
});
c3c.addCSourceFiles(.{
.root = b.path("dependencies/miniz"),
.files = &.{
"miniz.c",
},
});
c3c.addCSourceFiles(.{
.root = b.path("wrapper/src"),
.files = &.{
"wrapper.cpp",
},
});
if (use_tb) {
c3c.addCSourceFiles(.{
.root = b.path("src"),
.files = tilde_src,
});
}
// linking
c3c.linkSystemLibrary("z");
c3c.linkSystemLibrary("zstd");
for (clang_libs) |lib_name| {
c3c.linkSystemLibrary(lib_name);
}
for (lld_libs) |lib_name| {
c3c.linkSystemLibrary(lib_name);
}
for (llvm_libs) |lib_name| {
c3c.linkSystemLibrary(lib_name);
}
if (c3c.rootModuleTarget().abi != .msvc) {
c3c.linkLibCpp(); // libcxx + libunwind (statically linked) (mingw/glibc/musl/darwin/bsd)
} else c3c.linkLibC(); // search msvc runtime + winsdk
b.installArtifact(c3c);
}
const src = &.{
"build/build_options.c",
"build/builder.c",
"build/common_build.c",
"build/libraries.c",
"build/project.c",
"build/project_creation.c",
"compiler/abi/c_abi.c",
"compiler/abi/c_abi_aarch64.c",
"compiler/abi/c_abi_riscv.c",
"compiler/abi/c_abi_wasm.c",
"compiler/abi/c_abi_win64.c",
"compiler/abi/c_abi_x64.c",
"compiler/abi/c_abi_x86.c",
"compiler/asm_target.c",
"compiler/ast.c",
"compiler/bigint.c",
"compiler/codegen_asm.c",
"compiler/codegen_general.c",
"compiler/compiler.c",
"compiler/context.c",
"compiler/copying.c",
"compiler/decltable.c",
"compiler/diagnostics.c",
"compiler/expr.c",
"compiler/float.c",
"compiler/headers.c",
"compiler/json_output.c",
"compiler/lexer.c",
"compiler/linker.c",
"compiler/llvm_codegen.c",
"compiler/llvm_codegen_builtins.c",
"compiler/llvm_codegen_debug_info.c",
"compiler/llvm_codegen_expr.c",
"compiler/llvm_codegen_function.c",
"compiler/llvm_codegen_instr.c",
"compiler/llvm_codegen_module.c",
"compiler/llvm_codegen_stmt.c",
"compiler/llvm_codegen_storeload.c",
"compiler/llvm_codegen_type.c",
"compiler/llvm_codegen_value.c",
"compiler/mac_support.c",
"compiler/module.c",
"compiler/number.c",
"compiler/parse_expr.c",
"compiler/parse_global.c",
"compiler/parse_stmt.c",
"compiler/parser.c",
"compiler/sema_asm.c",
"compiler/sema_builtins.c",
"compiler/sema_casts.c",
"compiler/sema_decls.c",
"compiler/sema_errors.c",
"compiler/sema_expr.c",
"compiler/sema_initializers.c",
"compiler/sema_liveness.c",
"compiler/sema_name_resolution.c",
"compiler/sema_passes.c",
"compiler/sema_stmts.c",
"compiler/sema_types.c",
"compiler/semantic_analyser.c",
"compiler/source_file.c",
"compiler/subprocess.c",
"compiler/symtab.c",
"compiler/target.c",
"compiler/tokens.c",
"compiler/types.c",
"compiler/windows_support.c",
"compiler_tests/benchmark.c",
"compiler_tests/shorttest.c",
"compiler_tests/tests.c",
"main.c",
"utils/cpus.c",
"utils/errors.c",
"utils/file_utils.c",
"utils/find_msvc.c",
"utils/http.c",
"utils/json.c",
"utils/malloc.c",
"utils/stringutils.c",
"utils/taskqueue.c",
"utils/time.c",
"utils/unzipper.c",
"utils/vmem.c",
"utils/whereami.c",
};
const tilde_src = &.{
"compiler/tilde_codegen.c",
"compiler/tilde_codegen_abi.c",
"compiler/tilde_codegen_expr.c",
"compiler/tilde_codegen_instr.c",
"compiler/tilde_codegen_stmt.c",
"compiler/tilde_codegen_storeload.c",
"compiler/tilde_codegen_type.c",
"compiler/tilde_codegen_value.c",
};
// Based on Zig bootstrap
const clang_libs = [_][]const u8{
"clangFrontendTool",
"clangCodeGen",
"clangFrontend",
"clangDriver",
"clangSerialization",
"clangSema",
"clangStaticAnalyzerFrontend",
"clangStaticAnalyzerCheckers",
"clangStaticAnalyzerCore",
"clangAnalysis",
"clangASTMatchers",
"clangAST",
"clangParse",
"clangSema",
"clangAPINotes",
"clangBasic",
"clangEdit",
"clangLex",
"clangARCMigrate",
"clangRewriteFrontend",
"clangRewrite",
"clangCrossTU",
"clangIndex",
"clangToolingCore",
"clangExtractAPI",
"clangSupport",
};
const lld_libs = [_][]const u8{
"lldMinGW",
"lldELF",
"lldCOFF",
"lldWasm",
"lldMachO",
"lldCommon",
};
// This list can be re-generated with `llvm-config --libfiles` and then
// reformatting using your favorite text editor. Note we do not execute
// `llvm-config` here because we are cross compiling. Also omit LLVMTableGen
// from these libs.
const llvm_libs = [_][]const u8{
"LLVMWindowsManifest",
"LLVMXRay",
"LLVMLibDriver",
"LLVMDlltoolDriver",
"LLVMTextAPIBinaryReader",
"LLVMCoverage",
"LLVMLineEditor",
"LLVMXCoreDisassembler",
"LLVMXCoreCodeGen",
"LLVMXCoreDesc",
"LLVMXCoreInfo",
"LLVMX86TargetMCA",
"LLVMX86Disassembler",
"LLVMX86AsmParser",
"LLVMX86CodeGen",
"LLVMX86Desc",
"LLVMX86Info",
"LLVMWebAssemblyDisassembler",
"LLVMWebAssemblyAsmParser",
"LLVMWebAssemblyCodeGen",
"LLVMWebAssemblyUtils",
"LLVMWebAssemblyDesc",
"LLVMWebAssemblyInfo",
"LLVMVEDisassembler",
"LLVMVEAsmParser",
"LLVMVECodeGen",
"LLVMVEDesc",
"LLVMVEInfo",
"LLVMSystemZDisassembler",
"LLVMSystemZAsmParser",
"LLVMSystemZCodeGen",
"LLVMSystemZDesc",
"LLVMSystemZInfo",
"LLVMSparcDisassembler",
"LLVMSparcAsmParser",
"LLVMSparcCodeGen",
"LLVMSparcDesc",
"LLVMSparcInfo",
"LLVMRISCVTargetMCA",
"LLVMRISCVDisassembler",
"LLVMRISCVAsmParser",
"LLVMRISCVCodeGen",
"LLVMRISCVDesc",
"LLVMRISCVInfo",
"LLVMPowerPCDisassembler",
"LLVMPowerPCAsmParser",
"LLVMPowerPCCodeGen",
"LLVMPowerPCDesc",
"LLVMPowerPCInfo",
"LLVMNVPTXCodeGen",
"LLVMNVPTXDesc",
"LLVMNVPTXInfo",
"LLVMMSP430Disassembler",
"LLVMMSP430AsmParser",
"LLVMMSP430CodeGen",
"LLVMMSP430Desc",
"LLVMMSP430Info",
"LLVMMipsDisassembler",
"LLVMMipsAsmParser",
"LLVMMipsCodeGen",
"LLVMMipsDesc",
"LLVMMipsInfo",
"LLVMLoongArchDisassembler",
"LLVMLoongArchAsmParser",
"LLVMLoongArchCodeGen",
"LLVMLoongArchDesc",
"LLVMLoongArchInfo",
"LLVMLanaiDisassembler",
"LLVMLanaiCodeGen",
"LLVMLanaiAsmParser",
"LLVMLanaiDesc",
"LLVMLanaiInfo",
"LLVMHexagonDisassembler",
"LLVMHexagonCodeGen",
"LLVMHexagonAsmParser",
"LLVMHexagonDesc",
"LLVMHexagonInfo",
"LLVMBPFDisassembler",
"LLVMBPFAsmParser",
"LLVMBPFCodeGen",
"LLVMBPFDesc",
"LLVMBPFInfo",
"LLVMAVRDisassembler",
"LLVMAVRAsmParser",
"LLVMAVRCodeGen",
"LLVMAVRDesc",
"LLVMAVRInfo",
"LLVMARMDisassembler",
"LLVMARMAsmParser",
"LLVMARMCodeGen",
"LLVMARMDesc",
"LLVMARMUtils",
"LLVMARMInfo",
"LLVMAMDGPUTargetMCA",
"LLVMAMDGPUDisassembler",
"LLVMAMDGPUAsmParser",
"LLVMAMDGPUCodeGen",
"LLVMAMDGPUDesc",
"LLVMAMDGPUUtils",
"LLVMAMDGPUInfo",
"LLVMAArch64Disassembler",
"LLVMAArch64AsmParser",
"LLVMAArch64CodeGen",
"LLVMAArch64Desc",
"LLVMAArch64Utils",
"LLVMAArch64Info",
"LLVMOrcDebugging",
"LLVMOrcJIT",
"LLVMWindowsDriver",
"LLVMMCJIT",
"LLVMJITLink",
"LLVMInterpreter",
"LLVMExecutionEngine",
"LLVMRuntimeDyld",
"LLVMOrcTargetProcess",
"LLVMOrcShared",
"LLVMDWP",
"LLVMDebugInfoLogicalView",
"LLVMDebugInfoGSYM",
"LLVMOption",
"LLVMObjectYAML",
"LLVMObjCopy",
"LLVMMCA",
"LLVMMCDisassembler",
"LLVMLTO",
"LLVMPasses",
"LLVMHipStdPar",
"LLVMCFGuard",
"LLVMCoroutines",
"LLVMipo",
"LLVMVectorize",
"LLVMLinker",
"LLVMInstrumentation",
"LLVMFrontendOpenMP",
"LLVMFrontendOffloading",
"LLVMFrontendOpenACC",
"LLVMFrontendHLSL",
"LLVMFrontendDriver",
"LLVMExtensions",
"LLVMDWARFLinkerParallel",
"LLVMDWARFLinkerClassic",
"LLVMDWARFLinker",
"LLVMGlobalISel",
"LLVMMIRParser",
"LLVMAsmPrinter",
"LLVMSelectionDAG",
"LLVMCodeGen",
"LLVMTarget",
"LLVMObjCARCOpts",
"LLVMCodeGenTypes",
"LLVMIRPrinter",
"LLVMInterfaceStub",
"LLVMFileCheck",
"LLVMFuzzMutate",
"LLVMScalarOpts",
"LLVMInstCombine",
"LLVMAggressiveInstCombine",
"LLVMTransformUtils",
"LLVMBitWriter",
"LLVMAnalysis",
"LLVMProfileData",
"LLVMSymbolize",
"LLVMDebugInfoBTF",
"LLVMDebugInfoPDB",
"LLVMDebugInfoMSF",
"LLVMDebugInfoDWARF",
"LLVMObject",
"LLVMTextAPI",
"LLVMMCParser",
"LLVMIRReader",
"LLVMAsmParser",
"LLVMMC",
"LLVMDebugInfoCodeView",
"LLVMBitReader",
"LLVMFuzzerCLI",
"LLVMCore",
"LLVMRemarks",
"LLVMBitstreamReader",
"LLVMBinaryFormat",
"LLVMTargetParser",
"LLVMSupport",
"LLVMDemangle",
"LLVMXtensaAsmParser",
"LLVMXtensaDesc",
"LLVMXtensaInfo",
"LLVMXtensaCodeGen",
"LLVMXtensaDisassembler",
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment