From 41565ddfae04cdbb79eca87e3e659d5c7ab15ac4 Mon Sep 17 00:00:00 2001 From: Vaibhav Thakkar Date: Tue, 21 May 2024 23:11:39 +0200 Subject: [PATCH] Add a new cmake fn for using Clad with a library --- cmake/modules/AddClad.cmake | 27 +++++++++++++++++++++++---- unittests/CMakeLists.txt | 2 +- unittests/Misc/CMakeLists.txt | 6 ++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/cmake/modules/AddClad.cmake b/cmake/modules/AddClad.cmake index a765bb243..77c0aa871 100644 --- a/cmake/modules/AddClad.cmake +++ b/cmake/modules/AddClad.cmake @@ -14,14 +14,14 @@ set_property(DIRECTORY APPEND PROPERTY endif(CLAD_ENABLE_BENCHMARKS) #------------------------------------------------------------------------------- -# function ENABLE_CLAD_FOR_EXECUTABLE( +# function ENABLE_CLAD_FOR_TARGET( # DEPENDS dependencies... # A list of targets that the executable depends on. # LIBRARIES libraries... # A list of libraries to be linked in. Defaults to stdc++ pthread m. # ) #------------------------------------------------------------------------------- -function(ENABLE_CLAD_FOR_EXECUTABLE executable) +function(ENABLE_CLAD_FOR_TARGET executable) if (NOT TARGET ${executable}) message(FATAL_ERROR "'${executable}' is not a valid target.") endif() @@ -56,7 +56,26 @@ function(ENABLE_CLAD_FOR_EXECUTABLE executable) add_dependencies(${executable} ${ARG_DEPENDS}) endif(ARG_DEPENDS) -endfunction(ENABLE_CLAD_FOR_EXECUTABLE) +endfunction(ENABLE_CLAD_FOR_TARGET) + +#------------------------------------------------------------------------------- +# function ADD_CLAD_LIBRARY( sources... +# DEPENDS dependencies... +# A list of targets that the library depends on. +# LIBRARIES libraries... +# A list of libraries to be linked in. Defaults to stdc++ pthread m. +# ) +#------------------------------------------------------------------------------- +function(ADD_CLAD_LIBRARY library) + cmake_parse_arguments(ARG "" "DEPENDS;LIBRARIES" "" ${ARGN}) + + set(source_files ${ARG_UNPARSED_ARGUMENTS}) + + add_library (${library} ${source_files}) + ENABLE_CLAD_FOR_TARGET(${library} ${ARGN}) + +endfunction(ADD_CLAD_LIBRARY) + #------------------------------------------------------------------------------- # function ADD_CLAD_EXECUTABLE( sources... @@ -73,7 +92,7 @@ function(ADD_CLAD_EXECUTABLE executable) add_executable(${executable} ${source_files}) - ENABLE_CLAD_FOR_EXECUTABLE(${executable} ${ARGN}) + ENABLE_CLAD_FOR_TARGET(${executable} ${ARGN}) endfunction(ADD_CLAD_EXECUTABLE) diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index a8c5a946f..c714bee97 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -19,7 +19,7 @@ function(add_clad_unittest test_dirname) list(REMOVE_ITEM GTEST_LINKED_LIBS llvm_gtest_main llvm_gtest) set_property(TARGET ${test_dirname} PROPERTY LINK_LIBRARIES ${GTEST_LINKED_LIBS}) - enable_clad_for_executable(${test_dirname}) + enable_clad_for_target(${test_dirname}) # Add gtest dependencies. set(gtest_libs gtest gtest_main) diff --git a/unittests/Misc/CMakeLists.txt b/unittests/Misc/CMakeLists.txt index 4aaddf9d2..07fb60885 100644 --- a/unittests/Misc/CMakeLists.txt +++ b/unittests/Misc/CMakeLists.txt @@ -5,10 +5,8 @@ add_clad_unittest(MiscTests DynamicGraph.cpp ) -# Create a library from Defs.cpp -add_library(Defs SHARED Defs.cpp) -target_include_directories(Defs PUBLIC ${CMAKE_SOURCE_DIR}/include) -target_link_libraries(Defs PUBLIC stdc++ pthread m) +# Create a library from the Defs.cpp file +ADD_CLAD_LIBRARY(Defs Defs.cpp) # Link the library to the test target_link_libraries(MiscTests PRIVATE Defs)