diff --git a/CMakeLists.txt b/CMakeLists.txt index 6066c32..4e140e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,68 @@ add_executable(hyperpack ${CMAKE_CURRENT_SOURCE_DIR}/hyperpack.cpp) target_include_directories(hyperpack PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(hyperpack PRIVATE argparse hyperpage mio::mio) +# CMake function for creating hyperpack archives +# +# Usage: hyperpage_add_archive( ) +# +# This function creates a CMake target that will run hyperpack to create +# an archive database from the specified directory during the build process. +# +# Parameters: +# name - Name of the target and output database file (without .db extension) +# directory - Directory to pack into the hyperpage database +# +# The function will: +# - Create a custom target named +# - Generate .db in CMAKE_CURRENT_BINARY_DIR +# - Add dependency on the hyperpack executable +# - Set HYPERPAGE_ARCHIVE_FILE property on the target for the output file path +# +# Example: +# hyperpage_add_archive(my_content "${CMAKE_CURRENT_SOURCE_DIR}/web_assets") +# add_dependencies(my_server my_content) # Ensure archive is built before server +# +function(hyperpage_add_archive name directory) + # Validate arguments + if(NOT name) + message(FATAL_ERROR "hyperpage_add_archive: Archive name must be specified") + endif() + + if(NOT directory) + message(FATAL_ERROR "hyperpage_add_archive: Directory must be specified") + endif() + + # Convert to absolute path for better reliability + get_filename_component(abs_directory "${directory}" ABSOLUTE) + + # Create the output database file name + set(output_file "${CMAKE_CURRENT_BINARY_DIR}/${name}.db") + + # Create a custom target for this archive + add_custom_target(${name} + DEPENDS ${output_file} + COMMENT "Building hyperpack archive: ${name}" + ) + + # Add dependency on hyperpack executable + add_dependencies(${name} hyperpack) + + # Create custom command to generate the archive + add_custom_command( + OUTPUT ${output_file} + COMMAND $ -o ${output_file} ${abs_directory} + DEPENDS hyperpack + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Creating hyperpack archive ${name} from directory: ${abs_directory}" + VERBATIM + ) + + # Set a property so consumers can find the generated file + set_target_properties(${name} PROPERTIES + HYPERPAGE_ARCHIVE_FILE "${output_file}" + ) +endfunction() + if(HYPERPAGE_TESTS) include(CTest) enable_testing() diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index f2dc318..72782be 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -40,10 +40,21 @@ add_custom_command( COMMENT "Building React frontend" ) +# Traditional approach using custom commands add_custom_command( TARGET server POST_BUILD COMMAND $ -o $/hyperpage.db ${CMAKE_CURRENT_SOURCE_DIR}/react-app/dist COMMENT "Building hyperpack archive from React app dist folder" ) -add_dependencies(server hyperpack) \ No newline at end of file +add_dependencies(server hyperpack) + +# Alternative approach using the new hyperpage_add_archive function: +# This would replace the above custom command and dependency: +# +# hyperpage_add_archive(react_content "${CMAKE_CURRENT_SOURCE_DIR}/react-app/dist") +# add_dependencies(server react_content) +# +# Then in the C++ code, you could access the archive at: +# get_target_property(ARCHIVE_FILE react_content HYPERPAGE_ARCHIVE_FILE) +# Or simply use: ${CMAKE_CURRENT_BINARY_DIR}/react_content.db \ No newline at end of file