def __generate_memory_code(self, cost=None, grad=None, f1=None, f2=None): """ Creates file casadi_memory.h with memory sizes :param cost: cost function (cs.Function) :param grad: grad function (cs.Function) :param f1: mapping F1 (cs.Function) :param f2: mapping F2 (cs.Function) """ self.__logger.info("Generating casadi_memory.h") file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader) template = env.get_template('casadi_memory.h.template') output_template = template.render( cost=cost, grad=grad, f1=f1, f2=f2, build_config=self.__build_config, meta=self.__meta, timestamp_created=datetime.datetime.now()) memory_path = os.path.abspath( os.path.join(self.__icasadi_target_dir(), "extern/casadi_memory.h")) with open(memory_path, "w") as fh: fh.write(output_template)
def __generate_code_tcp_interface(self): logging.info("Generating code for TCP/IP interface (tcp_iface/src/main.rs)") logging.info("TCP server will bind at %s:%d", self.__build_config.tcp_interface_config.bind_ip, self.__build_config.tcp_interface_config.bind_port) target_dir = self.__target_dir() tcp_iface_dir = os.path.join(target_dir, "tcp_iface") tcp_iface_source_dir = os.path.join(tcp_iface_dir, "src") # make tcp_iface/ and tcp_iface/src make_dir_if_not_exists(tcp_iface_dir) make_dir_if_not_exists(tcp_iface_source_dir) # generate main.rs for tcp_iface file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader) template = env.get_template('tcp_server.rs.template') output_template = template.render(meta=self.__meta, tcp_server_config=self.__build_config.tcp_interface_config, timestamp_created=datetime.datetime.now()) target_scr_lib_rs_path = os.path.join(tcp_iface_source_dir, "main.rs") with open(target_scr_lib_rs_path, "w") as fh: fh.write(output_template) # generate Cargo.toml for tcp_iface template = env.get_template('tcp_server_cargo.toml.template') output_template = template.render(meta=self.__meta, timestamp_created=datetime.datetime.now()) target_scr_lib_rs_path = os.path.join(tcp_iface_dir, "Cargo.toml") with open(target_scr_lib_rs_path, "w") as fh: fh.write(output_template)
def __generate_c_bindings_makefile(self): self.__logger.info("Generating CMakeLists (do: `cmake .; make` to compile the autogenerated example)") target_dir = self.__target_dir() file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader, autoescape=True) template = env.get_template('example_cmakelists.template') output_template = template.render(meta=self.__meta, build_config=self.__build_config) target_scr_lib_rs_path = os.path.join(target_dir, "CMakeLists.txt") with open(target_scr_lib_rs_path, "w") as fh: fh.write(output_template)
def __generate_build_rs(self): logging.info("Generating build.rs for target optimizer") target_dir = self.__target_dir() file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader) template = env.get_template('optimizer_build.rs.template') output_template = template.render(meta=self.__meta, activate_clib_generation=self.__build_config.build_c_bindings) target_scr_lib_rs_path = os.path.join(target_dir, "build.rs") with open(target_scr_lib_rs_path, "w") as fh: fh.write(output_template)
def __copy__ros_files(self): self.__logger.info("Copying external dependencies") # 1. --- copy header file target_ros_dir = self.__ros_target_dir() header_file_name = self.__meta.optimizer_name + '_bindings.hpp' target_include_filename = os.path.abspath( os.path.join(target_ros_dir, 'include', header_file_name)) original_include_file = os.path.abspath( os.path.join(self.__target_dir(), header_file_name)) shutil.copyfile(original_include_file, target_include_filename) # 2. --- copy library file lib_file_name = 'lib' + self.__meta.optimizer_name + '.a' target_lib_file_name = \ os.path.abspath( os.path.join( target_ros_dir, 'extern_lib', lib_file_name)) original_lib_file = os.path.abspath( os.path.join(self.__target_dir(), 'target', self.__build_config.build_mode, lib_file_name)) shutil.copyfile(original_lib_file, target_lib_file_name) # 3. --- copy msg file OptimizationParameters.msg original_params_msg = os.path.abspath( os.path.join(og_dfn.templates_dir(), 'ros', 'OptimizationParameters.msg')) target_params_msg = \ os.path.abspath( os.path.join( target_ros_dir, 'msg', 'OptimizationParameters.msg')) shutil.copyfile(original_params_msg, target_params_msg) # 4. --- copy msg file OptimizationResult.msg original_result_msg = os.path.abspath( os.path.join(og_dfn.templates_dir(), 'ros', 'OptimizationResult.msg')) target_result_msg = \ os.path.abspath( os.path.join( target_ros_dir, 'msg', 'OptimizationResult.msg')) shutil.copyfile(original_result_msg, target_result_msg)
def __generate_c_bindings_example(self): self.__logger.info("Generating example_optimizer.c (C bindings example for your convenience)") target_dir = self.__target_dir() file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader, autoescape=True) template = env.get_template('example_optimizer_c_bindings.c.template') output_template = template.render(meta=self.__meta, build_config=self.__build_config, problem=self.__problem) target_scr_lib_rs_path = os.path.join(target_dir, "example_optimizer.c") with open(target_scr_lib_rs_path, "w") as fh: fh.write(output_template)
def __generate_icasadi_cargo_toml(self): """ Generate icasadi's Cargo.toml file """ self.__logger.info("Generating icasadi's Cargo.toml") file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader, autoescape=True) template = env.get_template('icasadi_cargo.toml.template') output_template = template.render(meta=self.__meta) icallocator_path = os.path.abspath( os.path.join(self.__icasadi_target_dir(), "Cargo.toml")) with open(icallocator_path, "w") as fh: fh.write(output_template)
def __generate_cargo_toml(self): """Generates Cargo.toml for generated project""" logging.info("Generating Cargo.toml for target optimizer") target_dir = self.__target_dir() file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader) template = env.get_template('optimizer_cargo.toml.template') output_template = template.render( meta=self.__meta, open_version=self.__build_config.open_version, activate_tcp_server=self.__build_config.tcp_interface_config is not None, activate_clib_generation=self.__build_config.build_c_bindings) cargo_toml_path = os.path.abspath(os.path.join(target_dir, "Cargo.toml")) with open(cargo_toml_path, "w") as fh: fh.write(output_template)
def __generate_main_project_code(self): logging.info("Generating main code for target optimizer (lib.rs)") target_dir = self.__target_dir() file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader) template = env.get_template('optimizer.rs.template') output_template = template.render(solver_config=self.__solver_config, meta=self.__meta, problem=self.__problem, timestamp_created=datetime.datetime.now(), activate_clib_generation=self.__build_config.build_c_bindings) target_source_path = os.path.join(target_dir, "src"); target_scr_lib_rs_path = os.path.join(target_source_path, "lib.rs") make_dir_if_not_exists(target_source_path) with open(target_scr_lib_rs_path, "w") as fh: fh.write(output_template)
def __generate_icasadi_lib(self): """Generates the Rust library file of icasadi Generates src/lib.rs """ logging.info("Generating icasadi Rust library file") file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader) template = env.get_template('icasadi_lib.rs.template') output_template = template.render(meta=self.__meta, problem=self.__problem, build_config=self.__build_config, timestamp_created=datetime.datetime.now()) icasadi_lib_rs_path = os.path.abspath( os.path.join(self.__icasadi_target_dir(), "src/lib.rs")) with open(icasadi_lib_rs_path, "w") as fh: fh.write(output_template)
def __generate_icasadi_c_interface(self): """ Generates the C interface file interface.c """ self.__logger.info("Generating intercafe.c (C interface)") file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader, autoescape=True) template = env.get_template('interface.c.template') output_template = template.render(meta=self.__meta, problem=self.__problem, build_config=self.__build_config, timestamp_created=datetime.datetime.now()) icallocator_path = os.path.abspath( os.path.join(self.__icasadi_target_dir(), "extern/interface.c")) with open(icallocator_path, "w") as fh: fh.write(output_template)
def __generate_code_python_bindings(self): self.__logger.info("Generating code for Python bindings") target_dir = self.__target_dir() python_bindings_dir_name = _PYTHON_BINDINGS_PREFIX + self.__meta.optimizer_name python_bindings_dir = os.path.join(target_dir, python_bindings_dir_name) python_bindings_source_dir = os.path.join(python_bindings_dir, "src") self.__logger.info( f"Generating code for Python bindings in {python_bindings_dir}") # make python_bindings/ and python_bindings/src make_dir_if_not_exists(python_bindings_dir) make_dir_if_not_exists(python_bindings_source_dir) # generate tcp_server.rs for python_bindings python_rs_template = OpEnOptimizerBuilder.__get_template( 'python_bindings.rs', 'python') python_rs_output_template = python_rs_template.render( meta=self.__meta, timestamp_created=datetime.datetime.now()) target_python_rs_path = os.path.join(python_bindings_source_dir, "lib.rs") with open(target_python_rs_path, "w") as fh: fh.write(python_rs_output_template) # generate Cargo.toml for python_bindings python_rs_template = OpEnOptimizerBuilder.__get_template( 'python_bindings_cargo.toml', 'python') python_rs_output_template = python_rs_template.render( meta=self.__meta, build_config=self.__build_config, timestamp_created=datetime.datetime.now()) target_python_rs_path = os.path.join(python_bindings_dir, "Cargo.toml") with open(target_python_rs_path, "w") as fh: fh.write(python_rs_output_template) # move cargo_config into .cargo/config target_cargo_config_dir = os.path.join(python_bindings_dir, '.cargo') make_dir_if_not_exists(target_cargo_config_dir) cargo_config_file = os.path.join(og_dfn.templates_dir(), 'python', 'cargo_config') shutil.copy(cargo_config_file, os.path.join(target_cargo_config_dir, 'config'))
def get_template(name): file_loader = jinja2.FileSystemLoader(og_dfn.templates_dir()) env = jinja2.Environment(loader=file_loader, autoescape=True) return env.get_template(name)