Skip to content

Commit

Permalink
Merge pull request commontk#874 from kislinsk/fix-pythonlibs-handling
Browse files Browse the repository at this point in the history
Fix PythonLibs handling
  • Loading branch information
jcfr authored Aug 21, 2019
2 parents acd419d + ff1800e commit 3e7286f
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 26 deletions.
32 changes: 32 additions & 0 deletions CMake/ctkFunctionExtractOptimizedLibrary.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!
#! Extracts the optimized library filepath from a list in the target_link_libraries()
#! format, e. g. "optimized;release.lib;debug;debug.lib".
#!
#! If the list is a single filepath, return it instead.
#!
#! \param lib_list A target_link_libraries() library list or a single filepath.
#! \param var_optimized_lib A variable name containing the output.
#!
function(ctkFunctionExtractOptimizedLibrary lib_list var_optimized_lib)

list(LENGTH ${lib_list} lib_list_length)
if(lib_list_length EQUAL 1)
set(${var_optimized_lib} "${${lib_list}}" PARENT_SCOPE)
return()
endif()

list(FIND ${lib_list} "optimized" optimized_keyword_index)
if(optimized_keyword_index EQUAL -1)
message(FATAL_ERROR "List \"${${lib_list}}\" does not contain \"optimized\" keyword")
endif()

math(EXPR last_lib_list_index "${lib_list_length} - 1")
if(NOT optimized_keyword_index LESS last_lib_list_index)
message(FATAL_ERROR "List \"${${lib_list}}\" ended unexpectedly")
endif()

math(EXPR optimized_lib_index "${optimized_keyword_index} + 1")
list(GET ${lib_list} ${optimized_lib_index} optimized_lib)
set(${var_optimized_lib} "${optimized_lib}" PARENT_SCOPE)

endfunction()
1 change: 1 addition & 0 deletions CMake/ctkMacroBuildLibWrapper.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ macro(ctkMacroBuildLibWrapper)
# Since the PythonQt decorator depends on PythonQt, Python and VTK, let's link against
# these ones to avoid complaints of MSVC
# Note: "LINK_DIRECTORIES" has to be invoked before "ADD_LIBRARY"
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
set(my_EXTRA_PYTHON_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})

# Does a header having the expected filename exists ?
Expand Down
27 changes: 16 additions & 11 deletions CMake/ctkMacroCompilePythonScript.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#

#
# By globally defining the variable CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME to a
# non-empty string or by specifying the macro option 'GLOBAL_TARGET',
# By globally defining the variable CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME to a
# non-empty string or by specifying the macro option 'GLOBAL_TARGET',
# the following targets will be defined for the whole build system:
# - Copy<GLOBAL_TARGET_NAME>PythonResourceFiles
# - Copy<GLOBAL_TARGET_NAME>PythonScriptFiles
Expand All @@ -28,7 +28,7 @@ macro(ctkMacroCompilePythonScript)
"NO_INSTALL_SUBDIR;GLOBAL_TARGET"
${ARGN}
)

# Sanity checks
foreach(varname TARGET_NAME SCRIPTS DESTINATION_DIR INSTALL_DIR)
if(NOT DEFINED MY_${varname})
Expand All @@ -39,7 +39,7 @@ macro(ctkMacroCompilePythonScript)
if(NOT DEFINED MY_SOURCE_DIR)
set(MY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
endif()

if("${CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME}" STREQUAL "")
set(target ${MY_TARGET_NAME})
else()
Expand Down Expand Up @@ -73,7 +73,7 @@ macro(ctkMacroCompilePythonScript)
set_property(GLOBAL APPEND PROPERTY
_CTK_${target}_PYTHON_SCRIPTS "${src}|${tgt_file}|${MY_DESTINATION_DIR}")
endforeach()

if(DEFINED MY_RESOURCES)
set(resource_input_files)
foreach(file ${MY_RESOURCES})
Expand All @@ -96,7 +96,7 @@ macro(ctkMacroCompilePythonScript)
install(DIRECTORY "${MY_DIRECTORY_TO_INSTALL}"
DESTINATION "${MY_INSTALL_DIR}" COMPONENT RuntimeLibraries
USE_SOURCE_PERMISSIONS)

if(NOT MY_GLOBAL_TARGET)
ctkFunctionAddCompilePythonScriptTargets(${target})
endif()
Expand Down Expand Up @@ -133,7 +133,7 @@ endfunction()

function(_ctk_add_compile_python_directories_target target)
set(target_name Compile${target}PythonFiles)
if(NOT TARGET ${target_name})
if(NOT TARGET ${target_name})
# Byte compile the Python files.
set(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${target}_python_scripts.py")

Expand All @@ -148,11 +148,16 @@ function(_ctk_add_compile_python_directories_target target)
set(_compileall_code "${_compileall_code}\nctk_compile_file('${tgt}', force=1)")
endforeach()

find_package(PythonInterp REQUIRED)
find_package(PythonLibs REQUIRED)
if(NOT PYTHONINTERP_FOUND)
find_package(PythonInterp REQUIRED)
endif()
if(NOT PYTHONLIBS_FOUND)
find_package(PythonLibs REQUIRED)
endif()

# Extract python lib path
get_filename_component(PYTHON_LIBRARY_PATH ${PYTHON_LIBRARY} PATH)
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
get_filename_component(PYTHON_LIBRARY_PATH "${PYTHON_LIBRARY}" PATH)

# Configure cmake script associated with the custom command
# required to properly update the library path with PYTHON_LIBRARY_PATH
Expand All @@ -171,7 +176,7 @@ function(_ctk_add_compile_python_directories_target target)
)

add_custom_target(${target_name} ALL
DEPENDS
DEPENDS
${CMAKE_CURRENT_BINARY_DIR}/python_compile_${target}_complete
)
endif()
Expand Down
6 changes: 4 additions & 2 deletions CMake/ctkMacroWrapPythonQt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ macro(ctkMacroWrapPythonQt WRAPPING_NAMESPACE TARGET SRCS_LIST_NAME SOURCES IS_W
# TODO: this find package seems not to work when called form a superbuild, but the call is needed
# in general to find the python interpreter. In CTK, the toplevel CMakeLists.txt does the find
# package so this is a no-op. Other uses of this file may need to have this call so it is still enabled.
find_package(PythonInterp)
if(NOT PYTHONINTERP_FOUND)
message(FATAL_ERROR "PYTHON_EXECUTABLE not specified or inexistent when calling ctkMacroWrapPythonQt")
find_package(PythonInterp)
if(NOT PYTHONINTERP_FOUND)
message(FATAL_ERROR "PYTHON_EXECUTABLE not specified or inexistent when calling ctkMacroWrapPythonQt")
endif()
endif()

set(SOURCES_TO_WRAP)
Expand Down
8 changes: 6 additions & 2 deletions CMakeExternals/PythonQt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ if(NOT DEFINED PYTHONQT_INSTALL_DIR)
endif()

# Python is required
find_package(PythonLibs)
if(NOT PYTHONLIBS_FOUND)
message(FATAL_ERROR "error: Python is required to build ${PROJECT_NAME}")
find_package(PythonLibs)
if(NOT PYTHONLIBS_FOUND)
message(FATAL_ERROR "error: Python is required to build ${PROJECT_NAME}")
endif()
endif()

ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)

if (CTK_QT_VERSION VERSION_GREATER "4")
set(revision_tag fae2301264529b4c8c85ee6e285f9605176471f0) # patched-8
else()
Expand Down
3 changes: 2 additions & 1 deletion CMakeExternals/VTK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ if(NOT DEFINED VTK_DIR AND NOT ${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj})
endif()

if(CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
list(APPEND additional_vtk_cmakevars
-DPYTHON_EXECUTABLE:PATH=${PYTHON_EXECUTABLE}
-DPYTHON_LIBRARIES:FILEPATH=${PYTHON_LIBRARIES}
-DPYTHON_LIBRARY:FILEPATH=${PYTHON_LIBRARY}
-DPYTHON_DEBUG_LIBRARIES:FILEPATH=${PYTHON_DEBUG_LIBRARIES}
)
endif()
Expand Down
13 changes: 9 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ foreach(file
CMake/ctkMacroListFilter.cmake
CMake/ctkMacroOptionUtils.cmake
CMake/ctkMacroBuildLib.cmake
CMake/ctkFunctionExtractOptimizedLibrary.cmake
CMake/ctkMacroBuildLibWrapper.cmake
CMake/ctkMacroBuildPlugin.cmake
CMake/ctkMacroBuildApp.cmake
Expand Down Expand Up @@ -885,13 +886,17 @@ endif()

# Check if dependencies are satisfied
if(CTK_LIB_Scripting/Python/Core)
find_package(PythonInterp)
if(NOT PYTHONINTERP_FOUND)
message(FATAL_ERROR "PYTHON_EXECUTABLE variable should be set to build CTK_LIB_Scripting/Python")
find_package(PythonInterp)
if(NOT PYTHONINTERP_FOUND)
message(FATAL_ERROR "PYTHON_EXECUTABLE variable should be set to build CTK_LIB_Scripting/Python")
endif()
endif()
find_package(PythonLibs)
if(NOT PYTHONLIBS_FOUND)
message(FATAL_ERROR "PYTHON_LIBRARIES and PYTHON_INCLUDE_DIRS should be set to build CTK_LIB_Scripting/Python")
find_package(PythonLibs)
if(NOT PYTHONLIBS_FOUND)
message(FATAL_ERROR "PYTHON_LIBRARIES and PYTHON_INCLUDE_DIRS should be set to build CTK_LIB_Scripting/Python")
endif()
endif()
endif()

Expand Down
1 change: 1 addition & 0 deletions Libs/Scripting/Python/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ configure_file(

# Target libraries - See CMake/ctkFunctionGetTargetLibraries.cmake
# The following macro will read the target libraries from the file 'target_libraries.cmake'
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
ctkFunctionGetTargetLibraries(KIT_target_libraries)

ctkMacroBuildLib(
Expand Down
6 changes: 3 additions & 3 deletions Libs/Scripting/Python/Core/target_libraries.cmake
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#
# See CMake/ctkFunctionGetTargetLibraries.cmake
#
#
# This file should list the libraries required to build the current CTK libraries
#

set(target_libraries
PYTHON_LIBRARIES
PYTHON_LIBRARY
PYTHONQT_LIBRARIES
CTKCore
)
)
1 change: 1 addition & 0 deletions Libs/Visualization/VTK/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ if(${CTK_USE_CHARTS})
endif()

if(CTK_LIB_Scripting/Python/Core AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
ctkFunctionExtractOptimizedLibrary(PYTHON_LIBRARIES PYTHON_LIBRARY)
list(APPEND VTK_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})
if(${VTK_VERSION_MAJOR} GREATER 5)
list(APPEND VTK_LIBRARIES vtkWrappingPythonCore)
Expand Down
8 changes: 5 additions & 3 deletions Utilities/CMake/FindPythonQt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#

# Python is required
find_package(PythonLibs)
if(NOT PYTHONLIBS_FOUND)
message(FATAL_ERROR "error: Python is required to build PythonQt")
find_package(PythonLibs)
if(NOT PYTHONLIBS_FOUND)
message(FATAL_ERROR "error: Python is required to build PythonQt")
endif()
endif()

if(NOT EXISTS "${PYTHONQT_INSTALL_DIR}")
Expand Down Expand Up @@ -46,7 +48,7 @@ endif()
set(PYTHONQT_FOUND 0)
set(PythonQt_FOUND 0)
if(PYTHONQT_INCLUDE_DIR AND PYTHONQT_LIBRARY)
# Currently CMake'ified PythonQt only supports building against a python Release build.
# Currently CMake'ified PythonQt only supports building against a python Release build.
# This applies independently of CTK build type (Release, Debug, ...)
add_definitions(-DPYTHONQT_USE_RELEASE_PYTHON_FALLBACK)
set(PYTHONQT_FOUND 1)
Expand Down

0 comments on commit 3e7286f

Please sign in to comment.