1# SPDX-License-Identifier: Apache-2.0
2
3# NB: This could be dangerous to execute.
4
5macro(print_usage)
6  message("
7usage: cmake -DBINARY_DIR=<build-path> -DSOURCE_DIR=<source-path>
8             -P ${CMAKE_SCRIPT_MODE_FILE}
9
10mandatory arguments:
11  -DBINARY_DIR=<build-path>:  Absolute path to the build directory to pristine
12  -DSOURCE_DIR=<source-path>: Absolute path to the source directory used when
13                              creating <build-path>
14")
15  # Making the usage itself a fatal error messes up the formatting when printing.
16  message(FATAL_ERROR "")
17endmacro()
18
19if(NOT DEFINED BINARY_DIR OR NOT DEFINED SOURCE_DIR)
20  print_usage()
21endif()
22
23if(NOT IS_ABSOLUTE ${BINARY_DIR} OR NOT IS_ABSOLUTE ${SOURCE_DIR})
24  print_usage()
25endif()
26
27get_filename_component(BINARY_DIR ${BINARY_DIR} REALPATH)
28get_filename_component(SOURCE_DIR ${SOURCE_DIR} REALPATH)
29
30string(FIND ${SOURCE_DIR} ${BINARY_DIR} INDEX)
31if(NOT INDEX EQUAL -1)
32  message(FATAL_ERROR "Refusing to run pristine in in-source build folder.")
33endif()
34
35file(GLOB build_dir_contents ${BINARY_DIR}/*)
36foreach(file ${build_dir_contents})
37  if (EXISTS ${file})
38     file(REMOVE_RECURSE ${file})
39  endif()
40endforeach(file)
41