about summary refs log tree commit diff
path: root/boot/build.c
blob: 1a1cb2129755ec621e4b9e665c76316afe74c86b (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * a simple routine to call out to a proper
 * compiler with our c source artifacts produced
 * by our bootstrapping transpiler to turn them
 * into real executable code.
 * currently we call out directly to the llvm-based
 * `clang` tool found in the system path to do
 * the real heavy lifting for us. :)
 * the catskill compiler does have an eventual goal
 * to achieve full self-hosting with real assembler
 * and linker implementations, however that is not
 * of interest to our bootstrap phase.
 *
 * Copyright (c) 2026, Mel G. <mel@rnrd.eu>
 *
 * SPDX-License-Identifier: MPL-2.0
 */

#pragma once

#include "catboot.h"

#ifndef MUSL_LIB
#error "MUSL_LIB not defined"
#endif

#ifndef MUSL_DEV
#error "MUSL_DEV not defined"
#endif

bool
create_build_directory(struct String* build_path)
{
    // we can't directly call mkdtemp to create nested directories,
    // so we create the parent /tmp/catskill/ first.
    ascii build_dir_template[] = "/tmp/catskill/build_XXXXXX";

    struct stat st;
    if (stat("/tmp/catskill", &st) == -1) {
        if (mkdir("/tmp/catskill", 0700) == -1) {
            log_error("failed to create /tmp/catskill/ directory\n");
            return false;
        }
    }

    ascii* build_dir_path = mkdtemp(build_dir_template);
    if (!build_dir_path) { return false; }

    *build_path = string_from_c_string(build_dir_path);
    return true;
}

// TODO: clean up build directory after!

struct Compiler_Command_Result
{
    integer exit_code;
    struct String log;
};

struct Compiler_Command_Result
run_compiler_command(const ascii* command)
{
    log_debug("running backend command: %s\n", command);

    FILE* command_pipe = popen(command, "r");
    if (!command_pipe) return (struct Compiler_Command_Result){ .exit_code = -1 };

    uint bytes_read;
    ascii log_buffer[8192] = { 0 };

    while ((bytes_read = fread(log_buffer, 1, ARRAY_SIZE(log_buffer), command_pipe)) > 0) {
        if (bytes_read >= ARRAY_SIZE(log_buffer)) {
            log_error(
                "compiler output surpassed maximum output length... truncating to %lu bytes\n",
                ARRAY_SIZE(log_buffer) - 1);
            break;
        }
    }

    integer exit_code = pclose(command_pipe);
    struct String log = string_from_c_string(log_buffer);

    return (struct Compiler_Command_Result){ .exit_code = exit_code, .log = log };
}

// TODO: move this over to common library
bool
write_build_file(struct String source, struct String input_path)
{
    FILE* input_file = fopen(string_c_str(input_path), "w");
    if (!input_file) { return false; }

    fwrite(source.data, 1, source.length, input_file);
    fclose(input_file);
    return true;
}

bool
copy_runtime_library(struct String build_path)
{
    const ascii core[] = CATSKILL_EMBED("./boot/runtime/core.c");
    const ascii runtime[] = CATSKILL_EMBED("./boot/runtime/runtime.c");
    const ascii stubs[] = CATSKILL_EMBED("./boot/runtime/stubs.c");

    struct String build_runtime_path = string_append_c_str(build_path, "/runtime/");
    if (mkdir(string_c_str(build_runtime_path), 0700) == -1) {
        log_error("failed to create build runtime directory\n");
        return false;
    }

    struct String
        core_path = string_append_c_str(build_runtime_path, "core.c"),
        runtime_path = string_append_c_str(build_runtime_path, "runtime.c"),
        stubs_path = string_append_c_str(build_runtime_path, "stubs.c");

    struct String
        core_source = string_from_c_string(core),
        runtime_source = string_from_c_string(runtime), stubs_source = string_from_c_string(stubs);

    if (!write_build_file(core_source, core_path)) return false;
    if (!write_build_file(runtime_source, runtime_path)) return false;
    if (!write_build_file(stubs_source, stubs_path)) return false;

    return true;
}

bool
copy_file(struct String source_path, struct String dest_path)
{
    FILE* source_file = fopen(string_c_str(source_path), "rb");
    if (!source_file) {
        log_error("failed to open source file for copying: %s\n", source_path);
        return false;
    }

    // create destination file with the right permissions first
    if (!creat(string_c_str(dest_path), 0700)) {
        log_error("failed to create destination file for copying: %s\n", dest_path);
        fclose(source_file);
        return false;
    }
    FILE* dest_file = fopen(string_c_str(dest_path), "wb");
    if (!dest_file) {
        log_error("failed to open destination file for copying: %s\n", dest_path);
        fclose(source_file);
        return false;
    }

    ascii buffer[4096];
    size_t bytes;
    while ((bytes = fread(buffer, 1, sizeof(buffer), source_file)) > 0) {
        fwrite(buffer, 1, bytes, dest_file);
    }
    fclose(source_file);
    fclose(dest_file);
    return true;
}

struct Build_Result
{
    bool success;

    integer compiler_exit_code;
    struct String compiler_log;
    struct String output_path;
};

struct Build_Result
compile_using_backend(
    struct String build_path, struct String source_path, struct String output_path)
{
    log_debug("current backend: LLVM Clang (CLI)\n");

    // TODO: right now we always run the compiler from this source directory,
    // so we can refer to the runtime libraries by a relative path "./boot/runtime/",
    // but in the future we should embed these files into this compiler, and write
    // them out into the build directory.

    // probably too many flags for our purposes, but we kind of want
    // this simple bootstrapping output to be the eventual target
    // of our self-hosted compiler stack, so we avoid anything fancy
    // for an easier target to hit.
    struct String runtime_path = string_append_c_str(build_path, "/runtime/");
    struct String stubs_path = string_append_c_str(runtime_path, "stubs.c");
    const ascii* arguments[] = {
        "clang",

        // high-level compiler behavior
        "-O0",
        "-g",
        "-std=c99",

        // low-level compiler settings, which we optimize to generate
        // extremely simple and human-readable assembly in our final
        // executable. we want it to feel almost hand-written.
        "-fno-omit-frame-pointer",
        "-fno-stack-protector",
        "-fno-plt",
        "-fno-builtin",
        "-fno-inline",
        "-fno-common",
        "-fno-ident",
        "-fno-exceptions",
        "-fno-asynchronous-unwind-tables", // we rely on frame pointer instead

        // output options, for integrating clang output into ours
        "-fno-color-diagnostics",
        "-Wall",
        "-Wextra", // with -w flag or on error

        // linker and header options, static compilation w/ musl
        "-static",
        "-nostdlib",
        "-I",
        string_c_str(runtime_path),
        "-isystem",
        MUSL_DEV "/include",
        // linking must follow this exact order
        MUSL_LIB "/lib/crt1.o",
        MUSL_LIB "/lib/crti.o",
        string_c_str(stubs_path), // stub out some software float implementations
        string_c_str(source_path),
        MUSL_LIB "/lib/libc.a",
        MUSL_LIB "/lib/crtn.o",

        "-o",
        string_c_str(output_path),

        "2>&1", // errors are output to stderr, we want to capture them
    };

    // append all flags to single command
    ascii command[1024] = { 0 };
    for (uint i = 0; i < ARRAY_SIZE(arguments); ++i) {
        strcat(command, arguments[i]);
        strcat(command, " ");
    }

    struct Compiler_Command_Result result = run_compiler_command(command);

    return (struct Build_Result){
        .success = result.exit_code == 0,
        .compiler_exit_code = result.exit_code,
        .compiler_log = result.log,
        .output_path = output_path,
    };
}

struct Build_Result
build_executable(struct String source, struct String output_path)
{
    struct String build_path;
    if (!create_build_directory(&build_path)) {
        log_error("failed to create build directory\n");
        return (struct Build_Result){};
    }
    log_debug("temporary build directory for compilation: %s\n", build_path);

    // temporary input and output paths
    struct String build_input = string_append_c_str(build_path, "/input.c");
    struct String build_output = string_append_c_str(build_path, "/output");

    if (!write_build_file(source, build_input)) {
        log_error("failed to create input source file\n");
        return (struct Build_Result){};
    }

    // the runtime library files need to be present in the build directory
    // for inclusion within the generated c source.
    if (!copy_runtime_library(build_path)) {
        log_error("failed to copy runtime library files\n");
        return (struct Build_Result){};
    }

    struct Build_Result result = compile_using_backend(build_path, build_input, build_output);
    if (!result.success) { return result; }

    // copy the output executable to the desired location
    if (!copy_file(build_output, output_path)) {
        log_error("failed to copy final executable to %s\n", output_path);
        return (struct Build_Result){};
    }

    result.output_path = output_path;
    return result;
}