From 11f0a82b2397030c3badc92058dd2fe3d096be7a Mon Sep 17 00:00:00 2001 From: Tiago Garcia Date: Thu, 18 Jul 2024 01:43:42 +0100 Subject: [PATCH] Install/uninstall cmake rules Signed-off-by: Tiago Garcia --- CMakeLists.txt | 56 ++++++++++++++++++++++++++++------------ gh-wh-handler.service.in | 2 +- uninstall.cmake.in | 22 ++++++++++++++++ 3 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 uninstall.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index cd25836..eec890f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,22 @@ cmake_minimum_required(VERSION 3.20) # Project name and version project(gh-wh-handler VERSION 0.1.0) +# Detect architecture +if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64") + set(ARCH "x86_64") +elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64") + set(ARCH "aarch64") +elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "armv7l") + set(ARCH "armv7l") +elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i686") + set(ARCH "i686") + # Add more architectures as needed +else() + set(ARCH "unknown") +endif() + # Set the executable name -set(EXECUTABLE_NAME "gh-wh-handler") +set(EXECUTABLE_NAME "gh-wh-handler.${ARCH}") # Set the C++ standard set(CMAKE_CXX_STANDARD 23) @@ -28,20 +42,30 @@ target_include_directories(${EXECUTABLE_NAME} PUBLIC ${INCLUDE_DIR}) target_compile_options(${EXECUTABLE_NAME} PRIVATE -Wall -Werror) # Set linker flags for static linking -set_target_properties(${EXECUTABLE_NAME} PROPERTIES - LINK_FLAGS "-static -static-libgcc -static-libstdc++" -) +set_target_properties(${EXECUTABLE_NAME} PROPERTIES LINK_FLAGS + "-static -static-libgcc -static-libstdc++") -# Install the service -set(CMAKE_INSTALL_PREFIX "/usr/") -install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin) -set(SERVICE_EXECUTABLE "/services/gh-wh-handler/gh-wh-handler.aarch64") -set(SERVICE_CONFIG_FILE "/services/gh-wh-handler/config.json") -configure_file(${CMAKE_SOURCE_DIR}/gh-wh-handler.service.in ${CMAKE_BINARY_DIR}/gh-wh-handler.service @ONLY) -install(FILES ${CMAKE_BINARY_DIR}/gh-wh-handler.service DESTINATION /etc/systemd/system) -install(CODE " - execute_process(COMMAND systemctl daemon-reload) - execute_process(COMMAND systemctl enable gh-wh-handler.service) - execute_process(COMMAND systemctl start gh-wh-handler.service) -") +# Install the executable +set(SERVICE_EXECUTABLE "/services/gh-wh-handler/${EXECUTABLE_NAME}") +set(SERVICE_CONFIG "/services/gh-wh-handler/config.json") +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/gh-wh-handler.service.in" + "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/gh-wh-handler.service" +@ONLY) +install(CODE "file(MAKE_DIRECTORY /services/gh-wh-handler)") +install(TARGETS ${EXECUTABLE_NAME} DESTINATION /services/gh-wh-handler) +install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink + /services/gh-wh-handler/${EXECUTABLE_NAME} /usr/bin/gh-wh-handler)") +install(FILES "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/gh-wh-handler.service" + DESTINATION /etc/systemd/system) +install(CODE "execute_process(COMMAND systemctl daemon-reload)") +install(CODE "execute_process(COMMAND systemctl enable gh-wh-handler)") +install(CODE "execute_process(COMMAND systemctl start gh-wh-handler)") + +if(NOT TARGET uninstall) + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/uninstall.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake" IMMEDIATE @ONLY) + add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake) +endif() diff --git a/gh-wh-handler.service.in b/gh-wh-handler.service.in index 6e2e360..2726a50 100644 --- a/gh-wh-handler.service.in +++ b/gh-wh-handler.service.in @@ -3,7 +3,7 @@ Description=Runs github webhook handler After=network.target [Service] -ExecStart=@SERVICE_EXECUTABLE@ @SERVICE_CONFIG_FILE@ +ExecStart=@SERVICE_EXECUTABLE@ @SERVICE_CONFIG@ Restart=always Type=simple diff --git a/uninstall.cmake.in b/uninstall.cmake.in new file mode 100644 index 0000000..894c2a8 --- /dev/null +++ b/uninstall.cmake.in @@ -0,0 +1,22 @@ +message(STATUS "[0%] Stopping systemd service...") +execute_process(COMMAND systemctl stop gh-wh-handler) + +message(STATUS "[15%] Disabling systemd service...") +execute_process(COMMAND systemctl disable gh-wh-handler) + +message(STATUS "[30%] Removing service configuration file from systemd directory...") +file(REMOVE /etc/systemd/system/gh-wh-handler.service) + +message(STATUS "[45%] Reloading systemd daemon...") +execute_process(COMMAND systemctl daemon-reload) + +message(STATUS "[55%] Removing symlink to service executable...") +execute_process(COMMAND rm /usr/bin/gh-wh-handler) + +message(STATUS "[70%] Removing service executable from service directory...") +file(REMOVE /services/gh-wh-handler/@EXECUTABLE_NAME@) + +message(STATUS "[85%] Removing service directory...") +file(REMOVE /services/gh-wh-handler) + +message(STATUS "[100%] Uninstallation complete!")