Skip to content

Commit

Permalink
Add a new cmake fn for using Clad with a library
Browse files Browse the repository at this point in the history
  • Loading branch information
vaithak authored and vgvassilev committed May 22, 2024
1 parent d629553 commit 41565dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
27 changes: 23 additions & 4 deletions cmake/modules/AddClad.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ set_property(DIRECTORY APPEND PROPERTY
endif(CLAD_ENABLE_BENCHMARKS)

#-------------------------------------------------------------------------------
# function ENABLE_CLAD_FOR_EXECUTABLE(<executable>
# function ENABLE_CLAD_FOR_TARGET(<executable>
# 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()
Expand Down Expand Up @@ -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(<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(<executable> sources...
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions unittests/Misc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 41565dd

Please sign in to comment.