44import platform
55from dataclasses import dataclass as py_dataclass , is_dataclass as py_is_dataclass
66from goto import with_goto
7- from numpy import get_include
8- from distutils .sysconfig import get_python_inc
97
108# TODO: this does not seem to restrict other imports
119__slots__ = ["i8" , "i16" , "i32" , "i64" , "u8" , "u16" , "u32" , "u64" , "f32" , "f64" , "c32" , "c64" , "CPtr" ,
@@ -572,11 +570,13 @@ def get_data_type(t):
572570 source_code = getsource (function )
573571 source_code = source_code [source_code .find ('\n ' ):]
574572
575- # TODO: Create a filename based on the function name
576- # filename = function.__name__ + ".py"
573+ dir_name = "./lpython_decorator_" + self .fn_name
574+ if not os .path .exists (dir_name ):
575+ os .mkdir (dir_name )
576+ filename = dir_name + "/" + self .fn_name
577577
578578 # Open the file for writing
579- with open ("a .py" , "w" ) as file :
579+ with open (filename + " .py" , "w" ) as file :
580580 # Write the Python source code to the file
581581 file .write ("@ccallable" )
582582 file .write (source_code )
@@ -682,7 +682,7 @@ def get_data_type(t):
682682#include <numpy/ndarrayobject.h>
683683
684684// LPython generated C code
685- #include "a .h"
685+ #include "{ self . fn_name } .h"
686686
687687// Define the Python module and method mappings
688688static PyObject* define_module(PyObject* self, PyObject* args) {{
@@ -700,13 +700,13 @@ def get_data_type(t):
700700// Define the module initialization function
701701static struct PyModuleDef module_def = {{
702702 PyModuleDef_HEAD_INIT,
703- "lpython_jit_module ",
703+ "lpython_module_ { self . fn_name } ",
704704 "Shared library to use LPython generated functions",
705705 -1,
706706 module_methods
707707}};
708708
709- PyMODINIT_FUNC PyInit_lpython_jit_module (void) {{
709+ PyMODINIT_FUNC PyInit_lpython_module_ { self . fn_name } (void) {{
710710 PyObject* module;
711711
712712 // Create the module object
@@ -720,33 +720,41 @@ def get_data_type(t):
720720"""
721721 # ----------------------------------------------------------------------
722722 # Write the C source code to the file
723- with open ("a .c" , "w" ) as file :
723+ with open (filename + " .c" , "w" ) as file :
724724 file .write (template )
725725
726726 # ----------------------------------------------------------------------
727727 # Generate the Shared library
728728 # TODO: Use LLVM instead of C backend
729- r = os .system ("lpython --show-c --disable-main a.py > a.h" )
729+ r = os .system ("lpython --show-c --disable-main "
730+ + filename + ".py > " + filename + ".h" )
730731 assert r == 0 , "Failed to create C file"
732+
731733 gcc_flags = ""
732734 if platform .system () == "Linux" :
733735 gcc_flags = " -shared -fPIC "
734736 elif platform .system () == "Darwin" :
735737 gcc_flags = " -bundle -flat_namespace -undefined suppress "
736738 else :
737739 raise NotImplementedError ("Platform not implemented" )
740+
741+ from numpy import get_include
742+ from distutils .sysconfig import get_python_inc , get_python_lib
738743 python_path = "-I" + get_python_inc () + " "
739- numpy_path = "-I" + get_include ()
744+ numpy_path = "-I" + get_include () + " "
740745 rt_path_01 = "-I" + get_rtlib_dir () + "/../libasr/runtime "
741746 rt_path_02 = "-L" + get_rtlib_dir () + " -Wl,-rpath " \
742747 + get_rtlib_dir () + " -llpython_runtime "
743- python_lib = "-L" "$CONDA_PREFIX/lib/ -lpython3.10 -lm"
748+ python_lib = "-L" + get_python_lib () + "/../.. -lpython3.10 -lm"
749+
744750 r = os .system ("gcc -g" + gcc_flags + python_path + numpy_path +
745- " a.c -o lpython_jit_module.so " + rt_path_01 + rt_path_02 + python_lib )
751+ filename + ".c -o lpython_module_" + self .fn_name + ".so " +
752+ rt_path_01 + rt_path_02 + python_lib )
746753 assert r == 0 , "Failed to create the shared library"
747754
748755 def __call__ (self , * args , ** kwargs ):
749756 import sys ; sys .path .append ('.' )
750757 # import the symbol from the shared library
751- function = getattr (__import__ ("lpython_jit_module" ), self .fn_name )
758+ function = getattr (__import__ ("lpython_module_" + self .fn_name ),
759+ self .fn_name )
752760 return function (* args , ** kwargs )
0 commit comments