Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

scripts
venv
build/
build-*/
compile_flags.txt
._*
Expand Down
65 changes: 52 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
# SPDX-License-Identifier: BSD-3-Clause

cmake_minimum_required(VERSION 3.20)
project(binsparse VERSION 1.0.0)

set ( BINSPARSE_DATE "FIXME, 2026" )
set ( BINSPARSE_VERSION_MAJOR 1 )
set ( BINSPARSE_VERSION_MINOR 0 )
set ( BINSPARSE_VERSION_PATCH 0 )

project(binsparse)

set ( PROJECT_VERSION
"${BINSPARSE_VERSION_MAJOR}.${BINSPARSE_VERSION_MINOR}.${BINSPARSE_VERSION_PATCH}")

cmake_policy(SET CMP0079 NEW)

Expand All @@ -12,16 +21,45 @@ set(CMAKE_CXX_STANDARD 20)

# FIXME: -march=native is not portable
# set(CMAKE_C_FLAGS "-O3 -march=native")
set(CMAKE_C_FLAGS "-g ")
set(CMAKE_CXX_FLAGS "-g ")
# set(CMAKE_C_FLAGS "-g ")
# set(CMAKE_CXX_FLAGS "-g ")

if ( NOT CMAKE_BUILD_TYPE )
set ( CMAKE_BUILD_TYPE Release )
endif ( )

if ( ${CMAKE_BUILD_TYPE} STREQUAL "Debug" )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_DEBUG}" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG}" )
else ( )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_RELEASE}" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}" )
endif ( )
message ( STATUS "Build type: ${CMAKE_BUILD_TYPE} ")
message ( STATUS "C flags: ${CMAKE_C_FLAGS}" )
message ( STATUS "C++ flags: ${CMAKE_CXX_FLAGS}" )

option(ENABLE_SANITIZERS "Enable Clang sanitizers" OFF)

include(GNUInstallDirs)

add_library(binsparse STATIC)
add_library(binsparse_static STATIC)
add_library(binsparse_dynamic SHARED)

set_target_properties ( binsparse_static PROPERTIES
OUTPUT_NAME binsparse
VERSION "${BINSPARSE_VERSION_MAJOR}.${BINSPARSE_VERSION_MINOR}.${BINSPARSE_VERSION_PATCH}"
SOVERSION ${BINSPARSE_VERSION_MAJOR}
WINDOWS_EXPORT_ALL_SYMBOLS ON
)

set_target_properties ( binsparse_dynamic PROPERTIES
OUTPUT_NAME binsparse
VERSION "${BINSPARSE_VERSION_MAJOR}.${BINSPARSE_VERSION_MINOR}.${BINSPARSE_VERSION_PATCH}"
SOVERSION ${BINSPARSE_VERSION_MAJOR}
WINDOWS_EXPORT_ALL_SYMBOLS ON
)

add_subdirectory(include)
add_subdirectory(src)

Expand All @@ -30,8 +68,9 @@ add_subdirectory(src)
# these to `PRIVATE` to use them only when building binsparse.

find_package(HDF5 REQUIRED COMPONENTS C)
target_link_libraries(binsparse PUBLIC ${HDF5_C_LIBRARIES})
target_link_libraries(binsparse_static PUBLIC ${HDF5_C_LIBRARIES})
target_link_libraries(binsparse_dynamic PUBLIC ${HDF5_C_LIBRARIES})
message ( STATUS "HD5F include: ${HDF5_INCLUDE_DIRS}" )

include(FetchContent)

Expand All @@ -52,11 +91,11 @@ FetchContent_MakeAvailable(cJSON)
set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_BACKUP})

configure_file(${cJSON_SOURCE_DIR}/cJSON.h ${CMAKE_BINARY_DIR}/include/cJSON/cJSON.h)
target_link_libraries(binsparse PRIVATE cjson)
target_link_libraries(binsparse_static PRIVATE cjson)
target_link_libraries(binsparse_dynamic PRIVATE cjson)

# Set up include directories properly for both build and install
target_include_directories(${PROJECT_NAME}
target_include_directories(binsparse_static
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Expand All @@ -73,8 +112,8 @@ target_include_directories(binsparse_dynamic
${HDF5_INCLUDE_DIRS})

# Installation rules - these are always needed when the library is built
install(TARGETS binsparse
EXPORT binsparse-targets
install(TARGETS binsparse_static
EXPORT binsparse-targets_static
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
Expand All @@ -93,8 +132,8 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/binsparse
PATTERN "*.hpp")

# Export targets
install(EXPORT binsparse-targets
FILE binsparse-targets.cmake
install(EXPORT binsparse-targets_static
FILE binsparse-targets_static.cmake
NAMESPACE binsparse::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/binsparse)
install(EXPORT binsparse-targets_dynamic
Expand Down Expand Up @@ -124,8 +163,8 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)

if (ENABLE_SANITIZERS)
set(SANITIZER_FLAGS "-fsanitize=address,undefined")
target_compile_options(binsparse INTERFACE ${SANITIZER_FLAGS} -g -O1 -fno-omit-frame-pointer)
target_link_options(binsparse INTERFACE ${SANITIZER_FLAGS})
target_compile_options(binsparse_static INTERFACE ${SANITIZER_FLAGS} -g -O1 -fno-omit-frame-pointer)
target_link_options(binsparse_static INTERFACE ${SANITIZER_FLAGS})
target_compile_options(binsparse_dynamic INTERFACE ${SANITIZER_FLAGS} -g -O1 -fno-omit-frame-pointer)
target_link_options(binsparse_dynamic INTERFACE ${SANITIZER_FLAGS})
endif()
Expand Down
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#-------------------------------------------------------------------------------
# binsparse-reference-c/Makefile
#-------------------------------------------------------------------------------

# SPDX-FileCopyrightText: 2024 Binsparse Developers
# SPDX-License-Identifier: BSD-3-Clause

default: library

library:
( cd build && cmake $(CMAKE_OPTIONS) .. && cmake --build . --config Release )

debug:
( cd build && cmake $(CMAKE_OPTIONS) -DCMAKE_BUILD_TYPE=Debug .. ; cmake --build . --config Debug )

all: library

tests:
( cd build ; make test )

install:
( cd build && cmake --install . )

# remove any installed libraries and #include files
uninstall:
- xargs rm < build/install_manifest.txt

# remove all files not in the distribution
clean: distclean

purge: distclean

distclean:
- $(RM) -rf build/* Config/*.tmp

docs:

72 changes: 17 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,58 +50,20 @@ all dependencies except for:
HDF5 should be automatically detected, provided an installation is present on
the system.

```bash
bbrock@rjohns3-mobl2:~/src/binsparse-reference-c$ cmake -B build
-- The C compiler identification is AppleClang 15.0.0.15000309
-- The CXX compiler identification is AppleClang 15.0.0.15000309
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found HDF5: /opt/homebrew/Cellar/hdf5/1.14.3/lib/libhdf5.dylib;/opt/homebrew/opt/libaec/lib/libsz.dylib;/Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk/usr/lib/libz.tbd;/Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk/usr/lib/libdl.tbd;/Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk/usr/lib/libm.tbd (found version "1.14.3") found components: C
CMake Deprecation Warning at build/_deps/cjson-src/CMakeLists.txt:2 (cmake_minimum_required):
Compatibility with CMake < 3.5 will be removed from a future version of
CMake.

Update the VERSION argument <min> value or use a ...<max> suffix to tell
CMake that the project does not need compatibility with older versions.


-- Performing Test FLAG_SUPPORTED_fvisibilityhidden
-- Performing Test FLAG_SUPPORTED_fvisibilityhidden - Success
-- Configuring done (6.4s)
-- Generating done (0.2s)
-- Build files have been written to: /Users/bbrock/src/binsparse-reference-c/build
bbrock@rjohns3-mobl2:~/src/binsparse-reference-c$ cd build/examples/
bbrock@rjohns3-mobl2:~/src/binsparse-reference-c/build/examples$ make
[ 8%] Building C object _deps/cjson-build/CMakeFiles/cjson.dir/cJSON.c.o
[ 16%] Linking C shared library libcjson.dylib
ld: warning: search path '/opt/homebrew/Cellar/fmt/10.1.1/lib' not found
[ 16%] Built target cjson
[ 25%] Building C object examples/CMakeFiles/simple_matrix_write.dir/simple_matrix_write.c.o
[ 33%] Linking C executable simple_matrix_write
ld: warning: search path '/opt/homebrew/Cellar/fmt/10.1.1/lib' not found
[ 33%] Built target simple_matrix_write
[ 41%] Building C object examples/CMakeFiles/simple_matrix_read.dir/simple_matrix_read.c.o
[ 50%] Linking C executable simple_matrix_read
ld: warning: search path '/opt/homebrew/Cellar/fmt/10.1.1/lib' not found
[ 50%] Built target simple_matrix_read
[ 58%] Building C object examples/CMakeFiles/simple_read.dir/simple_read.c.o
[ 66%] Linking C executable simple_read
ld: warning: search path '/opt/homebrew/Cellar/fmt/10.1.1/lib' not found
[ 66%] Built target simple_read
[ 75%] Building C object examples/CMakeFiles/simple_write.dir/simple_write.c.o
[ 83%] Linking C executable simple_write
ld: warning: search path '/opt/homebrew/Cellar/fmt/10.1.1/lib' not found
[ 83%] Built target simple_write
[ 91%] Building C object examples/CMakeFiles/convert_binsparse.dir/convert_binsparse.c.o
[100%] Linking C executable convert_binsparse
ld: warning: search path '/opt/homebrew/Cellar/fmt/10.1.1/lib' not found
[100%] Built target convert_binsparse
```
A simple top-level Makefile can be used to compile, test, and install
binsparse:

make
make tests
sudu make install

To remove all compiled files and libraries:

make distclean

FIXME: the above will segfault because assert(...) is removed for Release.
For now use:

make debug
make tests

4 changes: 4 additions & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore all files except this file.
*
*/
!.gitignore
4 changes: 2 additions & 2 deletions cmake/binsparse-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/binsparse-targets.cmake")
check_required_components(binsparse)
include("${CMAKE_CURRENT_LIST_DIR}/binsparse-targets_static.cmake")
check_required_components(binsparse_static)

# Include dependencies
include(CMakeFindDependencyMacro)
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

function(add_example example_name)
add_executable(${example_name} ${example_name}.c)
target_link_libraries(${example_name} binsparse)
target_link_libraries(${example_name} binsparse_static)
endfunction()

add_example(simple_matrix_write)
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

function(add_example example_name)
add_executable(${example_name}-cpp ${example_name}.cpp)
target_link_libraries(${example_name}-cpp binsparse)
target_link_libraries(${example_name}-cpp binsparse_static)
endfunction()

add_example(simple_matrix_write)
Expand Down
10 changes: 7 additions & 3 deletions include/binsparse/convert_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* SPDX-License-Identifier: BSD-3-Clause
*/


// FIXME: cannot use assert in production if it has any side effects when deleted
// with -NDEBUG

#pragma once

#include <assert.h>
Expand All @@ -30,7 +34,7 @@ bsp_convert_matrix_allocator(bsp_matrix_t matrix, bsp_matrix_format_t format,
bsp_allocator_t allocator) {
// Throw an error if matrix already in desired format.
if (matrix.format == format) {
assert(false);
assert(false); // FIXME: this does not throw an error when binsparse compiled for prodction
}

if (format == BSP_COOR) {
Expand Down Expand Up @@ -275,7 +279,7 @@ bsp_convert_matrix_allocator(bsp_matrix_t matrix, bsp_matrix_format_t format,
free(indices);
return result;
} else {
assert(false);
assert(false); // FIXME remove assert(...)
}
} else {
// Convert to any another format.
Expand Down Expand Up @@ -493,7 +497,7 @@ bsp_convert_matrix_allocator(bsp_matrix_t matrix, bsp_matrix_format_t format,
free(indices);
return result;
} else {
assert(false);
assert(false); // FIXME remove assert(...) in production
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/binsparse/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static inline size_t bsp_type_size(bsp_type_t type) {
} else if (type == BSP_COMPLEX_FLOAT64) {
return sizeof(double _Complex);
} else {
assert(false);
assert(false); // FIXME: assert(false) cannot appear in a production library
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: BSD-3-Clause

target_sources(binsparse PRIVATE
target_sources(binsparse_static PRIVATE
read_matrix.c
read_tensor.c
write_matrix.c
Expand Down
2 changes: 1 addition & 1 deletion test/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: BSD-3-Clause

add_executable(dvec_shape_test dvec_shape_test.c)
target_link_libraries(dvec_shape_test binsparse)
target_link_libraries(dvec_shape_test binsparse_static)

add_test(
NAME unit.dvec_shape
Expand Down
5 changes: 5 additions & 0 deletions test/c/dvec_shape_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

// FIXME asserts are empty if the library is compiled for Release.
// use a different macro.

// FIXME this test segfaults if binsparse is compiled with Release flags (-O3)

#include <assert.h>
#include <binsparse/binsparse_all.h>
#include <cJSON/cJSON.h>
Expand Down