From a25e553fa279092d6871236d2b5e8b23de71e9af Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Date: Thu, 4 Aug 2022 15:21:11 -0300 Subject: [PATCH] build, bindings: check if cppyy is available at runtime and format cmake --- bindings/python/ns__init__.py | 8 +++++++- build-support/macros-and-definitions.cmake | 12 +++++++----- examples/CMakeLists.txt | 1 - examples/realtime/CMakeLists.txt | 6 +++--- examples/wireless/CMakeLists.txt | 1 - src/tap-bridge/CMakeLists.txt | 4 +++- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/bindings/python/ns__init__.py b/bindings/python/ns__init__.py index f859c83d4..5e80cab30 100644 --- a/bindings/python/ns__init__.py +++ b/bindings/python/ns__init__.py @@ -33,7 +33,13 @@ def load_modules(): ns3_output_directory = values["out_dir"] libraries = {x.split(".")[0]: x for x in os.listdir(os.path.join(ns3_output_directory, "lib"))} - import cppyy + # Try to import Cppyy and warn the user in case it is not found + try: + import cppyy + except ModuleNotFoundError: + print("Cppyy is required by the ns-3 python bindings.") + print("You can install it with the following command: pip install cppyy") + exit(-1) # Enable full logs for debugging # cppyy.set_debug(True) diff --git a/build-support/macros-and-definitions.cmake b/build-support/macros-and-definitions.cmake index 509d8f018..313149994 100644 --- a/build-support/macros-and-definitions.cmake +++ b/build-support/macros-and-definitions.cmake @@ -756,12 +756,14 @@ macro(process_options) ) else() set(ENABLE_PYTHON_BINDINGS ON) - - set(destination_dir ${CMAKE_OUTPUT_DIRECTORY}/bindings/python/ns) - configure_file( - bindings/python/ns__init__.py ${destination_dir}/__init__.py COPYONLY - ) endif() + + # Copy the bindings file if we have python, which will prevent python + # scripts from failing due to the missing ns package + set(destination_dir ${CMAKE_OUTPUT_DIRECTORY}/bindings/python/ns) + configure_file( + bindings/python/ns__init__.py ${destination_dir}/__init__.py COPYONLY + ) endif() endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 1eb795c5c..224711434 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -13,7 +13,6 @@ if(${ENABLE_EXAMPLES}) CACHE INTERNAL "list of example folders" ) endforeach() - scan_python_examples(${CMAKE_CURRENT_SOURCE_DIR}) endif() scan_python_examples(${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/examples/realtime/CMakeLists.txt b/examples/realtime/CMakeLists.txt index 8124d5209..192706240 100644 --- a/examples/realtime/CMakeLists.txt +++ b/examples/realtime/CMakeLists.txt @@ -1,7 +1,7 @@ build_example( -NAME realtime-udp-echo -SOURCE_FILES realtime-udp-echo.cc -LIBRARIES_TO_LINK + NAME realtime-udp-echo + SOURCE_FILES realtime-udp-echo.cc + LIBRARIES_TO_LINK ${libcsma} ${libinternet} ${libapplications} diff --git a/examples/wireless/CMakeLists.txt b/examples/wireless/CMakeLists.txt index cad81d69f..8d78a00fa 100644 --- a/examples/wireless/CMakeLists.txt +++ b/examples/wireless/CMakeLists.txt @@ -291,4 +291,3 @@ build_example( SOURCE_FILES wifi-ofdm-eht-validation.cc LIBRARIES_TO_LINK ${libwifi} ) - diff --git a/src/tap-bridge/CMakeLists.txt b/src/tap-bridge/CMakeLists.txt index a0aa5588c..75151b076 100644 --- a/src/tap-bridge/CMakeLists.txt +++ b/src/tap-bridge/CMakeLists.txt @@ -1,4 +1,6 @@ -if(NOT ${ENABLE_EMU}) +if(NOT + ${ENABLE_EMU} +) message( ${HIGHLIGHTED_STATUS} "tap-bridge requires emulation feature"