26 #include "shell_compiler.hpp"
27 #include "casadi/core/casadi_misc.hpp"
28 #include "casadi/core/casadi_meta.hpp"
29 #include "casadi/core/casadi_logger.hpp"
33 #ifndef OBJECT_FILE_SUFFIX
34 #define OBJECT_FILE_SUFFIX CASADI_OBJECT_FILE_SUFFIX
42 int CASADI_IMPORTER_SHELL_EXPORT
45 plugin->name =
"shell";
47 plugin->version = CASADI_VERSION;
85 "Directory to put temporary objects in. Must end with a file separator."}},
88 "Compiler setup command. Intended to be fixed."
89 " The 'flag' option is the prefered way to set"
93 "Linker setup command. Intended to be fixed."
94 " The 'flag' option is the prefered way to set"
98 "Alias for 'compiler_flags'"}},
101 "Compile flags for the JIT compiler. Default: None"}},
104 "Linker flags for the JIT compiler. Default: None"}},
107 "Cleanup temporary files when unloading. Default: true"}},
108 {
"compiler_output_flag",
110 "Compiler flag to denote object output. Default: '-o '"}},
111 {
"linker_output_flag",
113 "Linker flag to denote shared library output. Default: '-o '"}},
116 "List of suffixes for extra files that the compiler may generate. Default: None"}},
119 "The file name used to write out compiled objects/libraries. "
120 "The actual file names used depend on 'temp_suffix' and include extensions. "
121 "Default: 'tmp_casadi_compiler_shell'"}},
124 "Use a temporary (seemingly random) filename suffix for file names. "
125 "This is desired for thread-safety. "
126 "This behaviour may defeat caching compiler wrappers. "
138 bool temp_suffix =
true;
139 std::string bare_name =
"tmp_casadi_compiler_shell";
140 std::string directory =
"";
142 std::vector<std::string> compiler_flags;
143 std::vector<std::string> linker_flags;
144 std::string suffix = OBJECT_FILE_SUFFIX;
147 std::string compiler =
"cl.exe";
148 std::string linker =
"link.exe";
149 std::string compiler_setup =
"/c";
150 std::string linker_setup =
"/DLL";
151 std::string compiler_output_flag =
"/Fo";
152 std::string linker_output_flag =
"/out:";
155 std::string compiler =
"gcc";
156 std::string linker =
"gcc";
157 std::string compiler_setup =
"-fPIC -c";
158 std::string linker_setup =
"-shared";
159 std::string compiler_output_flag =
"-o ";
160 std::string linker_output_flag =
"-o ";
164 for (
auto&& op : opts) {
165 if (op.first==
"compiler") {
166 compiler = op.second.to_string();
167 }
else if (op.first==
"linker") {
168 linker = op.second.to_string();
169 }
else if (op.first==
"directory") {
170 directory = op.second.to_string();
171 }
else if (op.first==
"compiler_setup") {
172 compiler_setup = op.second.to_string();
173 }
else if (op.first==
"cleanup") {
175 }
else if (op.first==
"linker_setup") {
176 linker_setup = op.second.to_string();
177 }
else if (op.first==
"compiler_flags" || op.first==
"flags") {
178 compiler_flags = op.second;
179 }
else if (op.first==
"linker_flags") {
180 linker_flags = op.second;
181 }
else if (op.first==
"compiler_output_flag") {
182 compiler_output_flag = op.second.to_string();
183 }
else if (op.first==
"linker_output_flag") {
184 linker_output_flag = op.second.to_string();
185 }
else if (op.first==
"extra_suffixes") {
187 }
else if (op.first==
"name") {
188 bare_name = op.second.to_string();
189 }
else if (op.first==
"temp_suffix") {
190 temp_suffix = op.second;
198 obj_name_ = directory + bare_name + suffix;
215 std::stringstream cccmd;
217 for (
auto i=compiler_flags.begin(); i!=compiler_flags.end(); ++i) {
220 cccmd <<
" " << compiler_setup;
223 cccmd <<
" " <<
name_;
226 cccmd <<
" " + compiler_output_flag <<
obj_name_;
229 if (
verbose_) casadi_message(
"calling \"" + cccmd.str() +
"\"");
230 if (system(cccmd.str().c_str())) {
231 casadi_error(
"Compilation failed. Tried \"" + cccmd.str() +
"\"");
235 std::stringstream ldcmd;
242 for (
auto i=linker_flags.begin(); i!=linker_flags.end(); ++i) {
245 ldcmd <<
" " << linker_setup;
248 if (
verbose_) casadi_message(
"calling \"" + ldcmd.str() +
"\"");
249 if (system(ldcmd.str().c_str())) {
250 casadi_error(
"Linking failed. Tried \"" + ldcmd.str() +
"\"");
254 handle_ = open_shared_library(
bin_name_, search_paths,
"ShellCompiler::init");
bool verbose_
Verbose – for debugging purposes.
virtual void init(const Dict &opts)
Initialize.
static const Options options_
Options.
std::string name_
C filename.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
std::string bin_name_
Temporary file.
std::string obj_name_
Temporary file.
static const Options options_
Options.
static ImporterInternal * creator(const std::string &name)
Create a new JIT function.
ShellCompiler(const std::string &name)
Constructor.
std::vector< std::string > extra_suffixes_
Extra files.
static const std::string meta_doc
A documentation string.
~ShellCompiler() override
Destructor.
bool cleanup_
Cleanup temporary files when unloading.
signal_t get_function(const std::string &symname) override
Get a function pointer for numerical evaluation.
void init(const Dict &opts) override
Initialize.
std::string library() const override
Get library name.
std::string temporary_file(const std::string &prefix, const std::string &suffix)
int CASADI_IMPORTER_SHELL_EXPORT casadi_register_importer_shell(ImporterInternal::Plugin *plugin)
std::vector< std::string > get_search_paths()
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void(* signal_t)(void)
Function pointer types for the C API.
bool remove(const std::string &path)
void CASADI_IMPORTER_SHELL_EXPORT casadi_load_importer_shell()
Options metadata for a class.