Skip to content

Commit

Permalink
Merge pull request #23 from robertmaynard/fea/add_cmake_write_version
Browse files Browse the repository at this point in the history
Introduce `rapids_cmake_write_version_file`
  • Loading branch information
robertmaynard authored May 28, 2021
2 parents 4ac411b + 99a71c3 commit b296824
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The `rapids-cmake` module contains helpful general CMake functionality
- `rapids_cmake_build_type( )` handles initialization of `CMAKE_BUILD_TYPE`
- `rapids_cmake_support_conda_env( target [MODIFY_PREFIX_PATH])` Establish a target that holds the CONDA enviornment
include and link directories.
- `rapids_cmake_write_version_file( <file> )` Write a C++ header with a projects MAJOR, MINOR, and PATCH defines

### cpm

Expand Down
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require.
/command/rapids_cmake_make_global
/command/rapids_cmake_parse_version
/command/rapids_cmake_support_conda_env
/command/rapids_cmake_write_version_file

CPM
***
Expand Down
1 change: 1 addition & 0 deletions docs/command/rapids_cmake_write_version_file.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. cmake-module:: ../../rapids-cmake/cmake/write_version_file.cmake
20 changes: 20 additions & 0 deletions rapids-cmake/cmake/template/version.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2021, 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.
*/
#pragma once

#define @PROJECT_NAME@_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define @PROJECT_NAME@_VERSION_MINOR @PROJECT_VERSION_MINOR@
#define @PROJECT_NAME@_VERSION_PATCH @PROJECT_VERSION_PATCH@
56 changes: 56 additions & 0 deletions rapids-cmake/cmake/write_version_file.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#=============================================================================
# Copyright (c) 2018-2021, 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)


#[=======================================================================[.rst:
rapids_cmake_write_version_file
-------------------------------

.. versionadded:: v21.08.00

Generate a C++ header file that hold the version (`X.Y.Z`) information of the calling project.

.. code-block:: cmake

rapids_cmake_write_version_file(file_path)

The file generated by :cmake:command:`rapids_cmake_write_version_file` will hold the following
components of a `X.Y.Z` version string as C++ defines, extracted from the
CMake :cmake:command:`project` call.

- <PROJECT_NAME>_VERSION_MAJOR (X)
- <PROJECT_NAME>_VERSION_MINOR (Y)
- <PROJECT_NAME>_VERSION_PATCH (Z)

``file_path``
Either an absolute or relative path.
When a relative path, the absolute location will be computed from
cmake:variable:`CMAKE_CURRENT_BINARY_DIR`

#]=======================================================================]
function(rapids_cmake_write_version_file file_path)
list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.cmake.write_version_file")

cmake_path(IS_RELATIVE file_path is_relative)
if(is_relative)
cmake_path(APPEND CMAKE_CURRENT_BINARY_DIR file_path OUTPUT_VARIABLE output_path)
else()
set(output_path "${file_path}")
endif()

configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/template/version.hpp.in" "${output_path}" @ONLY)
endfunction()
1 change: 1 addition & 0 deletions rapids-cmake/rapids-cmake.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ include_guard(GLOBAL)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/build_type.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/parse_version.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/support_conda_env.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/wrie_version_file.cmake)
3 changes: 3 additions & 0 deletions testing/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ add_cmake_config_test( parse_version-patch-valid.cmake )
add_cmake_config_test( parse_version-patch-invalid.cmake )
add_cmake_config_test( parse_version-major_minor-invalid.cmake )
add_cmake_config_test( parse_version-major_minor-valid.cmake )

add_cmake_config_test( write_version-absolute )
add_cmake_config_test( write_version-relative )
28 changes: 28 additions & 0 deletions testing/cmake/write_version-absolute/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#=============================================================================
# Copyright (c) 2018-2021, 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}/cmake/write_version_file.cmake)

cmake_minimum_required(VERSION 3.20)

project(DEMO VERSION 2.4 LANGUAGES NONE)
rapids_cmake_write_version_file("${CMAKE_CURRENT_BINARY_DIR}/demo/version.h")

project(NESTED VERSION 3.14.159 LANGUAGES NONE)
rapids_cmake_write_version_file("${CMAKE_CURRENT_BINARY_DIR}/nested/version.h")

enable_language(CXX)
add_executable(write_version main.cpp)
target_include_directories(write_version PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
40 changes: 40 additions & 0 deletions testing/cmake/write_version-absolute/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2019-2021, 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 <type_traits>
#include <demo/version.h>
#include <nested/version.h>

constexpr int dmajor = DEMO_VERSION_MAJOR;
constexpr int dminor = DEMO_VERSION_MINOR;
constexpr int dpatch = DEMO_VERSION_MPATCH;

constexpr int nmajor = NESTED_VERSION_MAJOR;
constexpr int nminor = NESTED_VERSION_MINOR;
constexpr int npatch = NESTED_VERSION_MPATCH;

int main()
{
static_assert(dmajor == 2);
static_assert(dminor == 4);
static_assert(dpatch == 0);


static_assert(nmajor == 3);
static_assert(nminor == 14);
static_assert(npatch == 159);
return 0;
}
28 changes: 28 additions & 0 deletions testing/cmake/write_version-relative/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#=============================================================================
# Copyright (c) 2018-2021, 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}/cmake/write_version_file.cmake)

cmake_minimum_required(VERSION 3.20)

project(DEMO VERSION 3.2.1 LANGUAGES NONE)
rapids_cmake_write_version_file(demo_version.hpp)

project(NESTED VERSION 3.14.159 LANGUAGES NONE)
rapids_cmake_write_version_file(nested_version.hpp)

enable_language(CXX)
add_executable(write_version main.cpp)
target_include_directories(write_version PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
39 changes: 39 additions & 0 deletions testing/cmake/write_version-relative/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2019-2021, 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 <type_traits>
#include <demo_version.hpp>
#include <nested_version.hpp>

constexpr int dmajor = DEMO_VERSION_MAJOR;
constexpr int dminor = DEMO_VERSION_MINOR;
constexpr int dpatch = DEMO_VERSION_MPATCH;

constexpr int nmajor = NESTED_VERSION_MAJOR;
constexpr int nminor = NESTED_VERSION_MINOR;
constexpr int npatch = NESTED_VERSION_MPATCH;

int main()
{
static_assert(dmajor == 3);
static_assert(dminor == 2);
static_assert(dpatch == 1);

static_assert(nmajor == 3);
static_assert(nminor == 14);
static_assert(npatch == 159);
return 0;
}

0 comments on commit b296824

Please sign in to comment.