99
1010#include < cstdlib>
1111#include < vector>
12- # include < dirent.h >
13- #include < libgen.h >
12+
13+ #include < filesystem >
1414
1515namespace scene_rdl2 {
1616namespace rdl2 {
1717
18- using util::Args;
18+ #ifdef _WIN32
19+ static const std::string sRaasRender (" moonray.exe" );
20+ static const std::string sOsPathSep (" ;" );
21+ #else
22+ static const std::string sRaasRender (" moonray" );
23+ static const std::string sOsPathSep (" :" );
24+ #endif
1925
20- int isMatching (const dirent* entry) {
21- std::string name = " raas_render" ;
22- if (name == std::string (entry->d_name )) {
23- return 1 ;
24- }
25-
26- return 0 ;
27- }
26+ using util::Args;
2827
2928std::string
3029DsoFinder::guessDsoPath ()
3130{
3231 std::string dsoPath = " " ;
33- dirent** nameList;
34-
32+
3533 // First, search PATH for raas_render executable
3634 const std::string pathEnv = util::getenv<std::string>(" PATH" );
3735 if (pathEnv.empty ()) {
3836 return " " ;
3937 }
40-
41- size_t found = pathEnv.find (' :' );
42- int numFound;
43- std::string path;
38+ bool pathFound = false ;
39+ size_t found = pathEnv.find (sOsPathSep );
40+ std::filesystem::path path;
4441 if (found == std::string::npos) { // single path
45- path = pathEnv;
46- numFound = scandir (path.c_str (), &nameList, isMatching, alphasort);
42+ path = std::filesystem::path (pathEnv).make_preferred ();
43+
44+ if (std::filesystem::exists (path)) {
45+ for (auto const & dirEntry : std::filesystem::directory_iterator (path)) {
46+ if (dirEntry.path ().filename ().string () == sRaasRender ) {
47+ pathFound = true ;
48+ break ;
49+ }
50+ }
51+ }
4752 } else {
4853 int counter = 0 ;
4954 while (found != std::string::npos) {
50- path = pathEnv.substr (counter, found - counter);
51- numFound = scandir (path.c_str (), &nameList, isMatching, alphasort);
52- if (numFound > 0 ) {
55+ path = std::filesystem::path (pathEnv.substr (counter, found - counter)).make_preferred ();
56+ if (std::filesystem::exists (path)) {
57+ for ( const auto & dirEntry : std::filesystem::directory_iterator (path)) {
58+ std::string file = dirEntry.path ().stem ().string ();
59+ if (file == sRaasRender ) {
60+ pathFound = true ;
61+ break ;
62+ }
63+ }
64+ }
65+ if (pathFound) {
5366 break ;
5467 }
5568 counter = found + 1 ;
56- found = pathEnv.find (' : ' , found + 1 );
69+ found = pathEnv.find (sOsPathSep , found + 1 );
5770 }
58-
59- if (numFound <= 0 ) { // Haven't found raas_render yet
60- // Process last path
61- path = pathEnv.substr (counter);
62- numFound = scandir (path.c_str (), &nameList, isMatching, alphasort);
71+ }
72+
73+ if (pathFound) {
74+ std::filesystem::path raasRdl2dso = std::filesystem::path (std::filesystem::absolute (path.parent_path ()) / " rdl2dso" ).make_preferred ();
75+ if (std::filesystem::exists (raasRdl2dso)) {
76+ dsoPath = raasRdl2dso.string ();
6377 }
6478 }
65-
66- if (numFound > 0 ) {
67- // We found raas_render, now construct path to rdl2dso
68- // This assumes that the immediate parent directory is /bin
69- char * buf = realpath (path.c_str (), NULL ); // Resolve any relative links
70- dsoPath = std::string (dirname (buf)) + " /" + " rdl2dso" ;
71- free (buf);
79+ #ifndef __APPLE__
80+ else {
81+ #ifdef _WIN32
82+ char exePathCStr[MAX_PATH];
83+ ::GetModuleFileNameA (nullptr , exePathCStr, MAX_PATH);
84+ std::filesystem::path exePath (exePathCStr);
85+ #else
86+ std::filesystem::path exePath (" /proc/self/exe" );
87+ exePath = std::filesystem::canonical (exePath);
88+ #endif
89+ std::filesystem::path raasRdl2dso = std::filesystem::path (std::filesystem::absolute (exePath.parent_path ().parent_path ()) / " rdl2dso" ).make_preferred ();
90+ if (std::filesystem::exists (raasRdl2dso)) {
91+ dsoPath = raasRdl2dso.string ();
92+ }
7293 }
73-
74- // clean up
75- /* while (numFound--) {
76- free(nameList[numFound]);
77- }*/
78- free (nameList);
79-
94+ #endif
8095 return dsoPath;
8196}
8297
@@ -85,14 +100,14 @@ std::string DsoFinder::find() {
85100 std::string dsoPathString = " ." ; // Search '.' path first
86101 if (const char * const dsoPathEnvVar = util::getenv<const char *>(" RDL2_DSO_PATH" )) {
87102 // append dso path as sourced from RDL2_DSO_PATH
88- dsoPathString += " : " + std::string (dsoPathEnvVar);
103+ dsoPathString += sOsPathSep + std::string (dsoPathEnvVar);
89104 }
90105
91106 // finally, guess dso path based on location of raas_render
92107 std::string guessedDsoPath = guessDsoPath ();
93108 if (!guessedDsoPath.empty ()) {
94109 // append dso path as sourced from location of raas_render executable
95- dsoPathString += " : " + guessedDsoPath;
110+ dsoPathString += sOsPathSep + guessedDsoPath;
96111 }
97112
98113 return dsoPathString;
@@ -125,7 +140,7 @@ std::string DsoFinder::parseDsoPath(int argc, char* argv[]) {
125140
126141 if (!dsoPath.empty ()) {
127142 // prepend dso path as sourced from command line
128- return dsoPath + " : " + findPath;
143+ return dsoPath + sOsPathSep + findPath;
129144 }
130145
131146 return findPath;
0 commit comments