extractfile.cmake.in 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ${CMAKE_VERSION}) # this file comes with cmake
  4. # Make file names absolute:
  5. #
  6. get_filename_component(filename "@filename@" ABSOLUTE)
  7. get_filename_component(directory "@directory@" ABSOLUTE)
  8. message(VERBOSE "extracting...
  9. src='${filename}'
  10. dst='${directory}'"
  11. )
  12. if(NOT EXISTS "${filename}")
  13. message(FATAL_ERROR "File to extract does not exist: '${filename}'")
  14. endif()
  15. # Prepare a space for extracting:
  16. #
  17. set(i 1234)
  18. while(EXISTS "${directory}/../ex-@name@${i}")
  19. math(EXPR i "${i} + 1")
  20. endwhile()
  21. set(ut_dir "${directory}/../ex-@name@${i}")
  22. file(MAKE_DIRECTORY "${ut_dir}")
  23. # Extract it:
  24. #
  25. message(VERBOSE "extracting... [tar @args@]")
  26. execute_process(COMMAND ${CMAKE_COMMAND} -E tar @args@ ${filename} @options@
  27. WORKING_DIRECTORY ${ut_dir}
  28. RESULT_VARIABLE rv
  29. )
  30. if(NOT rv EQUAL 0)
  31. message(VERBOSE "extracting... [error clean up]")
  32. file(REMOVE_RECURSE "${ut_dir}")
  33. message(FATAL_ERROR "Extract of '${filename}' failed")
  34. endif()
  35. # Analyze what came out of the tar file:
  36. #
  37. message(VERBOSE "extracting... [analysis]")
  38. file(GLOB contents "${ut_dir}/*")
  39. list(REMOVE_ITEM contents "${ut_dir}/.DS_Store")
  40. list(LENGTH contents n)
  41. if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}")
  42. set(contents "${ut_dir}")
  43. endif()
  44. # Move "the one" directory to the final directory:
  45. #
  46. message(VERBOSE "extracting... [rename]")
  47. file(REMOVE_RECURSE ${directory})
  48. get_filename_component(contents ${contents} ABSOLUTE)
  49. file(RENAME ${contents} ${directory})
  50. # Clean up:
  51. #
  52. message(VERBOSE "extracting... [clean up]")
  53. file(REMOVE_RECURSE "${ut_dir}")
  54. message(VERBOSE "extracting... done")