extractfile.cmake.in 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. cmake_minimum_required(VERSION 3.5)
  4. # Make file names absolute:
  5. #
  6. get_filename_component(filename "@filename@" ABSOLUTE)
  7. get_filename_component(directory "@directory@" ABSOLUTE)
  8. message(STATUS "extracting...
  9. src='${filename}'
  10. dst='${directory}'")
  11. if(NOT EXISTS "${filename}")
  12. message(FATAL_ERROR "File to extract does not exist: '${filename}'")
  13. endif()
  14. # Prepare a space for extracting:
  15. #
  16. set(i 1234)
  17. while(EXISTS "${directory}/../ex-@name@${i}")
  18. math(EXPR i "${i} + 1")
  19. endwhile()
  20. set(ut_dir "${directory}/../ex-@name@${i}")
  21. file(MAKE_DIRECTORY "${ut_dir}")
  22. # Extract it:
  23. #
  24. message(STATUS "extracting... [tar @args@]")
  25. execute_process(COMMAND ${CMAKE_COMMAND} -E tar @args@ ${filename}
  26. WORKING_DIRECTORY ${ut_dir}
  27. RESULT_VARIABLE rv)
  28. if(NOT rv EQUAL 0)
  29. message(STATUS "extracting... [error clean up]")
  30. file(REMOVE_RECURSE "${ut_dir}")
  31. message(FATAL_ERROR "Extract of '${filename}' failed")
  32. endif()
  33. # Analyze what came out of the tar file:
  34. #
  35. message(STATUS "extracting... [analysis]")
  36. file(GLOB contents "${ut_dir}/*")
  37. list(REMOVE_ITEM contents "${ut_dir}/.DS_Store")
  38. list(LENGTH contents n)
  39. if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}")
  40. set(contents "${ut_dir}")
  41. endif()
  42. # Move "the one" directory to the final directory:
  43. #
  44. message(STATUS "extracting... [rename]")
  45. file(REMOVE_RECURSE ${directory})
  46. get_filename_component(contents ${contents} ABSOLUTE)
  47. file(RENAME ${contents} ${directory})
  48. # Clean up:
  49. #
  50. message(STATUS "extracting... [clean up]")
  51. file(REMOVE_RECURSE "${ut_dir}")
  52. message(STATUS "extracting... done")