Skip to content

Commit

Permalink
cmake: pristine.cmake requires SOURCE_DIR and BINARY_DIR as arguments
Browse files Browse the repository at this point in the history
Calling cmake/pristine.cmake now requires SOURCE_DIR and BINARY_DIR as
arguments.

This ensures that pristine.cmake can evaluate if pristine is requested
on in-source builds, and bail out in such case with an error message.

All uses of `pristine.cmake` has been updated to use the new arguments.

Signed-off-by: Torsten Rasmussen <[email protected]>
  • Loading branch information
tejlmand authored and nashif committed Jan 23, 2021
1 parent 0520ec8 commit 774103d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cmake/app/boilerplate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ endif()

add_custom_target(
pristine
COMMAND ${CMAKE_COMMAND} -P ${ZEPHYR_BASE}/cmake/pristine.cmake
COMMAND ${CMAKE_COMMAND} -DBINARY_DIR=${APPLICATION_BINARY_DIR}
-DSOURCE_DIR=${APPLICATION_SOURCE_DIR}
-P ${ZEPHYR_BASE}/cmake/pristine.cmake
# Equivalent to rm -rf build/*
)

Expand Down
43 changes: 33 additions & 10 deletions cmake/pristine.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
# SPDX-License-Identifier: Apache-2.0

# NB: This could be dangerous to execute, it is assuming the user is
# checking that the build is out-of-source with code like this:
#
# if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
# message(FATAL_ERROR "Source directory equals build directory.\
# In-source builds are not supported.\
# Please specify a build directory, e.g. cmake -Bbuild -H.")
# endif()

file(GLOB build_dir_contents ${CMAKE_BINARY_DIR}/*)
# NB: This could be dangerous to execute.

macro(print_usage)
message("
usage: cmake -DBINARY_DIR=<build-path> -DSOURCE_DIR=<source-path>
-P ${CMAKE_SCRIPT_MODE_FILE}
mandatory arguments:
-DBINARY_DIR=<build-path>: Absolute path to the build directory to pristine
-DSOURCE_DIR=<source-path>: Absolute path to the source directory used when
creating <build-path>
")
# Making the usage itself a fatal error messes up the formatting when printing.
message(FATAL_ERROR "")
endmacro()

if(NOT DEFINED BINARY_DIR OR NOT DEFINED SOURCE_DIR)
print_usage()
endif()

if(NOT IS_ABSOLUTE ${BINARY_DIR} OR NOT IS_ABSOLUTE ${SOURCE_DIR})
print_usage()
endif()

get_filename_component(BINARY_DIR ${BINARY_DIR} REALPATH)
get_filename_component(SOURCE_DIR ${SOURCE_DIR} REALPATH)

string(FIND ${SOURCE_DIR} ${BINARY_DIR} INDEX)
if(NOT INDEX EQUAL -1)
message(FATAL_ERROR "Refusing to run pristine in in-source build folder.")
endif()

file(GLOB build_dir_contents ${BINARY_DIR}/*)
foreach(file ${build_dir_contents})
if (EXISTS ${file})
file(REMOVE_RECURSE ${file})
Expand Down
8 changes: 7 additions & 1 deletion scripts/west_commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,13 @@ def _run_pristine(self):
'Zephyr build system')

cache = CMakeCache.from_build_dir(self.build_dir)
cmake_args = ['-P', cache['ZEPHYR_BASE'] + '/cmake/pristine.cmake']

app_src_dir = cache.get('APPLICATION_SOURCE_DIR')
app_bin_dir = cache.get('APPLICATION_BINARY_DIR')

cmake_args = [f'-DBINARY_DIR={app_bin_dir}',
f'-DSOURCE_DIR={app_src_dir}',
'-P', cache['ZEPHYR_BASE'] + '/cmake/pristine.cmake']
run_cmake(cmake_args, cwd=self.build_dir, dry_run=self.args.dry_run)

def _run_build(self, target):
Expand Down

0 comments on commit 774103d

Please sign in to comment.