Skip to content

Commit

Permalink
Build generate_ctest_json in a try_compile()
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleFromNVIDIA committed Feb 7, 2024
1 parent 46ad1bb commit dd465ca
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 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 CXX)

find_package(CUDAToolkit QUIET)
find_package(Threads REQUIRED)

add_executable(generate_ctest_json generate_resource_spec.cpp)
target_link_libraries(generate_ctest_json PRIVATE Threads::Threads rt ${CMAKE_DL_LIBS})
if(CUDAToolkit_FOUND)
target_link_libraries(generate_ctest_json PRIVATE CUDA::cudart)
target_compile_definitions(generate_ctest_json PRIVATE HAVE_CUDA)
endif()

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
20 changes: 8 additions & 12 deletions rapids-cmake/test/generate_resource_spec.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,28 @@ function(rapids_test_generate_resource_spec DESTINATION filepath)
]=])

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}")
if(NOT DEFINED CMAKE_CXX_COMPILER)
set(compiler "${CMAKE_CUDA_COMPILER}")
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
generate_resource_spec SOURCE_DIR
"${CMAKE_CURRENT_FUNCTION_LIST_DIR}/detail/generate_resource_spec"
CMAKE_FLAGS "-DCMAKE_CXX_COMPILER=${compiler}"
"-DCUDAToolkit_ROOT=${CUDAToolkit_ROOT}" "-Doutput_file=${eval_exe}"
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 dd465ca

Please sign in to comment.