Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/userland/apps/mqjs/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const std = @import("std");
const AshetOS = @import("AshetOS");

pub fn build(b: *std.Build) void {
const target = AshetOS.standardTargetOption(b);
const optimize = b.standardOptimizeOption(.{});

const sdk = AshetOS.init(b, "AshetOS", .{ .target = target });

const mqjs = b.dependency("mqjs", .{
.target = target.resolve_target(b),
.optimize = .ReleaseSafe,
});

const mqjs_mod = mqjs.module("mqjs");

const app = sdk.addApp(.{
.name = "hello-world",
.target = target,
.optimize = optimize,
.root_source_file = b.path("hello-world.zig"),
.code_model = .default, // C code fails with clang error ".small not found"
.link_libc = true,
});

app.exe.root_module.addImport("mqjs", mqjs_mod);

sdk.installApp(app, .{});
}
14 changes: 14 additions & 0 deletions src/userland/apps/mqjs/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.{
.name = .mqjs,
.fingerprint = 0xeb25510e1a54d4fa,
.version = "0.1.0",
.paths = .{ "build.zig", "build.zig.zon", "src" },
.dependencies = .{
.AshetOS = .{
.path = "../../libs/libAshetOS",
},
.mqjs = .{
.path = "../../../../vendor/mqjs",
},
},
}
8 changes: 8 additions & 0 deletions src/userland/apps/mqjs/hello-world.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const std = @import("std");
const ashet = @import("ashet");

pub usingnamespace ashet.core;

pub fn main() !void {
try ashet.process.debug.log_writer(.notice).writeAll("Hello, World!\r\n");
}
42 changes: 40 additions & 2 deletions src/userland/libs/libAshetOS/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub fn init(b: *std.Build, dependency_name: []const u8, args: struct {
.owning_builder = b,
.dependency = dep,

.libc_file = dep.namedLazyPath("libc.txt"),
.ashex_tool_exe = dep.artifact("ashet-exe"),
.ashet_module = dep.module("ashet"),
.linker_script = dep.path("application.ld"),
Expand All @@ -72,6 +73,8 @@ pub const AshetSdk = struct {
ashex_tool_exe: *std.Build.Step.Compile,
mkicon_exe: *std.Build.Step.Compile,

libc_file: std.Build.LazyPath,

syscall_library: std.Build.LazyPath,
ashet_module: *std.Build.Module,
linker_script: std.Build.LazyPath,
Expand Down Expand Up @@ -120,6 +123,8 @@ pub const AshetSdk = struct {
.win32_manifest = null,
});

exe.setLibCFile(sdk.libc_file);

// if (zig_target.result.cpu.arch.isThumb()) {
// // Disable LTO on arm as it fails hard
// exe.want_lto = false;
Expand Down Expand Up @@ -254,7 +259,6 @@ pub const ExecutableOptions = struct {
};

pub fn build(b: *std.Build) void {

// Targets:
const debug_step = b.step("debug", "Installs a debug executable for disassembly");

Expand Down Expand Up @@ -288,6 +292,15 @@ pub fn build(b: *std.Build) void {

// Build:

const genfile_exe = b.addExecutable(.{
.name = "genfile",
.root_module = b.createModule(.{
.root_source_file = b.path("src/genfile.zig"),
.target = b.graph.host,
.optimize = .Debug,
}),
});

const gen_binding_exe = b.addExecutable(.{
.name = "gen_abi_binding",
.target = b.graph.host,
Expand All @@ -312,8 +325,33 @@ pub fn build(b: *std.Build) void {

const target = ashet_target.resolve_target(b);

const sub_build = b.addSystemCommand(&.{ b.graph.zig_exe, "build-lib" });
// Libraries
const libc = b.addStaticLibrary(.{
.name = "c",
.root_module = b.createModule(.{
.target = target,
.optimize = .ReleaseSafe,
.root_source_file = b.path("libc/src/libc.zig"),
.single_threaded = true,
}),
});
libc.root_module.addIncludePath(b.path("libc/include/"));
libc.root_module.addIncludePath(b.path("libc/sys_include/"));
b.installArtifact(libc);

const gen_libc_file = b.addRunArtifact(genfile_exe);
gen_libc_file.addPrefixedDirectoryArg("include_dir=", b.path("libc/include/"));
gen_libc_file.addPrefixedDirectoryArg("sys_include_dir=", b.path("libc/sys_include/"));
gen_libc_file.addPrefixedDirectoryArg("crt_dir=", b.path("libc/crt0")); // TODO: The directory that contains `crt1.o` or `crt2.o`.
gen_libc_file.addArg("msvc_lib_dir="); // Not required
gen_libc_file.addArg("kernel32_lib_dir="); // Not required
gen_libc_file.addArg("gcc_dir="); // Not required

const libc_txt_path = gen_libc_file.captureStdOut();

b.addNamedLazyPath("libc.txt", libc_txt_path);

const sub_build = b.addSystemCommand(&.{ b.graph.zig_exe, "build-lib" });
sub_build.addArg("-static");
sub_build.addArg("-fPIC");
sub_build.addArg("-O");
Expand Down
4 changes: 4 additions & 0 deletions src/userland/libs/libAshetOS/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
.libashet = .{
.path = "module",
},
.libc = .{
.url = "https://github.com/ZigEmbeddedGroup/microzig/releases/download/0.14.4/foundationlibc.tar.gz",
.hash = "foundationlibc-0.0.0-LAEuPNWTAADpACdmCKeEZZocc6PNjE99VjX97X85tghY",
},
},
}
26 changes: 26 additions & 0 deletions src/userland/libs/libAshetOS/libc/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
Language: Cpp
BasedOnStyle: LLVM

# basic indentation is 4 spaces, indented by spaces:
IndentWidth: 4
TabWidth: 4
UseTab: Never

# use east const for better readability:
QualifierAlignment: Right

# do not tack pointer to type nor value:
PointerAlignment: Middle

# align function names and keep them apart at least one line
AlignConsecutiveDeclarations: AcrossEmptyLines

# we sort includes to prevent merge conflicts:
SortIncludes: CaseSensitive

# disable column limit:
ColumnLimit: 0

---

48 changes: 48 additions & 0 deletions src/userland/libs/libAshetOS/libc/include/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef _FOUNDATION_LIBC_ASSERT_H_
#define _FOUNDATION_LIBC_ASSERT_H_

#include "foundation/builtins.h"

extern FOUNDATION_LIBC_NORETURN void foundation_libc_assert(char const * assertion, char const * file, unsigned line);

#if FOUNDATION_LIBC_ASSERT == FOUNDATION_LIBC_ASSERT_DEFAULT

#define assert(expr) \
do { \
if ((expr) == 0) { \
foundation_libc_assert(#expr, __FILE__, __LINE__); \
} \
} while (0)

#elif FOUNDATION_LIBC_ASSERT == FOUNDATION_LIBC_ASSERT_NOFILE

#define assert(expr) \
do { \
if ((expr) == 0) { \
foundation_libc_assert(#expr, "", __LINE__); \
} \
} while (0)

#elif FOUNDATION_LIBC_ASSERT == FOUNDATION_LIBC_ASSERT_NOMSG

#define assert(expr) \
do { \
if ((expr) == 0) { \
foundation_libc_assert("", "", __LINE__); \
} \
} while (0)

#elif FOUNDATION_LIBC_ASSERT == FOUNDATION_LIBC_ASSERT_EXPECTED

#define assert(expr) FOUNDATION_LIBC_EXPECT(expr)

#else
#error "bad definition of FOUNDATION_LIBC_ASSERT_DEFAULT!"
#endif

#if !defined(__cplusplus)
#undef static_assert
#define static_assert FOUNDATION_LIBC_STATIC_ASSERT
#endif

#endif
21 changes: 21 additions & 0 deletions src/userland/libs/libAshetOS/libc/include/ctype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _FOUNDATION_LIBC_CTYPE_H_
#define _FOUNDATION_LIBC_CTYPE_H_

#define EOF (-1)

int isalnum(int c);
int isalpha(int c);
int isblank(int c);
int iscntrl(int c);
int isdigit(int c);
int isgraph(int c);
int islower(int c);
int isprint(int c);
int ispunct(int c);
int isspace(int c);
int isupper(int c);
int isxdigit(int c);
int tolower(int c);
int toupper(int c);

#endif
12 changes: 12 additions & 0 deletions src/userland/libs/libAshetOS/libc/include/errno.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _FOUNDATION_LIBC_ERRNO_H_
#define _FOUNDATION_LIBC_ERRNO_H_

#define EDOM 1
#define EILSEQ 2
#define ERANGE 3

#define errno (*__get_errno())

int * __get_errno(void);

#endif
39 changes: 39 additions & 0 deletions src/userland/libs/libAshetOS/libc/include/foundation/builtins.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef _FOUNDATION_LIBC_BUILTINS_H_
#define _FOUNDATION_LIBC_BUILTINS_H_

#include <stddef.h>

#define FOUNDATION_LIBC_ASSERT_DEFAULT 0
#define FOUNDATION_LIBC_ASSERT_NOFILE 1
#define FOUNDATION_LIBC_ASSERT_NOMSG 2
#define FOUNDATION_LIBC_ASSERT_EXPECTED 3

#ifndef FOUNDATION_LIBC_ASSERT
#define FOUNDATION_LIBC_ASSERT FOUNDATION_LIBC_ASSERT_DEFAULT
#endif

#define FOUNDATION_LIBC_STATIC_ASSERT _Static_assert

#if defined(__clang__)

#define FOUNDATION_LIBC_NORETURN __attribute__((__noreturn__))
#define FOUNDATION_LIBC_EXPECT(expr) (__builtin_expect(!(expr), 0))

#elif defined(__GNUC__) || defined(__GNUG__)

#define FOUNDATION_LIBC_NORETURN __attribute__((__noreturn__))
#define FOUNDATION_LIBC_EXPECT(expr) (__builtin_expect(!(expr), 0))

#elif defined(_MSC_VER)

#define FOUNDATION_LIBC_NORETURN __declspec(noreturn)
#define FOUNDATION_LIBC_EXPECT(expr) (__assume((expr)))

#else

#define FOUNDATION_LIBC_NORETURN
#define FOUNDATION_LIBC_EXPECT(expr)

#endif

#endif
25 changes: 25 additions & 0 deletions src/userland/libs/libAshetOS/libc/include/foundation/libc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef _FOUNDATION_LIBC_INTERNALS_H_
#define _FOUNDATION_LIBC_INTERNALS_H_

#include <stddef.h>

///
/// Panic handler for undefined, but catchable behaviour in safe modes.
///
/// This will be invoked when Zig detects undefined behaviour at runtime,
/// or when foundation libc can recognize illegal arguments.
///
/// The function receives a non-terminated pointer to the panic message
/// with `msg_len` bytes of UTF-8 encoded payload.
///
/// It has a weak default implementation shipped, so just implement this
/// function to plug in your own custom behaviour.
/// The default implementation is done by invoking a `trap` instruction to
/// emit an illegal instruction or otherwise crash the program execution.
///
/// NOTE: This function must never return, because otherwise, the undefined
/// behaviour will be actually undefined!
///
void foundation_libc_panic_handler(char const * msg_ptr, size_t msg_len);

#endif
Loading
Loading