Skip to content

Commit

Permalink
Build generate_ctest_json in try_compile() (#537)
Browse files Browse the repository at this point in the history
Authors:
  - Kyle Edwards (https://github.com/KyleFromNVIDIA)
  - Robert Maynard (https://github.com/robertmaynard)

Approvers:
  - Robert Maynard (https://github.com/robertmaynard)

URL: #537
  • Loading branch information
KyleFromNVIDIA authored Feb 26, 2024
1 parent 59827ff commit 15f3588
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
31 changes: 31 additions & 0 deletions rapids-cmake/test/detail/generate_resource_spec/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#=============================================================================
# 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.
#=============================================================================
cmake_minimum_required(VERSION 3.23.1)
project(generate_resource_spec ${lang})

set(CMAKE_CUDA_ARCHITECTURES all)

add_executable(generate_ctest_json generate_resource_spec.cpp)
if(cuda_toolkit)
find_package(CUDAToolkit REQUIRED QUIET)
target_link_libraries(generate_ctest_json PRIVATE CUDA::cudart_static)
target_compile_definitions(generate_ctest_json PRIVATE HAVE_CUDA)
endif()
set_property(SOURCE generate_resource_spec.cpp PROPERTY LANGUAGE ${lang})

add_custom_command(TARGET generate_ctest_json POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:generate_ctest_json>
"${output_file}" COMMENT "Copying executable to destination")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023, 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
33 changes: 10 additions & 23 deletions rapids-cmake/test/generate_resource_spec.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,43 +51,30 @@ function(rapids_test_generate_resource_spec DESTINATION filepath)
)
endif()

set(gpu_json_contents
[=[
{
"version": {"major": 1, "minor": 0},
"local": [{
"gpus": [{"id":"0", "slots": 0}]
}]
}
]=])

include(${CMAKE_CURRENT_FUNCTION_LIST_DIR}/detail/default_names.cmake)
set(eval_file ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/detail/generate_resource_spec.cpp)
set(eval_exe ${PROJECT_BINARY_DIR}/rapids-cmake/${rapids_test_generate_exe_name})
set(error_file ${PROJECT_BINARY_DIR}/rapids-cmake/detect_gpus.stderr.log)

if(NOT EXISTS "${eval_exe}")
find_package(CUDAToolkit QUIET)
file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/rapids-cmake/")

if(CUDAToolkit_FOUND)
set(cuda_include_options ${CUDAToolkit_INCLUDE_DIRS})
list(TRANSFORM cuda_include_options PREPEND "-I")
set(compile_options ${cuda_include_options} "-DHAVE_CUDA")
endif()
set(link_options ${CUDA_cudart_LIBRARY} -lpthread -lrt -ldl)
set(compiler "${CMAKE_CXX_COMPILER}")
set(lang CXX)
if(NOT DEFINED CMAKE_CXX_COMPILER)
set(compiler "${CMAKE_CUDA_COMPILER}")
set(lang CUDA)
endif()

execute_process(COMMAND "${compiler}" "${eval_file}" ${compile_options} ${link_options} -o
"${eval_exe}" OUTPUT_VARIABLE compile_output
ERROR_VARIABLE compile_output RESULT_VARIABLE result)
try_compile(result "${PROJECT_BINARY_DIR}/rapids-cmake/generate_ctest_json-build"
"${CMAKE_CURRENT_FUNCTION_LIST_DIR}/detail/generate_resource_spec"
generate_resource_spec
CMAKE_FLAGS "-DCUDAToolkit_ROOT=${CUDAToolkit_ROOT}" "-Doutput_file=${eval_exe}"
"-Dlang=${lang}" "-Dcuda_toolkit=${CUDAToolkit_FOUND}"
OUTPUT_VARIABLE compile_output)

if(NOT result EQUAL 0)
if(NOT result)
string(REPLACE "\n" "\n " compile_output "${compile_output}")
message(FATAL_ERROR "rapids_test_generate_resource_spec failed to build detection executable.\nrapids_test_generate_resource_spec compile[${compiler} ${compile_options} ${link_options}] failure details are:\n ${compile_output}"
message(FATAL_ERROR "rapids_test_generate_resource_spec failed to build detection executable.\nfailure details are:\n ${compile_output}"
)
endif()
endif()
Expand Down

0 comments on commit 15f3588

Please sign in to comment.