libraries + self building + work on extensions

This commit is contained in:
Hunter 2024-09-28 20:21:30 -04:00
parent b6ee9d0dc3
commit 3ddb2943bc
14 changed files with 97 additions and 39 deletions

17
HConfig
View file

@ -1,6 +1,5 @@
grbc_want_version("1.0")
grbc_load_platform("platform.hcfg")
grbc_load_profile("profile.hcfg")
local grbc_lib = grbc_library(LibraryConfig.new({
name = "libgrbc",
@ -13,9 +12,10 @@ local grbc_lib = grbc_library(LibraryConfig.new({
grbc_file("src/utils.cc"),
grbc_file("src/generator.cc"),
grbc_file("src/target_lib.cc"),
grbc_file("src/package.cc")
grbc_file("src/package.cc"),
grbc_file("src/ext.cc")
},
lib_type = LibraryType.Static,
lib_type = LibraryType.Shared,
requirements = {},
compile_flags = {},
@ -28,7 +28,10 @@ local grbc_lib = grbc_library(LibraryConfig.new({
package_config = PackageConfig.new({
name = "libgrbc",
libraries = {},
include_dirs = {},
include_dirs = {
grbc_file("include"),
grbc_file("vendor/sol2/include")
},
compile_flags = {},
linker_flags = {},
})
@ -51,13 +54,9 @@ local grbc_exe = grbc_executable(ExecutableConfig.new({
grbc_pkg("libgrbc")
},
compile_flags = {
grbc_compiler_define("HCONFIG", "")
},
linker_flags = {},
include_dirs = {
grbc_file("include"),
grbc_file("vendor/sol2/include")
}
include_dirs = {}
}))
-- Output the final build script

View file

@ -43,55 +43,62 @@ rule archive
## Compile: src/file.cc ##
build $builddir/src/file.o: cxx src/file.cc
p_cflags = -Iinclude -Ivendor/sol2/include
p_cflags = -fPIC -Iinclude -Ivendor/sol2/include
## Compile: src/ninja.cc ##
build $builddir/src/ninja.o: cxx src/ninja.cc
p_cflags = -Iinclude -Ivendor/sol2/include
p_cflags = -fPIC -Iinclude -Ivendor/sol2/include
## Compile: src/platform.cc ##
build $builddir/src/platform.o: cxx src/platform.cc
p_cflags = -Iinclude -Ivendor/sol2/include
p_cflags = -fPIC -Iinclude -Ivendor/sol2/include
## Compile: src/target_exe.cc ##
build $builddir/src/target_exe.o: cxx src/target_exe.cc
p_cflags = -Iinclude -Ivendor/sol2/include
p_cflags = -fPIC -Iinclude -Ivendor/sol2/include
## Compile: src/utils.cc ##
build $builddir/src/utils.o: cxx src/utils.cc
p_cflags = -Iinclude -Ivendor/sol2/include
p_cflags = -fPIC -Iinclude -Ivendor/sol2/include
## Compile: src/generator.cc ##
build $builddir/src/generator.o: cxx src/generator.cc
p_cflags = -Iinclude -Ivendor/sol2/include
p_cflags = -fPIC -Iinclude -Ivendor/sol2/include
## Compile: src/target_lib.cc ##
build $builddir/src/target_lib.o: cxx src/target_lib.cc
p_cflags = -Iinclude -Ivendor/sol2/include
p_cflags = -fPIC -Iinclude -Ivendor/sol2/include
## Compile: src/package.cc ##
build $builddir/src/package.o: cxx src/package.cc
p_cflags = -Iinclude -Ivendor/sol2/include
p_cflags = -fPIC -Iinclude -Ivendor/sol2/include
## Link: libgrbc.a ##
## Compile: src/ext.cc ##
build $builddir/libgrbc.a: archive $builddir/src/file.o $builddir/src/ninja.o $builddir/src/platform.o $builddir/src/target_exe.o $builddir/src/utils.o $builddir/src/generator.o $builddir/src/target_lib.o $builddir/src/package.o
build $builddir/src/ext.o: cxx src/ext.cc
p_cflags = -fPIC -Iinclude -Ivendor/sol2/include
## Link: libgrbc.so ##
build $builddir/libgrbc.so: link_cxx $builddir/src/file.o $builddir/src/ninja.o $builddir/src/platform.o $builddir/src/target_exe.o $builddir/src/utils.o $builddir/src/generator.o $builddir/src/target_lib.o $builddir/src/package.o $builddir/src/ext.o
p_linker_flags = -shared -Lbuild -Wl,-rpath,build:.
p_cflags =
## Compile: src/main.cc ##
build $builddir/src/main.o: cxx src/main.cc
p_cflags = -fdiagnostics-color=always -DHCONFIG= -Iinclude -Ivendor/sol2/include
p_cflags = -Iinclude -Ivendor/sol2/include
## Link: grbc ##
build $builddir/grbc: link_cxx $builddir/src/main.o
p_linker_flags = -llua -lm -ldl build/libgrbc.a
build $builddir/grbc: link_cxx $builddir/src/main.o | $builddir/libgrbc.so
p_linker_flags = -llua -lm -ldl build/libgrbc.so -Lbuild -Wl,-rpath,build:.
p_cflags =

View file

@ -1,3 +1,8 @@
#pragma once
#include <string>
#define GRBC_EXT_pkg_config_NAME "GRBC_EXT_pkg_config"
struct Extension {
std::string name;
};

View file

@ -25,6 +25,7 @@ struct GeneratorCompileCommand {
struct GeneratorLinkTargetCommand {
std::vector<std::string> object_files;
std::vector<std::string> libraries; // Libraries which need to be built before this
std::string linker_flags;
std::string output_name;

View file

@ -25,6 +25,8 @@ struct Package {
std::string name;
std::string file_name;
std::string compiler_flags;
std::string linker_flags;

View file

@ -14,6 +14,9 @@ struct GState {
std::unordered_map<std::string, Package> packages;
std::string global_compiler_flags = " ";
std::string global_linker_flags = " ";
std::string ninja_output;
static GState& get() {

View file

@ -26,6 +26,9 @@ struct Package {
/// Name of the package
string name;
/// File path to the compiled library
string file_name;
/// Compiler flags used in this package
string compile_flags;

1
src/ext.cc Normal file
View file

@ -0,0 +1 @@
#include "grbc/ext.h"

View file

@ -5,6 +5,8 @@
#include <fstream>
void grbc_build(const std::string &generator_id) {
GState::get().global_linker_flags += "-L" + grbc_get_config().build_dir + " -Wl,-rpath," + grbc_get_config().build_dir + ":. ";
log_msg(("finding generator with name: " + generator_id).c_str());
for (auto &gen : GState::get().generators) {

View file

@ -74,7 +74,7 @@ ninja_build_rule_compile_file(const GeneratorCompileCommand &compile_cmd) {
result += "build $builddir/" + compile_cmd.object_file + ": " +
compiler_rule + " " + compile_cmd.source_file + "\n";
result += " p_cflags = " + compile_cmd.compiler_flags + "\n\n";
result += " p_cflags = " + compile_cmd.compiler_flags + GState::get().global_compiler_flags + "\n\n";
return result;
}
@ -104,8 +104,8 @@ ninja_build_rule_link_lib_target(const GeneratorLinkTargetCommand &link_cmd) {
result += "build $builddir/" + link_cmd.output_name + ": link_cxx " +
file_list + "\n";
result += " p_linker_flags = " + link_cmd.linker_flags + " -shared\n";
result += " p_cflags = \n\n";
result += " p_linker_flags = " + link_cmd.linker_flags + " -shared" + GState::get().global_linker_flags + "\n";
result += " p_cflags = " + GState::get().global_compiler_flags + "\n\n";
} else {
result += "build $builddir/" + link_cmd.output_name + ": archive " +
file_list + "\n\n";
@ -124,12 +124,25 @@ ninja_build_rule_link_exe_target(const GeneratorLinkTargetCommand &link_cmd) {
file_list += "$builddir/" + file + " ";
}
std::string lib_list;
if (!link_cmd.libraries.empty()) {
lib_list = "| ";
for (auto &lib : link_cmd.libraries) {
if (lib.empty()) continue;
lib_list += "$builddir/" + lib + " ";
}
lib_list.pop_back();
}
file_list.pop_back();
result += "build $builddir/" + link_cmd.output_name + ": link_cxx " +
file_list + "\n";
result += " p_linker_flags = " + link_cmd.linker_flags + "\n";
result += " p_cflags = \n\n";
file_list + " " + lib_list + "\n";
result += " p_linker_flags = " + link_cmd.linker_flags + GState::get().global_linker_flags + "\n";
result += " p_cflags = " + GState::get().global_compiler_flags + "\n\n";
return result;
}

View file

@ -30,6 +30,10 @@ Package grbc_bake_package_config(const PackageConfig &config) {
pkg.compiler_flags += compiler_flag + " ";
}
for (auto &include_dir : config.include_dirs) {
pkg.compiler_flags += "-I" + include_dir + " ";
}
// Remove trailing whitespace
if (pkg.compiler_flags.back() == ' ')

View file

@ -43,8 +43,8 @@ std::string grbc_find_compiler(const std::string &compiler_name) {
if (std::filesystem::exists(
std::filesystem::path(current_path) /
std::filesystem::path(compiler_name + ".exe"))) {
return std::filesystem::path(current_path) /
std::filesystem::path(compiler_name + ".exe");
return (std::filesystem::path(current_path) /
std::filesystem::path(compiler_name + ".exe")).generic_string();
}
}

View file

@ -30,6 +30,12 @@ TargetInfo grbc_executable(const ExecutableConfig &executable_config) {
compiler_args += grbc_include_dirs_to_cflags(executable_config.include_dirs);
std::vector<std::string> libraries;
for (auto &lib : executable_config.requirements) {
libraries.push_back(lib.file_name);
}
GeneratorTarget target{};
std::vector<std::string> object_files;
@ -53,6 +59,7 @@ TargetInfo grbc_executable(const ExecutableConfig &executable_config) {
executable_link_cmd.object_files = object_files;
executable_link_cmd.output_name = exe_name;
executable_link_cmd.target_type = GeneratorTargetType_Executable;
executable_link_cmd.libraries = libraries;
target.link_target_commands.push_back(executable_link_cmd);

View file

@ -24,6 +24,10 @@ TargetInfo grbc_library(const LibraryConfig &library_config) {
compiler_args += compiler_arg + " ";
}
if (library_config.lib_type == LibraryType_Shared) {
compiler_args += "-fPIC ";
}
// Include directories
compiler_args += grbc_include_dirs_to_cflags(library_config.include_dirs);
@ -44,21 +48,27 @@ TargetInfo grbc_library(const LibraryConfig &library_config) {
target.compile_commands.push_back(compile_cmd);
}
// Libraries which need to be built before us
std::vector<std::string> libraries;
for (auto &lib : library_config.requirements) {
libraries.push_back(lib.file_name);
}
// Generate package config
PackageConfig pkg_cfg{};
pkg_cfg.name = library_config.name;
pkg_cfg.libraries = library_config.package_config.libraries;
pkg_cfg.include_dirs = library_config.include_dirs;
if (library_config.lib_type == LibraryType_Static) {
pkg_cfg.linker_flags.push_back(grbc_get_config().build_dir + "/" + lib_name);
}
// FIXME: Shared library linker flags
if (!pkg_cfg.name.empty()) {
GState::get().packages.insert(
{pkg_cfg.name, grbc_bake_package_config(pkg_cfg)});
Package pkg = grbc_bake_package_config(pkg_cfg);
pkg.file_name = lib_name;
GState::get().packages.insert({pkg_cfg.name, pkg});
}
GeneratorLinkTargetCommand link_target{};
@ -72,6 +82,7 @@ TargetInfo grbc_library(const LibraryConfig &library_config) {
link_target.object_files = object_files;
link_target.linker_flags = linker_args;
link_target.output_name = lib_name;
link_target.libraries = libraries;
target.link_target_commands.push_back(link_target);