Skip to content

Commit

Permalink
Generate template copyright year at build time. (rapidsai#325)
Browse files Browse the repository at this point in the history
Rather than hard-coding the copyright year in each template file,
get the current year at build time and write it in the template.
  • Loading branch information
KyleFromNVIDIA committed Jan 11, 2024
1 parent 66816a1 commit 98b4c20
Show file tree
Hide file tree
Showing 18 changed files with 216 additions and 13 deletions.
3 changes: 2 additions & 1 deletion rapids-cmake/cpm/detail/generate_patch_command.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2022, NVIDIA CORPORATION.
# Copyright (c) 2022-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -100,6 +100,7 @@ function(rapids_cpm_generate_patch_command package_name version patch_command)
set(patch_script "${CMAKE_BINARY_DIR}/rapids-cmake/patches/${package_name}/patch.cmake")
set(log_file "${CMAKE_BINARY_DIR}/rapids-cmake/patches/${package_name}/log")
if(patch_files_to_run)
string(TIMESTAMP current_year "%Y" UTC)
configure_file(${rapids-cmake-dir}/cpm/patches/command_template.cmake.in "${patch_script}"
@ONLY)
set(${patch_command} ${CMAKE_COMMAND} -P ${patch_script} PARENT_SCOPE)
Expand Down
2 changes: 1 addition & 1 deletion rapids-cmake/cpm/patches/command_template.cmake.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2022, NVIDIA CORPORATION.
# Copyright (c) @current_year@, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion rapids-cmake/export/cpm.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -74,6 +74,7 @@ function(rapids_export_cpm type name export_set)
endif()
endif()

string(TIMESTAMP current_year "%Y" UTC)
configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/template/cpm.cmake.in"
"${CMAKE_BINARY_DIR}/rapids-cmake/${export_set}/${type}/cpm_${name}.cmake" @ONLY)

Expand Down
4 changes: 3 additions & 1 deletion rapids-cmake/export/export.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -282,6 +282,7 @@ if EXPORT_NAME isn't set for the export targets.")

set(scratch_dir "${PROJECT_BINARY_DIR}/rapids-cmake/${project_name}/export/${project_name}")

string(TIMESTAMP current_year "%Y" UTC)
configure_package_config_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/template/config.cmake.in"
"${scratch_dir}/${project_name}-config.cmake"
INSTALL_DESTINATION "${install_location}")
Expand Down Expand Up @@ -322,6 +323,7 @@ if EXPORT_NAME isn't set for the export targets.")

else()
set(install_location "${PROJECT_BINARY_DIR}")
string(TIMESTAMP current_year "%Y" UTC)
configure_package_config_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/template/config.cmake.in"
"${install_location}/${project_name}-config.cmake"
INSTALL_DESTINATION "${install_location}")
Expand Down
2 changes: 1 addition & 1 deletion rapids-cmake/export/template/config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021, NVIDIA CORPORATION.
# Copyright (c) @current_year@, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion rapids-cmake/export/template/cpm.cmake.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021, NVIDIA CORPORATION.
# Copyright (c) @current_year@, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion rapids-cmake/export/template/dependencies.cmake.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021, NVIDIA CORPORATION.
# Copyright (c) @current_year@, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion rapids-cmake/export/write_dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -130,6 +130,7 @@ endif()\n")

string(APPEND _RAPIDS_EXPORT_CONTENTS "set(rapids_global_targets ${global_targets})\n")

string(TIMESTAMP current_year "%Y" UTC)
configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/template/dependencies.cmake.in" "${file_path}"
@ONLY)
endfunction()
3 changes: 2 additions & 1 deletion rapids-cmake/find/generate_module.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -201,6 +201,7 @@ function(rapids_find_generate_module name)
endif()

# Need to generate the module
string(TIMESTAMP current_year "%Y" UTC)
configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/template/find_module.cmake.in"
"${CMAKE_BINARY_DIR}/cmake/find_modules/Find${name}.cmake" @ONLY)

Expand Down
2 changes: 1 addition & 1 deletion rapids-cmake/find/template/find_module.cmake.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021, NVIDIA CORPORATION.
# Copyright (c) @current_year@, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion testing/cpm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,6 +42,7 @@ add_cmake_config_test( cpm_package_override-simple.cmake )
add_cmake_config_test( cpm_generate_patch_command-invalid.cmake )
add_cmake_config_test( cpm_generate_patch_command-override.cmake )
add_cmake_config_test( cpm_generate_patch_command-current_json_dir.cmake )
add_cmake_build_test( cpm_generate_patch_command-verify-copyright-header.cmake )

add_cmake_config_test( cpm_cccl-simple.cmake )
add_cmake_config_test( cpm_cccl-export.cmake )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#=============================================================================
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
include(${rapids-cmake-dir}/cpm/init.cmake)
include(${rapids-cmake-dir}/cpm/package_override.cmake)
include(${rapids-cmake-dir}/cpm/detail/generate_patch_command.cmake)

rapids_cpm_init()

# Need to write out an override file
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/override.json
[=[
{
"packages" : {
"pkg_with_patch" : {
"version" : "10.2",
"git_tag" : "a_tag",
"git_shallow" : "OFF",
"exclude_from_all" : "ON",
"patches" : [
{
"file" : "e/example.diff",
"issue" : "explain",
"fixed_in" : ""
}
]
}
}
}
]=])
rapids_cpm_package_override(${CMAKE_CURRENT_BINARY_DIR}/override.json)

rapids_cpm_generate_patch_command(pkg_with_patch 10.2 patch_command)
if(NOT patch_command)
message(FATAL_ERROR "rapids_cpm_package_override specified a patch step for `pkg_with_patch`")
endif()

add_custom_target(verify_copyright_headers ALL
COMMAND ${CMAKE_COMMAND} "-Dfile=${CMAKE_BINARY_DIR}/rapids-cmake/patches/pkg_with_patch/patch.cmake" -P "${rapids-cmake-testing-dir}/utils/check_copyright_header_script.cmake"
)
3 changes: 2 additions & 1 deletion testing/export/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@ add_cmake_build_test( export-verify-doc-string )
add_cmake_build_test( export-verify-global-targets )
add_cmake_build_test( export-verify-install-components )
add_cmake_build_test( export-verify-no-namespace )
add_cmake_build_test( export-verify-copyright-header.cmake )

add_cmake_config_test( export-verify-bad-code-block-var.cmake SHOULD_FAIL "FINAL_CODE_BLOCK variable `var_doesn't_exist` doesn't exist")
add_cmake_config_test( export-verify-missing-components.cmake SHOULD_FAIL "is missing COMPONENTS as COMPONENTS_EXPORT_SET was provided")
Expand Down
49 changes: 49 additions & 0 deletions testing/export/export-verify-copyright-header.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#=============================================================================
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
include(${rapids-cmake-dir}/export/cpm.cmake)
include(${rapids-cmake-dir}/export/export.cmake)
include(${rapids-cmake-dir}/export/write_dependencies.cmake)

cmake_minimum_required(VERSION 3.23.1)
project(FakEProJecT LANGUAGES CXX VERSION 3.1.4)

add_library(fakeLib INTERFACE)
install(TARGETS fakeLib EXPORT fake_set)

rapids_export_cpm(BUILD RaFT FakEProJecT
CPM_ARGS
FAKE_PACKAGE_ARGS TRUE
)

rapids_export_write_dependencies(BUILD FakEProJecT dependencies.cmake)

rapids_export(BUILD FakEProJecT
EXPORT_SET fake_set
NAMESPACE test::
)
rapids_export(INSTALL FakEProJecT
EXPORT_SET fake_set
NAMESPACE test::
)

add_custom_target(verify_copyright_headers ALL
COMMAND ${CMAKE_COMMAND} -E rm -rf "${CMAKE_BINARY_DIR}/install"
COMMAND ${CMAKE_COMMAND} --install "${CMAKE_BINARY_DIR}" --prefix "${CMAKE_BINARY_DIR}/install"
COMMAND ${CMAKE_COMMAND} "-Dfile=${CMAKE_BINARY_DIR}/fakeproject-config.cmake" -P "${rapids-cmake-testing-dir}/utils/check_copyright_header_script.cmake"
COMMAND ${CMAKE_COMMAND} "-Dfile=${CMAKE_BINARY_DIR}/install/lib/cmake/fakeproject/fakeproject-config.cmake" -P "${rapids-cmake-testing-dir}/utils/check_copyright_header_script.cmake"
COMMAND ${CMAKE_COMMAND} "-Dfile=${CMAKE_BINARY_DIR}/rapids-cmake/FakEProJecT/build/cpm_RaFT.cmake" -P "${rapids-cmake-testing-dir}/utils/check_copyright_header_script.cmake"
COMMAND ${CMAKE_COMMAND} "-Dfile=${CMAKE_BINARY_DIR}/dependencies.cmake" -P "${rapids-cmake-testing-dir}/utils/check_copyright_header_script.cmake"
)
4 changes: 3 additions & 1 deletion testing/find/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,3 +44,5 @@ add_cmake_config_test( find_generate_module-header-only )
add_cmake_config_test( find_generate_module-code-blocks )
add_cmake_config_test( find_generate_module-back-initial-code-block-var.cmake SHOULD_FAIL "INITIAL_CODE_BLOCK variable `var_doesn't_exist` doesn't exist")
add_cmake_config_test( find_generate_module-back-final-code-block-var.cmake SHOULD_FAIL "FINAL_CODE_BLOCK variable `var_doesn't_exist` doesn't exist")

add_cmake_build_test( find_generate_module-verify-copyright-header.cmake )
25 changes: 25 additions & 0 deletions testing/find/find_generate_module-verify-copyright-header.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#=============================================================================
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
include(${rapids-cmake-dir}/find/generate_module.cmake)

rapids_find_generate_module( RapidsTest
HEADER_NAMES rapids-cmake-test-header_only.hpp
INSTALL_EXPORT_SET test_set
)

add_custom_target(verify_copyright_headers ALL
COMMAND ${CMAKE_COMMAND} "-Dfile=${CMAKE_BINARY_DIR}/cmake/find_modules/FindRapidsTest.cmake" -P "${rapids-cmake-testing-dir}/utils/check_copyright_header_script.cmake"
)
42 changes: 42 additions & 0 deletions testing/utils/check_copyright_header.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#=============================================================================
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
include_guard(GLOBAL)

function(check_copyright_header file)
string(TIMESTAMP current_year "%Y" UTC)
string(CONFIGURE [=[#=============================================================================
# Copyright (c) @current_year@, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
]=] expected_header @ONLY)
string(LENGTH "${expected_header}" expected_header_length)

file(READ "${file}" actual_header LIMIT "${expected_header_length}")
if(NOT actual_header STREQUAL expected_header)
message(FATAL_ERROR "File ${file} did not have expected copyright header")
endif()
endfunction()
25 changes: 25 additions & 0 deletions testing/utils/check_copyright_header_script.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#=============================================================================
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================

#=============================================================================
# This file is intended to be run as a standalone script, like so:
#
# $ cmake -Dfile=<file-to-check> \
# -P ${rapids-cmake-testing-dir}/utils/check_copyright_header_script.cmake
#=============================================================================
include("${CMAKE_CURRENT_LIST_DIR}/check_copyright_header.cmake")

check_copyright_header("${file}")

0 comments on commit 98b4c20

Please sign in to comment.