@@ -73,6 +73,68 @@ add_executable(hyperpack ${CMAKE_CURRENT_SOURCE_DIR}/hyperpack.cpp)
7373target_include_directories (hyperpack PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} )
7474target_link_libraries (hyperpack PRIVATE argparse hyperpage mio::mio)
7575
76+ # CMake function for creating hyperpack archives
77+ #
78+ # Usage: hyperpage_add_archive(<name> <directory>)
79+ #
80+ # This function creates a CMake target that will run hyperpack to create
81+ # an archive database from the specified directory during the build process.
82+ #
83+ # Parameters:
84+ # name - Name of the target and output database file (without .db extension)
85+ # directory - Directory to pack into the hyperpage database
86+ #
87+ # The function will:
88+ # - Create a custom target named <name>
89+ # - Generate <name>.db in CMAKE_CURRENT_BINARY_DIR
90+ # - Add dependency on the hyperpack executable
91+ # - Set HYPERPAGE_ARCHIVE_FILE property on the target for the output file path
92+ #
93+ # Example:
94+ # hyperpage_add_archive(my_content "${CMAKE_CURRENT_SOURCE_DIR}/web_assets")
95+ # add_dependencies(my_server my_content) # Ensure archive is built before server
96+ #
97+ function (hyperpage_add_archive name directory )
98+ # Validate arguments
99+ if (NOT name )
100+ message (FATAL_ERROR "hyperpage_add_archive: Archive name must be specified" )
101+ endif ()
102+
103+ if (NOT directory )
104+ message (FATAL_ERROR "hyperpage_add_archive: Directory must be specified" )
105+ endif ()
106+
107+ # Convert to absolute path for better reliability
108+ get_filename_component (abs_directory "${directory} " ABSOLUTE )
109+
110+ # Create the output database file name
111+ set (output_file "${CMAKE_CURRENT_BINARY_DIR} /${name} .db" )
112+
113+ # Create a custom target for this archive
114+ add_custom_target (${name}
115+ DEPENDS ${output_file}
116+ COMMENT "Building hyperpack archive: ${name} "
117+ )
118+
119+ # Add dependency on hyperpack executable
120+ add_dependencies (${name} hyperpack)
121+
122+ # Create custom command to generate the archive
123+ add_custom_command (
124+ OUTPUT ${output_file}
125+ COMMAND $<TARGET_FILE:hyperpack> -o ${output_file} ${abs_directory}
126+ DEPENDS hyperpack
127+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
128+ COMMENT "Creating hyperpack archive ${name} from directory: ${abs_directory} "
129+ VERBATIM
130+ )
131+
132+ # Set a property so consumers can find the generated file
133+ set_target_properties (${name} PROPERTIES
134+ HYPERPAGE_ARCHIVE_FILE "${output_file} "
135+ )
136+ endfunction ()
137+
76138if (HYPERPAGE_TESTS)
77139 include (CTest)
78140 enable_testing ()
0 commit comments