Skip to content

Commit

Permalink
Add version status and args handling
Browse files Browse the repository at this point in the history
  • Loading branch information
smokku committed Oct 14, 2024
1 parent b855133 commit c155d26
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ include_directories(external/sokol)
add_executable(emu
src/main.c
src/cli.c
${CMAKE_CURRENT_BINARY_DIR}/version.c
)
target_include_directories(emu PRIVATE ${READLINE_INCLUDE_DIRS})
target_link_libraries(emu
PRIVATE ${READLINE_LIBRARIES}
X11 Xi Xcursor EGL GLESv2 dl pthread m)
target_compile_options(emu PRIVATE ${READLINE_CFLAGS_OTHER})

# Add a custom command that produces version.c, plus
# a dummy output that's not actually produced, in order
# to force version.cmake to always be re-run before the build
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/version.c
${CMAKE_CURRENT_BINARY_DIR}/_version.c
COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_SOURCE_DIR}/version.cmake)
30 changes: 24 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
#include <unistd.h>

#define SOKOL_IMPL
#define SOKOL_GLES3
#define SOKOL_UNREACHABLE __builtin_unreachable()
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_glue.h"
#include "sokol_log.h"
#include "sokol_args.h"

#include "icon.c"
#include "cli.h"

extern const char* GIT_TAG;
extern const char* GIT_REV;
extern const char* GIT_BRANCH;
const char* APP_NAME = "X65 emu";

sg_pass_action pass_action;

void init(void) {
static void init(void) {
sg_setup(&(sg_desc){
.environment = sglue_environment(),
.logger.func = slog_func,
Expand All @@ -23,7 +31,7 @@ void init(void) {
cli_init();
}

void frame(void) {
static void frame(void) {
float g = pass_action.colors[0].clear_value.g + 0.01f;
pass_action.colors[0].clear_value.g = (g > 1.0f) ? 0.0f : g;
sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = sglue_swapchain() });
Expand All @@ -33,22 +41,32 @@ void frame(void) {
cli_update();
}

void cleanup(void) {
static void cleanup(void) {
sg_shutdown();
sargs_shutdown();

cli_cleanup();
}

sapp_desc sokol_main(int argc, char* argv[]) {
(void)argc;
(void)argv;
if (strlen(GIT_TAG))
printf("%s %s\n", APP_NAME, GIT_TAG);
else
printf("%s %s@%s\n", APP_NAME, GIT_REV, GIT_BRANCH);

sargs_setup(&(sargs_desc){
.argc = argc,
.argv = argv,
.buf_size = (int)sysconf(_SC_ARG_MAX),
});

return (sapp_desc){
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.width = 400,
.height = 300,
.window_title = "Clear (sokol app)",
.window_title = APP_NAME,
.icon.images = { { .width = app_icon.width,
.height = app_icon.height,
.pixels = (sapp_range){ &app_icon.pixel_data, app_icon.width * app_icon.height * 4 } } },
Expand Down
45 changes: 45 additions & 0 deletions version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Based on https://www.mattkeeter.com/blog/2018-01-06-versioning/

execute_process(COMMAND git log --pretty=format:'%h' -n 1
OUTPUT_VARIABLE GIT_REV
ERROR_QUIET)

# Check whether we got any revision (which isn't
# always the case, e.g. when someone downloaded a zip
# file from Github instead of a checkout)
if ("${GIT_REV}" STREQUAL "")
set(GIT_REV "N/A")
set(GIT_DIFF "")
set(GIT_TAG "N/A")
set(GIT_BRANCH "N/A")
else()
execute_process(
COMMAND bash -c "git diff --quiet --exit-code || echo +"
OUTPUT_VARIABLE GIT_DIFF)
execute_process(
COMMAND git describe --exact-match --tags
OUTPUT_VARIABLE GIT_TAG ERROR_QUIET)
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
OUTPUT_VARIABLE GIT_BRANCH)

string(STRIP "${GIT_REV}" GIT_REV)
string(SUBSTRING "${GIT_REV}" 1 7 GIT_REV)
string(STRIP "${GIT_DIFF}" GIT_DIFF)
string(STRIP "${GIT_TAG}" GIT_TAG)
string(STRIP "${GIT_BRANCH}" GIT_BRANCH)
endif()

set(VERSION "const char* GIT_REV=\"${GIT_REV}${GIT_DIFF}\";
const char* GIT_TAG=\"${GIT_TAG}\";
const char* GIT_BRANCH=\"${GIT_BRANCH}\";")

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/version.c)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/version.c VERSION_)
else()
set(VERSION_ "")
endif()

if (NOT "${VERSION}" STREQUAL "${VERSION_}")
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/version.c "${VERSION}")
endif()

0 comments on commit c155d26

Please sign in to comment.