Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Installing python modules correctly [20800] #119

Merged
merged 6 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions fastdds_python/src/swig/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ set(${PROJECT_NAME}_MODULE
fastdds
)

set(${PROJECT_NAME}_FILES
${${PROJECT_NAME}_MODULE}.i
set(${PROJECT_NAME}_FILE
${CMAKE_CURRENT_BINARY_DIR}/${${PROJECT_NAME}_MODULE}.i
)

SET_SOURCE_FILES_PROPERTIES(
${${PROJECT_NAME}_FILES}
PROPERTIES CPLUSPLUS ON
file(GENERATE
OUTPUT ${${PROJECT_NAME}_FILE}
INPUT ${${PROJECT_NAME}_MODULE}.i
)

set_source_files_properties(${${PROJECT_NAME}_FILE} PROPERTIES
CPLUSPLUS ON
USE_TARGET_INCLUDE_DIRECTORIES TRUE
INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}
)

option(BUILD_DOCUMENTATION "Use doxygen to create product documentation" OFF)
Expand All @@ -42,11 +47,14 @@ endif (BUILD_DOCUMENTATION)
swig_add_library(${PROJECT_NAME}
TYPE SHARED
LANGUAGE python
SOURCES ${${PROJECT_NAME}_FILES}
SOURCES
${${PROJECT_NAME}_FILE}
)

if(UNIX AND CMAKE_SIZEOF_VOID_P EQUAL 8)
set_property(TARGET ${PROJECT_NAME} PROPERTY SWIG_COMPILE_DEFINITIONS SWIGWORDSIZE64)
set_property(TARGET ${PROJECT_NAME} PROPERTY
SWIG_COMPILE_DEFINITIONS SWIGWORDSIZE64
)
endif()

if(MSVC OR MSVC_IDE)
Expand All @@ -67,16 +75,16 @@ execute_process(COMMAND ${Python3_EXECUTABLE} -c "from distutils import sysconfi
get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
SET (PYTHON_MODULE_PATH
${_REL_PYTHON_MODULE_PATH}
${_REL_PYTHON_MODULE_PATH}/${${PROJECT_NAME}_MODULE}
)

# Install
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}-targets
DESTINATION ${PYTHON_MODULE_PATH}
)
get_property(support_files TARGET ${PROJECT_NAME} PROPERTY SWIG_SUPPORT_FILES)
install(FILES ${support_files} DESTINATION ${PYTHON_MODULE_PATH})
get_property(generated_python_file TARGET ${PROJECT_NAME} PROPERTY SWIG_SUPPORT_FILES)
install(FILES ${generated_python_file} DESTINATION ${PYTHON_MODULE_PATH} RENAME __init__.py)

export(TARGETS ${PROJECT_NAME} FILE ${PROJECT_BINARY_DIR}/cmake/config/${PROJECT_NAME}-targets.cmake)

Expand Down
10 changes: 9 additions & 1 deletion fastdds_python/src/swig/fastdds.i
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

%module(directors="1", threads="1") fastdds
%module(directors="1", threads="1", moduleimport="if __import__('os').name == 'nt': import win32api; win32api.LoadLibrary('$<TARGET_FILE_NAME:fastdds>')\nif __package__ or '.' in __name__:\n from . import _fastdds_python\nelse:\n import _fastdds_python") fastdds

// Handle exceptions on python callbacks and send them back to C++ so that they can be catched
// Also, add some meaningful description of the error
Expand Down Expand Up @@ -43,6 +43,14 @@
}
}

// If using windows in debug, it would try to use python_d, which would not be found.
%begin %{
#ifdef _MSC_VER
#define SWIG_PYTHON_INTERPRETER_NO_DEBUG
#endif
#include <exception>
%}

%exception {
try { $action }
catch (Swig::DirectorException &e) { SWIG_fail; }
Expand Down
10 changes: 1 addition & 9 deletions fastdds_python/test/api/test_datareader.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
# until https://bugs.python.org/issue46276 is not fixed we can apply this
# workaround on windows
import os
if os.name == 'nt':
import win32api
win32api.LoadLibrary('test_complete')
win32api.LoadLibrary('test_modules')

import fastdds
import pytest
import time
Expand All @@ -24,7 +16,7 @@ def data_type(request):
pytest.dds_type = __import__("test_complete")
else:
pytest.dds_type = __import__("eprosima.test.test_modules",
fromlist=[None])
fromlist=["test_modules"])

@pytest.fixture
def participant():
Expand Down
2 changes: 1 addition & 1 deletion fastdds_python/test/api/test_datawriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def data_type(request):
pytest.dds_type = __import__("test_complete")
else:
pytest.dds_type = __import__("eprosima.test.test_modules",
fromlist=[None])
fromlist=["test_modules"])

@pytest.fixture
def participant():
Expand Down
2 changes: 1 addition & 1 deletion fastdds_python/test/api/test_domainparticipant.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def data_type(request):
pytest.dds_type = __import__("test_complete")
else:
pytest.dds_type = __import__("eprosima.test.test_modules",
fromlist=[None])
fromlist=["test_modules"])

@pytest.fixture
def not_autoenable_factory():
Expand Down
2 changes: 1 addition & 1 deletion fastdds_python/test/api/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def data_type(request):
pytest.dds_type = __import__("test_complete")
else:
pytest.dds_type = __import__("eprosima.test.test_modules",
fromlist=[None])
fromlist=["test_modules"])

@pytest.fixture
def participant_qos():
Expand Down
6 changes: 0 additions & 6 deletions fastdds_python/test/api/test_qos.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import os

# until https://bugs.python.org/issue46276 is not fixed we can apply this
# workaround on windows
if os.name == 'nt':
import win32api
win32api.LoadLibrary('_fastdds_python.pyd')

import fastdds

import inspect
Expand Down
2 changes: 1 addition & 1 deletion fastdds_python/test/api/test_subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def data_type(request):
pytest.dds_type = __import__("test_complete")
else:
pytest.dds_type = __import__("eprosima.test.test_modules",
fromlist=[None])
fromlist=["test_modules"])

@pytest.fixture
def participant_qos():
Expand Down
10 changes: 1 addition & 9 deletions fastdds_python/test/api/test_waitset.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
# until https://bugs.python.org/issue46276 is not fixed we can apply this
# workaround on windows
import os
if os.name == 'nt':
import win32api
win32api.LoadLibrary('test_complete')
win32api.LoadLibrary('test_modules')

import fastdds
import pytest

Expand All @@ -16,7 +8,7 @@ def data_type(request):
pytest.dds_type = __import__("test_complete")
else:
pytest.dds_type = __import__("eprosima.test.test_modules",
fromlist=[None])
fromlist=["test_modules"])

@pytest.fixture
def participant():
Expand Down
41 changes: 10 additions & 31 deletions fastdds_python/test/types/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

cmake_minimum_required(VERSION 3.22)
cmake_minimum_required(VERSION 3.20)

# SWIG: use standard target name.
if(POLICY CMP0078)
Expand Down Expand Up @@ -73,8 +73,6 @@ SET_SOURCE_FILES_PROPERTIES(
USE_TARGET_INCLUDE_DIRECTORIES TRUE
)

set_property(SOURCE ${PROJECT_NAME}.i PROPERTY OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/eprosima/test2")

SWIG_ADD_LIBRARY(${${PROJECT_NAME}_MODULE}
TYPE SHARED
LANGUAGE python
Expand All @@ -91,11 +89,6 @@ target_link_libraries(${${PROJECT_NAME}_MODULE}
${PROJECT_NAME}
)

set_target_properties(${${PROJECT_NAME}_MODULE}
PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/eprosima/test2"
)


# Find the installation path
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"
OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
Expand All @@ -105,7 +98,7 @@ execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig
get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
SET (PYTHON_MODULE_PATH
${_REL_PYTHON_MODULE_PATH}
${_REL_PYTHON_MODULE_PATH}/eprosima/test2/${PROJECT_NAME}
)

# Install
Expand All @@ -114,9 +107,9 @@ install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION lib/
ARCHIVE DESTINATION lib/
)
install(TARGETS ${${PROJECT_NAME}_MODULE} DESTINATION ${PYTHON_MODULE_PATH}/eprosima/test2)
install(TARGETS ${${PROJECT_NAME}_MODULE} DESTINATION ${PYTHON_MODULE_PATH})
get_property(support_files TARGET ${${PROJECT_NAME}_MODULE} PROPERTY SWIG_SUPPORT_FILES)
install(FILES ${support_files} DESTINATION ${PYTHON_MODULE_PATH}/eprosima/test2)
install(FILES ${support_files} DESTINATION ${PYTHON_MODULE_PATH} RENAME __init__.py)

###############################################################################
# Library for types defined in test_modules IDL
Expand Down Expand Up @@ -180,8 +173,6 @@ SET_SOURCE_FILES_PROPERTIES(
USE_TARGET_INCLUDE_DIRECTORIES TRUE
)

set_property(SOURCE ${PROJECT_NAME}.i PROPERTY OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/eprosima/test")

SWIG_ADD_LIBRARY(${${PROJECT_NAME}_MODULE}
TYPE SHARED
LANGUAGE python
Expand All @@ -198,11 +189,6 @@ target_link_libraries(${${PROJECT_NAME}_MODULE}
${PROJECT_NAME}
)

set_target_properties(${${PROJECT_NAME}_MODULE}
PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/eprosima/test"
)


# Find the installation path
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"
OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
Expand All @@ -212,7 +198,7 @@ execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig
get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
SET (PYTHON_MODULE_PATH
${_REL_PYTHON_MODULE_PATH}
${_REL_PYTHON_MODULE_PATH}/eprosima/test/${PROJECT_NAME}
)

# Install
Expand All @@ -221,9 +207,9 @@ install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION lib/
ARCHIVE DESTINATION lib/
)
install(TARGETS ${${PROJECT_NAME}_MODULE} DESTINATION ${PYTHON_MODULE_PATH}/eprosima/test)
install(TARGETS ${${PROJECT_NAME}_MODULE} DESTINATION ${PYTHON_MODULE_PATH})
get_property(support_files TARGET ${${PROJECT_NAME}_MODULE} PROPERTY SWIG_SUPPORT_FILES)
install(FILES ${support_files} DESTINATION ${PYTHON_MODULE_PATH}/eprosima/test)
install(FILES ${support_files} DESTINATION ${PYTHON_MODULE_PATH} RENAME __init__.py)

###############################################################################
# Library for types defined in test_complete IDL
Expand Down Expand Up @@ -288,8 +274,6 @@ SET_SOURCE_FILES_PROPERTIES(
USE_TARGET_INCLUDE_DIRECTORIES TRUE
)

set_property(SOURCE ${PROJECT_NAME}.i PROPERTY OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/")

SWIG_ADD_LIBRARY(${${PROJECT_NAME}_MODULE}
TYPE SHARED
LANGUAGE python
Expand All @@ -306,11 +290,6 @@ target_link_libraries(${${PROJECT_NAME}_MODULE}
${PROJECT_NAME}
)

set_target_properties(${${PROJECT_NAME}_MODULE}
PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/"
)


# Find the installation path
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"
OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
Expand All @@ -320,7 +299,7 @@ execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig
get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
SET (PYTHON_MODULE_PATH
${_REL_PYTHON_MODULE_PATH}
${_REL_PYTHON_MODULE_PATH}/${PROJECT_NAME}
)

# Install
Expand All @@ -329,7 +308,7 @@ install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION lib/
ARCHIVE DESTINATION lib/
)
install(TARGETS ${${PROJECT_NAME}_MODULE} DESTINATION ${PYTHON_MODULE_PATH}/)
install(TARGETS ${${PROJECT_NAME}_MODULE} DESTINATION ${PYTHON_MODULE_PATH})
get_property(support_files TARGET ${${PROJECT_NAME}_MODULE} PROPERTY SWIG_SUPPORT_FILES)
install(FILES ${support_files} DESTINATION ${PYTHON_MODULE_PATH}/)
install(FILES ${support_files} DESTINATION ${PYTHON_MODULE_PATH} RENAME __init__.py)

10 changes: 9 additions & 1 deletion fastdds_python/test/types/test_complete.i
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@
* This file was generated by the tool fastddsgen.
*/

%module test_complete
%module(moduleimport="if __import__('os').name == 'nt': import win32api; win32api.LoadLibrary('test_complete.dll')\nif __package__ or '.' in __name__:\n from . import _test_completeWrapper\nelse:\n import _test_completeWrapper") test_complete

// If using windows in debug, it would try to use python_d, which would not be found.
%begin %{
#ifdef _MSC_VER
#define SWIG_PYTHON_INTERPRETER_NO_DEBUG
#endif
#include <exception>
%}

// SWIG helper modules
%include "stdint.i"
Expand Down
10 changes: 9 additions & 1 deletion fastdds_python/test/types/test_included_modules.i
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@
* This file was generated by the tool fastddsgen.
*/

%module test_included_modules
%module(moduleimport="if __import__('os').name == 'nt': import win32api; win32api.LoadLibrary('test_included_modules.dll')\nif __package__ or '.' in __name__:\n from . import _test_included_modulesWrapper\nelse:\n import _test_included_modulesWrapper") test_included_modules

// If using windows in debug, it would try to use python_d, which would not be found.
%begin %{
#ifdef _MSC_VER
#define SWIG_PYTHON_INTERPRETER_NO_DEBUG
#endif
#include <exception>
%}

// SWIG helper modules
%include "stdint.i"
Expand Down
10 changes: 9 additions & 1 deletion fastdds_python/test/types/test_modules.i
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@
* This file was generated by the tool fastddsgen.
*/

%module test_modules
%module(moduleimport="if __import__('os').name == 'nt': import win32api; win32api.LoadLibrary('test_modules.dll')\nif __package__ or '.' in __name__:\n from . import _test_modulesWrapper\nelse:\n import _test_modulesWrapper") test_modules

// If using windows in debug, it would try to use python_d, which would not be found.
%begin %{
#ifdef _MSC_VER
#define SWIG_PYTHON_INTERPRETER_NO_DEBUG
#endif
#include <exception>
%}

// SWIG helper modules
%include "stdint.i"
Expand Down
Loading