Added build.zig to cross compile

Warning: currently you need to edit ConfigHeader.zig in Zig's std
library, and comment "try output.appendSlice(c_generated_line);"
This commit is contained in:
g012 2024-03-12 15:59:12 +01:00
parent 0a65af8621
commit 29f5666b20
8 changed files with 183 additions and 62 deletions

17
.gitignore vendored
View File

@ -1,8 +1,9 @@
/build
/tmp
*.bin
*.nes
*.nes.deb
*.sym
*.mlb
*.nl
/build
/tmp
/zig-*
*.bin
*.nes
*.nes.deb
*.sym
*.mlb
*.nl

View File

@ -8,8 +8,8 @@ set(L65_SOURCE_DIR ${PROJECT_SOURCE_DIR})
set(L65_BINARY_DIR ${PROJECT_BINARY_DIR})
set(L65_VERSION_MAJOR "1")
set(L65_VERSION_MINOR "2")
set(L65_VERSION_REVISION "2")
set(L65_VERSION_MINOR "3")
set(L65_VERSION_REVISION "0")
set(L65_VERSION "${L65_VERSION_MAJOR}.${L65_VERSION_MINOR}.${L65_VERSION_REVISION}")
configure_file("${L65_SOURCE_DIR}/l65cfg.lua.in" "${L65_BINARY_DIR}/l65cfg.lua")

View File

@ -1,7 +1,7 @@
# l65
[![Build Status](https://travis-ci.org/g012/l65.svg?branch=master)](https://travis-ci.org/g012/l65)
l65 is a 6502 assembler, operating from within Lua and written in Lua. This means assembler mnemonics become regular Lua statements anywhere in the middle of Lua code.
l7801 is a µPD7801 assembler, using l65 framework, created and maintained by [@MooZ] (https://github.com/BlockoS).
Table of Contents
=================

120
build.zig Normal file
View File

@ -0,0 +1,120 @@
const std = @import("std");
const major: u32 = 1;
const minor: u32 = 3;
const revision: u32 = 0;
const targets: []const std.Target.Query = &.{
.{ .os_tag = .windows, .cpu_arch = .x86_64, .cpu_model = .baseline },
.{ .os_tag = .windows, .cpu_arch = .aarch64, .cpu_model = .baseline },
.{ .os_tag = .macos, .cpu_arch = .x86_64, .cpu_model = .baseline, .os_version_min = std.zig.CrossTarget.OsVersion{ .semver = .{ .major = 10, .minor = 7, .patch = 0 } } },
.{ .os_tag = .macos, .cpu_arch = .aarch64, .cpu_model = .baseline, .os_version_min = std.zig.CrossTarget.OsVersion{ .semver = .{ .major = 11, .minor = 0, .patch = 0 } } },
.{ .os_tag = .linux, .cpu_arch = .x86_64, .cpu_model = .baseline, .abi = .musl },
.{ .os_tag = .linux, .cpu_arch = .aarch64, .cpu_model = .baseline },
};
fn setupExe(exe: *std.Build.Step.Compile, embed_output: *const std.Build.LazyPath) !void {
exe.addCSourceFiles(.{
.files = &.{ "lfs.c", "lpeg.c", "main.c" },
.flags = &.{},
});
exe.addIncludePath(embed_output.dirname());
exe.linkLibC();
}
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
///////////////////////////////////////////////////////////////////////////
// Embed tool
const version_storage = struct { var str: [32]u8 = undefined; };
const version = try std.fmt.bufPrint(version_storage.str[0..], "{d}.{d}.{d}", .{ major, minor, revision });
const l65cfg_lua_in = b.addConfigHeader(
.{
.style = .{ .cmake = .{ .path = "l65cfg.lua.in" } },
.include_path = "l65cfg.lua",
},
.{
.L65_VERSION_MAJOR = major,
.L65_VERSION_MINOR = minor,
.L65_VERSION_REVISION = revision,
.L65_VERSION = version,
},
);
// FIXME current version (0.12-dev) of ConfigHeader adds a C comment on top of file
// - comment "try output.appendSlice(c_generated_line);" in ConfigHeader.zig to avoid it
const embed_exe = b.addExecutable(.{
.name = "embed",
.target = target,
.optimize = optimize,
});
embed_exe.addCSourceFile(.{ .file = .{ .path = "embed.c" }, .flags = &.{ "-std=c99" } });
embed_exe.linkLibC();
const embed = b.addRunArtifact(embed_exe);
embed.step.dependOn(&l65cfg_lua_in.step);
embed.addArg("-o");
const embed_output = embed.addOutputFileArg("scripts.h");
embed.addFileArg(l65cfg_lua_in.getOutput());
embed.addFileArg(.{ .path = "asm.lua" });
embed.addFileArg(.{ .path = "6502.lua" });
embed.addFileArg(.{ .path = "dkjson.lua" });
embed.addFileArg(.{ .path = "l65.lua" });
embed.addFileArg(.{ .path = "re.lua" });
embed.addFileArg(.{ .path = "nes.l65" });
embed.addFileArg(.{ .path = "pce.l65" });
embed.addFileArg(.{ .path = "vcs.l65" });
///////////////////////////////////////////////////////////////////////////
// Build for current machine
var exe = b.addExecutable(.{
.name = "l65",
.target = target,
.optimize = optimize,
});
exe.step.dependOn(&embed.step);
try setupExe(exe, &embed_output);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run l65");
run_step.dependOn(&run_cmd.step);
///////////////////////////////////////////////////////////////////////////
// Release binaries
const release_step = b.step("release", "Generate a release");
for (targets) |t| {
const rel_exe = b.addExecutable(.{
.name = "l65",
.target = b.resolveTargetQuery(t),
.optimize = .ReleaseSmall,
});
const target_output = b.addInstallArtifact(rel_exe, .{
.dest_dir = .{
.override = .{
.custom = try t.zigTriple(b.allocator),
},
},
});
rel_exe.step.dependOn(&embed.step);
try setupExe(rel_exe, &embed_output);
release_step.dependOn(&target_output.step);
}
///////////////////////////////////////////////////////////////////////////
// Build samples as test
// TODO
}

68
embed.c
View File

@ -82,40 +82,40 @@ static int writer(lua_State* L, const void* p, size_t size, void* f)
// use custom version so that filename is not used for debug info
LUALIB_API int luaL_loadfilex2 (lua_State *L, const char *filename,
const char *mode, const char *chunkname) {
luai_LoadF lf;
int status, readstatus;
int c;
int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
if (filename == NULL) {
lua_pushliteral(L, "=stdin");
lf.f = stdin;
}
else {
lua_pushfstring(L, "@%s", filename);
lf.f = fopen(filename, "r");
if (lf.f == NULL) return luai_errfile(L, "open", fnameindex);
}
if (luai_skipcomment(&lf, &c)) /* read initial portion */
lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
if (lf.f == NULL) return luai_errfile(L, "reopen", fnameindex);
luai_skipcomment(&lf, &c); /* re-read initial portion */
}
if (c != EOF)
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
lua_pop(L, 1);
lua_pushstring(L, chunkname);
status = lua_load(L, luai_getF, &lf, lua_tostring(L, -1), mode);
readstatus = ferror(lf.f);
if (filename) fclose(lf.f); /* close file (even in case of errors) */
if (readstatus) {
lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
return luai_errfile(L, "read", fnameindex);
}
lua_remove(L, fnameindex);
return status;
const char *mode, const char *chunkname) {
luai_LoadF lf;
int status, readstatus;
int c;
int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
if (filename == NULL) {
lua_pushliteral(L, "=stdin");
lf.f = stdin;
}
else {
lua_pushfstring(L, "@%s", filename);
lf.f = fopen(filename, "r");
if (lf.f == NULL) return luai_errfile(L, "open", fnameindex);
}
if (luai_skipcomment(&lf, &c)) /* read initial portion */
lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
if (lf.f == NULL) return luai_errfile(L, "reopen", fnameindex);
luai_skipcomment(&lf, &c); /* re-read initial portion */
}
if (c != EOF)
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
lua_pop(L, 1);
lua_pushstring(L, chunkname);
status = lua_load(L, luai_getF, &lf, lua_tostring(L, -1), mode);
readstatus = ferror(lf.f);
if (filename) fclose(lf.f); /* close file (even in case of errors) */
if (readstatus) {
lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
return luai_errfile(L, "read", fnameindex);
}
lua_remove(L, fnameindex);
return status;
}
static int pmain(lua_State* L)

View File

@ -1,8 +1,8 @@
local M = {}
M.major = 0
M.minor = 0
M.revision = 0
M.version = "0.0.0"
return M
local M = {}
M.major = 0
M.minor = 0
M.revision = 0
M.version = "0.0.0"
return M

View File

@ -1,8 +1,8 @@
local M = {}
M.major = ${L65_VERSION_MAJOR}
M.minor = ${L65_VERSION_MINOR}
M.revision = ${L65_VERSION_REVISION}
M.version = "${L65_VERSION}"
return M
local M = {}
M.major = ${L65_VERSION_MAJOR}
M.minor = ${L65_VERSION_MINOR}
M.revision = ${L65_VERSION_REVISION}
M.version = "${L65_VERSION}"
return M

2
lua.h
View File

@ -20280,7 +20280,7 @@ static void luai_fchecksize (luai_LoadState *S, size_t size, const char *tname)
#define luai_checksize(S,t) luai_fchecksize(S,sizeof(t),#t)
static void luai_checkHeader (luai_LoadState *S) {
luai_checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
luai_checkliteral(S, (const char*)LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
if (luai_LoadByte(S) != LUAC_VERSION)
luai_error(S, "version mismatch in");
if (luai_LoadByte(S) != LUAC_FORMAT)