From 2dff09fe1fcba23dd05a69a64669f43fe81f6116 Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Fri, 21 Feb 2025 01:33:06 +0530 Subject: [PATCH 1/3] Added XEUS_SEARCH_PATH --- CMakeLists.txt | 9 +++++++++ include/xeus-cpp/xinterpreter.hpp | 1 + src/xinterpreter.cpp | 15 +++++++++++++++ test/CMakeLists.txt | 11 +++++++++++ test/custom_includes/test_header.hpp | 8 ++++++++ test/test_include_paths.cpp | 8 ++++++++ 6 files changed, 52 insertions(+) create mode 100644 test/custom_includes/test_header.hpp create mode 100644 test/test_include_paths.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 39798ec2..840ebb9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -460,6 +460,7 @@ if(EMSCRIPTEN) # 3) Shift the resource dir and the sysroot to a common location. # 4) Preload everything required together. endif() + # Tests # ===== @@ -467,6 +468,14 @@ if(XEUS_CPP_BUILD_TESTS) add_subdirectory(test) endif() +set(XEUS_SEARCH_PATH $,:>) + +if (NOT EMSCRIPTEN) + set(XEUS_SEARCH_PATH "${XEUS_SEARCH_PATH}:${CMAKE_CURRENT_SOURCE_DIR}/src") +endif() + +target_compile_definitions(xeus-cpp PRIVATE "XEUS_SEARCH_PATH=\"${XEUS_SEARCH_PATH}\"") + # Installation # ============ include(CMakePackageConfigHelpers) diff --git a/include/xeus-cpp/xinterpreter.hpp b/include/xeus-cpp/xinterpreter.hpp index 25eeb049..20c2ab4b 100644 --- a/include/xeus-cpp/xinterpreter.hpp +++ b/include/xeus-cpp/xinterpreter.hpp @@ -12,6 +12,7 @@ #define XEUS_CPP_INTERPRETER_HPP #include +#include #include #include #include diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 5ed527e3..f099cd36 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -112,6 +112,7 @@ __get_cxx_version () createInterpreter(Args(argv ? argv + 1 : argv, argv + argc)); m_version = get_stdopt(); redirect_output(); + init_includes(); init_preamble(); init_magic(); } @@ -356,6 +357,20 @@ __get_cxx_version () publish_stream("stderr", s); } + void interpreter::init_includes() + { + Cpp::AddIncludePath((xeus::prefix_path() + "/include/").c_str()); + if (const char* paths = std::getenv("XEUS_SEARCH_PATH")) { + std::istringstream stream(paths); + std::string path; + char delimiter = (std::string(paths).find(';') != std::string::npos) ? ';' : ':'; + + while (std::getline(stream, path, delimiter)) + if (!path.empty()) + Cpp::AddIncludePath(path.c_str()); + } + } + void interpreter::init_preamble() { //NOLINTBEGIN(cppcoreguidelines-owning-memory) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8ef68992..268c7d40 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -67,3 +67,14 @@ target_link_libraries(test_xeus_cpp xeus-cpp doctest::doctest ${CMAKE_THREAD_LIB target_include_directories(test_xeus_cpp PRIVATE ${XEUS_CPP_INCLUDE_DIR}) add_custom_target(xtest COMMAND test_xeus_cpp DEPENDS test_xeus_cpp) + +# Test for non-standard include paths +add_executable(test_include_paths test_include_paths.cpp) +target_link_libraries(test_include_paths PRIVATE doctest::doctest) +target_include_directories(test_include_paths PRIVATE + ${CMAKE_SOURCE_DIR}/test/custom_includes + ${DOCTEST_INCLUDE_DIR} +) +target_compile_definitions(test_include_paths PRIVATE DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN) +add_test(NAME test_include_paths COMMAND test_include_paths) + diff --git a/test/custom_includes/test_header.hpp b/test/custom_includes/test_header.hpp new file mode 100644 index 00000000..a6e34940 --- /dev/null +++ b/test/custom_includes/test_header.hpp @@ -0,0 +1,8 @@ +#ifndef TEST_HEADER_HPP +#define TEST_HEADER_HPP + +namespace test_ns { + constexpr int test_value = 42; +} + +#endif \ No newline at end of file diff --git a/test/test_include_paths.cpp b/test/test_include_paths.cpp new file mode 100644 index 00000000..36d1fae3 --- /dev/null +++ b/test/test_include_paths.cpp @@ -0,0 +1,8 @@ +#include +#include +#include "test_header.hpp" + +TEST_CASE("Test non-standard include paths") +{ + CHECK(test_ns::test_value == 42); +} \ No newline at end of file From 3fa8518279ee1e13ed6e7f7b72bb113e0a1ff6a9 Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Fri, 21 Feb 2025 03:19:33 +0530 Subject: [PATCH 2/3] refactoring --- CMakeLists.txt | 8 -------- src/xinterpreter.cpp | 11 ++++++++--- test/CMakeLists.txt | 7 +++++++ test/custom_includes/test_header.hpp | 3 ++- test/test_include_paths.cpp | 2 +- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 840ebb9d..f6c4a078 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -468,14 +468,6 @@ if(XEUS_CPP_BUILD_TESTS) add_subdirectory(test) endif() -set(XEUS_SEARCH_PATH $,:>) - -if (NOT EMSCRIPTEN) - set(XEUS_SEARCH_PATH "${XEUS_SEARCH_PATH}:${CMAKE_CURRENT_SOURCE_DIR}/src") -endif() - -target_compile_definitions(xeus-cpp PRIVATE "XEUS_SEARCH_PATH=\"${XEUS_SEARCH_PATH}\"") - # Installation # ============ include(CMakePackageConfigHelpers) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index f099cd36..4a347fde 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -360,14 +360,19 @@ __get_cxx_version () void interpreter::init_includes() { Cpp::AddIncludePath((xeus::prefix_path() + "/include/").c_str()); - if (const char* paths = std::getenv("XEUS_SEARCH_PATH")) { + if (const char* paths = std::getenv("XEUS_SEARCH_PATH")) + { std::istringstream stream(paths); std::string path; char delimiter = (std::string(paths).find(';') != std::string::npos) ? ';' : ':'; - + while (std::getline(stream, path, delimiter)) - if (!path.empty()) + { + if (!path.empty()) + { Cpp::AddIncludePath(path.c_str()); + } + } } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 268c7d40..e323a468 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -78,3 +78,10 @@ target_include_directories(test_include_paths PRIVATE target_compile_definitions(test_include_paths PRIVATE DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN) add_test(NAME test_include_paths COMMAND test_include_paths) +set(XEUS_SEARCH_PATH $,:>) + +if (NOT EMSCRIPTEN) + set(XEUS_SEARCH_PATH "${XEUS_SEARCH_PATH}:${CMAKE_CURRENT_SOURCE_DIR}/src") +endif() + +target_compile_definitions(xeus-cpp PRIVATE "XEUS_SEARCH_PATH=\"${XEUS_SEARCH_PATH}\"") \ No newline at end of file diff --git a/test/custom_includes/test_header.hpp b/test/custom_includes/test_header.hpp index a6e34940..16bded66 100644 --- a/test/custom_includes/test_header.hpp +++ b/test/custom_includes/test_header.hpp @@ -1,7 +1,8 @@ #ifndef TEST_HEADER_HPP #define TEST_HEADER_HPP -namespace test_ns { +namespace test_ns +{ constexpr int test_value = 42; } diff --git a/test/test_include_paths.cpp b/test/test_include_paths.cpp index 36d1fae3..95580364 100644 --- a/test/test_include_paths.cpp +++ b/test/test_include_paths.cpp @@ -1,6 +1,6 @@ +#include "test_header.hpp" #include #include -#include "test_header.hpp" TEST_CASE("Test non-standard include paths") { From 5aaddc5c52efa75c6999c468976ddf12ecc05401 Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Sun, 23 Feb 2025 14:13:53 +0530 Subject: [PATCH 3/3] bug fix: fixed init_includes --- src/xinterpreter.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 4a347fde..4e2b6e16 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -359,7 +359,6 @@ __get_cxx_version () void interpreter::init_includes() { - Cpp::AddIncludePath((xeus::prefix_path() + "/include/").c_str()); if (const char* paths = std::getenv("XEUS_SEARCH_PATH")) { std::istringstream stream(paths);