Skip to content

Commit 00eb7cc

Browse files
authored
Update build system (#22)
1 parent 42d5074 commit 00eb7cc

File tree

7 files changed

+78
-19
lines changed

7 files changed

+78
-19
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
scripts
66
venv
7-
build
7+
build*
88
._*

CMakeLists.txt

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,88 @@
22
#
33
# SPDX-License-Identifier: BSD-3-Clause
44

5-
cmake_minimum_required(VERSION 3.5)
6-
project(binsparse-rc)
5+
cmake_minimum_required(VERSION 3.20)
6+
project(binsparse VERSION 1.0.0)
77

88
cmake_policy(SET CMP0079 NEW)
99

1010
set(CMAKE_C_STANDARD 11)
11-
1211
set(CMAKE_CXX_STANDARD 20)
13-
1412
set(CMAKE_C_FLAGS "-O3 -march=native")
1513

16-
add_library(binsparse-rc STATIC)
14+
include(GNUInstallDirs)
15+
16+
add_library(binsparse STATIC)
1717

1818
add_subdirectory(include)
1919
add_subdirectory(src)
2020

2121
# NOTE: For now, both HDF5 and cJSON are `PUBLIC`, meaning that anything that
22-
# depends on `binsparse-rc` will also link/include HDF5 and cJSON. We can change
23-
# these to `PRIVATE` to use them only when building binsparse-rc.
22+
# depends on `binsparse` will also link/include HDF5 and cJSON. We can change
23+
# these to `PRIVATE` to use them only when building binsparse.
2424

2525
find_package(HDF5 REQUIRED COMPONENTS C)
26-
target_link_libraries(binsparse-rc PUBLIC ${HDF5_C_LIBRARIES})
27-
target_include_directories(binsparse-rc PUBLIC . ${HDF5_INCLUDE_DIRS})
26+
target_link_libraries(binsparse PUBLIC ${HDF5_C_LIBRARIES})
2827

2928
include(FetchContent)
3029
FetchContent_Declare(
3130
cJSON
32-
GIT_REPOSITORY https://github.com/DaveGamble/cJSON.git
33-
GIT_TAG v1.7.17
31+
# GIT_REPOSITORY https://github.com/DaveGamble/cJSON.git
32+
GIT_REPOSITORY https://github.com/p1k0chu/cJSON.git
33+
GIT_TAG 887642c0a93bd8a6616bf90daacac0ea7d4b095e
3434
)
3535
FetchContent_MakeAvailable(cJSON)
3636

3737
configure_file(${cJSON_SOURCE_DIR}/cJSON.h ${CMAKE_BINARY_DIR}/include/cJSON/cJSON.h)
38-
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/include)
3938
target_link_libraries(${PROJECT_NAME} PUBLIC cjson)
4039

40+
# Set up include directories properly for both build and install
41+
target_include_directories(${PROJECT_NAME}
42+
PUBLIC
43+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
44+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
45+
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
46+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
47+
${HDF5_INCLUDE_DIRS})
48+
4149
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
4250
add_subdirectory(examples)
4351
add_subdirectory(test)
4452
endif()
53+
54+
# Installation rules - these are always needed when the library is built
55+
install(TARGETS binsparse
56+
EXPORT binsparse-targets
57+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
58+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
59+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
60+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
61+
62+
# Install headers
63+
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/binsparse
64+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
65+
FILES_MATCHING PATTERN "*.h"
66+
PATTERN "*.hpp")
67+
68+
# Export targets
69+
install(EXPORT binsparse-targets
70+
FILE binsparse-targets.cmake
71+
NAMESPACE binsparse::
72+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse)
73+
74+
# Create and install package config files
75+
include(CMakePackageConfigHelpers)
76+
write_basic_package_version_file(
77+
"${CMAKE_CURRENT_BINARY_DIR}/binsparse-config-version.cmake"
78+
VERSION ${PROJECT_VERSION}
79+
COMPATIBILITY SameMajorVersion)
80+
81+
configure_package_config_file(
82+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/binsparse-config.cmake.in"
83+
"${CMAKE_CURRENT_BINARY_DIR}/binsparse-config.cmake"
84+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse)
85+
86+
install(FILES
87+
"${CMAKE_CURRENT_BINARY_DIR}/binsparse-config.cmake"
88+
"${CMAKE_CURRENT_BINARY_DIR}/binsparse-config-version.cmake"
89+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse)

cmake/binsparse-config.cmake.in

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: 2024 Binsparse Developers
2+
#
3+
# SPDX-License-Identifier: BSD-3-Clause
4+
5+
@PACKAGE_INIT@
6+
7+
include("${CMAKE_CURRENT_LIST_DIR}/binsparse-targets.cmake")
8+
check_required_components(binsparse)
9+
10+
# Include dependencies
11+
include(CMakeFindDependencyMacro)
12+
find_dependency(HDF5 COMPONENTS C)
13+
14+
# cJSON is bundled with binsparse, no need to find it externally

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
function(add_example example_name)
66
add_executable(${example_name} ${example_name}.c)
7-
target_link_libraries(${example_name} binsparse-rc)
7+
target_link_libraries(${example_name} binsparse)
88
endfunction()
99

1010
add_example(simple_matrix_write)

examples/cpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
function(add_example example_name)
66
add_executable(${example_name}-cpp ${example_name}.cpp)
7-
target_link_libraries(${example_name}-cpp binsparse-rc)
7+
target_link_libraries(${example_name}-cpp binsparse)
88
endfunction()
99

1010
add_example(simple_matrix_write)

include/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#
33
# SPDX-License-Identifier: BSD-3-Clause
44

5-
target_include_directories(binsparse-rc PUBLIC .)
5+
# No need for target_include_directories here - moved to main CMakeLists.txt

src/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: BSD-3-Clause
44

5-
target_sources(binsparse-rc PRIVATE
6-
src/read_matrix.c
7-
src/write_matrix.c
5+
target_sources(binsparse PRIVATE
6+
read_matrix.c
7+
write_matrix.c
88
)

0 commit comments

Comments
 (0)