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

Enhance Dynamic Include Path Management in xeus-cpp with XEUS_SEARCH_PATH Support #187

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

AhmedFatthy1040
Copy link

Description

This PR introduces improvements to the xeus-cpp library to handle dynamic include paths more effectively. It includes updates to the interpreter::init_includes function and the CMakeLists file. These changes aim to enhance flexibility and maintainability by leveraging the XEUS_SEARCH_PATH environment variable for managing include directories dynamically.

Key Changes

  1. Updated interpreter::init_includes Function:

    • Parses the XEUS_SEARCH_PATH environment variable to include non-standard directories.
    • Adds support for multiple include paths, separated by colons (:).
  2. Modified CMakeLists File:

    • Dynamically builds the XEUS_SEARCH_PATH variable by joining target include directories.
    • Includes the src directory for non-EMSCRIPTEN builds.
    • Passes the XEUS_SEARCH_PATH as a preprocessor definition to the compiler.

These updates simplify the configuration and usage of custom include paths.

Fixes #175

Type of change

Please tick all options which are relevant.

  • Bug fix
  • New feature
  • Added/removed dependencies
  • Required documentation updates

This change:
1. Replaces compile-time XEUS_SEARCH_PATH define with runtime environment variable
2. Removes CMake definition to eliminate redefinition warnings
3. Adds test to verify non-standard include path functionality
4. Makes include path configuration more flexible (can be changed without recompiling)

Resolves compiler-research#175
@AhmedFatthy1040
Copy link
Author

I have pushed new changes, please approve the awaiting workflows @vgvassilev

@AhmedFatthy1040
Copy link
Author

@JohanMabille @anutosh491

@anutosh491
Copy link
Collaborator

Hi,

I shall find the time to review this soon !

@codecov-commenter
Copy link

codecov-commenter commented Dec 6, 2024

Codecov Report

Attention: Patch coverage is 87.23404% with 6 lines in your changes missing coverage. Please review.

Project coverage is 80.44%. Comparing base (f98cca3) to head (944c962).
Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
src/xinterpreter.cpp 87.23% 6 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #187      +/-   ##
==========================================
- Coverage   80.80%   80.44%   -0.36%     
==========================================
  Files          19       19              
  Lines         974      987      +13     
  Branches       93       97       +4     
==========================================
+ Hits          787      794       +7     
- Misses        187      193       +6     
Files with missing lines Coverage Δ
src/xinterpreter.cpp 88.94% <87.23%> (-2.46%) ⬇️
Files with missing lines Coverage Δ
src/xinterpreter.cpp 88.94% <87.23%> (-2.46%) ⬇️

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Cpp::AddIncludePath((xeus::prefix_path() + "/include/").c_str());

// Get include paths from environment variable
const char* non_standard_paths = std::getenv("XEUS_SEARCH_PATH");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::getenv" is directly included [misc-include-cleaner]

src/xinterpreter.cpp:19:

- #ifndef EMSCRIPTEN
+ #include <cstdlib>
+ #ifndef EMSCRIPTEN

non_standard_paths = "";
}

if (std::strlen(non_standard_paths) > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::strlen" is directly included [misc-include-cleaner]

src/xinterpreter.cpp:19:

- #ifndef EMSCRIPTEN
+ #include <cstring>
+ #ifndef EMSCRIPTEN

if (std::strlen(non_standard_paths) > 0)
{
// Split the paths by colon ':' and add each one
std::istringstream stream(non_standard_paths);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::istringstream" is directly included [misc-include-cleaner]

src/xinterpreter.cpp:19:

- #ifndef EMSCRIPTEN
+ #include <sstream>
+ #ifndef EMSCRIPTEN

// Split the paths by colon ':' and add each one
std::istringstream stream(non_standard_paths);
std::string path;
while (std::getline(stream, path, ':'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::getline" is directly included [misc-include-cleaner]

            while (std::getline(stream, path, ':'))
                        ^

- Add <cstring> for std::strlen
- Add <sstream> for std::istringstream
- Add <string> for std::getline

Resolves include-cleaner warnings by explicitly including headers
for all used standard library symbols. This improves code maintainability
and prevents reliance on indirect includes.
@AhmedFatthy1040
Copy link
Author

I have fixed the warnings.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

src/xinterpreter.cpp Show resolved Hide resolved
CMakeLists.txt Outdated
Comment on lines 411 to 423

# Find doctest package
find_package(doctest REQUIRED)

# Test for non-standard include paths
add_executable(test_include_paths test/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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block should go in the test folder.

Comment on lines 367 to 385
// Get include paths from environment variable
const char* non_standard_paths = std::getenv("XEUS_SEARCH_PATH");
if (!non_standard_paths) {
non_standard_paths = "";
}

if (std::strlen(non_standard_paths) > 0)
{
// Split the paths by colon ':' and add each one
std::istringstream stream(non_standard_paths);
std::string path;
while (std::getline(stream, path, ':'))
{
if (!path.empty())
{
Cpp::AddIncludePath(path.c_str());
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify this by a lot, reduce the mount braces and also consider non-unix path delimiters.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

- Relocated the test_include_paths executable setup from the root CMakeLists.txt
  to the test folder's CMakeLists.txt.
- Included the necessary doctest package configuration and non-standard include paths.
- Ensured proper target linking and definitions for the test_include_paths target.
Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@AhmedFatthy1040
Copy link
Author

I have fixed the clang-format problem.

Comment on lines 384 to 385
while (std::getline(stream, path, path_separator))
{
if (!path.empty())
{
Cpp::AddIncludePath(path.c_str());
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while (std::getline(stream, path, path_separator))
{
if (!path.empty())
{
Cpp::AddIncludePath(path.c_str());
}
}
while (std::getline(stream, path, path_separator))
if (!path.empty())
Cpp::AddIncludePath(path.c_str());

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, I initially thought the braces were necessary for the clang-tidy format check to pass. I've now edited and correctly formatted the file.

Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@AhmedFatthy1040
Copy link
Author

Please approve the awaiting workflows. @anutosh491 @vgvassilev

Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@AhmedFatthy1040
Copy link
Author

AhmedFatthy1040 commented Dec 16, 2024

IDK why it's failing now.

White Cat

@mcbarton
Copy link
Collaborator

@AhmedFatthy1040 please squash the commits of the PR and perform git-clang-format on the single commit. Then the ci should pass. Then another review can be made.

@AhmedFatthy1040
Copy link
Author

@mcbarton I previously addressed this issue by adding braces here. However, @vgvassilev recommended removing them for code simplification, which I implemented. Unfortunately, this removal triggered a failure in the git clang-format check. I have now corrected the issue by re-adding the braces. Please approve the pending workflows.
Screenshot from 2024-12-19 20-27-06

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

std::istringstream stream(non_standard_paths);
std::string path;
while (std::getline(stream, path, path_separator))
+ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: expected expression [clang-diagnostic-error]

+        {
         ^

@mcbarton
Copy link
Collaborator

@mcbarton I previously addressed this issue by adding braces here. However, @vgvassilev recommended removing them for code simplification, which I implemented. Unfortunately, this removal triggered a failure in the git clang-format check. I have now corrected the issue by re-adding the braces. Please approve the pending workflows. Screenshot from 2024-12-19 20-27-06

@AhmedFatthy1040 Please revert this change. Squash the commits into 1 commit, then perform git-clang-format on this single commit. Then well see if the workflows pass. We can then review it from that point.

@AhmedFatthy1040
Copy link
Author

Sorry for the mistake in the last commit, i am fixing it now.

@AhmedFatthy1040
Copy link
Author

@mcbarton I previously addressed this issue by adding braces here. However, @vgvassilev recommended removing them for code simplification, which I implemented. Unfortunately, this removal triggered a failure in the git clang-format check. I have now corrected the issue by re-adding the braces. Please approve the pending workflows. Screenshot from 2024-12-19 20-27-06

@AhmedFatthy1040 Please revert this change. Squash the commits into 1 commit, then perform git-clang-format on this single commit. Then well see if the workflows pass. We can then review it from that point.

okay

@AhmedFatthy1040 AhmedFatthy1040 force-pushed the issue-175-dynamic-include-paths branch from c508161 to bdb66eb Compare December 19, 2024 19:42
@AhmedFatthy1040
Copy link
Author

I found out that i was using an old version of clang-format, so i updated to version 18 as it is the version used in the CI/CD pipeline, and formatted the code again @mcbarton

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 15. Check the log or trigger a new build to see more.

{
Args ClangArgs = {/*"-xc++"*/ "-v"}; // ? {"-Xclang", "-emit-llvm-only", "-Xclang",
// "-diagnostic-log-file", "-Xclang", "-", "-xc++"};
if (std::find_if(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::find_if" is directly included [misc-include-cleaner]

src/xinterpreter.cpp:17:

- #include <cstring>  // for std::strlen
+ #include <algorithm>
+ #include <cstring>  // for std::strlen

)
== ExtraArgs.end())
{
std::string resource_dir = Cpp::DetectResourceDir();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Cpp::DetectResourceDir" is directly included [misc-include-cleaner]

src/xinterpreter.cpp:17:

- #include <cstring>  // for std::strlen
+ #include <clang/Interpreter/CppInterOp.h>
+ #include <cstring>  // for std::strlen

std::string resource_dir = Cpp::DetectResourceDir();
if (resource_dir.empty())
{
std::cerr << "Failed to detect the resource-dir\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::cerr" is directly included [misc-include-cleaner]

src/xinterpreter.cpp:18:

- #include <sstream>  // for std::istringstream
+ #include <iostream>
+ #include <sstream>  // for std::istringstream

ClangArgs.push_back(resource_dir.c_str());
}
std::vector<std::string> CxxSystemIncludes;
Cpp::DetectSystemCompilerIncludePaths(CxxSystemIncludes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Cpp::DetectSystemCompilerIncludePaths" is directly included [misc-include-cleaner]

    Cpp::DetectSystemCompilerIncludePaths(CxxSystemIncludes);
         ^

ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
// FIXME: We should process the kernel input options and conditionally pass
// the gpu args here.
return Cpp::CreateInterpreter(ClangArgs /*, {"-cuda"}*/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Cpp::CreateInterpreter" is directly included [misc-include-cleaner]

    return Cpp::CreateInterpreter(ClangArgs /*, {"-cuda"}*/);
                ^

err = Cpp::EndStdStreamCapture();
std::cout << out;
}
struct StreamRedirectRAII
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: class 'StreamRedirectRAII' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions]

    struct StreamRedirectRAII
           ^

}
struct StreamRedirectRAII
{
std::string& err;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: member 'err' of type 'std::string &' (aka 'basic_string &') is a reference [cppcoreguidelines-avoid-const-or-ref-data-members]

        std::string& err;
                     ^

StreamRedirectRAII(std::string& e)
: err(e)
{
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Cpp::BeginStdStreamCapture" is directly included [misc-include-cleaner]

            Cpp::BeginStdStreamCapture(Cpp::kStdErr);
                 ^

StreamRedirectRAII(std::string& e)
: err(e)
{
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Cpp::kStdErr" is directly included [misc-include-cleaner]

            Cpp::BeginStdStreamCapture(Cpp::kStdErr);
                                            ^

: err(e)
{
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
Cpp::BeginStdStreamCapture(Cpp::kStdOut);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Cpp::kStdOut" is directly included [misc-include-cleaner]

            Cpp::BeginStdStreamCapture(Cpp::kStdOut);
                                            ^

@AhmedFatthy1040
Copy link
Author

I will try to fix the clang-tidy suggestions tomorrow. Additionally, there was a block of code that was refactored, but it got deleted by mistake when I reset some commits. I will fix that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adjust include path search according to dependencies
5 participants