Skip to content

Commit

Permalink
feat(cmake): Recreate all relevant install/uninstall Makefile targets.
Browse files Browse the repository at this point in the history
install-conf and install-datadir were not retained since their only
function was to create directories, which cmake does on its own.

The uninstall recipe was also improved a bit:

* Support CMake components.

* Switch from exec_program, which is deprecated, to execute_process.

* Generate manifest file if it's missing. This makes it possible to
  run an uninstall target without have run its install target first.
  This is quite relevant if doing `make install && make
  uninstall-examples`, for instance.

Ticket: MEN-6185
Changelog: None

Signed-off-by: Kristian Amlie <[email protected]>
  • Loading branch information
Kristian Amlie committed Jan 19, 2023
1 parent c45b65c commit 09fc892
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ config.h
/_deps/
*.cmake
/Testing
/install_manifest.txt
/install_manifest*.txt
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ configure_file(
IMMEDIATE @ONLY)

add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
)

add_custom_target(install-bin
DEPENDS install-mender-auth install-mender-update
)
add_custom_target(uninstall-bin
DEPENDS uninstall-mender-auth uninstall-mender-update
)

include(GoogleTest)
set(MENDER_TEST_FLAGS EXTRA_ARGS --gtest_output=xml:${CMAKE_SOURCE_DIR}/reports/)
Expand Down
42 changes: 33 additions & 9 deletions cmake_uninstall.cmake.in
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
# TODO: doesn't remove directories created for installed files
if(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt")
message(FATAL_ERROR "Cannot find install manifest: @CMAKE_BINARY_DIR@/install_manifest.txt")
if(CMAKE_INSTALL_COMPONENT)
set(CMAKE_INSTALL_MANIFEST "install_manifest_${CMAKE_INSTALL_COMPONENT}.txt")
else()
set(CMAKE_INSTALL_MANIFEST "install_manifest.txt")
endif()

file(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files)
if(NOT EXISTS "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_MANIFEST}")
# It's a bit silly to install right before uninstalling, but it appears to be
# the only way to generate the manifest file that we need. We are going to
# delete the exact same files that we install, so there is no ill behavior
# here, just using a few more CPU cycles.
if(CMAKE_INSTALL_COMPONENT)
execute_process(
COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR} --component=${CMAKE_INSTALL_COMPONENT}
OUTPUT_VARIABLE install_output
ERROR_VARIABLE install_output
)
else()
execute_process(
COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR}
OUTPUT_VARIABLE install_output
ERROR_VARIABLE install_output
)
endif()
if(NOT EXISTS "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_MANIFEST}")
message(FATAL_ERROR "Cannot find install manifest, even after attempting to generate it: ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_MANIFEST}: ${install_output}")
endif()
endif()

# TODO: doesn't remove directories created for installed files
file(READ "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_MANIFEST}" files)
string(REGEX REPLACE "\n" ";" files "${files}")
foreach(file ${files})
message(STATUS "Uninstalling $ENV{DESTDIR}${file}")
exec_program(
"@CMAKE_COMMAND@" ARGS "-E rm -f \"$ENV{DESTDIR}${file}\""
OUTPUT_VARIABLE rm_out
RETURN_VALUE rm_retval
)
execute_process(
COMMAND ${CMAKE_COMMAND} -E rm -f "$ENV{DESTDIR}${file}"
RESULT_VARIABLE rm_retval
)
if(NOT "${rm_retval}" STREQUAL 0)
message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}")
endif()
Expand Down
12 changes: 11 additions & 1 deletion mender-auth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ add_executable(mender-auth main.cpp)
target_link_libraries(mender-auth PRIVATE common_json common_kv_db common_log)
target_link_libraries(mender-auth PUBLIC mender_compiler_flags)
target_include_directories(mender-auth PUBLIC ../)
install(TARGETS mender-auth DESTINATION bin)
install(TARGETS mender-auth
DESTINATION bin
COMPONENT mender-auth
)
add_custom_target(install-mender-auth
COMMAND ${CMAKE_COMMAND} --install . --component=mender-auth
)
add_custom_target(uninstall-mender-auth
COMMAND ${CMAKE_COMMAND} -D CMAKE_INSTALL_COMPONENT=mender-auth -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
12 changes: 11 additions & 1 deletion mender-update/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ add_executable(mender-update main.cpp)
target_link_libraries(mender-update PRIVATE common_json common_kv_db common_log)
target_link_libraries(mender-update PUBLIC mender_compiler_flags)
target_include_directories(mender-update PUBLIC ../)
install(TARGETS mender-update DESTINATION bin)
install(TARGETS mender-update
DESTINATION bin
COMPONENT mender-update
)
add_custom_target(install-mender-update
COMMAND ${CMAKE_COMMAND} --install . --component=mender-update
)
add_custom_target(uninstall-mender-update
COMMAND ${CMAKE_COMMAND} -D CMAKE_INSTALL_COMPONENT=mender-update -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
75 changes: 75 additions & 0 deletions support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,100 @@ set(SYSTEMD_UNITS

install(PROGRAMS ${INVENTORYSCRIPTS}
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/mender/inventory
COMPONENT inventory-scripts
)
add_custom_target(install-inventory-scripts
COMMAND ${CMAKE_COMMAND} --install . --component=inventory-scripts
)
add_custom_target(uninstall-inventory-scripts
COMMAND ${CMAKE_COMMAND} -D CMAKE_INSTALL_COMPONENT=inventory-scripts -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

install(PROGRAMS ${INVENTORY_NETWORKSCRIPTS}
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/mender/inventory
COMPONENT inventory-network-scripts
# Disabled by default to avoid API throttling.
EXCLUDE_FROM_ALL
)
add_custom_target(install-inventory-network-scripts
COMMAND ${CMAKE_COMMAND} --install . --component=inventory-network-scripts
)
add_custom_target(uninstall-inventory-network-scripts
COMMAND ${CMAKE_COMMAND} -D CMAKE_INSTALL_COMPONENT=inventory-network-scripts -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

install(PROGRAMS ${IDENTITYSCRIPTS}
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/mender/identity
COMPONENT identity-scripts
)
add_custom_target(install-identity-scripts
COMMAND ${CMAKE_COMMAND} --install . --component=identity-scripts
)
add_custom_target(uninstall-identity-scripts
COMMAND ${CMAKE_COMMAND} -D CMAKE_INSTALL_COMPONENT=identity-scripts -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

install(FILES ${DBUS_POLICY_FILES}
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/dbus-1/system.d
COMPONENT dbus
)
add_custom_target(install-dbus
COMMAND ${CMAKE_COMMAND} --install . --component=dbus
)
add_custom_target(uninstall-dbus
COMMAND ${CMAKE_COMMAND} -D CMAKE_INSTALL_COMPONENT=dbus -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

install(PROGRAMS ${MODULES}
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/mender/modules/v3
COMPONENT modules
)
add_custom_target(install-modules
COMMAND ${CMAKE_COMMAND} --install . --component=modules
)
add_custom_target(uninstall-modules
COMMAND ${CMAKE_COMMAND} -D CMAKE_INSTALL_COMPONENT=modules -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

install(PROGRAMS ${MODULES_ARTIFACT_GENERATORS}
DESTINATION bin
COMPONENT modules-gen
# Disabled by default since devices don't typically need them.
EXCLUDE_FROM_ALL
)
add_custom_target(install-modules-gen
COMMAND ${CMAKE_COMMAND} --install . --component=modules-gen
)
add_custom_target(uninstall-modules-gen
COMMAND ${CMAKE_COMMAND} -D CMAKE_INSTALL_COMPONENT=modules-gen -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

install(FILES ${SYSTEMD_UNITS}
DESTINATION ${SYSTEMD_UNIT_DIR}
COMPONENT systemd
)
add_custom_target(install-systemd
COMMAND ${CMAKE_COMMAND} --install . --component=systemd
)
add_custom_target(uninstall-systemd
COMMAND ${CMAKE_COMMAND} -D CMAKE_INSTALL_COMPONENT=systemd -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

install(FILES ${DOCS_EXAMPLES}
DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples
COMPONENT examples
)
add_custom_target(install-examples
COMMAND ${CMAKE_COMMAND} --install . --component=examples
)
add_custom_target(uninstall-examples
COMMAND ${CMAKE_COMMAND} -D CMAKE_INSTALL_COMPONENT=examples -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

0 comments on commit 09fc892

Please sign in to comment.