|
1 | 1 | import subprocess
|
2 | 2 | import shutil
|
3 |
| -import datetime |
4 | 3 | import yaml
|
5 |
| -import warnings |
6 | 4 |
|
7 | 5 | import opengen.config as og_cfg
|
8 | 6 | import opengen.definitions as og_dfn
|
@@ -809,6 +807,38 @@ def __info(self):
|
809 | 807 | }
|
810 | 808 | return info
|
811 | 809 |
|
| 810 | + def __casadi_make_static(self): |
| 811 | + """Makes some casadi functions static to avoid clashes (see #362) |
| 812 | + """ |
| 813 | + self.__logger.info("Making CasADi functions static") |
| 814 | + |
| 815 | + def replace_in_casadi_file(casadi_source_fname, function_names): |
| 816 | + # Read casadi_source_fname, line by line, replace, write to destination |
| 817 | + # Open the source file in read mode |
| 818 | + # Replace and write to a different file (with extension .tmp) |
| 819 | + with open(casadi_source_fname, 'r') as fin, open(f"{casadi_source_fname}.tmp", 'w') as fout: |
| 820 | + for line_in in fin: |
| 821 | + for fnc in function_names: |
| 822 | + line_in = line_in.replace( |
| 823 | + f"casadi_real {fnc}", f"static casadi_real {fnc}") |
| 824 | + fout.write(line_in) |
| 825 | + # Move the .tmp file to replace the original one |
| 826 | + shutil.move(f"{casadi_source_fname}.tmp", f"{casadi_source_fname}") |
| 827 | + |
| 828 | + # Folder with external CasADi files (auto-generated C code) |
| 829 | + icasadi_extern_dir = os.path.join( |
| 830 | + self.__icasadi_target_dir(), "extern") # casadi extern folder |
| 831 | + # Function to make static |
| 832 | + fncs_list = ["casadi_sq", "casadi_fmax", |
| 833 | + "casadi_fmin", "casadi_hypot", "casadi_sign", |
| 834 | + "casadi_log1p", "casadi_expm1"] |
| 835 | + # make static |
| 836 | + for casadi_fname in [_AUTOGEN_COST_FNAME, _AUTOGEN_ALM_MAPPING_F1_FNAME, |
| 837 | + _AUTOGEN_GRAD_FNAME, _AUTOGEN_PNLT_CONSTRAINTS_FNAME, |
| 838 | + _AUTOGEN_PRECONDITIONING_FNAME]: |
| 839 | + replace_in_casadi_file(os.path.join( |
| 840 | + icasadi_extern_dir, casadi_fname), fncs_list) |
| 841 | + |
812 | 842 | def build(self):
|
813 | 843 | """Generate code and build project
|
814 | 844 |
|
@@ -837,6 +867,7 @@ def build(self):
|
837 | 867 | self.__generate_main_project_code()
|
838 | 868 | self.__generate_build_rs() # generate build.rs file
|
839 | 869 | self.__generate_yaml_data_file() # create YAML file with metadata
|
| 870 | + self.__casadi_make_static() # make casadi functions static |
840 | 871 |
|
841 | 872 | if not self.__generate_not_build:
|
842 | 873 | self.__logger.info("Building optimizer")
|
|
0 commit comments