Skip to content

Commit

Permalink
Verinfo: Add 'scripts/verinfo.sh' to generate git version info. [ref:…
Browse files Browse the repository at this point in the history
… ptt/pttbbs]

- Add 'scripts/verinfo.sh' to generate
   git version and build configuration information
- CMakeLists.txt & Makefile:
   - Add target `verinfo`
   - Make the source file include the output file `verinfo_export.conf`
      with flag `-imacros` when compiling
- Makefile: Add dependency `verinfo` to `all` and `njob`.
- CMakeLists.txt: Add dependency `verinfo` to all targets.
- .gitignore: Ignore the output file 'verinfo_export.conf'.
  • Loading branch information
IepIweidieng committed Feb 13, 2020
1 parent f1156d4 commit 538da2c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
make_export.conf
maple/make_export.conf

# git version info
verinfo_export.conf

# add split-out build directory for cmake
build/

Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ set(BBSCONF "${PROJECT_SOURCE_DIR}/dreambbs.conf")
set(BBSCONF_ORIGIN "${PROJECT_SOURCE_DIR}/include/config.h")
set(EXPORT_PROJECT "${PROJECT_SOURCE_DIR}/make_export.conf")
set(EXPORT_MAPLE "${PROJECT_SOURCE_DIR}/maple/make_export.conf")
set(EXPORT_VERINFO "${PROJECT_SOURCE_DIR}/verinfo_export.conf")

if(NOT EXISTS ${EXPORT_PROJECT})
execute_process(COMMAND touch ${EXPORT_PROJECT})
Expand Down Expand Up @@ -261,6 +262,13 @@ endfunction()

EXPORT_CONF(${CMAKE_CURRENT_SOURCE_DIR}/make_export.conf)

add_custom_target(verinfo
ALL
sh scripts/verinfo.sh CMake ${MULTIARCH}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)
add_compile_options(-imacros "${EXPORT_VERINFO}")

set(CMAKE_INSTALL_PREFIX ${BBSHOME})

add_subdirectory(lib)
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ EXPORT_FILE := "$(REALSRCROOT)/make_export.conf"
# some directories need to be compiled:
# lib innbbsd maple so util test

all:
all: verinfo
@(cd lib; $(MAKE) all)
@(cd maple; $(MAKE) all)
@(cd util; $(MAKE) all)
Expand All @@ -30,7 +30,7 @@ all:
.endif
@(cd test; $(MAKE) all)

njob:
njob: verinfo
@(cd lib; $(MAKE) -j$(NPROC) all)
@(cd maple; $(MAKE) -j$(NPROC) all)
@(cd util; $(MAKE) -j$(NPROC) all)
Expand Down Expand Up @@ -63,6 +63,9 @@ configure:
# Continue execution with a new `bmake` instace and stop current `bmake` instance
@if [ "$(TARGETS_REST)" ]; then sh -c "$(MAKE) $(TARGETS_REST) $(MAKEFLAGS)"; printf "\033[1;36mJob done. Force stop.\033[m\n" >&2; false; fi

verinfo: .PHONY
@sh scripts/verinfo.sh "BSD-make" "$(MULTIARCH)"

runtest:
@(cd test; $(MAKE) runtest)

Expand Down
2 changes: 2 additions & 0 deletions dreambbs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ EXPORTCONF = echo $(DEFCONF$(exconf::= $(exconf:M*))$(exvalue::= $(exvalue:M*)))
BBSCONF := $(REALSRCROOT)/dreambbs.conf
BBSCONF_ORIGIN := $(REALSRCROOT)/include/config.h
EXPORT_MAPLE := $(REALSRCROOT)/maple/make_export.conf
EXPORT_VERINFO := $(REALSRCROOT)/verinfo_export.conf
!= touch $(EXPORT_MAPLE)
CFLAGS_MK += -imacros "$(EXPORT_VERINFO)"

# User names and group names
BBSUSR != $(GETVAR$(var::= "$(BBSUSR)")$(else_var::= $(GETVALUE$(conf::= "BBSUSR")$(default::= "$(:!id -un!)")$(hdr::= $(BBSCONF_ORIGIN)))))
Expand Down
73 changes: 73 additions & 0 deletions scripts/verinfo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash

# prevent localized logs
LC_ALL=C
export LC_ALL

source_dir="${source_dir:-"."}"
build_make="$1"
build_arch="$2"

format_url() {
echo "$@" | sed 's/^https\?:\/\///g' | sed 's/\.git$//g'
}

format_commit() {
echo "c$(git rev-list --count "$1" 2>/dev/null)/$(git rev-parse --short "$1" 2>/dev/null)"
}

build_branch="$(git symbolic-ref --short HEAD 2>/dev/null)"
# if [ -z "${build_branch}" ]; then
# build_branch=""
# fi

build_head="$(format_commit HEAD)"
if ! git diff --quiet 2>/dev/null; then
build_head="${build_head} Modified"
fi

# `branch.<name>.pushRemote` > `remote.pushDefault` > `branch.<name>.remote`
build_remote="$(git config --get "branch.${build_branch}.pushRemote" 2>/dev/null)"
if [ -z "${build_remote}" ]; then
build_remote="$(git config --get "remote.pushDefault" 2>/dev/null)"
fi
if [ -z "${build_remote}" ]; then
build_remote="$(git config --get "branch.${build_branch}.remote" 2>/dev/null)"
fi
# `<remote>/<branch.<name>.merge>` > `<remote>/<branch>` > `<remote>/master` > `origin/master`
build_branch_remote="${build_remote}/$(git config --get "branch.${build_branch}.merge" 2>/dev/null | sed 's/^.*\///g')"
if ! git rev-parse -q --verify "${build_branch_remote}" >/dev/null; then
build_branch_remote="${build_remote}/${build_branch}"
fi
if ! git rev-parse -q --verify "${build_branch_remote}" >/dev/null; then
build_branch_remote="${build_remote}/master"
fi
if ! git rev-parse -q --verify "${build_branch_remote}" >/dev/null; then
build_branch_remote="origin/master"
fi
build_head_remote="$(format_commit ${build_branch_remote})"
build_remote="${build_branch_remote%/*}"
build_branch_remote="${build_branch_remote##*/}"

build_remote_url="$(format_url "$(git config --get "remote.${build_remote}.url" 2>/dev/null)")"

build_date="$(date)"
build_time="$(date -d"${build_date}" "+%s")"

export_file="${source_dir}/verinfo_export.conf"

printf "\033[1;36mGenerating '%s'...\033[0m\n" "${export_file}"
cat > "${export_file}" <<EOF
#define BUILD_MAKE "${build_make}"
#define BUILD_ARCH "${build_arch}"
#define BUILD_BRANCH "${build_branch}"
#define BUILD_HEAD "${build_head}"
#define BUILD_BRANCH_REMOTE "${build_branch_remote}"
#define BUILD_HEAD_REMOTE "${build_head_remote}"
#define BUILD_REMOTE_URL "${build_remote_url}"
#define BUILD_DATE "${build_date}"
#define BUILD_TIME ${build_time}L
EOF
printf "\033[1;33m"
cat "${export_file}"
printf "\033[0m"

0 comments on commit 538da2c

Please sign in to comment.