Skip to content

Commit

Permalink
remove the zstd mechanism from the build process
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Dec 10, 2022
1 parent 9d93b2c commit c51288f
Show file tree
Hide file tree
Showing 36 changed files with 14 additions and 20,644 deletions.
22 changes: 4 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,6 @@ set(ZIG_CONFIG_ZIG_OUT "${CMAKE_BINARY_DIR}/config.zig")

set(ZIG_WASM2C_SOURCES
"${CMAKE_SOURCE_DIR}/stage1/wasm2c.c"
"${CMAKE_SOURCE_DIR}/stage1/zstd/lib/decompress/huf_decompress.c"
"${CMAKE_SOURCE_DIR}/stage1/zstd/lib/decompress/zstd_ddict.c"
"${CMAKE_SOURCE_DIR}/stage1/zstd/lib/decompress/zstd_decompress.c"
"${CMAKE_SOURCE_DIR}/stage1/zstd/lib/decompress/zstd_decompress_block.c"
"${CMAKE_SOURCE_DIR}/stage1/zstd/lib/common/entropy_common.c"
"${CMAKE_SOURCE_DIR}/stage1/zstd/lib/common/error_private.c"
"${CMAKE_SOURCE_DIR}/stage1/zstd/lib/common/fse_decompress.c"
"${CMAKE_SOURCE_DIR}/stage1/zstd/lib/common/pool.c"
"${CMAKE_SOURCE_DIR}/stage1/zstd/lib/common/xxhash.c"
"${CMAKE_SOURCE_DIR}/stage1/zstd/lib/common/zstd_common.c"
)
set(ZIG_CPP_SOURCES
# These are planned to stay even when we are self-hosted.
Expand Down Expand Up @@ -738,21 +728,19 @@ else()
endif()
endif()

set(ZIG1_WASM_ZST_SOURCE "${CMAKE_SOURCE_DIR}/stage1/zig1.wasm.zst")
set(ZIG1_WASM_MODULE "${CMAKE_SOURCE_DIR}/stage1/zig1.wasm")
set(ZIG1_C_SOURCE "${CMAKE_BINARY_DIR}/zig1.c")
set(ZIG2_C_SOURCE "${CMAKE_BINARY_DIR}/zig2.c")
set(ZIG_COMPILER_RT_C_SOURCE "${CMAKE_BINARY_DIR}/compiler_rt.c")

add_executable(zig-wasm2c ${ZIG_WASM2C_SOURCES})
set_target_properties(zig-wasm2c PROPERTIES COMPILE_FLAGS "${ZIG_WASM2C_COMPILE_FLAGS}")
target_include_directories(zig-wasm2c PUBLIC "${CMAKE_SOURCE_DIR}/stage1/zstd/lib")
target_compile_definitions(zig-wasm2c PRIVATE ZSTD_DISABLE_ASM)

add_custom_command(
OUTPUT "${ZIG1_C_SOURCE}"
COMMAND zig-wasm2c "${ZIG1_WASM_ZST_SOURCE}" "${ZIG1_C_SOURCE}"
DEPENDS zig-wasm2c "${ZIG1_WASM_ZST_SOURCE}"
COMMENT STATUS "Converting ${ZIG1_WASM_ZST_SOURCE} to ${ZIG1_C_SOURCE}"
COMMAND zig-wasm2c "${ZIG1_WASM_MODULE}" "${ZIG1_C_SOURCE}"
DEPENDS zig-wasm2c "${ZIG1_WASM_MODULE}"
COMMENT STATUS "Converting ${ZIG1_WASM_MODULE} to ${ZIG1_C_SOURCE}"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
)

Expand All @@ -764,8 +752,6 @@ if(MSVC)
else()
target_link_libraries(zig1 LINK_PUBLIC m)
endif()
target_include_directories(zig1 PUBLIC "${CMAKE_SOURCE_DIR}/stage1/zstd/lib")
target_compile_definitions(zig1 PRIVATE ZSTD_DISABLE_ASM)

set(BUILD_ZIG2_ARGS
"${CMAKE_SOURCE_DIR}/lib"
Expand Down
13 changes: 2 additions & 11 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -502,17 +502,8 @@ fn addWasiUpdateStep(b: *Builder, version: [:0]const u8) !void {
run_opt.addArg("-o");
run_opt.addFileSourceArg(.{ .path = "stage1/zig1.wasm" });

const run_zstd = b.addSystemCommand(&.{ "zstd", "-19", "-f" });
run_zstd.step.dependOn(&run_opt.step);
run_zstd.addFileSourceArg(.{ .path = "stage1/zig1.wasm" });
run_zstd.addArg("-o");
run_zstd.addFileSourceArg(.{ .path = "stage1/zig1.wasm.zst" });

const cleanup = b.addRemoveDirTree("stage1/zig1.wasm");
cleanup.step.dependOn(&run_zstd.step);

const update_zig1_step = b.step("update-zig1", "Update stage1/zig1.wasm.zst");
update_zig1_step.dependOn(&cleanup.step);
const update_zig1_step = b.step("update-zig1", "Update stage1/zig1.wasm");
update_zig1_step.dependOn(&run_opt.step);
}

fn addCompilerStep(b: *Builder) *std.build.LibExeObjStep {
Expand Down
61 changes: 8 additions & 53 deletions stage1/InputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,35 @@
#include "panic.h"
#include "wasm.h"

#include <zstd.h>

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

struct InputStream {
FILE *stream;
ZSTD_DStream *ds;
ZSTD_outBuffer out;
ZSTD_inBuffer in;
size_t pos;
};

static void InputStream_open(struct InputStream *self, const char *path) {
self->stream = fopen(path, "rb");
if (self->stream == NULL) panic("unable to open input file");
self->ds = ZSTD_createDStream();
if (self->ds == NULL) panic("unable to create zstd context");
size_t in_size = ZSTD_initDStream(self->ds);
if (ZSTD_isError(in_size)) panic(ZSTD_getErrorName(in_size));
self->out.size = ZSTD_DStreamOutSize();
self->out.dst = malloc(self->out.size + ZSTD_DStreamInSize());
if (self->out.dst == NULL) panic("unable to allocate input buffers");
self->out.pos = 0;
self->in.src = (const char *)self->out.dst + self->out.size;
self->in.size = fread((void *)self->in.src, 1, in_size, self->stream);
self->in.pos = 0;
self->pos = 0;
}

static void InputStream_close(struct InputStream *self) {
free(self->out.dst);
ZSTD_freeDStream(self->ds);
fclose(self->stream);
}

static bool InputStream_atEnd(struct InputStream *self) {
while (self->pos >= self->out.pos) {
self->out.pos = 0;
self->pos = 0;
size_t in_size = ZSTD_decompressStream(self->ds, &self->out, &self->in);
if (ZSTD_isError(in_size)) panic(ZSTD_getErrorName(in_size));
if (self->in.pos >= self->in.size) {
size_t max_in_size = ZSTD_DStreamInSize();
if (in_size > max_in_size) in_size = max_in_size;
self->in.size = fread((void *)self->in.src, 1, in_size, self->stream);
self->in.pos = 0;
if (self->in.pos >= self->in.size) return true;
}
}
return false;
return feof(self->stream) != 0;
}

static uint8_t InputStream_readByte(struct InputStream *self) {
if (InputStream_atEnd(self)) panic("unexpected end of input stream");
uint8_t value = ((uint8_t *)self->out.dst)[self->pos];
self->pos += 1;
int value;
value = fgetc(self->stream);
if (value == EOF) panic("unexpected end of input stream");
return value;
}

Expand Down Expand Up @@ -168,26 +136,13 @@ static char *InputStream_readName(struct InputStream *self) {
uint32_t len = InputStream_readLeb128_u32(self);
char *name = malloc(len + 1);
if (name == NULL) panic("out of memory");
for (uint32_t i = 0; i < len; ) {
if (InputStream_atEnd(self)) panic("unexpected end of input stream");
size_t remaining = self->out.pos - self->pos;
if (remaining > len - i) remaining = len - i;
memcpy(&name[i], &((char *)self->out.dst)[self->pos], remaining);
i += remaining;
self->pos += remaining;
}
name[len] = '\0';
if (fread(name, 1, len, self->stream) != len) panic("unexpected end of input stream");
name[len] = 0;
return name;
}

static void InputStream_skipBytes(struct InputStream *self, size_t len) {
for (size_t i = 0; i < len; ) {
if (InputStream_atEnd(self)) panic("unexpected end of input stream");
size_t remaining = self->out.pos - self->pos;
if (remaining > len - i) remaining = len - i;
i += remaining;
self->pos += remaining;
}
if (fseek(self->stream, len, SEEK_CUR) == -1) panic("unexpected end of input stream");
}

static uint32_t InputStream_skipToSection(struct InputStream *self, uint8_t expected_id) {
Expand Down
30 changes: 0 additions & 30 deletions stage1/zstd/LICENSE

This file was deleted.

Loading

0 comments on commit c51288f

Please sign in to comment.