2 # Use 'cmake -Dfilename=${tar_or_tgz_file} -Dtmp=${tmp_directory} -Ddirectory=${final_directory}
3 # -P UntarFile.cmake' to call this script...
5 if(NOT DEFINED filename)
6 message(FATAL_ERROR "error: required variable 'filename' not defined...")
10 message(FATAL_ERROR "error: required variable 'tmp' not defined...")
13 if(NOT DEFINED directory)
14 message(FATAL_ERROR "error: required variable 'directory' not defined...")
18 if(filename MATCHES ".tar$")
22 if(filename MATCHES ".tgz$")
26 if(filename MATCHES ".tar.gz$")
32 # Make file names absolute:
34 get_filename_component(filename "${filename}" ABSOLUTE)
35 get_filename_component(tmp "${tmp}" ABSOLUTE)
36 get_filename_component(directory "${directory}" ABSOLUTE)
37 message(STATUS "filename='${filename}'")
38 message(STATUS "tmp='${tmp}'")
39 message(STATUS "directory='${directory}'")
42 # Prepare a space for untarring:
44 #message(STATUS "info: creating empty subdir of '${tmp}'...")
46 while(EXISTS "${tmp}/untar${i}")
47 math(EXPR i "${i} + 1")
49 set(ut_dir "${tmp}/untar${i}")
50 message(STATUS "ut_dir='${ut_dir}'")
51 file(MAKE_DIRECTORY "${ut_dir}")
56 #message(STATUS "info: untarring '${filename}' in '${ut_dir}' with '${args}'...")
57 execute_process(COMMAND ${CMAKE_COMMAND} -E tar ${args} ${filename}
58 WORKING_DIRECTORY ${ut_dir}
62 message(FATAL_ERROR "error: untar of '${filename}' failed")
66 # Analyze what came out of the tar file:
68 file(GLOB contents "${ut_dir}/*")
70 set(is_one_directory 0)
71 list(LENGTH contents n)
73 if(IS_DIRECTORY "${contents}")
74 set(is_one_directory 1)
79 # Copy "the one" directory to the final directory:
81 if(is_one_directory EQUAL 1)
82 #message(STATUS "info: (1) copying '${contents}' to '${directory}'...")
83 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${contents}" "${directory}"
86 #message(STATUS "info: (more) copying '${ut_dir}' to '${directory}'...")
87 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${ut_dir}" "${directory}"
92 message(FATAL_ERROR "error: copy_directory failed after untar in '${ut_dir}'")
98 file(REMOVE_RECURSE "${ut_dir}")