Skip to content

Commit

Permalink
cmake py target logic updates (#1272)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrush authored Apr 10, 2024
1 parent 06a09c3 commit 09a465b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
52 changes: 30 additions & 22 deletions src/cmake/thirdparty/SetupPython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ find_package(PythonInterp REQUIRED)
if(PYTHONINTERP_FOUND)
MESSAGE(STATUS "PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE}")

# clear extra python module dirs
set(EXTRA_PYTHON_MODULE_DIRS "")

execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import sys;from distutils.sysconfig import get_config_var; sys.stdout.write(get_config_var('VERSION'))"
"import sys;from sysconfig import get_config_var; sys.stdout.write(get_config_var('VERSION'))"
OUTPUT_VARIABLE PYTHON_CONFIG_VERSION
ERROR_VARIABLE ERROR_FINDING_PYTHON_VERSION)
MESSAGE(STATUS "PYTHON_CONFIG_VERSION ${PYTHON_CONFIG_VERSION}")

execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import sys;from distutils.sysconfig import get_python_inc;sys.stdout.write(get_python_inc())"
"import sys;from sysconfig import get_path;sys.stdout.write(get_path('include'))"
OUTPUT_VARIABLE PYTHON_INCLUDE_DIR
ERROR_VARIABLE ERROR_FINDING_INCLUDES)
MESSAGE(STATUS "PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR}")
Expand All @@ -39,7 +36,18 @@ if(PYTHONINTERP_FOUND)
MESSAGE(FATAL_ERROR "Reported PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR} does not exist!")
endif()

execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
#######################################################################
# Find main python package dirs for embedded use cases
# (used in Ascent, not Conduit)
#######################################################################
#
# TODO: replacing distutils.get_python_lib() isn't straight forward
# distutils had special logic for some platforms (ubuntu)
# which is not 1:1 using sysconfig.
# We may need several queries and a list of paths to replace
# get_python_lib()
#
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import sys;from distutils.sysconfig import get_python_lib;sys.stdout.write(get_python_lib())"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES_DIR
ERROR_VARIABLE ERROR_FINDING_SITE_PACKAGES_DIR)
Expand All @@ -48,9 +56,10 @@ if(PYTHONINTERP_FOUND)
if(NOT EXISTS ${PYTHON_SITE_PACKAGES_DIR})
MESSAGE(FATAL_ERROR "Reported PYTHON_SITE_PACKAGES_DIR ${PYTHON_SITE_PACKAGES_DIR} does not exist!")
endif()

# for embedded python, we need to know where the site packages dir is
set(EXTRA_PYTHON_MODULE_DIRS "")
list(APPEND EXTRA_PYTHON_MODULE_DIRS ${PYTHON_SITE_PACKAGES_DIR})
#######################################################################

# check if we need "-undefined dynamic_lookup" by inspecting LDSHARED flags
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
Expand All @@ -69,7 +78,7 @@ if(PYTHONINTERP_FOUND)
endif()

# our goal is to find the specific python lib, based on info
# we extract from distutils.sysconfig from the python executable
# we extract from sysconfig from the python executable
#
# check for python libs differs for windows python installs
if(NOT WIN32)
Expand All @@ -85,22 +94,22 @@ if(PYTHONINTERP_FOUND)
# LIBPL + LIBRARY

execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import sys;from distutils.sysconfig import get_config_var; sys.stdout.write(get_config_var('LIBDIR'))"
"import sys;from sysconfig import get_config_var; sys.stdout.write(get_config_var('LIBDIR'))"
OUTPUT_VARIABLE PYTHON_CONFIG_LIBDIR
ERROR_VARIABLE ERROR_FINDING_PYTHON_LIBDIR)

execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import sys;from distutils.sysconfig import get_config_var; sys.stdout.write(get_config_var('LIBPL'))"
"import sys;from sysconfig import get_config_var; sys.stdout.write(get_config_var('LIBPL'))"
OUTPUT_VARIABLE PYTHON_CONFIG_LIBPL
ERROR_VARIABLE ERROR_FINDING_PYTHON_LIBPL)

execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import sys;from distutils.sysconfig import get_config_var; sys.stdout.write(get_config_var('LDLIBRARY'))"
"import sys;from sysconfig import get_config_var; sys.stdout.write(get_config_var('LDLIBRARY'))"
OUTPUT_VARIABLE PYTHON_CONFIG_LDLIBRARY
ERROR_VARIABLE ERROR_FINDING_PYTHON_LDLIBRARY)

execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import sys;from distutils.sysconfig import get_config_var; sys.stdout.write(get_config_var('LIBRARY'))"
"import sys;from sysconfig import get_config_var; sys.stdout.write(get_config_var('LIBRARY'))"
OUTPUT_VARIABLE PYTHON_CONFIG_LIBRARY
ERROR_VARIABLE ERROR_FINDING_PYTHON_LIBRARY)

Expand Down Expand Up @@ -184,7 +193,7 @@ find_package_handle_standard_args(Python DEFAULT_MSG


##############################################################################
# Macro to use a pure python distutils setup script
# Macro to use a pure python pip setup script
##############################################################################
FUNCTION(PYTHON_ADD_PIP_SETUP)
set(singleValuedArgs NAME DEST_DIR PY_MODULE_DIR PY_SETUP_FILE FOLDER)
Expand All @@ -197,27 +206,27 @@ FUNCTION(PYTHON_ADD_PIP_SETUP)
# check req'd args
if(NOT DEFINED args_NAME)
message(FATAL_ERROR
"PYTHON_ADD_HYBRID_MODULE: Missing required argument NAME")
"PYTHON_ADD_PIP_SETUP: Missing required argument NAME")
endif()

if(NOT DEFINED args_DEST_DIR)
message(FATAL_ERROR
"PYTHON_ADD_HYBRID_MODULE: Missing required argument DEST_DIR")
"PYTHON_ADD_PIP_SETUP: Missing required argument DEST_DIR")
endif()

if(NOT DEFINED args_PY_MODULE_DIR)
message(FATAL_ERROR
"PYTHON_ADD_HYBRID_MODULE: Missing required argument PY_MODULE_DIR")
"PYTHON_ADD_PIP_SETUP: Missing required argument PY_MODULE_DIR")
endif()

if(NOT DEFINED args_PY_SETUP_FILE)
message(FATAL_ERROR
"PYTHON_ADD_HYBRID_MODULE: Missing required argument PY_SETUP_FILE")
"PYTHON_ADD_PIP_SETUP: Missing required argument PY_SETUP_FILE")
endif()

if(NOT DEFINED args_PY_SOURCES)
message(FATAL_ERROR
"PYTHON_ADD_HYBRID_MODULE: Missing required argument PY_SOURCES")
"PYTHON_ADD_PIP_SETUP: Missing required argument PY_SOURCES")
endif()

MESSAGE(STATUS "Configuring python pip setup: ${args_NAME}")
Expand Down Expand Up @@ -426,10 +435,10 @@ FUNCTION(PYTHON_ADD_HYBRID_MODULE)
SOURCES ${args_SOURCES}
FOLDER ${args_FOLDER})

ENDFUNCTION(PYTHON_ADD_HYBRID_MODULE)


# args_NAME depends on "${args_NAME}_py_setup"
add_dependencies( ${args_NAME} "${args_NAME}_py_setup")

ENDFUNCTION(PYTHON_ADD_HYBRID_MODULE)

#
# Also register python as a BLT dep,to support the case were we link python,
Expand All @@ -440,4 +449,3 @@ blt_register_library(NAME python
INCLUDES ${PYTHON_INCLUDE_DIR}
LIBRARIES ${PYTHON_LIBRARY} )


11 changes: 10 additions & 1 deletion src/libs/ascent/python/ascent_module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ SET(ascent_py_python_sources py_src/__init__.py
)

# built the pure python module
PYTHON_ADD_PIP_SETUP(NAME ascent_python_py
PYTHON_ADD_PIP_SETUP(NAME ascent_python_py_setup
DEST_DIR python-modules
PY_MODULE_DIR ascent
PY_SETUP_FILE setup.py
Expand All @@ -30,8 +30,12 @@ if (ENABLE_SERIAL)
PY_SOURCES "${ascent_py_cpp_sources}"
SOURCES ${ascent_py_cpp_sources})

# compiled modules depend on output dir structure created by main module setup
add_dependencies( ascent_python ascent_python_py_setup)

# link with the proper libs (beyond python)
target_link_libraries(ascent_python ascent)

endif()


Expand All @@ -48,8 +52,13 @@ if(MPI_FOUND)
PY_SOURCES "${ascent_mpi_py_python_sources}"
SOURCES ${ascent_mpi_py_cpp_sources})


# compiled modules depend on output dir structure created by main module setup
add_dependencies( ascent_mpi_python ascent_python_py_setup)

# link with the proper libs (beyond python)
target_link_libraries(ascent_mpi_python ascent_mpi)

endif()


Expand Down

0 comments on commit 09a465b

Please sign in to comment.