diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index a3b9a7eb4b..0078fe99fb 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -1132,7 +1132,7 @@ int compile_python_using_llvm( LCompilers::LPython::DynamicLibrary cpython_lib; LCompilers::LPython::DynamicLibrary symengine_lib; - if (compiler_options.enable_cpython) { + if (compiler_options.po.enable_cpython) { LCompilers::LPython::open_cpython_library(cpython_lib); } if (compiler_options.enable_symengine) { @@ -1151,7 +1151,7 @@ int compile_python_using_llvm( e.execfn("__module___main_____main__global_stmts"); } - if (compiler_options.enable_cpython) { + if (compiler_options.po.enable_cpython) { LCompilers::LPython::close_cpython_library(cpython_lib); } if (compiler_options.enable_symengine) { @@ -1921,7 +1921,7 @@ int main(int argc, char *argv[]) app.add_flag("--dump-all-passes", compiler_options.po.dump_all_passes, "Apply all the passes and dump the ASR into a file"); app.add_flag("--dump-all-passes-fortran", compiler_options.po.dump_fortran, "Apply all passes and dump the ASR after each pass into fortran file"); app.add_flag("--cumulative", compiler_options.po.pass_cumulative, "Apply all the passes cumulatively till the given pass"); - app.add_flag("--enable-cpython", compiler_options.enable_cpython, "Enable CPython runtime"); + app.add_flag("--enable-cpython", compiler_options.po.enable_cpython, "Enable CPython runtime"); app.add_flag("--enable-symengine", compiler_options.enable_symengine, "Enable Symengine runtime"); app.add_flag("--link-numpy", compiler_options.link_numpy, "Enable NumPy runtime (implies --enable-cpython)"); app.add_flag("--separate-compilation", separate_compilation, "Generates unique names for all the symbols"); @@ -1983,7 +1983,7 @@ int main(int argc, char *argv[]) } if (compiler_options.link_numpy) { - compiler_options.enable_cpython = true; + compiler_options.po.enable_cpython = true; } if (arg_version) { diff --git a/src/libasr/codegen/asr_to_c.cpp b/src/libasr/codegen/asr_to_c.cpp index 381e3c7902..ca0178ac89 100644 --- a/src/libasr/codegen/asr_to_c.cpp +++ b/src/libasr/codegen/asr_to_c.cpp @@ -820,7 +820,7 @@ R"( } std::string body; - if (compiler_options.enable_cpython) { + if (compiler_options.po.enable_cpython) { headers.insert("Python.h"); body += R"( Py_Initialize(); @@ -851,7 +851,7 @@ R"( // Initialise Numpy body += src; } - if (compiler_options.enable_cpython) { + if (compiler_options.po.enable_cpython) { body += R"( if (Py_FinalizeEx() < 0) { fprintf(stderr,"BindPython: Unknown Error\n"); diff --git a/src/libasr/pass/python_bind.cpp b/src/libasr/pass/python_bind.cpp index 47d171fba4..5a668a739e 100644 --- a/src/libasr/pass/python_bind.cpp +++ b/src/libasr/pass/python_bind.cpp @@ -387,6 +387,11 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt return; } + if (!pass_options.enable_cpython) { + // python_bind pass is skipped if CPython is not enabled + return; + } + std::vector fns; fns.push_back({"Py_Initialize", {}, ASRUtils::VOID}); fns.push_back({"Py_IsInitialized", {}, ASRUtils::I32}); @@ -412,21 +417,17 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt fns.push_back({"PyBool_FromLong", {ASRUtils::I32}, ASRUtils::PTR}); fns.push_back({"PyObject_IsTrue", {ASRUtils::PTR}, ASRUtils::I32}); - bool included_cpython_funcs = false; - Location *l = al.make_new(); l->first = 0; l->last = 0; + ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h"); + for (auto &item : unit.m_symtab->get_scope()) { if (ASR::is_a(*item.second)) { ASR::Function_t *f = ASR::down_cast(item.second); if (ASRUtils::get_FunctionType(f)->m_abi == ASR::abiType::BindPython) { if (f->n_body == 0 && f->m_module_file) { - if (!included_cpython_funcs) { - ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h"); - included_cpython_funcs = true; - } generate_body(al, *f, *unit.m_symtab); } } @@ -438,10 +439,6 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt ASR::Function_t *f = ASR::down_cast(module_item.second); if (ASRUtils::get_FunctionType(f)->m_abi == ASR::abiType::BindPython) { if (f->n_body == 0 && f->m_module_file) { - if (!included_cpython_funcs) { - ASRUtils::declare_functions(al, fns, *l, unit.m_symtab, "Python.h"); - included_cpython_funcs = true; - } generate_body(al, *f, *module->m_symtab); } } @@ -451,10 +448,8 @@ void pass_python_bind(Allocator &al, ASR::TranslationUnit_t &unit, const PassOpt } - if (included_cpython_funcs) { - PassUtils::UpdateDependenciesVisitor u(al); - u.visit_TranslationUnit(unit); - } + PassUtils::UpdateDependenciesVisitor u(al); + u.visit_TranslationUnit(unit); } } // namespace LCompilers diff --git a/src/libasr/utils.h b/src/libasr/utils.h index 018c0afefe..97417b2bf4 100644 --- a/src/libasr/utils.h +++ b/src/libasr/utils.h @@ -59,6 +59,7 @@ struct PassOptions { bool tree = false; bool with_intrinsic_mods = false; bool c_mangling = false; + bool enable_cpython = false; bool c_skip_bindpy_pass = false; }; @@ -99,7 +100,6 @@ struct CompilerOptions { std::string arg_o = ""; bool emit_debug_info = false; bool emit_debug_line_column = false; - bool enable_cpython = false; bool enable_symengine = false; bool link_numpy = false; bool run = false;