From a14896f9ed209dd3f9597722e5a5697bd7dbf531 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Mon, 4 Dec 2023 12:18:14 +0100 Subject: meta: Renamed folder containing source --- modules/tools/.nix/derivation.nix | 31 +++ modules/tools/SConscript | 32 +++ modules/tools/SConstruct | 71 +++++ modules/tools/c_gen_iface.cpp | 11 + modules/tools/c_gen_iface.hpp | 535 ++++++++++++++++++++++++++++++++++++++ modules/tools/cli_analyzer.hpp | 114 ++++++++ 6 files changed, 794 insertions(+) create mode 100644 modules/tools/.nix/derivation.nix create mode 100644 modules/tools/SConscript create mode 100644 modules/tools/SConstruct create mode 100644 modules/tools/c_gen_iface.cpp create mode 100644 modules/tools/c_gen_iface.hpp create mode 100644 modules/tools/cli_analyzer.hpp (limited to 'modules/tools') diff --git a/modules/tools/.nix/derivation.nix b/modules/tools/.nix/derivation.nix new file mode 100644 index 0000000..6e3fbe1 --- /dev/null +++ b/modules/tools/.nix/derivation.nix @@ -0,0 +1,31 @@ +{ lib +, stdenv +, scons +, clang-tools +, version +, forstio +}: + +let + +in stdenv.mkDerivation { + pname = "forstio-tools"; + inherit version; + src = ./..; + + enableParallelBuilding = true; + + nativeBuildInputs = [ + scons + clang-tools + ]; + + buildInputs = [ + forstio.core + forstio.async + forstio.io + forstio.codec + ]; + + outputs = ["out" "dev"]; +} diff --git a/modules/tools/SConscript b/modules/tools/SConscript new file mode 100644 index 0000000..7c9efd7 --- /dev/null +++ b/modules/tools/SConscript @@ -0,0 +1,32 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +tools_env = env.Clone(); + +tools_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +tools_env.headers = sorted(glob.glob(dir_path + "/*.h")) + +env.sources += tools_env.sources; +env.headers += tools_env.headers; + +## Static lib +objects_static = [] +tools_env.add_source_files(objects_static, tools_env.sources, shared=False); +tools_env.c_array_example = tools_env.Program('#build/forstio-c-array-example', [objects_static]); + +# Set Alias +env.Alias('tools_c_array_example', [tools_env.c_array_example]); + +env.targets += ['tools_c_array_example']; + +# Install +env.Install('$prefix/bin/', [tools_env.c_array_example]); diff --git a/modules/tools/SConstruct b/modules/tools/SConstruct new file mode 100644 index 0000000..9a02291 --- /dev/null +++ b/modules/tools/SConstruct @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +import sys +import os +import os.path +import glob +import re + + +if sys.version_info < (3,): + def isbasestring(s): + return isinstance(s,basestring) +else: + def isbasestring(s): + return isinstance(s, (str,bytes)) + +def add_kel_source_files(self, sources, filetype, lib_env=None, shared=False, target_post=""): + + if isbasestring(filetype): + dir_path = self.Dir('.').abspath + filetype = sorted(glob.glob(dir_path+"/"+filetype)) + + for path in filetype: + target_name = re.sub( r'(.*?)(\.cpp|\.c\+\+)', r'\1' + target_post, path ) + if shared: + target_name+='.os' + sources.append( self.SharedObject( target=target_name, source=path ) ) + else: + target_name+='.o' + sources.append( self.StaticObject( target=target_name, source=path ) ) + pass + +def isAbsolutePath(key, dirname, env): + assert os.path.isabs(dirname), "%r must have absolute path syntax" % (key,) + +env_vars = Variables( + args=ARGUMENTS +) + +env_vars.Add('prefix', + help='Installation target location of build results and headers', + default='/usr/local/', + validator=isAbsolutePath +) + +env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], + CPPDEFINES=['SAW_UNIX'], + CXXFLAGS=['-std=c++20','-g','-Wall','-Wextra'], + LIBS=[ + 'forstio-core', + 'forstio-async', + 'forstio-codec' + ] +); +env.__class__.add_source_files = add_kel_source_files +env.Tool('compilation_db'); +env.cdb = env.CompilationDatabase('compile_commands.json'); + +env.objects = []; +env.sources = []; +env.headers = []; +env.targets = []; + +Export('env') +SConscript('SConscript') + +env.Alias('cdb', env.cdb); +env.Alias('all', [env.targets]); +env.Default('all'); + +env.Alias('install', '$prefix') diff --git a/modules/tools/c_gen_iface.cpp b/modules/tools/c_gen_iface.cpp new file mode 100644 index 0000000..996f3c3 --- /dev/null +++ b/modules/tools/c_gen_iface.cpp @@ -0,0 +1,11 @@ +#include "c_gen_iface.hpp" + +int main(){ + // saw::generate_array_example(); + + auto eov = saw::generate_iface_example(); + if(eov.is_error()){ + return -1; + } + return 0; +} diff --git a/modules/tools/c_gen_iface.hpp b/modules/tools/c_gen_iface.hpp new file mode 100644 index 0000000..e79e27f --- /dev/null +++ b/modules/tools/c_gen_iface.hpp @@ -0,0 +1,535 @@ +#include +#include +#include + +#include +#include +#include + +#include + +#include + +namespace saw { + +namespace impl { +/** + * Type meant to provide future help if I decide to introduce more maps + */ +struct c_types { + struct c_member { + std::string key; + std::string name; + bool is_primitive; + }; + + struct c_struct { + std::string kind; + std::string type; + std::string cpp_schema; + std::vector members; + }; + + struct c_response { + std::string key; + bool is_primitive; + }; + + struct c_func { + std::string cpp_schema; + std::string cpp_name; + c_response response; + std::vector requests; + }; + + using c_struct_map = std::map; + using c_func_map = std::map; + + struct state { + std::string interface_schema; + c_struct_map struct_map; + c_func_map func_map; + std::string prefix; + std::string postfix; + std::string encoding; + }; +}; + +namespace schema { +using namespace saw::schema; +/** +Pseudo flattened +using Foo = Struct< + Member, "a"> + Member, "b"> +>; + +Needs to be flattened to + +template +using FlattenedSchemaElement = Struct< + Member, "path">, + Member +>; + +// Illegal, but doable with more lines of code +// Just use typename... T and +// "T..." for +// "Member,Names>...>" +// and specialize somewhere else +template +using FlattenedSchema = Struct< + Member, + Member, Names>... +>; + */ +template +struct schema_flattener { + static_assert(always_false, "Not supported"); +}; + +template +struct schema_flattener...>> { + +}; + +>; + +using StructBindingSchema = Struct< + Member, + Member, + Member, + Member, "members"> +>; + +using FunctionBindingSchema = Struct< + Member, + Member, + Member +>; + +using BindingSchema = Struct< + Member, + Member, + Member, + Member, + Member, "structs">, + Member, "functions"> +>; +} + +/** + * Helper to determine if we are dealing with primitive types + */ +template +struct c_is_primitive { + static constexpr bool value = false; +}; + +template +struct c_is_primitive> { + static constexpr bool value = true; +}; + +template +struct c_primitive_string { + static_assert(always_false, "Not supported"); +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "int8_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "int16_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "int32_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "int64_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "uint8_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "uint16_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "uint32_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "uint64_t"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "float"; +}; + +template<> +struct c_primitive_string { + static constexpr std::string_view value = "double"; +}; + +template +struct c_data_translater { + static_assert(always_false, "Not supported"); +}; + +template +struct c_data_translater> { + using StructMap = typename c_types::c_struct_map; + using Schema = schema::Primitive; + + static error_or generate(c_types::state& c_state, std::string& str){ + (void) c_state; + str += c_primitive_string::value; + return void_t{}; + } +}; + +template +struct c_data_translater> { + using Schema = schema::Array; + using StructMap = typename c_types::c_struct_map; + + static error_or generate(c_types::state& c_state, std::string& type_str){ + type_str = "array_"; + + std::string inner_type_str; + auto eov = impl::c_data_translater::generate(c_state, inner_type_str); + if(eov.is_error()){ + return eov; + } + + type_str += inner_type_str; + + if(Dim > 1){ + type_str += "_"; + type_str += std::to_string(Dim); + type_str += "d"; + } + + if(c_state.struct_map.find(type_str) != c_state.struct_map.end()){ + return void_t{}; + } + + c_types::c_struct str; + + try { + std::stringstream iss; + schema_stringify::apply(iss); + str.cpp_schema = iss.str(); + }catch(const std::exception& e){ + return make_error(); + } + + /** + * Adding heap alloc data ptr + */ + { + c_types::c_member memb; + memb.key = inner_type_str; + memb.name = "data"; + memb.is_primitive = c_is_primitive::value; + // fn.members.emplace_back(std::move(memb)); + + str.members.emplace_back(std::move(memb)); + } + { + c_types::c_member memb; + memb.key + } + + + str.def = "struct "; + str.def += type_str; + + str.def += " {\n"; + + str.def += "\t" + inner_type_str + "* data;\n"; + str.def += "\tsize_t size;\n"; + if( Dim > 1 ){ + str.def += "\tsize_t dims["+std::to_string(Dim)+"];\n"; + } + + str.def += "};\n"; + + c_state.struct_map.emplace(std::make_pair(type_str, std::move(str))); + return void_t{}; + } +}; + +template +struct c_iface_translater { + static_assert(always_false,"Not supported"); +}; + +template +struct c_iface_translater...>, Response>> { + using StructMap = typename c_types::c_struct_map; + using FuncMap = typename c_types::c_func_map; + + template + static error_or generate_request_member(c_types::state& c_state, std::array& type_arr){ + using Type = typename parameter_pack_type::type; + // constexpr string_literal lit = parameter_key_pack_type::literal; + + auto eov = c_data_translater::generate(c_state, type_arr.at(i)); + if(eov.is_error()){ + return eov; + } + + if constexpr ((i+1) < sizeof...(Request)){ + auto eov = generate_request_member(c_state, type_arr); + return eov; + } + + return void_t{}; + } + + template + static error_or generate_parameter(std::string& param_use, const std::array& req_type_arr){ + using Type = typename parameter_pack_type::type; + constexpr string_literal lit = parameter_key_pack_type::literal; + + std::string used_param; + if constexpr (c_is_primitive::value){ + used_param += req_type_arr.at(i); + } else { + used_param += "const " + req_type_arr.at(i) + "*"; + } + param_use += used_param + " " + std::string{lit.view()}; + + if constexpr ( (i+1) < sizeof...(Request) ){ + param_use += ", "; + return generate_parameter(param_use, req_type_arr); + } + + return void_t{}; + } + + static error_or generate(c_types::state& c_state, const std::string& func_name){ + std::array request_type_arr; + + if constexpr (sizeof...(Request) > 0){ + auto eov = generate_request_member<0>(c_state, request_type_arr); + if(eov.is_error()){ + return eov; + } + } + std::string response_type_str; + { + auto eov = c_data_translater::generate(c_state, response_type_str); + if(eov.is_error()){ + return eov; + } + } + std::string c_func_name = c_state.prefix + "_" + func_name; + + c_types::c_func fn; + fn.cpp_name = func_name; + fn.def = "int"; + std::string iface_ctx = c_state.prefix + "_iface_context* ctx"; + fn.def += " " + c_func_name + "(" + iface_ctx; + fn.def += ", " + response_type_str + "* out"; + if constexpr ( sizeof...(Request) > 0 ){ + fn.def += ", "; + auto eov = generate_parameter<0>(fn.def, request_type_arr); + if(eov.is_error()){ + return eov; + } + } + fn.def += ")"; + + fn.source = "{\n"; + // Check necessary pointers + fn.source += "\tassert(ctx);\n"; + fn.source += "\tif(!ctx){return -1;}\n\n"; + fn.source += "\tauto rmt = reinterpret_cast 0){ + // I need a recursive search for the types + } + + // Call the remote interface + fn.source += "\tauto eov = rmt->template call<\"" + fn.cpp_name + "\">(std::move(input));"; + fn.source += ");\n"; + + // translate the output data + fn.source += ""; + + // Close the brace + + fn.source += "}\n"; + + c_state.func_map.emplace(std::make_pair(c_func_name, std::move(fn))); + + return void_t{}; + } +}; + +template +struct c_iface_translater, Names>...>> { + using Schema = schema::Interface, Names>...>; + + template + static error_or generate_member(c_types::state& c_state){ + using Req = typename parameter_pack_type::type; + using Resp = typename parameter_pack_type::type; + constexpr string_literal lit = parameter_key_pack_type::literal; + + try{ + std::stringstream iss; + schema_stringify::apply(iss); + c_state.interface_schema = iss.str(); + }catch(const std::exception& e){ + (void) e; + return make_error(); + } + + std::string c_func_name = c_state.prefix + "_" + std::string{lit.view()}; + if(c_state.func_map.find(c_func_name) != c_state.func_map.end()){ + return make_error(); + } + + { + std::string type_str; + auto eov = c_iface_translater>::generate(c_state, std::string{lit.view()}); + if(eov.is_error()){ + return eov; + } + } + + if constexpr ((i+1) < sizeof...(Requests) ){ + return generate_member(c_state); + } + return void_t{}; + } + + static error_or generate(c_types::state& c_state){ + if constexpr ( sizeof...(Requests) > 0 ){ + return generate_member<0>(c_state); + } + + return void_t{}; + } +}; +} + +template +error_or generate_c_interface(typename impl::c_types::state& state){ + auto eov = impl::c_iface_translater::generate(state); + return eov; +} + +error_or generate_iface_example(){ + using Request1 = schema::Struct< + schema::Member, + schema::Member, + schema::Member, "baz"> + >; + using Response1 = schema::Int32; + + using Func1 = schema::Function; + + using Iface = schema::Interface< + schema::Member + >; + + impl::c_types::state c_state; + c_state.prefix = "c_saw"; + c_state.encoding = "saw::encode::Native"; + + typename impl::c_types::c_struct_map& type_map = c_state.struct_map; + typename impl::c_types::c_func_map& func_map = c_state.func_map; + auto eov = generate_c_interface(c_state); + if(eov.is_error()){ + return eov; + } + std::cout<<"Interface: "< generate_array_example(){ + using Schema1 = schema::Array; + + using Schema2 = schema::Array; + + using Schema3 = schema::Array; + + std::string prefix = "c_saw"; + impl::c_types::state c_state; + c_state.prefix = prefix; + { + std::string type_str; + auto eov = impl::c_data_translater::generate(c_state, type_str); + if(eov.is_error()){ + return eov; + } + } + { + std::string type_str; + auto eov = impl::c_data_translater::generate(c_state, type_str); + if(eov.is_error()){ + return eov; + } + } + { + std::string type_str; + auto eov = impl::c_data_translater::generate(c_state, type_str); + if(eov.is_error()){ + return eov; + } + } + + std::cout<<"Prefix: "< + +#include + +namespace saw { +namespace impl { +struct cli_mode { + struct read {}; + struct write {}; +}; + +template +struct cli_traverser { + static_assert(always_false, "Not supported"); +}; + +template +struct cli_modifier { + codec json; + codec encoded; + + error_or read(data& enc_data, std::deque& sch_path, std::string& json_data_str){ + data native; + { + auto eov = encoded.decode(enc_data, native); + if(eov.is_error()){ + return eov; + } + } + { + data json_data; + auto eov = json.encode(native, json_data); + if(eov.is_error()){ + return eov; + } + + json_data_str = convert_to_string(json_data.get_buffer()); + } + + return void_t{}; + } + + error_or write(data& enc_data, std::deque& sch_path, std::string& json_data_str){ + data native; + { + /// @todo string to data + data json_data{ std::string_view{json_data_str} }; + auto eov = json.decode(json_data, native); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = encoded.encode(native, enc_data); + if(eov.is_error()){ + return eov; + } + }i + + return void_t{}; + + } +}; + +template +struct cli_traverser...>, Encoding> { + using Schema = schema::Struct...>; + + template + static error_or traverse(std::deque& sch_path, std::string& json_data){ + if(sch_path.empty()){ + cli_modifier mod; + if constexpr (std::is_same_v){ + return mod.read(sch_path, json_data); + } else if constexpr (std::is_same_v) { + return mod.write(sch_path, json_data); + } else { + return make_error(); + } + } else { + + } + + return void_t{}; + } +}; +} + +template +int modify_data_on_cli(int argc, char** argv){ + + /// @todo parse cli data + bool read_mode = true; + + std::deque sch_path; + std::string json_data; + + if (read_mode) { + auto eov = impl::cli_modifier::traverse(sch_path, json_data); + if(eov.is_error()){ + return -1; + } + } else { + auto eov = impl::cli_modifier::traverse(sch_path, json_data); + if(eov.is_error()){ + return -1; + } + } + + return 0; +} +} -- cgit v1.2.3