Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add version applet #53

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ build
.vscode*
.DS_Store

# version
/src/version.h

# for package files
lpac-*.deb
lpac-*.zip
Expand Down
25 changes: 25 additions & 0 deletions cmake/git-version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# from https://github.com/nocnokneo/cmake-git-versioning-example

if(GIT_EXECUTABLE)
get_filename_component(SRC_DIR ${SRC} DIRECTORY)
# Generate a git-describe version string from Git repository tags
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --dirty --match "v*"
WORKING_DIRECTORY ${SRC_DIR}
OUTPUT_VARIABLE GIT_DESCRIBE_VERSION
RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT GIT_DESCRIBE_ERROR_CODE)
set(LPAC_VERSION ${GIT_DESCRIBE_VERSION})
endif()
endif()

# Final fallback: Just use a bogus version string that is semantically older
# than anything else and spit out a warning to the developer.
if(NOT DEFINED LPAC_VERSION)
set(LPAC_VERSION v0.0.0-unknown)
message(WARNING "Failed to determine LPAC_VERSION from Git tags. Using default version \"${LPAC_VERSION}\".")
endif()

configure_file(${SRC} ${DST} @ONLY)
10 changes: 10 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ set_target_properties(lpac PROPERTIES
target_link_libraries(lpac euicc ${DL_LIBRARY})
target_include_directories(lpac PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

find_package(Git)
add_custom_target(version
${CMAKE_COMMAND}
-D SRC=${CMAKE_CURRENT_SOURCE_DIR}/version.h.in
-D DST=${CMAKE_CURRENT_SOURCE_DIR}/version.h
-D GIT_EXECUTABLE=${GIT_EXECUTABLE}
-P ${CMAKE_MODULE_PATH}/git-version.cmake
)
add_dependencies(lpac version)

if(UNIX)
install(TARGETS lpac RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
endif()
14 changes: 14 additions & 0 deletions src/applet/version.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "version.h"

#include <main.h>

static int applet_main(int argc, char **argv)
{
jprint_success(cJSON_CreateString(LPAC_VERSION));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议用IFNDEF检查一下如果没有定义就输出unknown,不要依赖cmake

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

return 0;
}

struct applet_entry applet_version = {
.name = "version",
.main = applet_main,
};
6 changes: 6 additions & 0 deletions src/applet/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "../version.h"
#include <applet.h>

extern struct applet_entry applet_version;
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
#include "applet/chip.h"
#include "applet/profile.h"
#include "applet/notification.h"
#include "applet/version.h"

static const struct applet_entry *applets[] = {
&applet_dlsym_interface,
&applet_chip,
&applet_profile,
&applet_notification,
&applet_version,
NULL,
};

Expand Down
4 changes: 4 additions & 0 deletions src/version.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef LPAC_VERSION_H_
#define LPAC_VERSION_H_
#define LPAC_VERSION "@LPAC_VERSION@"
#endif /* LPAC_VERSION_H_ */