diff --git a/.gitignore b/.gitignore index a1e6811..07a2a6b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ scripts venv -build +build* ._* diff --git a/CMakeLists.txt b/CMakeLists.txt index a0c78b7..349bbe8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,43 +2,88 @@ # # SPDX-License-Identifier: BSD-3-Clause -cmake_minimum_required(VERSION 3.5) -project(binsparse-rc) +cmake_minimum_required(VERSION 3.20) +project(binsparse VERSION 1.0.0) cmake_policy(SET CMP0079 NEW) set(CMAKE_C_STANDARD 11) - set(CMAKE_CXX_STANDARD 20) - set(CMAKE_C_FLAGS "-O3 -march=native") -add_library(binsparse-rc STATIC) +include(GNUInstallDirs) + +add_library(binsparse STATIC) add_subdirectory(include) add_subdirectory(src) # NOTE: For now, both HDF5 and cJSON are `PUBLIC`, meaning that anything that -# depends on `binsparse-rc` will also link/include HDF5 and cJSON. We can change -# these to `PRIVATE` to use them only when building binsparse-rc. +# depends on `binsparse` will also link/include HDF5 and cJSON. We can change +# these to `PRIVATE` to use them only when building binsparse. find_package(HDF5 REQUIRED COMPONENTS C) -target_link_libraries(binsparse-rc PUBLIC ${HDF5_C_LIBRARIES}) -target_include_directories(binsparse-rc PUBLIC . ${HDF5_INCLUDE_DIRS}) +target_link_libraries(binsparse PUBLIC ${HDF5_C_LIBRARIES}) include(FetchContent) FetchContent_Declare( cJSON - GIT_REPOSITORY https://github.com/DaveGamble/cJSON.git - GIT_TAG v1.7.17 + # GIT_REPOSITORY https://github.com/DaveGamble/cJSON.git + GIT_REPOSITORY https://github.com/p1k0chu/cJSON.git + GIT_TAG 887642c0a93bd8a6616bf90daacac0ea7d4b095e ) FetchContent_MakeAvailable(cJSON) configure_file(${cJSON_SOURCE_DIR}/cJSON.h ${CMAKE_BINARY_DIR}/include/cJSON/cJSON.h) -target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/include) target_link_libraries(${PROJECT_NAME} PUBLIC cjson) +# Set up include directories properly for both build and install +target_include_directories(${PROJECT_NAME} + PUBLIC + $ + $ + $ + $ + ${HDF5_INCLUDE_DIRS}) + if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) add_subdirectory(examples) add_subdirectory(test) endif() + +# Installation rules - these are always needed when the library is built +install(TARGETS binsparse + EXPORT binsparse-targets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + +# Install headers +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/binsparse + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + FILES_MATCHING PATTERN "*.h" + PATTERN "*.hpp") + +# Export targets +install(EXPORT binsparse-targets + FILE binsparse-targets.cmake + NAMESPACE binsparse:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse) + +# Create and install package config files +include(CMakePackageConfigHelpers) +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/binsparse-config-version.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion) + +configure_package_config_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/binsparse-config.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/binsparse-config.cmake" + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse) + +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/binsparse-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/binsparse-config-version.cmake" + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse) diff --git a/cmake/binsparse-config.cmake.in b/cmake/binsparse-config.cmake.in new file mode 100644 index 0000000..052bc3b --- /dev/null +++ b/cmake/binsparse-config.cmake.in @@ -0,0 +1,14 @@ +# SPDX-FileCopyrightText: 2024 Binsparse Developers +# +# SPDX-License-Identifier: BSD-3-Clause + +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/binsparse-targets.cmake") +check_required_components(binsparse) + +# Include dependencies +include(CMakeFindDependencyMacro) +find_dependency(HDF5 COMPONENTS C) + +# cJSON is bundled with binsparse, no need to find it externally diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 7e5bbd1..0246602 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -4,7 +4,7 @@ function(add_example example_name) add_executable(${example_name} ${example_name}.c) - target_link_libraries(${example_name} binsparse-rc) + target_link_libraries(${example_name} binsparse) endfunction() add_example(simple_matrix_write) diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt index 2b8dcdf..9334981 100644 --- a/examples/cpp/CMakeLists.txt +++ b/examples/cpp/CMakeLists.txt @@ -4,7 +4,7 @@ function(add_example example_name) add_executable(${example_name}-cpp ${example_name}.cpp) - target_link_libraries(${example_name}-cpp binsparse-rc) + target_link_libraries(${example_name}-cpp binsparse) endfunction() add_example(simple_matrix_write) diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index f8ebad3..613fc3c 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -2,4 +2,4 @@ # # SPDX-License-Identifier: BSD-3-Clause -target_include_directories(binsparse-rc PUBLIC .) +# No need for target_include_directories here - moved to main CMakeLists.txt diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 50ffb26..5e1f106 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: BSD-3-Clause -target_sources(binsparse-rc PRIVATE - src/read_matrix.c - src/write_matrix.c +target_sources(binsparse PRIVATE + read_matrix.c + write_matrix.c )