From 2a6b92ed130ade1ea7542ee210e6957427a0aa81 Mon Sep 17 00:00:00 2001 From: dacctal <120422854+dacctal@users.noreply.github.com> Date: Sat, 14 Mar 2026 12:11:52 +0000 Subject: lua build system integration --- .clangd | 2 ++ Makefile | 5 +++++ bldit | 1 - pkgit | Bin 534504 -> 164376 bytes src/build.cc | 13 ++++++++++++ src/build_pkg.cc | 27 ++++--------------------- src/create_pkg.cc | 3 +++ src/fetch_git.cc | 23 +++++++++------------ src/fetch_pwd.cc | 5 +++++ src/fetch_src.cc | 4 +++- src/install_pkg.cc | 5 +++-- src/lua_build.cc | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cc | 4 ++-- src/vars.cc | 2 ++ 14 files changed, 109 insertions(+), 43 deletions(-) create mode 100644 .clangd create mode 100644 src/build.cc create mode 100644 src/fetch_pwd.cc create mode 100644 src/lua_build.cc diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..ffabaa3 --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-I/usr/include/luajit-2.1] diff --git a/Makefile b/Makefile index e02bad1..63d8a1a 100644 --- a/Makefile +++ b/Makefile @@ -12,3 +12,8 @@ pkgit: src/main.cc install: pkgit install -d $(DESTDIR)$(PREFIX)/bin install -m 755 pkgit $(DESTDIR)$(PREFIX)/bin/pkgit + + +luatest: src/lua_build.cc + $(CC) -o luatest src/lua_build.cc -I"./include" $(CXXFLAGS) + chmod +x luatest diff --git a/bldit b/bldit index 9e89e95..bec03bc 100755 --- a/bldit +++ b/bldit @@ -1,3 +1,2 @@ #!/usr/bin/env sh - make diff --git a/pkgit b/pkgit index 8d7ce6b..20c2e9f 100755 Binary files a/pkgit and b/pkgit differ diff --git a/src/build.cc b/src/build.cc new file mode 100644 index 0000000..0195412 --- /dev/null +++ b/src/build.cc @@ -0,0 +1,13 @@ +#include + +#include "lua_build.cc" + +void build(std::filesystem::path build_dir) { + if (build_dir != std::filesystem::current_path().string()) { + std::filesystem::current_path(build_dir); + } + for (auto const &dir_entry : std::filesystem::directory_iterator(fs::current_path().string())) { + if (dir_entry.path().filename() == "bldit") { system("./bldit"); } + else { lua_build(build_dir.c_str()); } + } +} diff --git a/src/build_pkg.cc b/src/build_pkg.cc index 67c67b8..7627e16 100644 --- a/src/build_pkg.cc +++ b/src/build_pkg.cc @@ -1,36 +1,17 @@ #include -#include "build_systems.cc" - -const std::map> builds = { - {"bldit", bldit_build}, - {"compile.sh", compilesh_build}, - {"build.sh", buildsh_build}, - {"autogen.sh", autogen_build}, - {"configure", autotools_build}, - {"configure.ac", autotools_build}, - {"Makefile", make_build}, - {"Makefile.am", make_build}, - {"CMakeLists.txt", cmake_build}, - {"meson.build", meson_build}, - {"build.ninja", ninja_build}, - {"Cargo.toml", cargo_build}, - {"go.mod", go_build}, - {"gradle.build", gradle_build}, - {"pnpm-lock.yaml", pnpm_build}, - {"pyproject.toml", python_build}, - {"build.zig", zig_build} -}; +#include "build_map.cc" void build_pkg(std::filesystem::path build_dir) { if (build_dir != fs::current_path().string()) { fs::current_path(build_dir); } - for (auto const& dir_entry : fs::directory_iterator(fs::current_path().string())) { + for (auto const &dir_entry : + fs::directory_iterator(fs::current_path().string())) { for (auto build : builds) { if (dir_entry.path().filename() == build.first) { build.second(); } - } + } } } diff --git a/src/create_pkg.cc b/src/create_pkg.cc index b243afd..5cea49e 100644 --- a/src/create_pkg.cc +++ b/src/create_pkg.cc @@ -10,6 +10,9 @@ Pkg create_pkg(std::string arg) { if (arg.rfind("http", 0) == 0) { pkg.url = arg; pkg.name = name_from_url(arg); + } else if (arg == ".") { + pkg.url = ""; + pkg.name = name_from_url(fs::current_path().string()); } pkg.ver = "HEAD"; diff --git a/src/fetch_git.cc b/src/fetch_git.cc index 3306e42..b8df6bb 100644 --- a/src/fetch_git.cc +++ b/src/fetch_git.cc @@ -4,21 +4,16 @@ int fetch_git(Pkg pkg) { std::string clone_cmds[] = { - "git -c advice.detachedHead=false clone --depth 1 " + pkg.url + " " + - pkg.src.c_str(), - "git -c advice.detachedHead=false clone --branch " + pkg.ver + - " --depth 1 " + pkg.url + " " + pkg.src.c_str()}; + "git -c advice.detachedHead=false clone --depth 1 " + pkg.url + + " " + pkg.src.c_str(), + "git -c advice.detachedHead=false clone --branch " + pkg.ver + + " --depth 1 " + pkg.url + " " + pkg.src.c_str() + }; if (strcmp(pkg.ver.c_str(), "HEAD") == 0) { - if (WEXITSTATUS(system(clone_cmds[0].c_str())) == 0x10) { - return 0; - } else { - return 1; - } + if (WEXITSTATUS(system(clone_cmds[0].c_str())) == 0x10) { return 0; } + else { return 1; } } else { - if (WEXITSTATUS(system(clone_cmds[1].c_str())) == 0x10) { - return 0; - } else { - return 1; - } + if (WEXITSTATUS(system(clone_cmds[1].c_str())) == 0x10) { return 0; } + else { return 1; } } } diff --git a/src/fetch_pwd.cc b/src/fetch_pwd.cc new file mode 100644 index 0000000..dbd5fcf --- /dev/null +++ b/src/fetch_pwd.cc @@ -0,0 +1,5 @@ +#include "vars.cc" + +void fetch_pwd(Pkg pkg) { + std::filesystem::copy(std::filesystem::current_path(), pkg.src); +} diff --git a/src/fetch_src.cc b/src/fetch_src.cc index 27a4b23..3e5cca3 100644 --- a/src/fetch_src.cc +++ b/src/fetch_src.cc @@ -3,5 +3,7 @@ void fetch_src(Pkg pkg) { if (fs::exists(pkg.src)) { fs::remove_all(pkg.src); } - if (fetch_git(pkg) == 0) { return; } else { exit(EXIT_FAILURE); } + + if (pkg.url == "") { std::filesystem::create_directories(pkg.src); return; } + else if (fetch_git(pkg) == 0) { return; } else { exit(EXIT_FAILURE); } } diff --git a/src/install_pkg.cc b/src/install_pkg.cc index a710831..13cef81 100644 --- a/src/install_pkg.cc +++ b/src/install_pkg.cc @@ -1,9 +1,10 @@ #include "fetch_src.cc" -#include "build_pkg.cc" +#include "build.cc" #include "link_install.cc" void install_pkg(Pkg pkg) { fetch_src(pkg); - build_pkg(pkg.src); + //build_pkg(pkg.src); + build(pkg.src.c_str()); link_install(pkg.src); } diff --git a/src/lua_build.cc b/src/lua_build.cc new file mode 100644 index 0000000..b75e2f3 --- /dev/null +++ b/src/lua_build.cc @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include + +#include "vars.cc" + +std::map build_files; + +void lua_build (const char *path) { + lua_State *L = lua_open(); + luaL_openlibs(L); + + if (luaL_loadfile(L, config_file.c_str()) || lua_pcall(L, 0, 0, 0)){ + std::cout << print_error << "cannot run configuration script: " << lua_tostring(L, -1) << "\n"; + } + + lua_getglobal(L, "build_systems"); + + if (!lua_istable(L, -1)) { + std::cout << print_error << "lua variable 'build_systems' is not a table.\n"; + } + + lua_pushnil(L); + + bool build_found = false; + while (lua_next(L, -2) != 0) { + const char *key = lua_tostring(L, -2); + int value = lua_type(L, -1); + + if (lua_isfunction(L, -1) == 0) { + std::cout << print_error << "build value is not a function\n"; + lua_pop(L, 1); + continue; + } + + build_files[key] = value; + + for (auto const& dir_entry : std::filesystem::directory_iterator(std::filesystem::current_path().string())) { + std::string string_key = key; + if (dir_entry.path().filename() != string_key) { continue; } + build_found = true; + lua_pushvalue(L, -1); + lua_pushstring(L, std::filesystem::current_path().string().c_str()); + std::cout << "calling lua build function according to key filename '"<< key << "'...\n"; + if (lua_pcall(L, 1, 0, 0) != 0) { + std::cout << print_error << "lua build function failed to run\n"; + } + if (build_found) { break; } + } + lua_pop(L, 1); + } + if (!build_found) { + std::cout << print_error << "no usable build system was found\n"; + } +} diff --git a/src/main.cc b/src/main.cc index c020c87..bcc45df 100644 --- a/src/main.cc +++ b/src/main.cc @@ -14,9 +14,9 @@ int main(int argc, char *argv[]) { if (argv[1]) { if (strcmp(argv[1], "build") == 0 || strcmp(argv[1], "b") == 0) { if (argv[2]) { - build_pkg(argv[2]); + build(argv[2]); } else { - build_pkg(fs::current_path().string()); + build(fs::current_path().string().c_str()); } } else if (strcmp(argv[1], "install") == 0 || strcmp(argv[1], "i") == 0) { pkg = create_pkg(argv[2]); diff --git a/src/vars.cc b/src/vars.cc index 4957be4..b4c2abe 100644 --- a/src/vars.cc +++ b/src/vars.cc @@ -15,6 +15,8 @@ struct Pkg { const std::string home_dir = std::getenv("HOME"); +const std::string config_file = home_dir + "/.config/pkgit/init.lua"; + const std::string bin = home_dir + "/.local/bin"; const std::string lib = home_dir + "/.local/lib"; const std::string include = home_dir + "/.local/include"; -- cgit v1.2.3