Skip to content

Commit 0e5a980

Browse files
committed
Add install/uninstall targets for cmake
1 parent 2ef25af commit 0e5a980

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/CMakeLists.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,60 @@ add_custom_target(shared_lib
132132
add_custom_target(lib
133133
DEPENDS static_lib shared_lib
134134
)
135+
136+
# Install the main executable
137+
install(TARGETS kanzi
138+
RUNTIME DESTINATION bin
139+
)
140+
141+
# Install the libraries (static and shared)
142+
install(TARGETS libkanzi libkanzi_shared
143+
ARCHIVE DESTINATION lib # For static libraries (.a, .lib)
144+
LIBRARY DESTINATION lib # For shared libraries (.so, .dylib, .dll)
145+
)
146+
147+
# Dynamically discover and install public headers
148+
# We assume all header files (.h and .hpp) in the project directories
149+
# (excluding test/ and app/ directories) are public headers.
150+
151+
# Define the base directories where headers might reside
152+
# PROJECT_SOURCE_DIR refers to the directory containing CMakeLists.txt
153+
set(HEADER_BASE_DIRS
154+
${CMAKE_CURRENT_SOURCE_DIR} # For headers like Global.h, Event.h at root
155+
${CMAKE_CURRENT_SOURCE_DIR}/api
156+
${CMAKE_CURRENT_SOURCE_DIR}/bitstream
157+
${CMAKE_CURRENT_SOURCE_DIR}/entropy
158+
${CMAKE_CURRENT_SOURCE_DIR}/io
159+
${CMAKE_CURRENT_SOURCE_DIR}/transform
160+
)
161+
162+
# List to hold all found header files
163+
set(ALL_HEADERS)
164+
165+
# Iterate through the base directories and find headers
166+
foreach(dir IN LISTS HEADER_BASE_DIRS)
167+
file(GLOB_RECURSE CURRENT_DIR_HEADERS "${dir}/*.h" "${dir}/*.hpp")
168+
list(APPEND ALL_HEADERS ${CURRENT_DIR_HEADERS})
169+
endforeach()
170+
171+
# Install each header, preserving its relative path within the 'kanzi' include directory
172+
foreach(header_file IN LISTS ALL_HEADERS)
173+
# Calculate the relative path from the project root
174+
file(RELATIVE_PATH REL_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${header_file})
175+
176+
# Install to include/kanzi/ followed by its relative path
177+
install(FILES "${header_file}"
178+
DESTINATION "include/kanzi/${REL_PATH}"
179+
)
180+
endforeach()
181+
182+
# Uninstall all files added during install
183+
configure_file(
184+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
185+
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
186+
@ONLY)
187+
188+
add_custom_target(uninstall
189+
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
190+
COMMENT "Uninstalling..."
191+
)

src/cmake_uninstall.cmake.in

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Used by smake to uninstall kanzi binary, libraries and header files
2+
if(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
3+
message(FATAL_ERROR "Cannot find install manifest")
4+
endif()
5+
6+
file(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files)
7+
string(REPLACE "\n" ";" files "${files}")
8+
9+
foreach(file ${files})
10+
if(EXISTS "${file}")
11+
message(STATUS "Removing file: ${file}")
12+
file(REMOVE "${file}")
13+
endif()
14+
endforeach()
15+
16+
set(install_dirs "")
17+
foreach(file ${files})
18+
get_filename_component(dir "${file}" DIRECTORY)
19+
list(APPEND install_dirs "${dir}")
20+
endforeach()
21+
22+
# Remove duplicates
23+
list(REMOVE_DUPLICATES install_dirs)
24+
25+
# Sort deepest directories first
26+
list(SORT install_dirs)
27+
list(REVERSE install_dirs)
28+
29+
foreach(dir ${install_dirs})
30+
if(IS_DIRECTORY "${dir}")
31+
file(GLOB children "${dir}/*")
32+
list(LENGTH children num_children)
33+
if(num_children EQUAL 0)
34+
message(STATUS "Removing empty directory: ${dir}")
35+
file(REMOVE_RECURSE "${dir}")
36+
endif()
37+
endif()
38+
endforeach()
39+

0 commit comments

Comments
 (0)