library_unix.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /************************************************************************************
  2. * *
  3. * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
  4. * *
  5. * This file is part of RTTR (Run Time Type Reflection) *
  6. * License: MIT License *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining *
  9. * a copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
  24. * SOFTWARE. *
  25. * *
  26. *************************************************************************************/
  27. #include "rttr/detail/base/core_prerequisites.h"
  28. #if RTTR_PLATFORM != RTTR_PLATFORM_WINDOWS
  29. #include "rttr/detail/library/library_p.h"
  30. #include "rttr/detail/misc/utility.h"
  31. #include <vector>
  32. #include <dlfcn.h>
  33. #include <cstdio>
  34. namespace
  35. {
  36. bool is_absolute_path(const std::string& file_name)
  37. {
  38. return (!file_name.empty() && file_name[0] == '/');
  39. }
  40. std::vector<std::string> get_suffixes_sys(const std::string& version)
  41. {
  42. std::vector<std::string> result;
  43. #if RTTR_PLATFORM == RTTR_PLATFORM_LINUX
  44. if (!version.empty())
  45. result.emplace_back(".so." + version);
  46. else
  47. result.emplace_back(".so");
  48. #endif
  49. #if RTTR_PLATFORM == RTTR_PLATFORM_APPLE
  50. if (!version.empty())
  51. {
  52. result.emplace_back("." + version + ".bundle");
  53. result.emplace_back("." + version + ".dylib");
  54. }
  55. else
  56. {
  57. result.emplace_back(".bundle");
  58. result.emplace_back(".dylib");
  59. }
  60. #endif
  61. return result;
  62. }
  63. bool file_exist(const std::string& fileName)
  64. {
  65. if (auto file = fopen(fileName.c_str(), "r"))
  66. {
  67. fclose(file);
  68. return true;
  69. } else
  70. {
  71. return false;
  72. }
  73. }
  74. static rttr::string_view get_error()
  75. {
  76. using namespace rttr;
  77. auto err = dlerror();
  78. return err ? string_view(err): string_view();
  79. }
  80. } // end namespace anonymous
  81. namespace rttr
  82. {
  83. namespace detail
  84. {
  85. /////////////////////////////////////////////////////////////////////////////////////////
  86. bool library_private::load_native()
  87. {
  88. std::vector<std::string> prefix_list = {"lib"};
  89. std::vector<std::string> suffix_list = get_suffixes_sys(m_version);
  90. if (is_absolute_path(m_file_name))
  91. {
  92. suffix_list.insert(suffix_list.begin(), std::string());
  93. prefix_list.insert(prefix_list.begin(), std::string());
  94. }
  95. else
  96. {
  97. suffix_list.push_back(std::string());
  98. prefix_list.push_back(std::string());
  99. }
  100. int dl_flags = RTLD_NOW;
  101. auto retry = true;
  102. std::string attempt;
  103. for (auto prefix = 0u; retry && !m_handle && prefix < prefix_list.size(); ++prefix)
  104. {
  105. for (auto suffix = 0u; retry && !m_handle && suffix < suffix_list.size(); ++suffix)
  106. {
  107. // don't attach prefix or suffix when it is already there
  108. if (!prefix_list[prefix].empty() && starts_with(m_file_name, prefix_list[prefix]))
  109. continue;
  110. if (!suffix_list[suffix].empty() && ends_with(m_file_name, suffix_list[suffix]))
  111. continue;
  112. attempt = prefix_list[prefix] + m_file_name + suffix_list[suffix];
  113. m_handle = dlopen(attempt.c_str(), dl_flags);
  114. if (!m_handle && is_absolute_path(m_file_name) && file_exist(attempt))
  115. {
  116. retry = false;
  117. }
  118. }
  119. }
  120. if (!m_handle)
  121. {
  122. m_error_string = "Cannot load library " + m_file_name + " " + get_error();
  123. }
  124. else
  125. {
  126. m_qualifed_file_name = attempt;
  127. m_error_string.clear();
  128. }
  129. return (m_handle != nullptr);
  130. }
  131. /////////////////////////////////////////////////////////////////////////////////////////
  132. bool library_private::unload_native()
  133. {
  134. if (dlclose(m_handle))
  135. {
  136. m_error_string = "Cannot unload library: '" + m_file_name + "'" + get_error();
  137. return false;
  138. }
  139. return true;
  140. }
  141. /////////////////////////////////////////////////////////////////////////////////////////
  142. } // end namespace detail
  143. } // end namespace rttr
  144. #endif // NOT RTTR_PLATFORM_WINDOWS