Skip to content

Commit

Permalink
Merge pull request #8 from UCBoulder/feat/add-header-only-compile-option
Browse files Browse the repository at this point in the history
Feat/add header only compile option
  • Loading branch information
NateAM authored Jan 7, 2025
2 parents 9f4a559 + 59f1ae0 commit 81a10bd
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/github-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: MiniConda setup
uses: conda-incubator/setup-miniconda@v2
with:
miniforge-variant: Mambaforge
miniforge-variant: Miniforge3
channels: conda-forge,defaults
channel-priority: true
auto-activate-base: false
Expand Down
26 changes: 26 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ set(CMAKE_SRC_PATH "src/cmake")
# Add a cached variable to build the python interface
set(TARDIGRADE_ERROR_TOOLS_BUILD_PYTHON_BINDINGS ON CACHE BOOL "Boolean flag for whether the python bindings should be built")

set(TARDIGRADE_HEADER_ONLY "Set this to true to build the library as a header-only project" CACHE BOOL False)

if(${TARDIGRADE_HEADER_ONLY})
add_compile_definitions(TARDIGRADE_HEADER_ONLY)
message("BUILDING IN HEADER ONLY MODE")
endif()

# Add the cmake folder to locate project CMake module(s)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/${CMAKE_SRC_PATH}" ${CMAKE_MODULE_PATH})

Expand All @@ -20,6 +27,25 @@ if(cmake_build_type_lower STREQUAL "conda-test")
set(not_conda_test "false")
endif()

if(NOT ${TARDIGRADE_HEADER_ONLY})
set(project_link_string ${PROJECT_NAME})
endif()
if(cmake_build_type_lower STREQUAL "release")
set(upstream_required "REQUIRED")
elseif(cmake_build_type_lower STREQUAL "conda-test")
set(upstream_required "REQUIRED")
set(not_conda_test "false")
if (NOT ${TARDIGRADE_HEADER_ONLY})
# Find the installed project library
find_file(installed_linked_library
"lib${PROJECT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}"
PATHS "$ENV{CONDA_PREFIX}/${CMAKE_INSTALL_LIBDIR}"
NO_CACHE
REQUIRED)
set(project_link_string ${installed_linked_library})
endif()
endif()

# Get version number from Git
set(VERSION_UPDATE_FROM_GIT True)
if(${not_conda_test} STREQUAL "true")
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Changelog
0.8.1 (unreleased)
******************

New Features
============
- Added header only build (:pull:`8`). By `Nathan Miller`_.

Bug Fixes
=========
- Removed whitespace in add_library call in CMakeLists file (:pull:`6`). By `Nathan Miller`_.
Expand Down
39 changes: 32 additions & 7 deletions src/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
add_library(${PROJECT_NAME} "${PROJECT_NAME}.cpp" "${PROJECT_NAME}.h")
set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER ${PROJECT_NAME}.h)
target_compile_options(${PROJECT_NAME} PUBLIC)
install(TARGETS ${PROJECT_NAME}
if(${TARDIGRADE_HEADER_ONLY})

add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/${CPP_SRC_PATH}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}_Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${PROJECT_NAME}.h ${PROJECT_NAME}.cpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

foreach(package ${ADDITIONAL_HEADER_ONLY_LIBRARIES})
install(TARGETS ${package}
EXPORT ${package}_Targets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${package}.h ${package}.cpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endforeach(package)

else()
add_library(${PROJECT_NAME} "${PROJECT_NAME}.cpp" "${PROJECT_NAME}.h")
set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER ${PROJECT_NAME}.h)
target_compile_options(${PROJECT_NAME} PUBLIC)
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}_Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()
46 changes: 27 additions & 19 deletions src/cpp/tardigrade_error_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,51 @@
namespace tardigradeErrorTools{

void replaceAll( std::string& str, const std::string& from, const std::string& to ) {
/*!
* Replace all instances of 'from' and replace them with 'to' in the string 'str'
*
* \param &str: The string to be modified
* \param &from: The sub-string to be replaced
* \param &to: The replacement string
*/

size_t start_pos = 0;
while ( ( start_pos = str.find( from, start_pos ) ) != std::string::npos ) {
str.replace( start_pos, from.length( ), to );
start_pos += to.length( ); // ...
}
}

/**
* Add another layer to the errors
*
* Will be depreciated because it encourages unsafe coding practices
*
* \param Node &newNode: The new node to be added
*/
void Node::addNext( Node *newNode ){
/*!
* Add another layer to the errors
*
* Will be depreciated because it encourages unsafe coding practices
*
* \param &newNode: The new node to be added
*/
this->next.reset( newNode );
return;
}

/**
* Add another layer to the errors
*
* \param Node &newNode: The new node to be added
*/
void Node::addNext( std::unique_ptr< Node > &newNode ){
/*!
* Add another layer to the errors
*
* \param &newNode: The new node to be added
*/
this->next = std::move( newNode );
return;
}

/**
* Print the errors in a list of nodes.
*
* \param header const bool Flag which indicates if the header
* should be printed
*
*/
void Node::print( const bool header ){
/*!
* Print the errors in a list of nodes.
*
* \param header: Flag which indicates if the header
* should be printed
*
*/
if ( header ){
std::cerr << "\n***************\n";
std::cerr << "* ERROR *\n";
Expand Down
3 changes: 3 additions & 0 deletions src/cpp/tardigrade_error_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,8 @@ namespace tardigradeErrorTools{

}

#ifdef TARDIGRADE_HEADER_ONLY
#include "tardigrade_error_tools.cpp"
#endif

#endif

0 comments on commit 81a10bd

Please sign in to comment.