diff --git a/CMakeLists.txt b/CMakeLists.txt index bed53353872bb9603379e1d63b8bff2deaf6bb5a..719edc2a649a6dc2a1611b18ac7c942d034abca0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,15 +11,20 @@ list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") option(ENABLE_EXTRAE "Enable Extrae" OFF) option(ENABLE_DLB "Enable DLB" OFF) - +option(BUILD_C_FORTRAN "Enable C and Fortran Interfaces" ON) add_library(sit SHARED) -set(SIT_API_INCLUDES ${PROJECT_SOURCE_DIR}/include) -target_include_directories(sit PRIVATE ${SIT_API_INCLUDES}) +set(SIT_PUBLIC_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include) + +target_include_directories(sit PUBLIC + $ + $ + $ + $ +) -include(GenerateExportHeader) -generate_export_header(sit) +#set_target_properties(sit PROPERTIES PUBLIC_HEADER "${SIT_PUBLIC_HEADERS}") add_subdirectory(src) @@ -27,3 +32,69 @@ add_subdirectory(src) enable_testing() add_subdirectory(tests) + + +# +# Install Targets for sit +# + +# Setup General Install for: +# precice - the library +include(GNUInstallDirs) +message(STATUS ${CMAKE_INSTALL_INCLUDEDIR}) + + + +if(BUILD_C_FORTRAN) + +install(TARGETS sit sit_f + EXPORT sitTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sit + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sit + ) +else() + +install(TARGETS sit + EXPORT sitTargets + FILE_SET HEADERS + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sit + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sit + ) +install(FILES ${SIT_PUBLIC_HEADERS}/sit/sit.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/sit) +endif() + + + +# Export the Targets to install +install(EXPORT sitTargets + FILE sitTargets.cmake + NAMESPACE sit:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/sit + ) + +# Generate a Package Config File for precice +include(CMakePackageConfigHelpers) +write_basic_package_version_file("sitConfigVersion.cmake" + VERSION 1.0.0 + COMPATIBILITY SameMajorVersion + ) + +# Install the Config and the ConfigVersion files +install(FILES "cmake/sitConfig.cmake" "${sit_BINARY_DIR}/sitConfigVersion.cmake" + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/sit + ) + +# Setup the config in the build directory +export(EXPORT sitTargets + NAMESPACE sit:: + FILE "sitTargets.cmake") +file(COPY "cmake/sitConfig.cmake" + DESTINATION "${sit_BINARY_DIR}") + +# Add an alias to allow subprojects to use the namespaced name +add_library(sit::sit ALIAS sit) + diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..c13af4204a7904d0ecd569d7480490993f32d46d --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# SIT? + +## CXX bindings + +How to configure and install: + +1. `cmake .. -DCMAKE_INSTALL_PREFIX= -DBUILD_C_FORTRAN=OFF` +2. `make install` + +### How to use + +```cpp +#include "sit/sit.hpp" + +int main() { + sit::init("Default"); + sit::region_start("peter"); + + sit::region_start("marie"); + + sit::region_stop("marie"); + + sit::region_stop("peter"); + sit::finalize(); +} + +You **must** call `sit::init` exactly once to init the datastructures before you can use the other functionality. +Also some backends require finalization, so please make sure to also call `finalize` in the end. + +To add it to your code, currently just install it and use something similar to: +`-I/include -L-I/lib -lsit` in the compiler options diff --git a/cmake/sitConfig.cmake b/cmake/sitConfig.cmake new file mode 100644 index 0000000000000000000000000000000000000000..9423ad2ba7dc789091aaac7069a48bd6034590e5 --- /dev/null +++ b/cmake/sitConfig.cmake @@ -0,0 +1 @@ +include("${CMAKE_CURRENT_LIST_DIR}/sitTargets.cmake") \ No newline at end of file diff --git a/include/sit.hpp b/include/sit/sit.hpp similarity index 100% rename from include/sit.hpp rename to include/sit/sit.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c84c72a4ade1872bf17a4bb32dff1aef2b8428ac..5825ab7701e7c7eacd803f5f41f44f50a0b572c4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,7 @@ - add_subdirectory(backends) -target_include_directories(sit PRIVATE .) -target_sources(sit PRIVATE sit_delegator.cpp sit.cpp) \ No newline at end of file +target_sources(sit PRIVATE sit_delegator.cpp sit.cpp) + +if(BUILD_C_FORTRAN) + add_subdirectory(bindings) +endif() + diff --git a/src/bindings/CMakeLists.txt b/src/bindings/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..69845ae9d2d656df3a8d557f8a61c8f408d83039 --- /dev/null +++ b/src/bindings/CMakeLists.txt @@ -0,0 +1,34 @@ + +if(EXISTS ${SHROUD_EXECUTABLE}) + execute_process(COMMAND ${SHROUD_EXECUTABLE} + --cmake ${CMAKE_CURRENT_BINARY_DIR}/SetupShroud.cmake + ERROR_VARIABLE SHROUD_cmake_error + OUTPUT_STRIP_TRAILING_WHITESPACE ) + if(${SHROUD_cmake_error}) + message(FATAL_ERROR "Error from Shroud: ${SHROUD_cmake_error}") + endif() + include(${CMAKE_CURRENT_BINARY_DIR}/SetupShroud.cmake) + add_shroud( + YAML_INPUT_FILE sit.yaml + C_FORTRAN_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} + ) + + set_source_files_properties(wrap${_basename}.cpp PROPERTIES GENERATED TRUE) + set_source_files_properties(wrapf${_basename}.f PROPERTIES GENERATED TRUE) + set(CMAKE_Fortran_FORMAT FREE) + list (APPEND SIT_PUBLIC_HEADERS "${LIB_INC}") + + + + add_library(${_basename}_f wrapf${_basename}.f wrap${_basename}.cpp ${LIB_SRC}) + + target_include_directories(${_basename}_f PRIVATE ${SIT_PUBLIC_HEADERS}) + + + set_target_properties(${_basename}_f PROPERTIES LINKER_LANGUAGE Fortran) + set_target_properties(${_basename}_f PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + set_target_properties(${_basename}_f PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include) + add_dependencies(${_basename}_f generate_${_basename}) +else() +message(FATAL_ERROR "Cannot build Fortran and C bindings without -DSHROUD_EXECUTABLE specified") +endif() diff --git a/src/bindings/sit.yaml b/src/bindings/sit.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1c7bf3816881eeb86c77ddb24adbe1b01e1f3903 --- /dev/null +++ b/src/bindings/sit.yaml @@ -0,0 +1,11 @@ +library: sit +namespace: sit +cxx_header: sit/sit.hpp +format: + C_prefix: 'sit_' + +declarations: +- decl: void init(const std::string &backend) +- decl: void region_start(const std::string &name) +- decl: void region_stop(const std::string &name) +- decl: void finalize() \ No newline at end of file diff --git a/src/sit.cpp b/src/sit.cpp index 334c1d7ec5c9de231540ab7deac5166a8ee46996..099c54295549ab32f709058a335f419747720847 100644 --- a/src/sit.cpp +++ b/src/sit.cpp @@ -1,5 +1,5 @@ -#include "sit.hpp" +#include "sit/sit.hpp" #include "sit_delegator.hpp" #include namespace sit { diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0337b8035fe2a4ebb716792138d4667a691ed808 --- /dev/null +++ b/test.cpp @@ -0,0 +1,19 @@ +#include "sit/sit.hpp" + +int main() { + // Initialize the SIT library with a default configuration + sit::init("Default"); + + // Start a region named "Default" + sit::region_start("Default"); + + // Do some work here (for demonstration purposes, we won't do anything) + + // Stop the region named "Default" + sit::region_stop("Default"); + + // Finalize the SIT library + sit::finalize(); + + return 0; +} \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c1c6d27f346a25cf89ae5ca1bd9ae8a06c9b6193..279243ee98c1d55a456b1ab141169b6d71285720 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,7 +1,38 @@ -add_executable(TestCppLink integration/cpp/TestCppLink.cpp) -target_link_libraries(TestCppLink sit) +add_executable(TestCppLink cpp/TestCppLink.cpp) +target_link_libraries(TestCppLink sit::sit) target_include_directories(TestCppLink - PRIVATE ${SIT_API_INCLUDES}) + PRIVATE ${SIT_PUBLIC_HEADERS}) + add_test(NAME TestCppLink - COMMAND TestCppLink) \ No newline at end of file + COMMAND TestCppLink) + + +if(BUILD_C_FORTRAN) + add_executable(TestCLink c/TestCLink.c) + target_link_libraries(TestCLink sit) + target_include_directories(TestCLink + PRIVATE ${SIT_PUBLIC_HEADERS}) + + + add_test(NAME TestCLink + COMMAND TestCLink) + + + + + add_executable(TestFortranLink fortran/TestFortranModule.f90) + target_link_libraries(TestFortranLink sit sit_f) + target_include_directories(TestFortranLink + PRIVATE ${SIT_PUBLIC_HEADERS}) + + message(STATUS ${SIT_PUBLIC_HEADERS}) + + target_include_directories(TestFortranLink + PRIVATE ${CMAKE_BINARY_DIR}/include) + + + + add_test(NAME TestFortranLink + COMMAND TestFortranLink) +endif() \ No newline at end of file diff --git a/tests/c/TestCLink.c b/tests/c/TestCLink.c new file mode 100644 index 0000000000000000000000000000000000000000..1824f72e6d5f055f5ece9b6febbd81e7c4939dcb --- /dev/null +++ b/tests/c/TestCLink.c @@ -0,0 +1,3 @@ +//#include "wrapsit.h" + +int main() {} \ No newline at end of file diff --git a/tests/integration/cpp/TestCppLink.cpp b/tests/cpp/TestCppLink.cpp similarity index 84% rename from tests/integration/cpp/TestCppLink.cpp rename to tests/cpp/TestCppLink.cpp index 0c2e150d71cd488dcbe86dd638057cee3fee2f27..7f8baec5664857880b0e6f2135282b2389660e9c 100644 --- a/tests/integration/cpp/TestCppLink.cpp +++ b/tests/cpp/TestCppLink.cpp @@ -1,4 +1,4 @@ -#include "sit.hpp" +#include "sit/sit.hpp" int main() { sit::init("Default"); diff --git a/tests/fortran/TestFortranModule.f90 b/tests/fortran/TestFortranModule.f90 new file mode 100644 index 0000000000000000000000000000000000000000..9194e19adf5c3774dc97f82c34721ebdadb82b70 --- /dev/null +++ b/tests/fortran/TestFortranModule.f90 @@ -0,0 +1,5 @@ +program do_sample + use sit_mod + call init("Default") + call region_start("test") +end program do_sample \ No newline at end of file diff --git a/tests/integration/c/CMakeLists.txt b/tests/integration/c/CMakeLists.txt deleted file mode 100644 index 84d814cebc8a1c0d4f5eb7c85abb0a709d081505..0000000000000000000000000000000000000000 --- a/tests/integration/c/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -cmake_minimum_required (VERSION 3.22) -project(test-c VERSION 4.0.1 LANGUAGES C) - - -option(ENABLE_EXTRAE "Enable Extrae" OFF) -option(ENABLE_DLB "Enable DLB" OFF) - -add_executable(test-c) -find_package(sit REQUIRED) -target_link_libraries(test-c sit) diff --git a/tests/integration/c/test-c.c b/tests/integration/c/test-c.c deleted file mode 100644 index ace88c0e2e1727427784bb55161e29caeb231e25..0000000000000000000000000000000000000000 --- a/tests/integration/c/test-c.c +++ /dev/null @@ -1,3 +0,0 @@ -#include "sit.h" - -int main() {} \ No newline at end of file