From e1633750a947f8a500e1571d3ceee34b8a86e24d Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Tue, 10 Jan 2012 13:14:11 +0200 Subject: [PATCH] Detect git revision and have version shown --- .gitattributes | 1 + .gitignore | 1 + Makefile | 11 +++- VERSION | 1 + buildaux/version.cpp | 85 +++++++++++++++++++++++++++++++ include/lsnes.hpp | 1 + src/core/misc.cpp | 1 - src/plat-sdl/main.cpp | 5 ++ src/plat-wxwidgets/mainwindow.cpp | 9 ++++ 9 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 .gitattributes create mode 100644 VERSION create mode 100644 buildaux/version.cpp diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..316441ee --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +buildaux/version.cpp export-subst diff --git a/.gitignore b/.gitignore index 31d07e0c..2981d859 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ docs rom /core src/fonts/font.cpp +src/core/version.cpp diff --git a/Makefile b/Makefile index d50f96a5..eefddb0e 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ FONT_SRC := unifontfull-5.1.20080820.hex #Compilers. CC := g++ REALCC = $(CROSS_PREFIX)$(CC) +HOSTCC = $(CC) #Flags. HOSTCCFLAGS = -std=gnu++0x @@ -23,7 +24,7 @@ JOYSTICK = SDL #Core objects and what to build. CORE_OBJECTS = $(patsubst %.cpp,%.$(OBJECT_SUFFIX),$(wildcard src/core/*.cpp)) \ $(patsubst %.cpp,%.$(OBJECT_SUFFIX),$(wildcard avi/*.cpp)) \ - src/fonts/font.$(OBJECT_SUFFIX) + src/fonts/font.$(OBJECT_SUFFIX) src/core/version.$(OBJECT_SUFFIX) PROGRAMS = lsnes.$(EXECUTABLE_SUFFIX) movieinfo.$(EXECUTABLE_SUFFIX) lsnes-dumpavi.$(EXECUTABLE_SUFFIX) sdmp2sox.$(EXECUTABLE_SUFFIX) all: $(PROGRAMS) @@ -160,5 +161,13 @@ src/fonts/font.$(OBJECT_SUFFIX): src/fonts/$(FONT_SRC) echo ";" >>src/fonts/font.cpp $(REALCC) $(CORE_CFLAGS) -c -o $@ src/fonts/font.cpp +#Version info. +buildaux/version.exe: buildaux/version.cpp VERSION + $(HOSTCC) $(HOSTCCFLAGS) -o $@ $< +src/core/version.cpp: buildaux/version.exe FORCE + buildaux/version.exe >$@ + +.PHONY: FORCE + clean: rm -f $(PROGRAMS) src/*.$(OBJECT_SUFFIX) src/*/*.$(OBJECT_SUFFIX) avi/*.$(OBJECT_SUFFIX) src/fonts/font.o src/fonts/font.cpp diff --git a/VERSION b/VERSION new file mode 100644 index 00000000..ac261028 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +1-β3 diff --git a/buildaux/version.cpp b/buildaux/version.cpp new file mode 100644 index 00000000..7c1b4ba3 --- /dev/null +++ b/buildaux/version.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +std::string X = "$Format:%h by %cn on %ci$"; + +std::string derive_format(std::string kwformat) +{ + if(kwformat[0] != '$' || kwformat[1] != 'F' || kwformat[kwformat.length() - 1] != '$') { + std::cerr << "Bad keyword format '" << kwformat << "'" << std::endl; + exit(1); + } + return "--pretty=f" + kwformat.substr(2, kwformat.length() - 3); +} + +std::string shellquote(std::string arg) +{ + std::ostringstream x; + x << "'"; + for(size_t i = 0; i < arg.length(); i++) { + if(arg[i] == '\'') + x << "\\'"; + else + x << arg[i]; + } + x << "'"; + return x.str(); +} + +std::string runlog(std::string logformat) +{ + std::string command = "git log " + shellquote(logformat) + " -1"; + std::string retval; + int r; + char buf[4096] = {0}; + FILE* out = popen(command.c_str(), "r"); + if(!out) { + std::cerr << "Can't invoke git to get the version" << std::endl; + exit(1); + } + while((r = fread(buf, 1, 4095, out)) > 0) { + buf[r] = 0; + retval = retval + buf; + } + if(ferror(out)) { + std::cerr << "Error reading git version output" << std::endl; + exit(1); + } + pclose(out); + return retval; +} + +std::string get_main_version() +{ + std::ifstream x("VERSION"); + if(!x) { + std::cerr << "Error reading main version" << std::endl; + exit(1); + } + std::string out; + std::getline(x, out); + if(out == "") { + std::cerr << "Error reading main version" << std::endl; + exit(1); + } + return out; +} + +int main() +{ + std::string gitversion; + std::string mainversion = get_main_version(); + if(X[0] == '$') { + std::string logformat = derive_format(X); + gitversion = runlog(logformat); + } else + gitversion = X; + std::cout << "#include " << std::endl; + std::cout << "std::string lsnes_git_revision = \"" << gitversion << "\";" << std::endl; + std::cout << "std::string lsnes_version = \"" << mainversion << "\";" << std::endl; + return 0; +} diff --git a/include/lsnes.hpp b/include/lsnes.hpp index dce0538b..38200d22 100644 --- a/include/lsnes.hpp +++ b/include/lsnes.hpp @@ -15,5 +15,6 @@ extern std::string bsnes_core_version; extern std::string lsnes_version; +extern std::string lsnes_git_revision; #endif diff --git a/src/core/misc.cpp b/src/core/misc.cpp index d3d090bc..941c06c9 100644 --- a/src/core/misc.cpp +++ b/src/core/misc.cpp @@ -307,4 +307,3 @@ std::string format_address(void* addr) } std::string bsnes_core_version; -std::string lsnes_version = "1-β3"; diff --git a/src/plat-sdl/main.cpp b/src/plat-sdl/main.cpp index 3eadb158..4819e32a 100644 --- a/src/plat-sdl/main.cpp +++ b/src/plat-sdl/main.cpp @@ -144,6 +144,11 @@ int main(int argc, char** argv) std::vector cmdline; for(int i = 1; i < argc; i++) cmdline.push_back(argv[i]); + if(cmdline.size() == 1 && cmdline[0] == "--version") { + std::cout << "lsnes rr" << lsnes_version << " (" << lsnes_git_revision << ")" << std::endl; + std::cout << snes_library_id() << " (" << SNES::Info::Profile << " core)" << std::endl; + return 0; + } my_interfaced intrf; SNES::interface = &intrf; diff --git a/src/plat-wxwidgets/mainwindow.cpp b/src/plat-wxwidgets/mainwindow.cpp index fb6369b9..38aed3df 100644 --- a/src/plat-wxwidgets/mainwindow.cpp +++ b/src/plat-wxwidgets/mainwindow.cpp @@ -775,6 +775,8 @@ wxwin_mainwindow::wxwin_mainwindow() menu_entry(wxID_EDIT_AUTHORS, wxT("&Edit game name && authors")); menu_separator(); menu_entry(wxID_EXIT, wxT("&Quit")); + menu_separator(); + menu_entry(wxID_ABOUT, wxT("About")); //File menu: (ACFOS)DELMNPRTV menu_start(wxT("&File")); menu_entry_check(wxID_READONLY_MODE, wxT("Reado&nly mode")); @@ -1001,6 +1003,13 @@ void wxwin_mainwindow::handle_menu_click(wxCommandEvent& e) case wxID_LOAD_MEMORYWATCH: menu_load_memorywatch(e); break; + case wxID_ABOUT: { + std::ostringstream str; + str << "lsnes rr" << lsnes_version << " (" << lsnes_git_revision << ")" << std::endl; + str << bsnes_core_version << std::endl; + wxMessageBox(towxstring(str.str()), _T("About"), wxICON_INFORMATION | wxOK, this); + } + break; }; }