task support

This commit is contained in:
Hunter 2024-09-29 15:24:13 -04:00
parent d9e4642472
commit dfcf57aae1
12 changed files with 134 additions and 12 deletions

View file

@ -7,5 +7,5 @@ add_subdirectory("vendor/sol2")
include_directories("include") include_directories("include")
include_directories("vendor/sol2/include") include_directories("vendor/sol2/include")
add_executable(grbc src/main.cc src/utils.cc src/file.cc src/target_exe.cc src/platform.cc src/ninja.cc src/generator.cc src/target_lib.cc src/package.cc src/ext.cc src/ext_pkg_config.cc) add_executable(grbc src/main.cc src/utils.cc src/file.cc src/target_exe.cc src/platform.cc src/ninja.cc src/generator.cc src/target_lib.cc src/package.cc src/ext.cc src/ext_pkg_config.cc src/task.cc src/ext_profiles.cc)
target_link_libraries(grbc sol2 lua) target_link_libraries(grbc sol2 lua)

View file

@ -46,7 +46,8 @@ local grbc_lib = grbc_library(LibraryConfig.new({
grbc_file("src/generator.cc"), grbc_file("src/generator.cc"),
grbc_file("src/target_lib.cc"), grbc_file("src/target_lib.cc"),
grbc_file("src/package.cc"), grbc_file("src/package.cc"),
grbc_file("src/ext.cc") grbc_file("src/ext.cc"),
grbc_file("src/task.cc")
}, },
lib_type = LibraryType.Static, lib_type = LibraryType.Static,
@ -88,5 +89,11 @@ local grbc_exe = grbc_executable(ExecutableConfig.new({
include_dirs = {} include_dirs = {}
})) }))
grbc_task(TaskConfig.new({
name = "Clean build",
task_id = "clean",
shell_script = "ninja -t clean"
}))
-- Output the final build script -- Output the final build script
grbc_build("ninja") grbc_build("ninja")

View file

@ -1,4 +1,5 @@
### GRBC BUILT-IN NINJA GENERATOR ### ### GENERATED BY THE GRBC BUILT-IN NINJA GENERATOR ###
### GENERATED ON: 1727637831 ###
## Default variables ## ## Default variables ##
@ -99,9 +100,14 @@ build $builddir/src/package.o: cxx src/package.cc
build $builddir/src/ext.o: cxx src/ext.cc build $builddir/src/ext.o: cxx src/ext.cc
p_cflags = -Iinclude -Ivendor/sol2/include p_cflags = -Iinclude -Ivendor/sol2/include
## Compile: src/task.cc ##
build $builddir/src/task.o: cxx src/task.cc
p_cflags = -Iinclude -Ivendor/sol2/include
## Link: libgrbc.a ## ## Link: libgrbc.a ##
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 $builddir/src/ext.o 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 $builddir/src/ext.o $builddir/src/task.o
## Compile: src/main.cc ## ## Compile: src/main.cc ##
@ -114,3 +120,13 @@ build $builddir/grbc: link_cxx $builddir/src/main.o | $builddir/libgrbc.a $buil
p_linker_flags = -llua -lm -ldl build/libgrbc.a build/libgrbc_extensions.a -O3 -Lbuild -Wl,-rpath,build:. p_linker_flags = -llua -lm -ldl build/libgrbc.a build/libgrbc_extensions.a -O3 -Lbuild -Wl,-rpath,build:.
p_cflags = p_cflags =
## clean ##
rule clean_task
command = ninja -t clean
description = Running task: Clean build (clean)
build clean: clean_task
build all: phony $builddir/libgrbc_extensions.a $builddir/libgrbc.a $builddir/grbc
default all

View file

@ -38,6 +38,12 @@ struct GeneratorTarget {
std::vector<GeneratorLinkTargetCommand> link_target_commands; std::vector<GeneratorLinkTargetCommand> link_target_commands;
}; };
struct GeneratorTask {
std::string description, id;
std::string shell_script;
};
typedef GeneratorResult (*Generator_Run)(); typedef GeneratorResult (*Generator_Run)();
struct Generator { struct Generator {

View file

@ -49,3 +49,8 @@ ninja_build_rule_link_exe_target(const GeneratorLinkTargetCommand &link_cmd);
*/ */
std::string std::string
ninja_build_rule_link_lib_target(const GeneratorLinkTargetCommand &link_cmd); ninja_build_rule_link_lib_target(const GeneratorLinkTargetCommand &link_cmd);
/**
* Generate a line which describes the given task
*/
std::string ninja_build_rule_task(const GeneratorTask &task);

View file

@ -128,6 +128,23 @@ struct ExecutableConfig {
std::vector<std::string> include_dirs{}; std::vector<std::string> include_dirs{};
}; };
struct TaskConfig {
TaskConfig(const sol::table &table) {
name = table.get<std::string>("name");
task_id = table.get<std::string>("task_id");
shell_script = table.get<std::string>("shell_script");
}
/// Name of the task
std::string name;
/// ID of the task
std::string task_id;
/// Shell script content for this task
std::string shell_script;
};
struct LibraryConfig { struct LibraryConfig {
LibraryConfig(const sol::table &table) { LibraryConfig(const sol::table &table) {
name = table.get<std::string>("name"); name = table.get<std::string>("name");
@ -253,4 +270,6 @@ bool grbc_has_ext(const std::string &extension_id);
std::string grbc_platform_file(PlatformType platform_type, std::string grbc_platform_file(PlatformType platform_type,
const std::string &file_name); const std::string &file_name);
bool grbc_is_ext_loaded(const std::string &extension_name); bool grbc_is_ext_loaded(const std::string &extension_name);
void grbc_task(const TaskConfig &config);

View file

@ -11,6 +11,7 @@ struct GState {
Platform current_platform{}; Platform current_platform{};
sol::state lua; sol::state lua;
std::vector<GeneratorTask> tasks;
std::vector<GeneratorTarget> targets; std::vector<GeneratorTarget> targets;
std::vector<Generator> generators; std::vector<Generator> generators;
@ -19,7 +20,7 @@ struct GState {
std::vector<Extension> extensions; std::vector<Extension> extensions;
std::string global_compiler_flags = ""; std::string global_compiler_flags = "";
std::string global_linker_flags = ""; std::string global_linker_flags = " ";
std::string ninja_output; std::string ninja_output;

View file

@ -162,4 +162,18 @@ enum LanguageType {
LanguageType_CPP, LanguageType_CPP,
LanguageType_C LanguageType_C
}; };
```
## TaskConfig
```c++
struct TaskConfig {
/// Name of the task, can have spaces unlike task_id
string task_name;
/// ID used when invoking the task, ex: ```ninja task:helloworld```
string task_id;
/// Shell script to be executed when this task is called
string shell_script;
};
``` ```

View file

@ -80,4 +80,7 @@ Generate a compiler flag to define a variable
Get the given extension for a library file on the current platform Get the given extension for a library file on the current platform
## [X] grbc_bake_package_config(config: PackageConfig) -> Package ## [X] grbc_bake_package_config(config: PackageConfig) -> Package
Convert a PackageConfig into a Package Convert a PackageConfig into a Package
## [X] grbc_task(config: TaskConfig) -> Void
Create a task and add it to the build script

View file

@ -1,6 +1,8 @@
#include "grbc/ext_pkg_config.h" #include "grbc/ext_pkg_config.h"
#include "grbc/ext_profiles.h" #include "grbc/ext_profiles.h"
#include "grbc/state.h" #include "grbc/state.h"
#include <sol/raii.hpp>
#include <sol/table.hpp>
#define SOL_ALL_SAFETIES_ON 1 #define SOL_ALL_SAFETIES_ON 1
#include "grbc/helpers.h" #include "grbc/helpers.h"
@ -80,6 +82,12 @@ int main() {
&Package::compiler_flags, "linker_flags", &Package::compiler_flags, "linker_flags",
&Package::linker_flags); &Package::linker_flags);
// TaskConfig
lua.new_usertype<TaskConfig>(
"TaskConfig", sol::constructors<TaskConfig(sol::table)>(), "name",
&TaskConfig::name, "task_id", &TaskConfig::task_id, "shell_script",
&TaskConfig::shell_script);
lua.set("grbc_want_version", grbc_want_version); lua.set("grbc_want_version", grbc_want_version);
lua.set("grbc_exception", grbc_exception); lua.set("grbc_exception", grbc_exception);
lua.set("grbc_get_config", grbc_get_config); lua.set("grbc_get_config", grbc_get_config);
@ -107,6 +115,7 @@ int main() {
lua.set("grbc_has_ext", grbc_has_ext); lua.set("grbc_has_ext", grbc_has_ext);
lua.set("grbc_platform_file", grbc_platform_file); lua.set("grbc_platform_file", grbc_platform_file);
lua.set("grbc_is_ext_loaded", grbc_is_ext_loaded); lua.set("grbc_is_ext_loaded", grbc_is_ext_loaded);
lua.set("grbc_task", grbc_task);
// Load generators // Load generators

View file

@ -74,7 +74,8 @@ ninja_build_rule_compile_file(const GeneratorCompileCommand &compile_cmd) {
result += "build $builddir/" + compile_cmd.object_file + ": " + result += "build $builddir/" + compile_cmd.object_file + ": " +
compiler_rule + " " + compile_cmd.source_file + "\n"; compiler_rule + " " + compile_cmd.source_file + "\n";
result += " p_cflags = " + compile_cmd.compiler_flags + GState::get().global_compiler_flags + "\n\n"; result += " p_cflags = " + compile_cmd.compiler_flags +
GState::get().global_compiler_flags + "\n\n";
return result; return result;
} }
@ -104,7 +105,8 @@ ninja_build_rule_link_lib_target(const GeneratorLinkTargetCommand &link_cmd) {
result += "build $builddir/" + link_cmd.output_name + ": link_cxx " + result += "build $builddir/" + link_cmd.output_name + ": link_cxx " +
file_list + "\n"; file_list + "\n";
result += " p_linker_flags = " + link_cmd.linker_flags + " -shared" + GState::get().global_linker_flags + "\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"; result += " p_cflags = " + GState::get().global_compiler_flags + "\n\n";
} else { } else {
result += "build $builddir/" + link_cmd.output_name + ": archive " + result += "build $builddir/" + link_cmd.output_name + ": archive " +
@ -129,7 +131,8 @@ ninja_build_rule_link_exe_target(const GeneratorLinkTargetCommand &link_cmd) {
if (!link_cmd.libraries.empty()) { if (!link_cmd.libraries.empty()) {
lib_list = "| "; lib_list = "| ";
for (auto &lib : link_cmd.libraries) { for (auto &lib : link_cmd.libraries) {
if (lib.empty()) continue; if (lib.empty())
continue;
lib_list += "$builddir/" + lib + " "; lib_list += "$builddir/" + lib + " ";
} }
@ -141,12 +144,26 @@ ninja_build_rule_link_exe_target(const GeneratorLinkTargetCommand &link_cmd) {
result += "build $builddir/" + link_cmd.output_name + ": link_cxx " + result += "build $builddir/" + link_cmd.output_name + ": link_cxx " +
file_list + " " + lib_list + "\n"; file_list + " " + lib_list + "\n";
result += " p_linker_flags = " + link_cmd.linker_flags + GState::get().global_linker_flags + "\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"; result += " p_cflags = " + GState::get().global_compiler_flags + "\n\n";
return result; return result;
} }
std::string ninja_build_rule_task(const GeneratorTask &task) {
std::string result;
result += "## " + task.id + " ##\n";
result += "rule " + task.id + "_task\n";
result += " command = " + task.shell_script + "\n";
result += " description = " + task.description += "\n\n";
result += "build " + task.id + ": " + task.id + "_task\n\n";
return result;
}
// Ninja generator entry // Ninja generator entry
GeneratorResult ninja_generator() { GeneratorResult ninja_generator() {
@ -156,7 +173,9 @@ GeneratorResult ninja_generator() {
result.file_name = "build.ninja"; result.file_name = "build.ninja";
result.content += "### GRBC BUILT-IN NINJA GENERATOR ###\n\n"; result.content += "### GENERATED BY THE GRBC BUILT-IN NINJA GENERATOR ###\n";
result.content += "### GENERATED ON: " + std::to_string(std::time(nullptr)) + " ###\n\n";
result.content += ninja_build_default_variables(); result.content += ninja_build_default_variables();
result.content += ninja_build_rule_compile_cc(); result.content += ninja_build_rule_compile_cc();
result.content += ninja_build_rule_compile_cxx(); result.content += ninja_build_rule_compile_cxx();
@ -164,6 +183,8 @@ GeneratorResult ninja_generator() {
result.content += ninja_build_rule_link_cxx(); result.content += ninja_build_rule_link_cxx();
result.content += ninja_build_rule_archive_library(); result.content += ninja_build_rule_archive_library();
std::string targets;
for (auto &target : GState::get().targets) { for (auto &target : GState::get().targets) {
for (auto &compile_cmd : target.compile_commands) { for (auto &compile_cmd : target.compile_commands) {
result.content += ninja_build_rule_compile_file(compile_cmd); result.content += ninja_build_rule_compile_file(compile_cmd);
@ -175,8 +196,17 @@ GeneratorResult ninja_generator() {
} else { } else {
result.content += ninja_build_rule_link_lib_target(link_cmd); result.content += ninja_build_rule_link_lib_target(link_cmd);
} }
targets += "$builddir/" + link_cmd.output_name + " ";
} }
} }
for (auto &task : GState::get().tasks) {
result.content += ninja_build_rule_task(task);
}
result.content += "build all: phony " + targets + "\n\n";
result.content += "default all\n";
return result; return result;
} }

12
src/task.cc Normal file
View file

@ -0,0 +1,12 @@
#include "grbc/generator.h"
#include "grbc/spec.h"
#include "grbc/state.h"
void grbc_task(const TaskConfig &config) {
GeneratorTask task{};
task.description = "Running task: " + config.name + " (" + config.task_id + ")";
task.id = config.task_id;
task.shell_script = config.shell_script;
GState::get().tasks.push_back(task);
}