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 file(REMOVE_RECURSE "${ut_dir}")
63 message(FATAL_ERROR "error: untar of '${filename}' failed")
67 # Analyze what came out of the tar file:
69 file(GLOB contents "${ut_dir}/*")
70 list(LENGTH contents n)
71 if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}")
72 set(contents "${ut_dir}")
76 # Copy "the one" directory to the final directory:
78 file(COPY "${contents}/" DESTINATION ${directory})
83 file(REMOVE_RECURSE "${ut_dir}")