4
4
import platform
5
5
from dataclasses import dataclass as py_dataclass , is_dataclass as py_is_dataclass
6
6
from goto import with_goto
7
- from numpy import get_include
8
- from distutils .sysconfig import get_python_inc
9
7
10
8
# TODO: this does not seem to restrict other imports
11
9
__slots__ = ["i8" , "i16" , "i32" , "i64" , "u8" , "u16" , "u32" , "u64" , "f32" , "f64" , "c32" , "c64" , "CPtr" ,
@@ -572,11 +570,13 @@ def get_data_type(t):
572
570
source_code = getsource (function )
573
571
source_code = source_code [source_code .find ('\n ' ):]
574
572
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
577
577
578
578
# Open the file for writing
579
- with open ("a .py" , "w" ) as file :
579
+ with open (filename + " .py" , "w" ) as file :
580
580
# Write the Python source code to the file
581
581
file .write ("@ccallable" )
582
582
file .write (source_code )
@@ -682,7 +682,7 @@ def get_data_type(t):
682
682
#include <numpy/ndarrayobject.h>
683
683
684
684
// LPython generated C code
685
- #include "a .h"
685
+ #include "{ self . fn_name } .h"
686
686
687
687
// Define the Python module and method mappings
688
688
static PyObject* define_module(PyObject* self, PyObject* args) {{
@@ -700,13 +700,13 @@ def get_data_type(t):
700
700
// Define the module initialization function
701
701
static struct PyModuleDef module_def = {{
702
702
PyModuleDef_HEAD_INIT,
703
- "lpython_jit_module ",
703
+ "lpython_module_ { self . fn_name } ",
704
704
"Shared library to use LPython generated functions",
705
705
-1,
706
706
module_methods
707
707
}};
708
708
709
- PyMODINIT_FUNC PyInit_lpython_jit_module (void) {{
709
+ PyMODINIT_FUNC PyInit_lpython_module_ { self . fn_name } (void) {{
710
710
PyObject* module;
711
711
712
712
// Create the module object
@@ -720,33 +720,41 @@ def get_data_type(t):
720
720
"""
721
721
# ----------------------------------------------------------------------
722
722
# Write the C source code to the file
723
- with open ("a .c" , "w" ) as file :
723
+ with open (filename + " .c" , "w" ) as file :
724
724
file .write (template )
725
725
726
726
# ----------------------------------------------------------------------
727
727
# Generate the Shared library
728
728
# 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" )
730
731
assert r == 0 , "Failed to create C file"
732
+
731
733
gcc_flags = ""
732
734
if platform .system () == "Linux" :
733
735
gcc_flags = " -shared -fPIC "
734
736
elif platform .system () == "Darwin" :
735
737
gcc_flags = " -bundle -flat_namespace -undefined suppress "
736
738
else :
737
739
raise NotImplementedError ("Platform not implemented" )
740
+
741
+ from numpy import get_include
742
+ from distutils .sysconfig import get_python_inc , get_python_lib
738
743
python_path = "-I" + get_python_inc () + " "
739
- numpy_path = "-I" + get_include ()
744
+ numpy_path = "-I" + get_include () + " "
740
745
rt_path_01 = "-I" + get_rtlib_dir () + "/../libasr/runtime "
741
746
rt_path_02 = "-L" + get_rtlib_dir () + " -Wl,-rpath " \
742
747
+ 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
+
744
750
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 )
746
753
assert r == 0 , "Failed to create the shared library"
747
754
748
755
def __call__ (self , * args , ** kwargs ):
749
756
import sys ; sys .path .append ('.' )
750
757
# 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 )
752
760
return function (* args , ** kwargs )
0 commit comments