Add version option

This commit is contained in:
zawz 2020-11-14 17:21:47 +01:00
parent fced49c98e
commit 144ff45830
4 changed files with 50 additions and 7 deletions

View file

@ -15,7 +15,7 @@ LDFLAGS = -lpthread
CC=g++
# compiler flags
CXXFLAGS= -I$(IDIR) -Wall -pedantic -std=c++17
ifeq ($(DEBUG),true)
ifeq ($(DEBUG),true)
# debugging flags
CC=clang++
CXXFLAGS += -g -pg
@ -23,6 +23,11 @@ else
# release flags
CXXFLAGS += -Ofast
endif
ifneq ($(RELEASE), true)
VSUFFIX=-dev-$(SHA_SHORT)
endif
ifeq ($(STATIC),true)
# static links
LDFLAGS += -l:libztd.a
@ -33,22 +38,27 @@ endif
## END CONFIG ##
$(shell ./generate_version.sh)
$(shell mkdir -p $(ODIR))
$(shell mkdir -p $(BINDIR))
# automatically find .h and .hpp
DEPS = $(shell find $(IDIR) -type f -regex '.*\.hp?p?')
DEPS = $(shell find $(IDIR) -type f -regex '.*\.hp?p?' ! -name 'g_version.h')
# automatically find .c and .cpp and make the corresponding .o rule
OBJ = $(shell find $(SRCDIR) -type f -regex '.*\.cp?p?' | sed 's|\.cpp|.o|g;s|\.c|.o|g;s|^$(SRCDIR)/|$(ODIR)/|g')
build: lxsh $(OBJ) $(DEPS)
$(ODIR)/%.o: $(SRCDIR)/%.c $(DEPS)
$(CC) $(CXXFLAGS) -c -o $@ $<
$(ODIR)/%.o: $(SRCDIR)/%.cpp $(DEPS)
$(CC) $(CXXFLAGS) -c -o $@ $<
$(BINDIR)/$(NAME): $(OBJ)
$(ODIR)/main.o: $(SRCDIR)/main.cpp $(DEPS) $(IDIR)/g_version.h
$(CC) $(CXXFLAGS) -c -o $@ $<
lxsh: $(OBJ)
$(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
test: $(BINDIR)/$(NAME)

23
generate_version.sh Executable file
View file

@ -0,0 +1,23 @@
#!/bin/sh
SHA_FULL=$(git rev-parse HEAD)
IDIR=$(grep '^IDIR=' Makefile | cut -d '=' -f2-)
file="$IDIR/g_version.h"
OLD_SHA=$(grep 'VERSION_SHA' "$file" | cut -d '"' -f2)
OLD_SUFFIX=$(grep 'VERSION_SUFFIX' "$file" | cut -d '"' -f2)
[ "$RELEASE" != "true" ] && SUFFIX="-dev-$(echo "$SHA_FULL" | cut -c1-10)"
if [ "$OLD_SHA" != "$SHA_FULL" ] || [ "$OLD_SUFFIX" != "$SUFFIX" ] ; then
cat > "$file" << EOF
#ifndef G_VERSION_H
#define G_VERSION_H
#define VERSION_SUFFIX "$SUFFIX"
#define VERSION_SHA "$SHA_FULL"
#endif
EOF
fi
exit 0

View file

@ -15,20 +15,27 @@
#include "minimize.hpp"
#include "resolve.hpp"
#include "version.h"
#include "g_version.h"
void oneshot_opt_process(const char* arg0)
{
if(options['h'])
{
print_help(arg0);
exit(1);
}
if(options["help-commands"])
else if(options["version"])
{
printf("%s %s%s\n", arg0, VERSION_STRING, VERSION_SUFFIX);
printf("%s\n", VERSION_SHA);
}
else if(options["help-commands"])
{
print_include_help();
printf("\n\n");
print_resolve_help();
exit(1);
}
exit(0);
}
int main(int argc, char* argv[])
@ -47,6 +54,8 @@ int main(int argc, char* argv[])
return 1;
}
oneshot_opt_process(argv[0]);
// resolve input
std::string file;
if(args.size() > 0) // argument provided

View file

@ -15,6 +15,7 @@ ztd::option_set gen_options()
ret.add(
ztd::option("\r [Help]"),
ztd::option('h', "help", false, "Display this help message"),
ztd::option("version", false, "Display version"),
ztd::option("help-commands", false, "Print help for linker commands"),
ztd::option("\r [Output]"),
ztd::option('o', "output", true , "Output result script to file", "file"),