Skip to content

Commit

Permalink
Merge pull request #1181 from Mytherin/improveversion
Browse files Browse the repository at this point in the history
Extend DuckDB::LibraryVersion() to output dev version in format `0.2.3-devXXX`
  • Loading branch information
Mytherin committed Dec 2, 2020
2 parents 644370a + c374102 commit 4d5f7d1
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 20 deletions.
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ execute_process(
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND git describe --tags --abbrev=0
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_LAST_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND git describe --tags --long
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_ITERATION
OUTPUT_STRIP_TRAILING_WHITESPACE)

string(REGEX REPLACE "v([0-9]+.[0-9]+.)[0-9]+" "\\1" DUCKDB_MAIN_VERSION "${GIT_LAST_TAG}")
string(REGEX REPLACE "v[0-9]+.[0-9]+.([0-9]+)" "\\1" DUCKDB_PATCH_VERSION "${GIT_LAST_TAG}")
string(REGEX REPLACE ".*-([0-9]+)-.*" "\\1" DUCKDB_DEV_ITERATION "${GIT_ITERATION}")

if(${DUCKDB_DEV_ITERATION}==0)
# on a tag; directly use the version
set(DUCKDB_VERSION "${DUCKDB_MAIN_VERSION}${DUCKDB_PATCH_VERSION}")
else()
# not on a tag, increment the patch version by one and add a -devX suffix
MATH(EXPR DUCKDB_PATCH_VERSION "${DUCKDB_PATCH_VERSION}+1")

set(DUCKDB_VERSION "${DUCKDB_MAIN_VERSION}${DUCKDB_PATCH_VERSION}-dev${DUCKDB_DEV_ITERATION}")
endif()

option(AMALGAMATION_BUILD
"Build from the amalgamation files, rather than from the normal sources."
Expand Down
19 changes: 16 additions & 3 deletions scripts/amalgamation.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,20 @@ def copy_if_different(src, dest):
shutil.copyfile(src, dest)

def git_commit_hash():
return subprocess.check_output(['git','log','-1','--format=%h']).strip()

return subprocess.check_output(['git','log','-1','--format=%h']).strip().decode('utf8')

def git_dev_version():
version = subprocess.check_output(['git','describe','--tags','--abbrev=0']).strip().decode('utf8')
long_version = subprocess.check_output(['git','describe','--tags','--long']).strip().decode('utf8')
version_splits = version.lstrip('v').split('.')
dev_version = long_version.split('-')[1]
if int(dev_version) == 0:
# directly on a tag: emit the regular version
return '.'.join(version_splits)
else:
# not on a tag: increment the version by one and add a -devX suffix
version_splits[2] = str(int(version_splits[2]) + 1)
return '.'.join(version_splits) + "-dev" + dev_version

def generate_duckdb_hpp(header_file):
print("-----------------------")
Expand All @@ -201,7 +213,8 @@ def generate_duckdb_hpp(header_file):

hfile.write("#pragma once\n")
hfile.write("#define DUCKDB_AMALGAMATION 1\n")
hfile.write("#define DUCKDB_SOURCE_ID \"%s\"\n" % git_commit_hash().decode('utf8'))
hfile.write("#define DUCKDB_SOURCE_ID \"%s\"\n" % git_commit_hash())
hfile.write("#define DUCKDB_VERSION \"%s\"\n" % git_dev_version())
for fpath in main_header_files:
hfile.write(write_file(fpath))

Expand Down
44 changes: 34 additions & 10 deletions scripts/package_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ def get_relative_path(source_dir, target_file):
target_file = target_file.replace(source_dir, "").lstrip('/')
return target_file

def git_commit_hash():
return subprocess.check_output(['git','log','-1','--format=%h']).strip().decode('utf8')

def git_dev_version():
version = subprocess.check_output(['git','describe','--tags','--abbrev=0']).strip().decode('utf8')
long_version = subprocess.check_output(['git','describe','--tags','--long']).strip().decode('utf8')
version_splits = version.lstrip('v').split('.')
dev_version = long_version.split('-')[1]
if int(dev_version) == 0:
# directly on a tag: emit the regular version
return '.'.join(version_splits)
else:
# not on a tag: increment the version by one and add a -devX suffix
version_splits[2] = str(int(version_splits[2]) + 1)
return '.'.join(version_splits) + "-dev" + dev_version

def build_package(target_dir, linenumbers = False):
if not os.path.isdir(target_dir):
os.mkdir(target_dir)
Expand Down Expand Up @@ -111,26 +127,34 @@ def copy_file(src, target_dir):
for inc in include_files:
copy_file(inc, target_dir)

# handle pragma_version.cpp: paste #define DUCKDB_SOURCE_ID there
# read the source id
proc = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=os.path.join(scripts_dir, '..'))
githash = proc.stdout.read().strip().decode('utf8')
# handle pragma_version.cpp: paste #define DUCKDB_SOURCE_ID and DUCKDB_VERSION there
curdir = os.getcwd()
os.chdir(os.path.join(scripts_dir, '..'))
githash = git_commit_hash()
dev_version = git_dev_version()
os.chdir(curdir)
# open the file and read the current contents
fpath = os.path.join(target_dir, 'src', 'function', 'table', 'version', 'pragma_version.cpp')
with open_utf8(fpath, 'r') as f:
text = f.read()
# now add the DUCKDB_SOURCE_ID define, if it is not there already
found = False
found_hash = False
found_dev = False
lines = text.split('\n')
for i in range(len(lines)):
if '#define DUCKDB_SOURCE_ID ' in lines[i]:
lines[i] = '#define DUCKDB_SOURCE_ID "{}"'.format(githash)
found = True
found_hash = True
break
if not found:
text = '#ifndef DUCKDB_SOURCE_ID\n#define DUCKDB_SOURCE_ID "{}"\n#endif\n'.format(githash) + text
else:
text = '\n'.join(text)
if '#define DUCKDB_VERSION ' in lines[i]:
lines[i] = '#define DUCKDB_VERSION "{}"'.format(dev_version)
found_dev = True
break
if not found_hash:
lines = ['#ifndef DUCKDB_SOURCE_ID', '#define DUCKDB_SOURCE_ID "{}"'.format(githash), '#endif'] + lines
if not found_hash:
lines = ['#ifndef DUCKDB_VERSION', '#define DUCKDB_VERSION "{}"'.format(dev_version), '#endif'] + lines
text = '\n'.join(lines)
with open_utf8(fpath, 'w+') as f:
f.write(text)

Expand Down
1 change: 1 addition & 0 deletions src/function/table/version/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_definitions(-DDUCKDB_SOURCE_ID="\""${GIT_COMMIT_HASH}"\"")
add_definitions(-DDUCKDB_VERSION="\""${DUCKDB_VERSION}"\"")

add_library_unity(duckdb_func_table_version OBJECT pragma_version.cpp)

Expand Down
2 changes: 1 addition & 1 deletion src/function/table/version/pragma_version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const char *DuckDB::SourceID() {
}

const char *DuckDB::LibraryVersion() {
return "DuckDB";
return DUCKDB_VERSION;
}

} // namespace duckdb
5 changes: 1 addition & 4 deletions test/sql/pragma/test_pragma_version.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@ PRAGMA version;
statement ok
select * from pragma_version();

query I
statement ok
select library_version from pragma_version();
----
DuckDB

4 changes: 2 additions & 2 deletions tools/shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -20241,7 +20241,7 @@ static const char zOptions[] =
#endif
" -stats print memory stats before each finalize\n"
" -table set output mode to 'table'\n"
" -version show SQLite version\n"
" -version show DuckDB version\n"
" -vfs NAME use NAME as the default VFS\n"
#ifdef SQLITE_ENABLE_VFSTRACE
" -vfstrace enable tracing of all VFS calls\n"
Expand All @@ -20253,7 +20253,7 @@ static const char zOptions[] =
static void usage(int showDetail){
utf8_printf(stderr,
"Usage: %s [OPTIONS] FILENAME [SQL]\n"
"FILENAME is the name of an SQLite database. A new database is created\n"
"FILENAME is the name of an DuckDB database. A new database is created\n"
"if the file does not previously exist.\n", Argv0);
if( showDetail ){
utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
Expand Down

0 comments on commit 4d5f7d1

Please sign in to comment.