bindings: replace pybindgen bindings support with cppyy bindings
This commit is contained in:
@@ -160,11 +160,6 @@ macro(write_configtable)
|
||||
string(APPEND out "PyViz visualizer : ")
|
||||
check_on_or_off("${NS3_VISUALIZER}" "${ENABLE_VISUALIZER}")
|
||||
|
||||
string(APPEND out "Python API Scanning Support : ")
|
||||
check_on_or_off(
|
||||
"${NS3_SCAN_PYTHON_BINDINGS}" "${ENABLE_SCAN_PYTHON_BINDINGS}"
|
||||
)
|
||||
|
||||
string(APPEND out "Python Bindings : ")
|
||||
check_on_or_off("${NS3_PYTHON_BINDINGS}" "${ENABLE_PYTHON_BINDINGS}")
|
||||
|
||||
|
||||
@@ -69,9 +69,6 @@ function(write_lock)
|
||||
cache_cmake_flag(NS3_BRITE "ENABLE_BRITE" lock_contents)
|
||||
cache_cmake_flag(NS3_ENABLE_SUDO "ENABLE_SUDO" lock_contents)
|
||||
cache_cmake_flag(NS3_PYTHON_BINDINGS "ENABLE_PYTHON_BINDINGS" lock_contents)
|
||||
cache_cmake_flag(
|
||||
NS3_SCAN_PYTHON_BINDINGS "ENABLE_SCAN_PYTHON_BINDINGS" lock_contents
|
||||
)
|
||||
|
||||
string(APPEND lock_contents "EXAMPLE_DIRECTORIES = [")
|
||||
foreach(example_folder ${ns3-example-folders})
|
||||
|
||||
@@ -276,198 +276,6 @@ function(build_lib)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
# Get architecture pair for python bindings
|
||||
if((${CMAKE_SIZEOF_VOID_P} EQUAL 8) AND (NOT APPLE))
|
||||
set(arch gcc_LP64)
|
||||
set(arch_flags -m64)
|
||||
else()
|
||||
set(arch gcc_ILP32)
|
||||
set(arch_flags)
|
||||
endif()
|
||||
|
||||
# Add target to scan python bindings
|
||||
if(${ENABLE_SCAN_PYTHON_BINDINGS}
|
||||
AND (EXISTS ${CMAKE_HEADER_OUTPUT_DIRECTORY}/${BLIB_LIBNAME}-module.h)
|
||||
AND (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/bindings")
|
||||
)
|
||||
set(bindings_output_folder ${PROJECT_SOURCE_DIR}/${FOLDER}/bindings)
|
||||
file(MAKE_DIRECTORY ${bindings_output_folder})
|
||||
set(module_api_ILP32 ${bindings_output_folder}/modulegen__gcc_ILP32.py)
|
||||
set(module_api_LP64 ${bindings_output_folder}/modulegen__gcc_LP64.py)
|
||||
|
||||
set(modulescan_modular_command
|
||||
${Python3_EXECUTABLE}
|
||||
${PROJECT_SOURCE_DIR}/bindings/python/ns3modulescan-modular.py
|
||||
)
|
||||
|
||||
set(header_map "")
|
||||
# We need a python map that takes header.h to module e.g. "ptr.h": "core"
|
||||
foreach(header ${BLIB_HEADER_FILES})
|
||||
# header is a relative path to the current working directory
|
||||
get_filename_component(
|
||||
header_name ${CMAKE_CURRENT_SOURCE_DIR}/${header} NAME
|
||||
)
|
||||
string(APPEND header_map "\"${header_name}\":\"${BLIB_LIBNAME}\",")
|
||||
endforeach()
|
||||
|
||||
set(ns3-headers-to-module-map "${ns3-headers-to-module-map}${header_map}"
|
||||
CACHE INTERNAL "Map connecting headers to their modules"
|
||||
)
|
||||
|
||||
# API scan needs the include directories to find a few headers (e.g. mpi.h)
|
||||
get_target_includes(${lib${BLIB_LIBNAME}} modulegen_include_dirs)
|
||||
|
||||
set(module_to_generate_api ${module_api_ILP32})
|
||||
set(LP64toILP32)
|
||||
if("${arch}" STREQUAL "gcc_LP64")
|
||||
set(module_to_generate_api ${module_api_LP64})
|
||||
set(LP64toILP32
|
||||
${Python3_EXECUTABLE}
|
||||
${PROJECT_SOURCE_DIR}/build-support/pybindings-LP64-to-ILP32.py
|
||||
${module_api_LP64} ${module_api_ILP32}
|
||||
)
|
||||
endif()
|
||||
|
||||
add_custom_target(
|
||||
${lib${BLIB_LIBNAME}}-apiscan
|
||||
COMMAND
|
||||
${modulescan_modular_command} ${CMAKE_OUTPUT_DIRECTORY} ${BLIB_LIBNAME}
|
||||
${PROJECT_BINARY_DIR}/header_map.json ${module_to_generate_api}
|
||||
\"${arch_flags} ${modulegen_include_dirs}\" 2>
|
||||
${bindings_output_folder}/apiscan.log
|
||||
COMMAND ${LP64toILP32}
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
DEPENDS ${lib${BLIB_LIBNAME}}
|
||||
)
|
||||
add_dependencies(apiscan-all ${lib${BLIB_LIBNAME}}-apiscan)
|
||||
endif()
|
||||
|
||||
# Build pybindings if requested and if bindings subfolder exists in
|
||||
# NS3/src/BLIB_LIBNAME
|
||||
if(${ENABLE_PYTHON_BINDINGS} AND (EXISTS
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/bindings")
|
||||
)
|
||||
set(bindings_output_folder ${CMAKE_OUTPUT_DIRECTORY}/${FOLDER}/bindings)
|
||||
file(MAKE_DIRECTORY ${bindings_output_folder})
|
||||
set(module_src ${bindings_output_folder}/ns3module.cc)
|
||||
set(module_hdr ${bindings_output_folder}/ns3module.h)
|
||||
|
||||
string(REPLACE "-" "_" BLIB_LIBNAME_sub ${BLIB_LIBNAME}) # '-' causes
|
||||
# problems (e.g.
|
||||
# csma-layout), replace with '_' (e.g. csma_layout)
|
||||
|
||||
# Set prefix of binding to _ if a ${BLIB_LIBNAME}.py exists, and copy the
|
||||
# ${BLIB_LIBNAME}.py to the output folder
|
||||
set(prefix)
|
||||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/bindings/${BLIB_LIBNAME}.py)
|
||||
set(prefix _)
|
||||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/bindings/${BLIB_LIBNAME}.py
|
||||
DESTINATION ${CMAKE_OUTPUT_DIRECTORY}/bindings/python/ns
|
||||
)
|
||||
endif()
|
||||
|
||||
# Run modulegen-modular to generate the bindings sources
|
||||
if((NOT EXISTS ${module_hdr}) OR (NOT EXISTS ${module_src})) # OR TRUE) # to
|
||||
# force
|
||||
# reprocessing
|
||||
string(REPLACE ";" "," ENABLED_FEATURES
|
||||
"${ns3-libs};${BLIB_MODULE_ENABLED_FEATURES}"
|
||||
)
|
||||
set(modulegen_modular_command
|
||||
GCC_RTTI_ABI_COMPLETE=True NS3_ENABLED_FEATURES="${ENABLED_FEATURES}"
|
||||
${Python3_EXECUTABLE}
|
||||
${PROJECT_SOURCE_DIR}/bindings/python/ns3modulegen-modular.py
|
||||
)
|
||||
execute_process(
|
||||
COMMAND
|
||||
${CMAKE_COMMAND} -E env
|
||||
PYTHONPATH=${CMAKE_OUTPUT_DIRECTORY}:$ENV{PYTHONPATH}
|
||||
${modulegen_modular_command} ${CMAKE_CURRENT_SOURCE_DIR} ${arch}
|
||||
${prefix}${BLIB_LIBNAME_sub} ${module_src}
|
||||
TIMEOUT 60
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
OUTPUT_FILE ${module_hdr}
|
||||
ERROR_FILE ${bindings_output_folder}/ns3modulegen.log
|
||||
RESULT_VARIABLE error_code
|
||||
)
|
||||
if(${error_code} OR NOT (EXISTS ${module_hdr}))
|
||||
# Delete broken bindings to make sure we will his this error again if
|
||||
# nothing changed
|
||||
if(EXISTS ${module_src})
|
||||
file(REMOVE ${module_src})
|
||||
endif()
|
||||
if(EXISTS ${module_hdr})
|
||||
file(REMOVE ${module_hdr})
|
||||
endif()
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Something went wrong during processing of the python bindings of module ${BLIB_LIBNAME}."
|
||||
" Make sure you have the latest version of Pybindgen."
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Add core module helper sources
|
||||
set(python_module_files ${module_hdr} ${module_src})
|
||||
file(GLOB custom_python_module_files
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/bindings/*.cc
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/bindings/*.h
|
||||
)
|
||||
list(APPEND python_module_files ${custom_python_module_files})
|
||||
set(bindings-name lib${BLIB_LIBNAME}-bindings)
|
||||
add_library(${bindings-name} SHARED "${python_module_files}")
|
||||
target_include_directories(
|
||||
${bindings-name} PUBLIC ${Python3_INCLUDE_DIRS} ${bindings_output_folder}
|
||||
)
|
||||
|
||||
# If there is any, remove the "lib" prefix of libraries (search for
|
||||
# "set(lib${BLIB_LIBNAME}")
|
||||
list(LENGTH ns_libraries_to_link num_libraries)
|
||||
if(num_libraries GREATER "0")
|
||||
string(REPLACE ";" "-bindings;" bindings_to_link
|
||||
"${ns_libraries_to_link};"
|
||||
) # add -bindings suffix to all lib${name}
|
||||
endif()
|
||||
target_link_libraries(
|
||||
${bindings-name}
|
||||
PUBLIC ${LIB_AS_NEEDED_PRE} ${lib${BLIB_LIBNAME}} "${bindings_to_link}"
|
||||
"${BLIB_LIBRARIES_TO_LINK}" ${LIB_AS_NEEDED_POST}
|
||||
PRIVATE ${Python3_LIBRARIES}
|
||||
)
|
||||
target_include_directories(
|
||||
${bindings-name} PRIVATE ${PROJECT_SOURCE_DIR}/src/core/bindings
|
||||
)
|
||||
|
||||
set(suffix)
|
||||
if(APPLE)
|
||||
# Python doesn't like Apple's .dylib and will refuse to load bindings
|
||||
# unless its an .so
|
||||
set(suffix SUFFIX .so)
|
||||
endif()
|
||||
|
||||
# Set binding library name and output folder
|
||||
set_target_properties(
|
||||
${bindings-name}
|
||||
PROPERTIES OUTPUT_NAME ${prefix}${BLIB_LIBNAME_sub}
|
||||
PREFIX ""
|
||||
${suffix} LIBRARY_OUTPUT_DIRECTORY
|
||||
${CMAKE_OUTPUT_DIRECTORY}/bindings/python/ns
|
||||
)
|
||||
|
||||
set(ns3-python-bindings-modules
|
||||
"${bindings-name};${ns3-python-bindings-modules}"
|
||||
CACHE INTERNAL "list of modules python bindings"
|
||||
)
|
||||
|
||||
# Make sure all bindings are built before building the visualizer module
|
||||
# that makes use of them
|
||||
if(${ENABLE_VISUALIZER} AND (visualizer IN_LIST libs_to_build))
|
||||
if(NOT (${BLIB_LIBNAME} STREQUAL visualizer))
|
||||
add_dependencies(${libvisualizer} ${bindings-name})
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Handle package export
|
||||
install(
|
||||
TARGETS ${lib${BLIB_LIBNAME}}
|
||||
|
||||
@@ -766,15 +766,15 @@ macro(process_options)
|
||||
if(${NS3_PYTHON_BINDINGS})
|
||||
if(NOT ${Python3_FOUND})
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Bindings: python bindings require Python, but it could not be found"
|
||||
${HIGHLIGHTED_STATUS}
|
||||
"Bindings: python bindings require Python, but it could not be found"
|
||||
)
|
||||
else()
|
||||
check_python_packages("pybindgen" missing_packages)
|
||||
check_python_packages("cppyy" missing_packages)
|
||||
if(missing_packages)
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Bindings: python bindings disabled due to the following missing dependencies: ${missing_packages}"
|
||||
${HIGHLIGHTED_STATUS}
|
||||
"Bindings: python bindings disabled due to the following missing dependencies: ${missing_packages}"
|
||||
)
|
||||
else()
|
||||
set(ENABLE_PYTHON_BINDINGS ON)
|
||||
@@ -794,44 +794,6 @@ macro(process_options)
|
||||
add_compile_options(-Wno-potentially-evaluated-expression)
|
||||
endif()
|
||||
|
||||
set(ENABLE_SCAN_PYTHON_BINDINGS OFF)
|
||||
if(${NS3_SCAN_PYTHON_BINDINGS})
|
||||
if(NOT ${Python3_FOUND})
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Bindings: scanning python bindings require Python, but it could not be found"
|
||||
)
|
||||
else()
|
||||
# Check if pybindgen, pygccxml, cxxfilt and castxml are installed
|
||||
check_python_packages(
|
||||
"pybindgen;pygccxml;cxxfilt;castxml" missing_packages
|
||||
)
|
||||
|
||||
# If castxml has not been found via python import, fallback to searching
|
||||
# the binary
|
||||
if(castxml IN_LIST missing_packages)
|
||||
mark_as_advanced(CASTXML)
|
||||
find_program(CASTXML castxml)
|
||||
if(NOT ("${CASTXML}" STREQUAL "CASTXML-NOTFOUND"))
|
||||
list(REMOVE_ITEM missing_packages castxml)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# If packages were not found, print message
|
||||
if(missing_packages)
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Bindings: scanning of python bindings disabled due to the following missing dependencies: ${missing_packages}"
|
||||
)
|
||||
else()
|
||||
set(ENABLE_SCAN_PYTHON_BINDINGS ON)
|
||||
# empty scan target that will depend on other module scan targets to
|
||||
# scan all of them
|
||||
add_custom_target(apiscan-all)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(ENABLE_VISUALIZER FALSE)
|
||||
if(${NS3_VISUALIZER})
|
||||
if((NOT ${ENABLE_PYTHON_BINDINGS}) OR (NOT ${Python3_FOUND}))
|
||||
@@ -2079,20 +2041,6 @@ function(find_external_library)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(write_header_to_modules_map)
|
||||
if(${ENABLE_SCAN_PYTHON_BINDINGS})
|
||||
set(header_map ${ns3-headers-to-module-map})
|
||||
|
||||
# Trim last comma
|
||||
string(LENGTH "${header_map}" header_map_len)
|
||||
math(EXPR header_map_len "${header_map_len}-1")
|
||||
string(SUBSTRING "${header_map}" 0 ${header_map_len} header_map)
|
||||
|
||||
# Then write to header_map.json for consumption of pybindgen
|
||||
file(WRITE ${PROJECT_BINARY_DIR}/header_map.json "{${header_map}}")
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(get_target_includes target output)
|
||||
set(include_directories)
|
||||
get_target_property(include_dirs ${target} INCLUDE_DIRECTORIES)
|
||||
|
||||
Reference in New Issue
Block a user