1# Copyright 2023 The BoringSSL Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# binary_dir_relative_path sets outvar to
16# ${CMAKE_CURRENT_BINARY_DIR}/${cur_bin_dir_relative}, but expressed relative to
17# ${CMAKE_BINARY_DIR}.
18#
19# TODO(davidben): When we require CMake 3.20 or later, this can be replaced with
20# the built-in cmake_path(RELATIVE_PATH) function.
21function(binary_dir_relative_path cur_bin_dir_relative outvar)
22  string(LENGTH "${CMAKE_BINARY_DIR}/" root_dir_length)
23  string(SUBSTRING "${CMAKE_CURRENT_BINARY_DIR}/${cur_bin_dir_relative}" ${root_dir_length} -1 result)
24  set(${outvar} ${result} PARENT_SCOPE)
25endfunction()
26
27# copy_post_build causes targets in ${ARGN} to be copied to
28# ${CMAKE_CURRENT_BINARY_DIR}/${dir} after being built.
29function(copy_post_build dir)
30  foreach(target ${ARGN})
31    add_custom_command(
32      TARGET ${target}
33      POST_BUILD
34      COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/${dir}"
35      COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> "${CMAKE_CURRENT_BINARY_DIR}/${dir}")
36  endforeach()
37endfunction()
38