Skip to content

Commit

Permalink
[core] Improve Include PATH handling & add test (#14753)
Browse files Browse the repository at this point in the history
* [core] Improve Include PATH handling & add test

Should fix #10866

* [core] clang-format
  • Loading branch information
bellenot committed Feb 19, 2024
1 parent db4c03b commit 85ad181
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/base/src/TSystem.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3041,27 +3041,29 @@ int TSystem::CompileMacro(const char *filename, Option_t *opt,

// Calculate the -I lines
TString includes = GetIncludePath();
includes.ReplaceAll("-I ", "-I");
includes.Prepend(' ');

{
// I need to replace the -Isomerelativepath by -I../ (or -I..\ on NT)
TRegexp rel_inc(" -I[^\"/\\$%-][^:-]+");
TRegexp rel_inc(" -I[^\"/\\\\$\\%-][^:\\s]+");
Int_t len,pos;
pos = rel_inc.Index(includes,&len);
while( len != 0 ) {
TString sub = includes(pos,len);
sub.Remove(0,3); // Remove ' -I'
AssignAndDelete( sub, ConcatFileName( WorkingDirectory(), sub ) );
sub.Prepend(" -I\"");
sub.Chop(); // Remove trailing space (i.e between the -Is ...
if (sub.EndsWith(" "))
sub.Chop(); // Remove trailing space (i.e between the -Is ...
sub.Append("\" ");
includes.Replace(pos,len,sub);
pos = rel_inc.Index(includes,&len);
}
}
{
// I need to replace the -I"somerelativepath" by -I"$cwd/ (or -I"$cwd\ on NT)
TRegexp rel_inc(" -I\"[^/\\$%-][^:-]+");
// I need to replace the -I"somerelativepath" by -I"$cwd/ (or -I"$cwd\ on NT)
TRegexp rel_inc(" -I\"[^/\\\\$\\%-][^:\\s]+");
Int_t len,pos;
pos = rel_inc.Index(includes,&len);
while( len != 0 ) {
Expand Down
3 changes: 3 additions & 0 deletions core/base/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ ROOT_ADD_GTEST(CoreBaseTests
LIBRARIES ${extralibs} RIO Core)

ROOT_ADD_GTEST(CoreErrorTests TErrorTests.cxx LIBRARIES Core)

configure_file(Foo.C Foo.C COPYONLY)
ROOT_ADD_GTEST(IncludePathTest IncludePathTest.cxx LIBRARIES Core)
8 changes: 8 additions & 0 deletions core/base/test/Foo.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>

class Foo {
public:
Foo() { std::cout << "Foo" << std::endl; }
~Foo() { std::cout << "~Foo" << std::endl; }
void f() { std::cout << "f" << std::endl; }
};
31 changes: 31 additions & 0 deletions core/base/test/IncludePathTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "gtest/gtest.h"
#include "TSystem.h"

#include <string>

TEST(TSystem, IncludePath)
{
ASSERT_TRUE(gSystem);

gSystem->AddIncludePath("-I /some/path/with-xin-it -I ./some/relative-path");
gSystem->AddIncludePath("-I %ROOTSYS%\\include -I ${ROOTSYS}/include");
#ifdef WIN32
gSystem->AddIncludePath(
"-I \"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.38.33130\\include\"");
#endif

testing::internal::CaptureStderr();
gSystem->CompileMacro("Foo.C", "f");
std::cerr.flush();
std::string errors = testing::internal::GetCapturedStderr();

gSystem->Unload("Foo_C");
gSystem->CleanCompiledMacros();
#ifdef WIN32
EXPECT_TRUE((errors.find("cl : Command line warning D9002 : ignoring unknown option ") == std::string::npos));
gSystem->Exec("del Foo_C*");
#else
EXPECT_TRUE((errors.find("c++: error: unrecognized command line option ") == std::string::npos));
gSystem->Exec("rm -f Foo_C*");
#endif
}

0 comments on commit 85ad181

Please sign in to comment.