1
0

UntarFile.cmake 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #
  2. # Use 'cmake -Dfilename=${tar_or_tgz_file} -Dtmp=${tmp_directory} -Ddirectory=${final_directory}
  3. # -P UntarFile.cmake' to call this script...
  4. #
  5. if(NOT DEFINED filename)
  6. message(FATAL_ERROR "error: required variable 'filename' not defined...")
  7. endif()
  8. if(NOT DEFINED tmp)
  9. message(FATAL_ERROR "error: required variable 'tmp' not defined...")
  10. endif()
  11. if(NOT DEFINED directory)
  12. message(FATAL_ERROR "error: required variable 'directory' not defined...")
  13. endif()
  14. if(NOT DEFINED args)
  15. if(filename MATCHES ".tar$")
  16. set(args xf)
  17. endif()
  18. if(filename MATCHES ".tgz$")
  19. set(args xfz)
  20. endif()
  21. if(filename MATCHES ".tar.gz$")
  22. set(args xfz)
  23. endif()
  24. endif()
  25. # Make file names absolute:
  26. #
  27. get_filename_component(filename "${filename}" ABSOLUTE)
  28. get_filename_component(tmp "${tmp}" ABSOLUTE)
  29. get_filename_component(directory "${directory}" ABSOLUTE)
  30. message(STATUS "filename='${filename}'")
  31. message(STATUS "tmp='${tmp}'")
  32. message(STATUS "directory='${directory}'")
  33. # Prepare a space for untarring:
  34. #
  35. #message(STATUS "info: creating empty subdir of '${tmp}'...")
  36. set(i 1)
  37. while(EXISTS "${tmp}/untar${i}")
  38. math(EXPR i "${i} + 1")
  39. endwhile()
  40. set(ut_dir "${tmp}/untar${i}")
  41. message(STATUS "ut_dir='${ut_dir}'")
  42. file(MAKE_DIRECTORY "${ut_dir}")
  43. # Untar it:
  44. #
  45. #message(STATUS "info: untarring '${filename}' in '${ut_dir}' with '${args}'...")
  46. execute_process(COMMAND ${CMAKE_COMMAND} -E tar ${args} ${filename}
  47. WORKING_DIRECTORY ${ut_dir}
  48. RESULT_VARIABLE rv)
  49. if(NOT rv EQUAL 0)
  50. file(REMOVE_RECURSE "${ut_dir}")
  51. message(FATAL_ERROR "error: untar of '${filename}' failed")
  52. endif()
  53. # Analyze what came out of the tar file:
  54. #
  55. file(GLOB contents "${ut_dir}/*")
  56. list(LENGTH contents n)
  57. if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}")
  58. set(contents "${ut_dir}")
  59. endif()
  60. # Copy "the one" directory to the final directory:
  61. #
  62. file(COPY "${contents}/" DESTINATION ${directory})
  63. # Clean up:
  64. #
  65. file(REMOVE_RECURSE "${ut_dir}")