Skip to content

Commit

Permalink
finalized option for installing without a fortran compiler
Browse files Browse the repository at this point in the history
Cleaned a bit the imports for fortran modules.

This needs to be streamlined in another way.

Possibly a template module should be present which adds
the required routines and raising an error.

A mock-like package.

Running tests without fortran will not skip the f-tests.
Should be done.

Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Apr 19, 2023
1 parent 94a101f commit c346788
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 79 deletions.
139 changes: 78 additions & 61 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ if(SKBUILD)
message(STATUS "Building with scikit-build-core: ${SKBUILD_CORE_VERSION}")
endif()


project(sisl
DESCRIPTION "Cythonized or fortran codes"
HOMEPAGE_URL "https://github.com/zerothi/sisl"
LANGUAGES C CXX Fortran
LANGUAGES C CXX
# cmake requires a version of X.Y.Z or some other form, it does not accept setuptools_scm dirty tags
# VERSION "${SKBUILD_PROJECT_VERSION}"
)
Expand Down Expand Up @@ -38,42 +39,7 @@ add_compile_definitions(CYTHON_NO_PYINIT_EXPORT=1)
set(CMAKE_SHARED_MODULE_PREFIX "")


# Retrieve header information
# Define f2py executable
set(F2PY_EXECUTABLE "${PYTHON_EXECUTABLE}" -m numpy.f2py)

# Get numpy.f2py header
set(Python_NumPy_F2Py_INCLUDE_DIR)
foreach(dir IN LISTS Python_NumPy_INCLUDE_DIRS)
if(IS_DIRECTORY "${dir}/../../f2py/src")
set(Python_NumPy_F2Py_INCLUDE_DIR "${dir}/../../f2py/src")
break()
endif()
endforeach()


if(NOT Python_NumPy_F2Py_INCLUDE_DIR)
message(STATUS "Could not locate f2py sources in ${NumPy_NumPy_INCLUDE_DIRS}/../../f2py/src/ trying numpy.f2py.get_include()")
execute_process(COMMAND
"${PYTHON_EXECUTABLE}" -c "import numpy.f2py; print(numpy.f2py.get_include())"
OUTPUT_VARIABLE Python_NumPy_F2Py_INCLUDE_DIR
RESULT_VARIABLE NumPy_F2Py_ERR
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NumPy_F2Py_ERR)
message(SEND_ERROR "ERROR: NumPy.F2PY header not found (${err}), looked in: ${NumPy_NumPy_INCLUDE_DIRS}/../../f2py/src.")
endif()
endif()

list(POP_BACK CMAKE_MESSAGE_INDENT)


# f2py requires a basic object
add_library(f2py_fortranobject OBJECT "${Python_NumPy_F2Py_INCLUDE_DIR}/fortranobject.c")
target_link_libraries(f2py_fortranobject PUBLIC Python::NumPy)
target_include_directories(f2py_fortranobject PUBLIC "${Python_NumPy_F2Py_INCLUDE_DIR}")
set_property(TARGET f2py_fortranobject PROPERTY POSITION_INDEPENDENT_CODE ON)
set(F2PY_LIBRARIES f2py_fortranobject)

# Define all options for the user

set(F2PY_REPORT_ON_ARRAY_COPY 10
CACHE STRING
Expand All @@ -85,13 +51,6 @@ option(WITH_F2PY_REPORT_EXIT
option(WITH_FORTRAN
"Whether to compile and ship fortran sources (default True)" TRUE)

if(WITH_F2PY_REPORT_EXIT)
target_compile_definitions(f2py_fortranobject PRIVATE F2PY_REPORT_ATEXIT)
endif()
if(WITH_F2PY_REPORT_COPY)
target_compile_definitions(f2py_fortranobject PRIVATE F2PY_REPORT_ON_ARRAY_COPY=${F2PY_REPORT_ON_ARRAY_COPY})
endif()


#option(WITH_COVERAGE
# "Add instructions for coverage in Cython files" FALSE)
Expand All @@ -103,7 +62,56 @@ option(WITH_GDB
"Add GDB-enabled Cython sources" FALSE)


# Parse options to global options
# Decide for fortran stuff
if(WITH_FORTRAN)

# add the language
enable_language(Fortran)

# Retrieve header information
# Define f2py executable
set(F2PY_EXECUTABLE "${PYTHON_EXECUTABLE}" -m numpy.f2py)

# Get numpy.f2py header
set(Python_NumPy_F2Py_INCLUDE_DIR)
foreach(dir IN LISTS Python_NumPy_INCLUDE_DIRS)
if(IS_DIRECTORY "${dir}/../../f2py/src")
set(Python_NumPy_F2Py_INCLUDE_DIR "${dir}/../../f2py/src")
break()
endif()
endforeach()


if(NOT Python_NumPy_F2Py_INCLUDE_DIR)
message(STATUS "Could not locate f2py sources in ${NumPy_NumPy_INCLUDE_DIRS}/../../f2py/src/ trying numpy.f2py.get_include()")
execute_process(COMMAND
"${PYTHON_EXECUTABLE}" -c "import numpy.f2py; print(numpy.f2py.get_include())"
OUTPUT_VARIABLE Python_NumPy_F2Py_INCLUDE_DIR
RESULT_VARIABLE NumPy_F2Py_ERR
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NumPy_F2Py_ERR)
message(SEND_ERROR "ERROR: NumPy.F2PY header not found (${err}), looked in: ${NumPy_NumPy_INCLUDE_DIRS}/../../f2py/src.")
endif()
endif()

# f2py requires a basic object
add_library(f2py_fortranobject OBJECT "${Python_NumPy_F2Py_INCLUDE_DIR}/fortranobject.c")
target_link_libraries(f2py_fortranobject PUBLIC Python::NumPy)
target_include_directories(f2py_fortranobject PUBLIC "${Python_NumPy_F2Py_INCLUDE_DIR}")
set_property(TARGET f2py_fortranobject PROPERTY POSITION_INDEPENDENT_CODE ON)
set(F2PY_LIBRARIES f2py_fortranobject)

if(WITH_F2PY_REPORT_EXIT)
target_compile_definitions(f2py_fortranobject PRIVATE F2PY_REPORT_ATEXIT)
endif()
if(WITH_F2PY_REPORT_COPY)
target_compile_definitions(f2py_fortranobject PRIVATE F2PY_REPORT_ON_ARRAY_COPY=${F2PY_REPORT_ON_ARRAY_COPY})
endif()

endif(WITH_FORTRAN)


# Parse Cython global options
if(WITH_ANNOTATE)
list(APPEND CYTHON_FLAGS --annotate)
endif()
Expand All @@ -114,41 +122,50 @@ if(WITH_GDB)
list(APPEND CYTHON_FLAGS --gdb)
endif()

# Add definitions for f2py sources
message(STATUS "sisl options")
list(APPEND CMAKE_MESSAGE_INDENT " ")

cmake_print_variables(WITH_COVERAGE)
cmake_print_variables(WITH_ANNOTATE)
cmake_print_variables(WITH_LINE_DIRECTIVES)
cmake_print_variables(WITH_GDB)

cmake_print_variables(WITH_FORTRAN)
if(WITH_FORTRAN)
cmake_print_variables(WITH_F2PY_REPORT_COPY)
if(WITH_F2PY_REPORT_COPY)
cmake_print_variables(F2PY_REPORT_ON_ARRAY_COPY)
endif()
cmake_print_variables(WITH_F2PY_REPORT_EXIT)
endif()

list(POP_BACK CMAKE_MESSAGE_INDENT)


message(STATUS "Python variables:")
list(APPEND CMAKE_MESSAGE_INDENT " ")

cmake_print_variables(Python_INCLUDE_DIRS)
cmake_print_variables(Python_NumPy_INCLUDE_DIRS)
cmake_print_variables(Python_NumPy_F2Py_INCLUDE_DIR)
if(WITH_FORTRAN)
cmake_print_variables(Python_NumPy_F2Py_INCLUDE_DIR)
endif()

list(POP_BACK CMAKE_MESSAGE_INDENT)


message(STATUS "Compilation flags")
list(APPEND CMAKE_MESSAGE_INDENT " ")

cmake_print_variables(CMAKE_C_FLAGS)
cmake_print_variables(CMAKE_CXX_FLAGS)
cmake_print_variables(CMAKE_Fortran_FLAGS)
if(WITH_FORTRAN)
cmake_print_variables(CMAKE_Fortran_FLAGS)
endif()

list(POP_BACK CMAKE_MESSAGE_INDENT)

message(STATUS "Options")
list(APPEND CMAKE_MESSAGE_INDENT " ")

cmake_print_variables(WITH_COVERAGE)
cmake_print_variables(WITH_ANNOTATE)
cmake_print_variables(WITH_LINE_DIRECTIVES)
cmake_print_variables(WITH_GDB)
cmake_print_variables(WITH_F2PY_REPORT_COPY)
if(WITH_F2PY_REPORT_COPY)
cmake_print_variables(F2PY_REPORT_ON_ARRAY_COPY)
endif()
cmake_print_variables(WITH_F2PY_REPORT_EXIT)

list(POP_BACK CMAKE_MESSAGE_INDENT)

function(sisl_has_substring _bool _string _substring)
string(FIND "${_string}" "${_substring}" index)
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ using :code:`pip` or :code:`conda` methods:
.. tab:: editable|pip

Editable installs are currently not fully supported by :code:`scikit-build-core` and
is considered experimental. One _may_ get it to work by doing:
is considered experimental. One *may* get it to work by doing:

.. code-block:: bash
Expand Down
8 changes: 4 additions & 4 deletions src/sisl/io/siesta/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import sisl._array as _a
try:
from . import _siesta
found_module = True
except Exception as e:
found_module = False
has_fortran_module = True
except ImportError:
has_fortran_module = False

__all__ = ['_csr_from_siesta', '_csr_from_sc_off']
__all__ += ['_csr_to_siesta', '_csr_to_sc_off']
Expand Down Expand Up @@ -59,7 +59,7 @@ def _csr_to_siesta(geom, csr, diag=True):
_csr_to_sc_off(geom, _siesta.siesta_sc_off(*geom.nsc).T, csr)


if not found_module:
if not has_fortran_module:
def _csr_from_siesta(geom, csr):
raise SislError('sisl cannot convert the sparse matrix from a Siesta conforming sparsity pattern! Please install with fortran support!')

Expand Down
9 changes: 4 additions & 5 deletions src/sisl/io/siesta/binaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

try:
from . import _siesta
found_module = True
except Exception as e:
print(e)
found_module = False
has_fortran_module = True
except ImportError:
has_fortran_module = False

from ..sile import add_sile, SileError, SileWarning
from .sile import SileBinSiesta
Expand Down Expand Up @@ -2275,7 +2274,7 @@ def _type(name, obj, dic=None):
tsgfSileSiesta = _type("tsgfSileSiesta", _gfSileSiesta)
gridSileSiesta = _type("gridSileSiesta", _gridSileSiesta, {"grid_unit": 1.})

if found_module:
if has_fortran_module:
add_sile("TSHS", tshsSileSiesta)
add_sile("onlyS", onlysSileSiesta)
add_sile("TSDE", tsdeSileSiesta)
Expand Down
6 changes: 4 additions & 2 deletions src/sisl/io/siesta/siesta_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
from ._help import *
try:
from . import _siesta
except Exception:
pass
# TODO make checks where appropiate
has_fortran_module = True
except ImportError:
has_fortran_module = False


__all__ = ['ncSileSiesta']
Expand Down
7 changes: 3 additions & 4 deletions src/sisl/io/siesta/sile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

try:
from . import _siesta
found_bin_module = True
except Exception as e:
print(e)
found_bin_module = False
has_fortran_module = True
except ImportError:
has_fortran_module = False

from sisl._internal import set_module
from ..sile import Sile, SileCDF, SileBin, SileError
Expand Down
6 changes: 4 additions & 2 deletions src/sisl/io/tbtrans/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
from ..siesta._help import _csr_to_siesta, _csr_from_sc_off, _mat_spin_convert
try:
from ..siesta._siesta import siesta_sc_off
except:
pass
# TODO add checks
has_fortran_module = True
except ImportError:
has_fortran_module = False


__all__ = ['deltancSileTBtrans']
Expand Down

0 comments on commit c346788

Please sign in to comment.