Skip to content

Commit

Permalink
Improve Python Package Installation (#174)
Browse files Browse the repository at this point in the history
Improve Python package installation
  • Loading branch information
taylor-a-barnes authored Jan 23, 2025
1 parent f758354 commit 26a8626
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ jobs:
make VERBOSE=1
sudo make install
cd ..
python -c "import mdi; print(\"PATCH VERSION: \", mdi.MDI_PATCH_VERSION)"
- name: Install local build
run: |
Expand Down Expand Up @@ -319,6 +320,7 @@ jobs:
mingw32-make VERBOSE=1
mingw32-make install
cd ..
python -c "import mdi; print(mdi.MDI_PATCH_VERSION)"
- name: Install local build
run: |
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ else()
endif()

project(mdi
VERSION 1.4.34
VERSION 1.4.35
LANGUAGES ${use_C} ${use_CXX} ${use_Fortran})

# Check for MPI
Expand Down
81 changes: 47 additions & 34 deletions MDI_Library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ if( mpi STREQUAL "OFF" )
endif()

# Add Fortran support, if requested
if( (NOT language) OR (language STREQUAL "Fortran") )
if( use_Fortran )
list(APPEND sources "mdi_f90.F90")
endif()

Expand Down Expand Up @@ -136,8 +136,12 @@ else()

endif()

set(MDI_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR})
set(MDI_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR})
set(MDI_INSTALL_BINDIR ${CMAKE_INSTALL_BINDIR})

# Do any Python-specific work
if( (NOT language) OR (language STREQUAL "Python") )
if( use_Python )

if( libtype STREQUAL "STATIC" )
if( NOT language )
Expand All @@ -151,10 +155,24 @@ if( (NOT language) OR (language STREQUAL "Python") )
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/__init__.py ${CMAKE_CURRENT_BINARY_DIR}/__init__.py COPYONLY)

# Write the name of the mdi library, for use by mdi.py
file(GENERATE
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mdi_name
CONTENT $<TARGET_FILE_NAME:mdi>
)
if( python_package )
if( WIN32 )
file(GENERATE
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mdi_name
CONTENT "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/$<TARGET_FILE_NAME:mdi>"
)
else()
file(GENERATE
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mdi_name
CONTENT "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/$<TARGET_FILE_NAME:mdi>"
)
endif()
else()
file(GENERATE
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mdi_name
CONTENT $<TARGET_FILE_NAME:mdi>
)
endif()

endif()

Expand All @@ -180,48 +198,43 @@ if( python_package STREQUAL "ON" )
set(PYTHON_SITE_PACKAGES "${_site_packages}" CACHE PATH INTERNAL)
unset(_site_packages)

set(MDI_INSTALL_INCLUDEDIR ${PYTHON_SITE_PACKAGES}/mdi)
set(MDI_INSTALL_LIBDIR ${PYTHON_SITE_PACKAGES}/mdi)
set(MDI_INSTALL_BINDIR ${PYTHON_SITE_PACKAGES}/mdi)

else()

set(MDI_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR})
set(MDI_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR})
set(MDI_INSTALL_BINDIR ${CMAKE_INSTALL_BINDIR})

endif()

# Set the RPATH
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
if(APPLE)
set(CMAKE_INSTALL_RPATH "\@loader_path/../lib")
set(CMAKE_INSTALL_RPATH "\@loader_path/../lib")
elseif(UNIX)
set(CMAKE_INSTALL_RPATH "\$ORIGIN/../lib")
set(CMAKE_INSTALL_RPATH "\$ORIGIN/../lib")
endif()

# Perform the installation
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mdi.h
DESTINATION ${MDI_INSTALL_INCLUDEDIR})
if( (NOT language) OR (language STREQUAL "Fortran") )
if( python_package STREQUAL "OFF" )
if( use_Fortran )
install(CODE "
# Check for both lowercase and uppercase versions of the file
if(EXISTS \"${CMAKE_CURRENT_BINARY_DIR}/mdi.mod\")
file(INSTALL \"${CMAKE_CURRENT_BINARY_DIR}/mdi.mod\" DESTINATION \"${CMAKE_INSTALL_PREFIX}/${MDI_INSTALL_INCLUDEDIR}\")
elseif(EXISTS \"${CMAKE_CURRENT_BINARY_DIR}/MDI.mod\")
file(INSTALL \"${CMAKE_CURRENT_BINARY_DIR}/MDI.mod\" DESTINATION \"${CMAKE_INSTALL_PREFIX}/${MDI_INSTALL_INCLUDEDIR}\")
else()
message(FATAL_ERROR \"Fortran module file not found\")
endif()
")
# Check for both lowercase and uppercase versions of the file
if(EXISTS \"${CMAKE_CURRENT_BINARY_DIR}/mdi.mod\")
file(INSTALL \"${CMAKE_CURRENT_BINARY_DIR}/mdi.mod\" DESTINATION \"${CMAKE_INSTALL_PREFIX}/${MDI_INSTALL_INCLUDEDIR}\")
elseif(EXISTS \"${CMAKE_CURRENT_BINARY_DIR}/MDI.mod\")
file(INSTALL \"${CMAKE_CURRENT_BINARY_DIR}/MDI.mod\" DESTINATION \"${CMAKE_INSTALL_PREFIX}/${MDI_INSTALL_INCLUDEDIR}\")
else()
message(FATAL_ERROR \"Fortran module file not found\")
endif()
")
endif()
if( (NOT language) OR (language STREQUAL "Python") )
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mdi.py
${CMAKE_CURRENT_BINARY_DIR}/mdi_name
${CMAKE_CURRENT_BINARY_DIR}/__init__.py
DESTINATION ${MDI_INSTALL_LIBDIR})
if( use_Python )
if( python_package STREQUAL "ON" )
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mdi.py
${CMAKE_CURRENT_BINARY_DIR}/mdi_name
${CMAKE_CURRENT_BINARY_DIR}/__init__.py
DESTINATION ${PYTHON_SITE_PACKAGES}/mdi)
else()
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mdi.py
${CMAKE_CURRENT_BINARY_DIR}/mdi_name
${CMAKE_CURRENT_BINARY_DIR}/__init__.py
DESTINATION ${MDI_INSTALL_LIBDIR})
endif()
endif()

install(TARGETS mdi
Expand Down
10 changes: 8 additions & 2 deletions MDI_Library/mdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
use_mpi4py = False
MPI = None

# get the path to the MDI Library
# get the name of the MDI Library
mdi_name_path = os.path.join( dir_path, "mdi_name" )
mdi_name_file = open( mdi_name_path, "r" )
mdi_name = mdi_name_file.read()
mdi_path = os.path.join( dir_path, mdi_name )
mdi_name_file.close()

# check if mdi_name is a full path to the MDI Library
if os.path.exists(mdi_name):
mdi_path = mdi_name
else:
# the mdi_name file includes only the name of the library
mdi_path = os.path.join( dir_path, mdi_name )

# load the MDI Library
try:
mdi = ctypes.CDLL(mdi_path, ctypes.RTLD_GLOBAL)
Expand Down
2 changes: 1 addition & 1 deletion MDI_Library/mdi_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// MDI version numbers
#define MDI_MAJOR_VERSION_ 1
#define MDI_MINOR_VERSION_ 4
#define MDI_PATCH_VERSION_ 34
#define MDI_PATCH_VERSION_ 35

// length of an MDI command in characters
#define MDI_COMMAND_LENGTH_ 256
Expand Down

0 comments on commit 26a8626

Please sign in to comment.