Skip to content

Commit acd218c

Browse files
Copilotjohnpatek
andauthored
Add CMake function for hyperpack archives (#27)
* Initial plan * Implement hyperpage_add_archive CMake function Co-authored-by: johnpatek <[email protected]> * Add usage example and clean up test artifacts Co-authored-by: johnpatek <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: johnpatek <[email protected]>
1 parent 50ae22e commit acd218c

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

CMakeLists.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,68 @@ add_executable(hyperpack ${CMAKE_CURRENT_SOURCE_DIR}/hyperpack.cpp)
7373
target_include_directories(hyperpack PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
7474
target_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+
76138
if(HYPERPAGE_TESTS)
77139
include(CTest)
78140
enable_testing()

example/CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,21 @@ add_custom_command(
4040
COMMENT "Building React frontend"
4141
)
4242

43+
# Traditional approach using custom commands
4344
add_custom_command(
4445
TARGET server POST_BUILD
4546
COMMAND $<TARGET_FILE:hyperpack> -o $<TARGET_FILE_DIR:server>/hyperpage.db ${CMAKE_CURRENT_SOURCE_DIR}/react-app/dist
4647
COMMENT "Building hyperpack archive from React app dist folder"
4748
)
4849

49-
add_dependencies(server hyperpack)
50+
add_dependencies(server hyperpack)
51+
52+
# Alternative approach using the new hyperpage_add_archive function:
53+
# This would replace the above custom command and dependency:
54+
#
55+
# hyperpage_add_archive(react_content "${CMAKE_CURRENT_SOURCE_DIR}/react-app/dist")
56+
# add_dependencies(server react_content)
57+
#
58+
# Then in the C++ code, you could access the archive at:
59+
# get_target_property(ARCHIVE_FILE react_content HYPERPAGE_ARCHIVE_FILE)
60+
# Or simply use: ${CMAKE_CURRENT_BINARY_DIR}/react_content.db

0 commit comments

Comments
 (0)