+
+def setup_output_dir(output_dir, work_in_output, branch, no_subdirs, col,
+ clean_dir):
+ """Set up the output directory
+
+ Args:
+ output_dir (str): Output directory provided by the user, or None if none
+ work_in_output (bool): True to work in the output directory
+ branch (str): Name of branch to build, or None if none
+ no_subdirs (bool): True to put the output in the top-level output dir
+ clean_dir: Used for tests only, indicates that the existing output_dir
+ should be removed before starting the build
+
+ Returns:
+ str: Updated output directory pathname
+ """
+ if not output_dir:
+ if work_in_output:
+ sys.exit(col.build(col.RED, '-w requires that you specify -o'))
+ output_dir = '..'
+ if branch and not no_subdirs:
+ # As a special case allow the board directory to be placed in the
+ # output directory itself rather than any subdirectory.
+ dirname = branch.replace('/', '_')
+ output_dir = os.path.join(output_dir, dirname)
+ if clean_dir and os.path.exists(output_dir):
+ shutil.rmtree(output_dir)
+ return output_dir
+
+