From 8e89bbb8d2dde80b59b2533563ff67c935addd41 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 20 Jan 2017 18:26:53 -0500 Subject: [PATCH] Add methods for accessing the git commit used when compiling --- SConstruct | 5 +++++ doc/SConscript | 3 ++- include/cantera/base/global.h | 3 +++ include/cantera/clib/ct.h | 1 + interfaces/cython/cantera/__init__.py | 2 +- interfaces/cython/cantera/_cantera.pxd | 1 + interfaces/cython/cantera/utils.pyx | 2 ++ src/SConscript | 8 +++++++- src/base/global.cpp | 9 +++++++++ src/clib/ct.cpp | 9 +++++++++ src/matlab/ctfunctions.cpp | 8 ++++++++ test/python/runCythonTests.py | 4 +++- 12 files changed, 51 insertions(+), 4 deletions(-) diff --git a/SConstruct b/SConstruct index 28a5889c1..ee02edb86 100644 --- a/SConstruct +++ b/SConstruct @@ -667,6 +667,11 @@ ctversion = StrictVersion(env['cantera_version']) env['cantera_pure_version'] = '.'.join(str(x) for x in ctversion.version) env['cantera_short_version'] = '.'.join(str(x) for x in ctversion.version[:2]) +try: + env['git_commit'] = getCommandOutput('git', 'rev-parse', '--short', 'HEAD') +except OSError: + env['git_commit'] = '' + # Print values of all build options: print "Configuration variables read from 'cantera.conf' and command line:" for line in open('cantera.conf'): diff --git a/doc/SConscript b/doc/SConscript index b273ca3e0..8787142c8 100644 --- a/doc/SConscript +++ b/doc/SConscript @@ -213,7 +213,8 @@ if localenv['sphinx_docs']: 'Hydrogen.m', 'Methane.m', 'Nitrogen.m', 'oneatm.m', 'Oxygen.m', 'Water.m'], '@Utilities': ['adddir.m', 'ck2cti.m', 'cleanup.m', 'geterr.m', - 'getDataDirectories.m', 'canteraVersion.m'] + 'getDataDirectories.m', 'canteraVersion.m', + 'canteraGitCommit.m'] } # These files do not need to be documented in the MATLAB classes because they diff --git a/include/cantera/base/global.h b/include/cantera/base/global.h index b63b88c86..140026d21 100644 --- a/include/cantera/base/global.h +++ b/include/cantera/base/global.h @@ -129,6 +129,9 @@ void appdelete(); //! @copydoc Application::thread_complete void thread_complete(); +//! Returns the hash of the git commit from which Cantera was compiled, if known +std::string gitCommit(); + //! Returns root directory where %Cantera is installed /*! * @returns a string containing the name of the base directory where %Cantera is diff --git a/include/cantera/clib/ct.h b/include/cantera/clib/ct.h index d679ab078..bc2bb75f1 100644 --- a/include/cantera/clib/ct.h +++ b/include/cantera/clib/ct.h @@ -157,6 +157,7 @@ extern "C" { CANTERA_CAPI int ct_addCanteraDirectory(size_t buflen, const char* buf); CANTERA_CAPI int ct_getDataDirectories(int buflen, char* buf, const char* sep); CANTERA_CAPI int ct_getCanteraVersion(int buflen, char* buf); + CANTERA_CAPI int ct_getGitCommit(int buflen, char* buf); CANTERA_CAPI int ct_suppress_thermo_warnings(int suppress); CANTERA_CAPI int ct_clearStorage(); diff --git a/interfaces/cython/cantera/__init__.py b/interfaces/cython/cantera/__init__.py index 4b6022d02..ccec1e211 100644 --- a/interfaces/cython/cantera/__init__.py +++ b/interfaces/cython/cantera/__init__.py @@ -2,7 +2,7 @@ # at http://www.cantera.org/license.txt for license and copyright information. from ._cantera import * -from ._cantera import __version__, __sundials_version__ +from ._cantera import __version__, __sundials_version__, __git_commit__ from .composite import * from .liquidvapor import * from .onedim import * diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 9d72b94a7..caf457c6e 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -54,6 +54,7 @@ cdef extern from "cantera/base/global.h" namespace "Cantera": cdef XML_Node* CxxGetXmlFromString "Cantera::get_XML_from_string" (string) except +translate_exception cdef void Cxx_make_deprecation_warnings_fatal "Cantera::make_deprecation_warnings_fatal" () cdef void Cxx_suppress_thermo_warnings "Cantera::suppress_thermo_warnings" (cbool) + cdef string CxxGitCommit "Cantera::gitCommit" () cdef extern from "": cppclass shared_ptr "std::shared_ptr" [T]: diff --git a/interfaces/cython/cantera/utils.pyx b/interfaces/cython/cantera/utils.pyx index b4e60db06..c435e0903 100644 --- a/interfaces/cython/cantera/utils.pyx +++ b/interfaces/cython/cantera/utils.pyx @@ -40,6 +40,8 @@ __sundials_version__ = '.'.join(str(get_sundials_version())) __version__ = pystr(get_cantera_version()) +__git_commit__ = pystr(CxxGitCommit()) + def appdelete(): """ Delete all global Cantera C++ objects """ CxxAppdelete() diff --git a/src/SConscript b/src/SConscript index 1c13c0870..952bd021e 100644 --- a/src/SConscript +++ b/src/SConscript @@ -12,14 +12,20 @@ def applicationSetup(env, subdir, extensions): env.Append(CPPDEFINES={'CANTERA_DATA': escaped_datadir}) return defaultSetup(env, subdir, extensions) +def globalSetup(env, subdir, extensions): + # Add #define variables unique to global.cpp + env.Append(CPPDEFINES={'GIT_COMMIT': '\\"{0}\\"'.format(env['git_commit'])}) + return defaultSetup(env, subdir, extensions) + def baseSetup(env, subdir, extensions): # All files in base except for application.cpp return [f for f in defaultSetup(env, subdir, extensions) - if f.name != 'application.cpp'] + if f.name != 'application.cpp' and f.name != 'global.cpp'] # (subdir, (file extensions), (extra setup(env))) libs = [('base', ['cpp'], baseSetup), ('base', ['^application.cpp'], applicationSetup), + ('base', ['^global.cpp'], globalSetup), ('thermo', ['cpp'], defaultSetup), ('tpx', ['cpp'], defaultSetup), ('equil', ['cpp','c'], defaultSetup), diff --git a/src/base/global.cpp b/src/base/global.cpp index 686718c5a..1ac081598 100644 --- a/src/base/global.cpp +++ b/src/base/global.cpp @@ -93,6 +93,15 @@ void thread_complete() app()->thread_complete(); } +std::string gitCommit() +{ +#ifdef GIT_COMMIT + return GIT_COMMIT; +#else + return "unknown"; +#endif +} + XML_Node* get_XML_File(const std::string& file, int debug) { return app()->get_XML_File(file, debug); diff --git a/src/clib/ct.cpp b/src/clib/ct.cpp index 0a0efc692..263dac056 100644 --- a/src/clib/ct.cpp +++ b/src/clib/ct.cpp @@ -1428,6 +1428,15 @@ extern "C" { } } + int ct_getGitCommit(int buflen, char* buf) + { + try { + return copyString(gitCommit(), buf, buflen); + } catch (...) { + return handleAllExceptions(-1, ERR); + } + } + int ct_suppress_thermo_warnings(int suppress) { try { diff --git a/src/matlab/ctfunctions.cpp b/src/matlab/ctfunctions.cpp index 6efe14d88..beddf362c 100644 --- a/src/matlab/ctfunctions.cpp +++ b/src/matlab/ctfunctions.cpp @@ -98,6 +98,14 @@ void ctfunctions(int nlhs, mxArray* plhs[], plhs[0] = mxCreateString(output_buf); return; + // get cantera git commit + case 7: + buflen = ct_getGitCommit(0, 0); + output_buf = (char*)mxCalloc(buflen, sizeof(char)); + iok = ct_getGitCommit(buflen, output_buf); + plhs[0] = mxCreateString(output_buf); + return; + default: mexErrMsgTxt("ctfunctions: unknown job"); } diff --git a/test/python/runCythonTests.py b/test/python/runCythonTests.py index df6407f9d..b41176581 100644 --- a/test/python/runCythonTests.py +++ b/test/python/runCythonTests.py @@ -72,7 +72,9 @@ class TestResult(unittest.TextTestResult): if __name__ == '__main__': print('\n* INFO: using Cantera module found at this location:') - print('* ', repr(cantera.__file__), '\n') + print('* ', repr(cantera.__file__)) + print('* INFO: Cantera version:', cantera.__version__) + print('* INFO: Git commit:', cantera.__git_commit__, '\n') sys.stdout.flush() loader = unittest.TestLoader()