From 15f35883eb4508fb9295afc7f066eb173552b424 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Mon, 26 Feb 2024 10:01:28 -0500 Subject: [PATCH] Build generate_ctest_json in try_compile() (#537) Authors: - Kyle Edwards (https://github.com/KyleFromNVIDIA) - Robert Maynard (https://github.com/robertmaynard) Approvers: - Robert Maynard (https://github.com/robertmaynard) URL: https://github.com/rapidsai/rapids-cmake/pull/537 --- .../generate_resource_spec/CMakeLists.txt | 31 +++++++++++++++++ .../generate_resource_spec.cpp | 2 +- .../test/generate_resource_spec.cmake | 33 ++++++------------- 3 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 rapids-cmake/test/detail/generate_resource_spec/CMakeLists.txt rename rapids-cmake/test/detail/{ => generate_resource_spec}/generate_resource_spec.cpp (97%) diff --git a/rapids-cmake/test/detail/generate_resource_spec/CMakeLists.txt b/rapids-cmake/test/detail/generate_resource_spec/CMakeLists.txt new file mode 100644 index 00000000..dfd7c97f --- /dev/null +++ b/rapids-cmake/test/detail/generate_resource_spec/CMakeLists.txt @@ -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 $ + "${output_file}" COMMENT "Copying executable to destination") diff --git a/rapids-cmake/test/detail/generate_resource_spec.cpp b/rapids-cmake/test/detail/generate_resource_spec/generate_resource_spec.cpp similarity index 97% rename from rapids-cmake/test/detail/generate_resource_spec.cpp rename to rapids-cmake/test/detail/generate_resource_spec/generate_resource_spec.cpp index c0d93ca3..111993d6 100644 --- a/rapids-cmake/test/detail/generate_resource_spec.cpp +++ b/rapids-cmake/test/detail/generate_resource_spec/generate_resource_spec.cpp @@ -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. diff --git a/rapids-cmake/test/generate_resource_spec.cmake b/rapids-cmake/test/generate_resource_spec.cmake index 02609412..9c87f608 100644 --- a/rapids-cmake/test/generate_resource_spec.cmake +++ b/rapids-cmake/test/generate_resource_spec.cmake @@ -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()