GetRE2.cmake 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #=============================================================================
  2. # Copyright 2017-2026, Manticore Software LTD (https://manticoresearch.com)
  3. #
  4. # Distributed under the OSI-approved BSD License (the "License");
  5. # see accompanying file Copyright.txt for details.
  6. #
  7. # This software is distributed WITHOUT ANY WARRANTY; without even the
  8. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. # See the License for more information.
  10. #=============================================================================
  11. # This file need to get RE2 library sources
  12. # First it try 'traditional' way - find RE2 lib.
  13. # Then (if it is not found) it try to look into ${LIBS_BUNDLE} for file named 're2-master.zip'
  14. # It is supposed, that file (if any) contains github's master archive of RE2 sources.
  15. # If no file found, it will try to fetch it from github, address
  16. # https://github.com/manticoresoftware/re2/archive/master.zip
  17. set ( RE2_REPO "https://github.com/manticoresoftware/re2" )
  18. set ( RE2_BRANCH "2015-06-01" ) # specific tag for reproducable builds
  19. set ( RE2_SRC_MD5 "023053ef20051a0fc5911a76869be59f" )
  20. set ( RE2_GITHUB "${RE2_REPO}/archive/${RE2_BRANCH}.zip" )
  21. set ( RE2_BUNDLE "${LIBS_BUNDLE}/re2-${RE2_BRANCH}.zip" )
  22. cmake_minimum_required ( VERSION 3.17 FATAL_ERROR )
  23. include ( update_bundle )
  24. # if it is allowed to use system library - try to use it
  25. if (NOT WITH_RE2_FORCE_STATIC)
  26. find_package ( re2 MODULE QUIET )
  27. return_if_target_found ( re2::re2 "as default (sys or other) lib" )
  28. endif ()
  29. # determine destination folder where we expect pre-built re2
  30. find_package ( re2 QUIET CONFIG )
  31. return_if_target_found ( re2::re2 "found ready (no need to build)" )
  32. function ( PATCH_GIT RESULT RE2_SRC )
  33. find_package ( Git QUIET )
  34. if (NOT GIT_EXECUTABLE)
  35. return ()
  36. endif ()
  37. execute_process ( COMMAND "${GIT_EXECUTABLE}" apply libre2.patch WORKING_DIRECTORY "${RE2_SRC}" )
  38. set ( ${RESULT} TRUE PARENT_SCOPE )
  39. endfunction ()
  40. function ( PATCH_PATCH RESULT RE2_SRC )
  41. find_program ( PATCH_PROG patch )
  42. if (NOT PATCH_PROG)
  43. return ()
  44. endif ()
  45. execute_process ( COMMAND "${PATCH_PROG}" -p1 --binary -i libre2.patch WORKING_DIRECTORY "${RE2_SRC}" )
  46. set ( ${RESULT} TRUE PARENT_SCOPE )
  47. endfunction ()
  48. # cb to finalize RE2 sources (patch, add cmake)
  49. function ( PREPARE_RE2 RE2_SRC )
  50. # check if it is already patched before
  51. if (EXISTS "${RE2_SRC}/is_patched.txt")
  52. return ()
  53. endif ()
  54. file ( COPY "${MANTICORE_SOURCE_DIR}/libre2/libre2.patch" DESTINATION "${RE2_SRC}" )
  55. patch_git ( PATCHED ${RE2_SRC} )
  56. if (NOT PATCHED)
  57. patch_patch ( PATCHED ${RE2_SRC} )
  58. endif ()
  59. if (NOT PATCHED)
  60. message ( FATAL_ERROR "Couldn't patch RE2 distro. No Git or Patch found" )
  61. return ()
  62. endif ()
  63. file ( WRITE "${RE2_SRC}/is_patched.txt" "ok" )
  64. execute_process ( COMMAND ${CMAKE_COMMAND} -E copy_if_different "${MANTICORE_SOURCE_DIR}/libre2/CMakeLists.txt" "${RE2_SRC}/CMakeLists.txt" )
  65. endfunction ()
  66. # prepare sources
  67. select_nearest_url ( RE2_PLACE re2 ${RE2_BUNDLE} ${RE2_GITHUB} )
  68. fetch_and_check ( re2 ${RE2_PLACE} ${RE2_SRC_MD5} RE2_SRC )
  69. prepare_re2 ( ${RE2_SRC} )
  70. # build
  71. get_build ( RE2_BUILD re2 )
  72. external_build ( re2 RE2_SRC RE2_BUILD )
  73. # now it should find
  74. find_package ( re2 REQUIRED CONFIG )
  75. return_if_target_found ( re2::re2 "was built" )