[lib][version] add a library that provides a structure with the build version

Change-Id: Ifa425f601f47410b4ffbb62ca13bfcfe3cb5a267
This commit is contained in:
Travis Geiselbrecht
2015-05-11 17:58:22 -07:00
parent 49dbd25d34
commit a2b9df457f
4 changed files with 165 additions and 0 deletions

10
lib/version/buildid.sh Executable file
View File

@@ -0,0 +1,10 @@
#!/bin/bash
eval `date -u +'BYR=%Y BMON=%-m BDOM=%-d BHR=%-H BMIN=%-M'`
chr () {
printf \\$(($1/64*100+$1%64/8*10+$1%8))
}
b36 () {
if [ $1 -le 9 ]; then echo $1; else chr $((0x41 + $1 - 10)); fi
}
printf '%c%c%c%c%c\n' `chr $((0x41 + $BYR - 2011))` `b36 $BMON` `b36 $BDOM` `b36 $BHR` `b36 $(($BMIN/2))`

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2013 Google, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __LIB_VERSION_H
#define __LIB_VERSION_H
#include <compiler.h>
__BEGIN_CDECLS
#define VERSION_STRUCT_VERSION 0x1
typedef struct {
unsigned int struct_version;
const char *arch;
const char *platform;
const char *target;
const char *project;
const char *buildid;
} lk_version_t;
extern const lk_version_t version;
void print_version(void);
__END_CDECLS
#endif

34
lib/version/rules.mk Normal file
View File

@@ -0,0 +1,34 @@
LOCAL_DIR := $(GET_LOCAL_DIR)
MODULE := $(LOCAL_DIR)
GLOBAL_INCLUDES += $(LOCAL_DIR)/include
MODULE_SRCS += \
$(LOCAL_DIR)/version.c
# if no one else has defined it by now, build us a default buildid
# based on the current time.
# suffix it with _LOCAL if OFFICIAL_BUILD is unset
ifeq ($(strip $(BUILDID)),)
ifneq ($(OFFICIAL_BUILD),)
BUILDID := "$(shell $(LOCAL_DIR)/buildid.sh)"
else
BUILDID := "$(shell $(LOCAL_DIR)/buildid.sh)_LOCAL"
endif
endif
# Generate a buildid.h file, lazy evaulated BUILDID_DEFINE at the end
# of the first make pass. This lets modules that haven't been
# included yet set BUILDID.
BUILDID_DEFINE="BUILDID=\"$(BUILDID)\""
BUILDID_H := $(BUILDDIR)/buildid.h
$(BUILDID_H): .PHONY
@$(call MAKECONFIGHEADER,$@,BUILDID_DEFINE)
GENERATED += $(BUILDID_H)
# make sure the module properly depends on and can find buildid.h
MODULE_SRCDEPS := $(BUILDID_H)
include make/module.mk

73
lib/version/version.c Normal file
View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2013 Google, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <lib/version.h>
#include <debug.h>
#include <stdio.h>
/* generated for us */
#include <buildid.h>
/* ARCH, PLATFORM, TARGET, PROJECT should be defined by the build system */
/* BUILDID is optional, and may be defined anywhere */
#ifndef BUILDID
#define BUILDID ""
#endif
const lk_version_t version = {
.struct_version = VERSION_STRUCT_VERSION,
.arch = ARCH,
.platform = PLATFORM,
.target = TARGET,
.project = PROJECT,
.buildid = BUILDID
};
void print_version(void)
{
printf("version:\n");
printf("\tarch: %s\n", version.arch);
printf("\tplatform: %s\n", version.platform);
printf("\ttarget: %s\n", version.target);
printf("\tproject: %s\n", version.project);
printf("\tbuildid: %s\n", version.buildid);
}
#if WITH_LIB_CONSOLE
#include <debug.h>
#include <lib/console.h>
static int cmd_version(int argc, const cmd_args *argv)
{
print_version();
return 0;
}
STATIC_COMMAND_START
{ "version", "print version", &cmd_version },
STATIC_COMMAND_END(version);
#endif // WITH_LIB_CONSOLE