diff --git a/.github/workflows/test-meson.yml b/.github/workflows/test-meson.yml index 518106f1..bf827c01 100644 --- a/.github/workflows/test-meson.yml +++ b/.github/workflows/test-meson.yml @@ -65,16 +65,16 @@ jobs: meson setup build meson compile -C build - - name: Build wheels - uses: pypa/cibuildwheel@v2.16.5 - env: - # SETUPTOOLS_USE_DISTUTILS: stdlib - CC: gcc - CXX: g++ - with: - # package-dir: ./src/supy - output-dir: wheelhouse - # config-file: ./src/supy/pyproject.toml + # - name: Build wheels + # uses: pypa/cibuildwheel@v2.16.5 + # env: + # # SETUPTOOLS_USE_DISTUTILS: stdlib + # CC: gcc + # CXX: g++ + # with: + # # package-dir: ./src/supy + # output-dir: wheelhouse + # # config-file: ./src/supy/pyproject.toml # - name: Setup tmate session for debugging # if: failure() diff --git a/src/supy_driver/meson.build b/src/supy_driver/meson.build index 2e74907d..94e17f50 100644 --- a/src/supy_driver/meson.build +++ b/src/supy_driver/meson.build @@ -35,7 +35,7 @@ fpp_flags = ['-E', '-x', 'f95-cpp-input', '-fPIC'] # include the SUEWS library dir_suews = meson.current_source_dir() / '../suews' -include_dir_suews = include_directories( '../suews/mod') +include_dir_suews = include_directories('../suews/mod') dir_src_suews = dir_suews / 'src' @@ -134,52 +134,65 @@ endforeach prog_f90wrap = find_program('f90wrap') file_kind_map = files('kind_map') +# gen_f90wrap_f90 = custom_target( +# 'f90wrap_f90', +# command: [ +# prog_f90wrap, +# '-m', 'supy_driver', +# '@INPUT@', +# '-k', file_kind_map, +# '-v', +# '--skip', 'error_hint', +# '&&', +# py, +# '@CURRENT_SOURCE_DIR@/move_output_gen.py', +# '@OUTPUT@', +# '@OUTDIR@', +# ], +# input: [gen_fpp_files], +# build_by_default: true, +# output: [out_wrap_f90], +# depends: build_suews, +# ) gen_f90wrap_f90 = custom_target( - 'f90wrap_f90', command: [ - prog_f90wrap, - '-m', 'supy_driver', - '@INPUT@', - '-k', file_kind_map, - '-v', - '--skip', 'error_hint', - '&&', py, - '@CURRENT_SOURCE_DIR@/move_output_gen.py', - '@OUTPUT@', + '@CURRENT_SOURCE_DIR@/run_f90wrap.py', # Path to your Python wrapper script + prog_f90wrap, # Assuming this is defined elsewhere as the f2py executable + 'supy_driver', + '@CURRENT_SOURCE_DIR@', '@OUTDIR@', + '@INPUT@', + '@OUTPUT@', ], - input: [gen_fpp_files, 'move_output_gen.py'], - build_by_default: true, + input: [gen_fpp_files], output: [out_wrap_f90], depends: build_suews, + build_by_default: true, ) ################################################################## # 3. use f2py-f90wrap to generate the C file for the python interface # build the driver module c file prog_f2py_f90wrap = find_program('f2py-f90wrap') -# message('Output files will be generated at:', meson.current_build_dir()) gen_module_c = custom_target( - output: [ - '_supy_drivermodule.c', - '_supy_driver-f2pywrappers.f', - ], command: [ - prog_f2py_f90wrap, - '-m', '_supy_driver', - '@INPUT@', - # IMPORTANT: lower case the module name - '--lower', '&&', py, - '@CURRENT_SOURCE_DIR@/move_output_gen.py', - '@OUTPUT@', + '@CURRENT_SOURCE_DIR@/run_f2py.py', # Path to your Python wrapper script + prog_f2py_f90wrap, # Assuming this is defined elsewhere as the f2py executable + '_supy_driver', + '@CURRENT_SOURCE_DIR@', '@OUTDIR@', + '@INPUT@', + '@OUTPUT@', ], input: [gen_f90wrap_f90], + output: [ + '_supy_drivermodule.c', + '_supy_driver-f2pywrappers.f', + ], depends: [gen_f90wrap_f90], build_by_default: true, - ) ################################################################## @@ -224,7 +237,7 @@ dep_fortranobject = declare_dependency( dep_lib_src = declare_dependency( link_args: [ - '-L'+meson.current_build_dir(), # the path is relative to the build directory when running meson + '-L' + meson.current_build_dir(), # the path is relative to the build directory when running meson '-lsrc', ], ) @@ -237,7 +250,7 @@ dep_lib_suews = declare_dependency( ) dep_suews = [dep_lib_src, dep_lib_suews] -inc_suews_mod = include_directories( '../suews/mod') +inc_suews_mod = include_directories('../suews/mod') ext_supy_driver = py.extension_module( '_supy_driver', @@ -260,22 +273,17 @@ ext_supy_driver = py.extension_module( subdir: 'supy', ) +# move the generated `supy_driver.py` to the correct directory +# this is kind of dummy, but it is necessary to make the installation work gen_supy_driver_py = custom_target( 'supy_driver', command: [ - prog_f90wrap, - '-m', 'supy_driver', - '@INPUT@', - '-k', file_kind_map, - '-v', - '--skip', 'error_hint', - '&&', py, '@CURRENT_SOURCE_DIR@/move_output_gen.py', - '@OUTPUT@', + meson.current_build_dir() / 'supy_driver.py', # this file is actually generated by gen_f90wrap_f90 '@OUTDIR@', ], - input: [gen_fpp_files, 'move_output_gen.py'], + input: [gen_fpp_files], build_by_default: true, output: ['supy_driver.py'], depends: ext_supy_driver, @@ -283,13 +291,12 @@ gen_supy_driver_py = custom_target( install_dir: [py.get_install_dir() / 'supy'], # this is the directory where the python module will be installed ) -################################################################## -# 5. add the test for the python module +# ################################################################## +# # 5. add the test for the python module -# add test for the python module -test( - 'test_supy_driver', - py, - args: ['-c', 'import supy_driver; print(dir(supy_driver))'], - depends: ext_supy_driver, -) \ No newline at end of file +# # add test for the python module +# test( +# 'test_supy_driver', +# py, +# args: ['-c', 'import supy_driver; print(dir(supy_driver))'], +# depends: ext_supy_driver, \ No newline at end of file diff --git a/src/supy_driver/run_f2py.py b/src/supy_driver/run_f2py.py new file mode 100644 index 00000000..d124854d --- /dev/null +++ b/src/supy_driver/run_f2py.py @@ -0,0 +1,51 @@ +import subprocess +import sys +import os + + +def main(): + f2py_executable = sys.argv[1] # path to f2py or f2py part of numpy + module_name = sys.argv[2] + current_source_dir = sys.argv[3] + output_dir = sys.argv[4] + input_output_files = sys.argv[5:] + input_files = input_output_files[0:-2] + output_files = input_output_files[-2:] + # output_files = sys.argv[5:] # List of output file paths + + # Call f2py to generate the modules + try: + subprocess.check_call( + [ + f2py_executable, + "-m", + module_name, + *input_files, + "--lower", + ] + ) + print("f2py call successful") + except subprocess.CalledProcessError as e: + print(f"Error calling f2py: {e}") + return 1 + + # Move generated files to the output directory + try: + subprocess.check_call( + [ + "python", + os.path.join(current_source_dir, "move_output_gen.py"), + *output_files, + output_dir, + ] + ) + print("Output files moved successfully") + except subprocess.CalledProcessError as e: + print(f"Error moving output files: {e}") + return 1 + + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/supy_driver/run_f90wrap.py b/src/supy_driver/run_f90wrap.py new file mode 100644 index 00000000..e5d19f06 --- /dev/null +++ b/src/supy_driver/run_f90wrap.py @@ -0,0 +1,58 @@ +import subprocess +import sys +import os + + +def main(): + f90wrap_executable = sys.argv[1] # path to f2py or f2py part of numpy + module_name = sys.argv[2] + current_source_dir = sys.argv[3] + output_dir = sys.argv[4] + input_output_files = sys.argv[5:] + # only .fpp files are input files + input_files = [f for f in input_output_files if f.endswith(".fpp")] + # only .f90 files are output files + output_files = [f for f in input_output_files if f.endswith(".f90")] + # input_files = input_output_files[0:-2] + # output_files = input_output_files[-2:] + # output_files = sys.argv[5:] # List of output file paths + + # Call f2py to generate the modules + try: + subprocess.check_call( + [ + f90wrap_executable, + "-m", + module_name, + *input_files, + "-k", + os.path.join(current_source_dir, "kind_map"), + "--skip", + "error_hint", + ] + ) + print("f90wrap call successful") + except subprocess.CalledProcessError as e: + print(f"Error calling f2py: {e}") + return 1 + + # Move generated files to the output directory + try: + subprocess.check_call( + [ + "python", + os.path.join(current_source_dir, "move_output_gen.py"), + *output_files, + output_dir, + ] + ) + print("Output files moved successfully") + except subprocess.CalledProcessError as e: + print(f"Error moving output files: {e}") + return 1 + + return 0 + + +if __name__ == "__main__": + sys.exit(main())