From 3ddb2943bcb69685ecf7dac21ac34d8e291728c7 Mon Sep 17 00:00:00 2001 From: Interfiber Date: Sat, 28 Sep 2024 20:21:30 -0400 Subject: [PATCH] libraries + self building + work on extensions --- HConfig | 17 ++++++++--------- build.ninja | 35 +++++++++++++++++++++-------------- include/grbc/ext.h | 7 ++++++- include/grbc/generator.h | 1 + include/grbc/spec.h | 2 ++ include/grbc/state.h | 3 +++ spec/datatypes.md | 3 +++ src/ext.cc | 1 + src/generator.cc | 2 ++ src/ninja.cc | 25 +++++++++++++++++++------ src/package.cc | 4 ++++ src/platform.cc | 4 ++-- src/target_exe.cc | 7 +++++++ src/target_lib.cc | 25 ++++++++++++++++++------- 14 files changed, 97 insertions(+), 39 deletions(-) create mode 100644 src/ext.cc diff --git a/HConfig b/HConfig index 78f4c09..e6a3a9d 100644 --- a/HConfig +++ b/HConfig @@ -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 diff --git a/build.ninja b/build.ninja index 27d117f..e53a4c6 100644 --- a/build.ninja +++ b/build.ninja @@ -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 - p_cflags = +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 = diff --git a/include/grbc/ext.h b/include/grbc/ext.h index 918821d..c10d95e 100644 --- a/include/grbc/ext.h +++ b/include/grbc/ext.h @@ -1,3 +1,8 @@ #pragma once +#include -#define GRBC_EXT_pkg_config_NAME "GRBC_EXT_pkg_config" \ No newline at end of file +#define GRBC_EXT_pkg_config_NAME "GRBC_EXT_pkg_config" + +struct Extension { + std::string name; +}; \ No newline at end of file diff --git a/include/grbc/generator.h b/include/grbc/generator.h index 56aa727..9f0385e 100644 --- a/include/grbc/generator.h +++ b/include/grbc/generator.h @@ -25,6 +25,7 @@ struct GeneratorCompileCommand { struct GeneratorLinkTargetCommand { std::vector object_files; + std::vector libraries; // Libraries which need to be built before this std::string linker_flags; std::string output_name; diff --git a/include/grbc/spec.h b/include/grbc/spec.h index 53eae61..2a8f1fe 100644 --- a/include/grbc/spec.h +++ b/include/grbc/spec.h @@ -25,6 +25,8 @@ struct Package { std::string name; + std::string file_name; + std::string compiler_flags; std::string linker_flags; diff --git a/include/grbc/state.h b/include/grbc/state.h index 35654d4..045c3f0 100644 --- a/include/grbc/state.h +++ b/include/grbc/state.h @@ -14,6 +14,9 @@ struct GState { std::unordered_map packages; + std::string global_compiler_flags = " "; + std::string global_linker_flags = " "; + std::string ninja_output; static GState& get() { diff --git a/spec/datatypes.md b/spec/datatypes.md index 4e9a563..26e0044 100644 --- a/spec/datatypes.md +++ b/spec/datatypes.md @@ -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; diff --git a/src/ext.cc b/src/ext.cc new file mode 100644 index 0000000..976374e --- /dev/null +++ b/src/ext.cc @@ -0,0 +1 @@ +#include "grbc/ext.h" \ No newline at end of file diff --git a/src/generator.cc b/src/generator.cc index 61d11be..7c2955d 100644 --- a/src/generator.cc +++ b/src/generator.cc @@ -5,6 +5,8 @@ #include 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) { diff --git a/src/ninja.cc b/src/ninja.cc index 431e79b..2740699 100644 --- a/src/ninja.cc +++ b/src/ninja.cc @@ -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; } diff --git a/src/package.cc b/src/package.cc index a2ebcab..adbc86e 100644 --- a/src/package.cc +++ b/src/package.cc @@ -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() == ' ') diff --git a/src/platform.cc b/src/platform.cc index 732a34b..8d59bc4 100644 --- a/src/platform.cc +++ b/src/platform.cc @@ -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(); } } diff --git a/src/target_exe.cc b/src/target_exe.cc index 3d87bfb..18d3318 100644 --- a/src/target_exe.cc +++ b/src/target_exe.cc @@ -30,6 +30,12 @@ TargetInfo grbc_executable(const ExecutableConfig &executable_config) { compiler_args += grbc_include_dirs_to_cflags(executable_config.include_dirs); + std::vector libraries; + + for (auto &lib : executable_config.requirements) { + libraries.push_back(lib.file_name); + } + GeneratorTarget target{}; std::vector 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); diff --git a/src/target_lib.cc b/src/target_lib.cc index e7123ed..a3fb820 100644 --- a/src/target_lib.cc +++ b/src/target_lib.cc @@ -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 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 + pkg_cfg.linker_flags.push_back(grbc_get_config().build_dir + "/" + lib_name); 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);