Skip to content

Commit

Permalink
[python] First time successfully debugging Python extensions from Vis…
Browse files Browse the repository at this point in the history
…ual Studio
  • Loading branch information
KerstinKeller committed Oct 16, 2024
1 parent 1036657 commit 7b0e263
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 246 deletions.
107 changes: 106 additions & 1 deletion lang/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ========================= eCAL LICENSE =================================
#
# Copyright (C) 2016 - 2019 Continental Corporation
# Copyright (C) 2016 - 2024 Continental Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,86 @@ cmake_minimum_required(VERSION 3.18...3.26)
project(ecal_python)
find_package(Python REQUIRED COMPONENTS Development.Module Interpreter)

option(ECAL_PYTHON_BUILD_SAMPLES "Includes the python samples" ON)
option(ECAL_PYTHON_BUILD_TESTS "Includes the python tests" ON)
option(ECAL_PYTHON_HAS_HDF5 "Enables eCAL application cmd line interfaces" ON)

set(ECAL_PYTHON_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})


function(copy_python_code)
set(singleValueArgs TARGET PYTHON_CODE_ROOT)
set(multiValueArgs PYTHON_FILES)
cmake_parse_arguments(ARGUMENTS
""
"${singleValueArgs}"
"${multiValueArgs}" ${ARGN} )

cmake_path(
ABSOLUTE_PATH ARGUMENTS_PYTHON_CODE_ROOT
OUTPUT_VARIABLE absolute_path_python_code_root
)

foreach (python_file ${ARGUMENTS_PYTHON_FILES})
cmake_path(
ABSOLUTE_PATH python_file
OUTPUT_VARIABLE absolute_path_python_file
)

cmake_path(
RELATIVE_PATH absolute_path_python_file
BASE_DIRECTORY ${absolute_path_python_code_root}
OUTPUT_VARIABLE relative_path)


# Now we actually copy the file to the correct directory
set(origin_file ${absolute_path_python_file})
set(destination_file ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>/python/${relative_path})
file(
GENERATE
OUTPUT ${destination_file}
INPUT ${origin_file}
)

endforeach()
endfunction()

function(ecal_python_set_output_directory TARGET_NAME)
set_target_properties(${TARGET_NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY $<IF:$<BOOL:${WIN32}>,${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>/python/ecal,${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/python/ecal>
)
endfunction()

function(ecal_python_add_sample)
set(singleValueArgs PY_FILE TARGET_NAME)
cmake_parse_arguments(ARGUMENTS
""
"${singleValueArgs}"
"" ${ARGN} )

set(ECAL_PYPROJ_FILE ${ARGUMENTS_PY_FILE})
string(UUID ECAL_PYPROJ_GUID NAMESPACE 8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942 NAME ${ARGUMENTS_TARGET_NAME}
TYPE MD5)
get_target_property(ECAL_PYPROJ_INTERPRETER_DEBUG Python::Interpreter LOCATION)
get_target_property(ECAL_PYPROJ_INTERPRETER_RELEASE Python::Interpreter LOCATION)
set(ECAL_PYPROJ_NAME ${ARGUMENTS_TARGET_NAME})
set(ECAL_PYPROJ_PYTHON_VERSION ${Python_VERSION})
set(ECAL_PYPROJ_SEARCH_PATH_DEBUG ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Debug/python)
set(ECAL_PYPROJ_SEARCH_PATH_RELEASE ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Release/python)
set(ECAL_PYPROJ_SEARCH_PATH_RELWITHDEBINFO ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/RelWithDebInfo/python/ecal)

set(generated_pyproj_path ${CMAKE_CURRENT_BINARY_DIR}/${ARGUMENTS_TARGET_NAME}.pyproj)

# Generate the .pyproj file from the template
configure_file(
${ECAL_PYTHON_DIRECTORY}/sample.pyproj.in
${generated_pyproj_path}
@ONLY
)

include_external_msproject(${ARGUMENTS_TARGET_NAME} ${generated_pyproj_path})
endfunction()

# Convenience target to have all Python targets buildable via one name
add_custom_target(${PROJECT_NAME})

Expand All @@ -47,3 +127,28 @@ else()
message(WARNING "Building Python bindings without HDF5 support")
endif()

if (ECAL_PYTHON_BUILD_SAMPLES)
add_subdirectory(samples)
endif()

if (WIN32)
add_custom_target(copy_ecal_core_dll ALL
COMMAND cmake -E copy_if_different "$<TARGET_FILE:eCAL::core>" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>/python/ecal"
COMMENT "Copy eCAL Core DLL to python directory"
DEPENDS eCAL::core
)
set_property(TARGET copy_ecal_core_dll PROPERTY FOLDER lang/python/core)
endif()

#This is a really hacky way. Let's do this differently.
if (WIN32 AND HAS_HDF5)
if (TARGET hdf5::hdf5-shared)
add_custom_target(copy_hdf5_dll ALL
COMMAND cmake -E copy_if_different "$<TARGET_FILE:hdf5::hdf5-shared>" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>/python/ecal"
COMMENT "Copy eCAL Core DLL to python directory"
DEPENDS hdf5::hdf5-shared
)
set_property(TARGET copy_hdf5_dll PROPERTY FOLDER lang/python/core)
endif()
endif()

51 changes: 41 additions & 10 deletions lang/python/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ========================= eCAL LICENSE =================================
#
# Copyright (C) 2016 - 2019 Continental Corporation
# Copyright (C) 2016 - 2024 Continental Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,33 +18,64 @@

project(_ecal_core_py)

######
# build
######
python_add_library(${PROJECT_NAME} MODULE WITH_SOABI
src/ecal_clang.cpp
src/ecal_clang.h
src/ecal_wrap.cxx
)

set(python_files
ecal/__init__.py
ecal/core/__init__.py
ecal/core/core.py
ecal/core/publisher.py
ecal/core/service.py
ecal/core/subscriber.py
ecal/proto/__init__.py
ecal/proto/helper.py
)

target_sources(${PROJECT_NAME}
PUBLIC
FILE_SET ecal_core_python_files
TYPE HEADERS
BASE_DIRS .
FILES
${python_files}
)

target_link_libraries(${PROJECT_NAME}
PRIVATE
eCAL::core
)

target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)

set_target_properties(${PROJECT_NAME} PROPERTIES
FOLDER lang/python/core
)

########
# installation
########
install(TARGETS ${PROJECT_NAME} core
RUNTIME DESTINATION ecal COMPONENT python EXCLUDE_FROM_ALL
LIBRARY DESTINATION ecal COMPONENT python EXCLUDE_FROM_ALL NAMELINK_SKIP
)

if(ECAL_INCLUDE_PY_SAMPLES)
if(WIN32)

include_external_msproject(ecal_core_py ${CMAKE_CURRENT_SOURCE_DIR}/ecal_core_py.pyproj)
set_property(TARGET ecal_core_py PROPERTY FOLDER lang/python/core)
##############
# IDE appearance
##############
set_target_properties(${PROJECT_NAME} PROPERTIES
FOLDER lang/python/core
)
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" PREFIX "Python Files" FILES ${python_files})


##########
# IDE Debugging / Runtime
###########
copy_python_code(TARGET ${PROJECT_NAME} PYTHON_FILES ${python_files} PYTHON_CODE_ROOT .)

endif()
endif()
ecal_python_set_output_directory(${PROJECT_NAME})
47 changes: 36 additions & 11 deletions lang/python/ecalhdf5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ========================= eCAL LICENSE =================================
#
# Copyright (C) 2016 - 2019 Continental Corporation
# Copyright (C) 2016 - 2024 Continental Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,21 +18,39 @@

project(_ecal_hdf5_py)

######
# build
######
python_add_library(${PROJECT_NAME} MODULE WITH_SOABI
src/ecalhdf5_wrap.cxx
)

set(python_files
ecal/measurement/hdf5.py
ecal/measurement/measurement.py
ecal/measurement/writer.py
ecal/measurement/__init__.py
)

target_sources(${PROJECT_NAME}
PUBLIC
FILE_SET ecal_hdf5_python_files
TYPE HEADERS
BASE_DIRS .
FILES
${python_files}
)

target_link_libraries(${PROJECT_NAME}
PRIVATE
eCAL::hdf5
)

target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)

set_target_properties(${PROJECT_NAME} PROPERTIES
FOLDER lang/python/hdf5
)

########
# installation
########
if(TARGET hdf5-shared)
install(TARGETS ${PROJECT_NAME} hdf5-shared
RUNTIME DESTINATION ecal COMPONENT python EXCLUDE_FROM_ALL
Expand All @@ -44,11 +62,18 @@ install(TARGETS ${PROJECT_NAME}
DESTINATION ecal COMPONENT python EXCLUDE_FROM_ALL
)

if(ECAL_INCLUDE_PY_SAMPLES)
if(WIN32)

include_external_msproject(ecal_hdf5_py ${CMAKE_CURRENT_SOURCE_DIR}/ecal_hdf5_py.pyproj)
set_property(TARGET ecal_hdf5_py PROPERTY FOLDER lang/python/hdf5)
##############
# IDE appearance
##############
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" PREFIX "Python Files" FILES ${python_files})
set_target_properties(${PROJECT_NAME} PROPERTIES
FOLDER lang/python/hdf5
)

##########
# IDE Debugging / Runtime
###########
copy_python_code(TARGET ${PROJECT_NAME} PYTHON_FILES ${python_files} PYTHON_CODE_ROOT .)

endif()
endif()
ecal_python_set_output_directory(${PROJECT_NAME})
50 changes: 50 additions & 0 deletions lang/python/sample.pyproj.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == 'Release' "></Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>@ECAL_PYPROJ_GUID@</ProjectGuid>
<ProjectHome>.</ProjectHome>
<StartupFile>@ECAL_PYPROJ_FILE@</StartupFile>
<SearchPath>@ECAL_PYPROJ_SEARCH_PATH_RELEASE@</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<Name>@ECAL_PYPROJ_NAME@</Name>
<RootNamespace>@ECAL_PYPROJ_NAME@</RootNamespace>
<InterpreterId>MSBuild|debugging-env|$(MSBuildProjectFullPath)</InterpreterId>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'RelWithDebInfo' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Compile Include="@ECAL_PYPROJ_FILE@" />
</ItemGroup>
<ItemGroup>
<Interpreter Include="C:/Users/uidv8497/AppData/Local/Programs/Python/Python312">
<Id>debugging-env</Id>
<Version>0.0</Version>
<Description>eCAL Debugging Environment (Python @ECAL_PYPROJ_PYTHON_VERSION@ )</Description>
<InterpreterPath>@ECAL_PYPROJ_INTERPRETER_RELEASE@</InterpreterPath>
<WindowsInterpreterPath>@ECAL_PYPROJ_INTERPRETER_RELEASE@</WindowsInterpreterPath>
<PathEnvironmentVariable>PYTHONPATH</PathEnvironmentVariable>
<Architecture>X64</Architecture>
</Interpreter>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
<!-- Uncomment the CoreCompile target to enable the Build command in
Visual Studio and specify your pre- and post-build commands in
the BeforeBuild and AfterBuild targets below. -->
<!--<Target Name="CoreCompile" />-->
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
</Project>
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ sdist.only-include = [
"/README.md",
"/app/app_pb/",
"/app/apps/", # TODO: Remove this directory
"/app/rec/rec_addon_*/", # TODO: Remove this directory
"/app/rec/rec_addon_core/", # TODO: Remove this directory
"/app/rec/rec_addon_dummy/", # TODO: Remove this directory
"/cmake/",
"/contrib/",
"/cpack/",
Expand Down
37 changes: 15 additions & 22 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,26 @@ endif(HAS_QT)
if(BUILD_PY_BINDING)
if(WIN32)
# benchmarks
add_subdirectory(python/benchmarks/latency_rec)
add_subdirectory(python/benchmarks/latency_rec_cb)
add_subdirectory(python/benchmarks/latency_snd)

# measurement
if(HAS_HDF5)
add_subdirectory(python/measurement/ecalhdf5_rw)
add_subdirectory(python/measurement/ecalhdf5_protobuf_rw)
add_subdirectory(python/measurement/measurement_read)
endif(HAS_HDF5)
#add_subdirectory(python/benchmarks/latency_rec)
#add_subdirectory(python/benchmarks/latency_rec_cb)
#add_subdirectory(python/benchmarks/latency_snd)

# monitoring
add_subdirectory(python/monitoring/monitoring)
add_subdirectory(python/monitoring/monitoring_json)
#add_subdirectory(python/monitoring/monitoring)
#add_subdirectory(python/monitoring/monitoring_json)

# pubsub
add_subdirectory(python/pubsub/protobuf/person_rec)
add_subdirectory(python/pubsub/protobuf/person_rec_cb)
add_subdirectory(python/pubsub/protobuf/person_snd)
add_subdirectory(python/pubsub/string/minimal_rec)
add_subdirectory(python/pubsub/string/minimal_rec_cb)
add_subdirectory(python/pubsub/string/minimal_snd)
add_subdirectory(python/pubsub/binary/binary_rec)
add_subdirectory(python/pubsub/binary/binary_rec_cb)
add_subdirectory(python/pubsub/binary/binary_snd)
#add_subdirectory(python/pubsub/protobuf/person_rec)
#add_subdirectory(python/pubsub/protobuf/person_rec_cb)
#add_subdirectory(python/pubsub/protobuf/person_snd)
#add_subdirectory(python/pubsub/string/minimal_rec)
#add_subdirectory(python/pubsub/string/minimal_rec_cb)
#add_subdirectory(python/pubsub/string/minimal_snd)
#add_subdirectory(python/pubsub/binary/binary_rec)
#add_subdirectory(python/pubsub/binary/binary_rec_cb)
#add_subdirectory(python/pubsub/binary/binary_snd)

# services
add_subdirectory(python/services/minimal_service)
#add_subdirectory(python/services/minimal_service)
endif(WIN32)
endif(BUILD_PY_BINDING)
Loading

0 comments on commit 7b0e263

Please sign in to comment.