Skip to content

Commit

Permalink
removed setup.py and cleaned many details
Browse files Browse the repository at this point in the history
Also ensured no overlinking by explicitly setting
which wrappers are needed/created

Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Apr 17, 2023
1 parent 5319557 commit 445ac83
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 88 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
__pycache__
ci
.git
.github
.eggs
.pytest_cache
dist
Expand Down
134 changes: 73 additions & 61 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,7 @@ find_package(Python COMPONENTS
find_program(CYTHON_EXECUTABLE "cython" REQUIRED)


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

cmake_print_variables(Python_INCLUDE_DIRS)
cmake_print_variables(F2PY_INCLUDE_DIR)
cmake_print_variables(F2PY_INCLUDE_DIRS)
cmake_print_variables(Python_NumPy_INCLUDE_DIRS)

list(POP_BACK CMAKE_MESSAGE_INDENT)

message(STATUS "General 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)

list(POP_BACK CMAKE_MESSAGE_INDENT)


# Define global definitions
# We will never use the deprecated API any-more
add_compile_definitions(NPY_NO_DEPRECATED_API=0)
add_compile_definitions(CYTHON_NO_PYINIT_EXPORT=1)
Expand All @@ -57,44 +38,63 @@ add_compile_definitions(CYTHON_NO_PYINIT_EXPORT=1)
set(CMAKE_SHARED_MODULE_PREFIX "")


# Get numpy header
execute_process(COMMAND
"${PYTHON_EXECUTABLE}" -c "import numpy; print(numpy.get_include())"
OUTPUT_VARIABLE NUMPY_INCLUDE_DIR
RESULT_VARIABLE NUMPY_ERR
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NUMPY_ERR)
message(SEND_ERROR "ERROR: NumPy header not found (${NUMPY_ERR}).")
endif()

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

# Get numpy.f2py header
execute_process(COMMAND
"${PYTHON_EXECUTABLE}" -c "import numpy.f2py; print(numpy.f2py.get_include())"
OUTPUT_VARIABLE F2PY_INCLUDE_DIR
RESULT_VARIABLE F2PY_ERR
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(F2PY_ERR)
# Check whether we can locate the header anyways
set(F2PY_INCLUDE_DIR "${NUMPY_INCLUDE_DIR}/../../f2py/src")
if(NOT IS_DIRECTORY "${F2PY_INCLUDE_DIR}")
message(SEND_ERROR "ERROR: NumPy.F2PY header not found (${F2PY_ERR}), also looked in ${F2PY_INCLUDE_DIR}.")
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 "${F2PY_INCLUDE_DIR}/fortranobject.c")
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 "${F2PY_INCLUDE_DIR}")
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)



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)

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)

list(POP_BACK CMAKE_MESSAGE_INDENT)


function(sisl_file_as_gentarget _name _output)
if(IS_ABSOLUTE "${_name}")
# shorten the generated target (easier on debugging)
Expand Down Expand Up @@ -210,7 +210,7 @@ function(add_cython_library)
set_source_files_properties("${_c_output_full}"
PROPERTIES
SISL_GENERATE_TARGET "${_gen_target}"
INCLUDE_DIRECTORIES "${NUMPY_INCLUDE_DIR}"
INCLUDE_DIRECTORIES "${Python_NumPy_INCLUDE_DIRS}"
)

# now we have a source file, lets create a library if required
Expand Down Expand Up @@ -302,6 +302,10 @@ function(create_f2py_signature)
endfunction(create_f2py_signature)


# Create a library from a previously created f2py signature file.
# If this signature file has been created from `create_f2py_signature` the command is very
# simple as there are properties set on the signature file enabling full automatic discovery.
#
# Create a library from a f2py signature created file
# Note that.
# For fortran 77 files we have this:
Expand All @@ -322,7 +326,7 @@ endfunction(create_f2py_signature)
# When WITHOUT_MODULES is present the f90 file will be generated
# The code will adapt which wrappers are generated
function(add_f2py_library)
set(options WITH_FUNCTIONS WITHOUT_MODULES WITH_COMMON)
set(options WITH_F_WRAPPER WITH_F90_WRAPPER)
set(oneValueArgs
SIGNATURE # in
LIBRARY # in[opt] default to module name
Expand Down Expand Up @@ -382,26 +386,34 @@ function(add_f2py_library)
list(APPEND _has_unknown ${ext})
endif()
endforeach()
if(_has_unknown)
message(WARNING "Unknown extensions (${_has_unknown}) found for library ${_f_LIBRARY} (module: ${_f_MODULE})")
endif()


set(_wrappers)
if(_has_f)
if(_f_WITH_COMMON)
list(APPEND _wrappers "${CMAKE_CURRENT_BINARY_DIR}/${_f_MODULE}-f2pywrappers.f")
endif()
if(_f_WITH_F_WRAPPER)
list(APPEND _wrappers "${CMAKE_CURRENT_BINARY_DIR}/${_f_MODULE}-f2pywrappers.f")
endif()
if(_has_f90)
if(NOT _f_WITHOUT_MODULES)
list(APPEND _wrappers "${CMAKE_CURRENT_BINARY_DIR}/${_f_MODULE}-f2pywrappers2.f90")
endif()
if(_f_WITH_FUNCTIONS)
list(APPEND _wrappers "${CMAKE_CURRENT_BINARY_DIR}/${_f_MODULE}-f2pywrappers.f")
endif()
if(_f_WITH_F90_WRAPPER)
list(APPEND _wrappers "${CMAKE_CURRENT_BINARY_DIR}/${_f_MODULE}-f2pywrappers2.f90")
endif()
list(REMOVE_DUPLICATES _wrappers)

if(_has_unknown)
message(WARNING "Unknown extensions (${_has_unknown}) found for library ${_f_LIBRARY} (module: ${_f_MODULE})")
endif()
#set(_wrappers)
#if(_has_f)
# if(_f_WITH_COMMON)
# list(APPEND _wrappers "${CMAKE_CURRENT_BINARY_DIR}/${_f_MODULE}-f2pywrappers.f")
# endif()
#endif()
#if(_has_f90)
# if(NOT _f_WITHOUT_MODULES)
# list(APPEND _wrappers "${CMAKE_CURRENT_BINARY_DIR}/${_f_MODULE}-f2pywrappers2.f90")
# endif()
# if(_f_WITH_FUNCTIONS)
# list(APPEND _wrappers "${CMAKE_CURRENT_BINARY_DIR}/${_f_MODULE}-f2pywrappers.f")
# endif()
#endif()
list(REMOVE_DUPLICATES _wrappers)


# default to a C output, I don't think f2py can do anything differently
Expand All @@ -413,7 +425,7 @@ function(add_f2py_library)
add_custom_target(${_gen_target} DEPENDS "${_module_file}")
add_custom_command(
OUTPUT "${_module_file}"
BYPRODUCTS ${_wrappers}
BYPRODUCTS "${_wrappers}"
DEPENDS ${_f_SOURCES} ${_f_DEPENDS} "${_f_SIGNATURE}" ${_gen_signature_target}
COMMAND "${_f_F2PY_EXECUTABLE}" "${_f_F2PY_FLAGS}" "${_f_SIGNATURE}" "--lower"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
Expand Down
19 changes: 1 addition & 18 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,6 @@ classifiers = [
# We purposefully get the version dynamically
dynamic = ["version"]

[tool.setuptools]
platforms = ["any"]
zip-safe = false
# also default to true, but..
include-package-data = true


[tool.setuptools.packages.find]
# all sub-folders will be packages
where = ["src"]
# sisl_toolbox is a namespace package
namespaces = true

#[tool.setuptools.package-data]
## all pxd files
#"*" = ["*.pxd"]
#"sisl_toolbox.siesta.minimizer" = ["*.yaml"]

[tool.setuptools_scm]
write_to = "src/sisl/_version.py"
Expand All @@ -111,7 +94,7 @@ cmake.minimum-version = "3.20"

# source configuration
sdist.reproducible = true
sdist.include = ["src/sisl/_version.py"]
sdist.include = ["src/sisl/_version.py", "*/CMakeLists.txt"]


[project.scripts]
Expand Down
9 changes: 0 additions & 9 deletions setup.py

This file was deleted.

3 changes: 3 additions & 0 deletions src/sisl/io/siesta/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ create_f2py_signature(
)

add_f2py_library(
# We have one module (io_m) and no f-files with common blocks
# Nor do we have any f90 files with functions that should be exposed
WITH_F90_WRAPPER
SIGNATURE "${_siesta_signature}"
)

Expand Down

0 comments on commit 445ac83

Please sign in to comment.