Skip to content

Commit dc30ee2

Browse files
committed
Generate version info and timestamp at build time
* Print this info from `p21read -v` and in a comment in generated source, not just from `fedex_plus -v` * Use a function `const char* scl_version()`, instead of using the macro `SCL_COMMIT_ID` * Fail gracefully if the `.git` directory does not exist (thanks CY!) * Fixes issue #89
1 parent fa615a1 commit dc30ee2

File tree

8 files changed

+105
-32
lines changed

8 files changed

+105
-32
lines changed

CMakeLists.txt

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ENDIF(COMMAND CMAKE_POLICY)
4848
PROJECT(SCL)
4949

5050
if( CMAKE_BUILD_TYPE STREQUAL "" )
51-
message( "Debug build - to override, rerun cmake with '-DCMAKE_BUILD_TYPE=Release'." )
51+
message( "-- Debug build - to override, rerun cmake with '-DCMAKE_BUILD_TYPE=Release'." )
5252
set( CMAKE_BUILD_TYPE Debug )
5353
endif()
5454

@@ -176,8 +176,8 @@ IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
176176
IF ("${CMAKE_BUILD_TYPE}" MATCHES "Release")
177177
SET(CMAKE_INSTALL_PREFIX "/usr")
178178
ELSEIF ("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
179-
MESSAGE("setting debug output dir")
180179
SET(CMAKE_INSTALL_PREFIX "${SCL_SOURCE_DIR}/../scl-install")
180+
MESSAGE("--- Setting debug install dir to ${CMAKE_INSTALL_PREFIX}")
181181
ELSE("${CMAKE_BUILD_TYPE}" MATCHES "Release")
182182
SET(CMAKE_INSTALL_PREFIX "/usr/local")
183183
ENDIF ("${CMAKE_BUILD_TYPE}" MATCHES "Release")
@@ -213,23 +213,26 @@ CHECK_FUNCTION_EXISTS(memmove HAVE_MEMMOVE)
213213

214214
CHECK_TYPE_SIZE("ssize_t" SSIZE_T)
215215

216-
#set GIT_COMMIT_ID to the output of `git describe`
217-
find_package(Git)
218-
if(GIT_FOUND)
219-
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags RESULT_VARIABLE res_var OUTPUT_VARIABLE GIT_COM_ID )
220-
if( NOT ${res_var} EQUAL 0 )
221-
set( GIT_COMMIT_ID "unknown (git failed!)")
222-
message( WARNING "Git failed (not a repo?). Build will not contain git revision info." )
223-
endif()
224-
string( REPLACE "\n" "" GIT_COMMIT_ID ${GIT_COM_ID} )
225-
else()
226-
set( GIT_COMMIT_ID "unknown (git not found!)")
227-
message( WARNING "Git not found. Build will not contain git revision info." )
228-
endif()
229-
230216
# Now that all the tests are done, configure the scl_cf.h file:
231217
configure_file(${SCL_BINARY_DIR}/include/scl_cf.h.in ${SCL_BINARY_DIR}/include/scl_cf.h)
232218

219+
################ create scl_version_string.h, http://stackoverflow.com/questions/3780667
220+
# Using 'ver_string' instead of 'scl_version_string.h' is a trick to force the
221+
# command to always execute when the custom target is built. It works because
222+
# a file by that name never exists.
223+
add_custom_target(version_string ALL
224+
DEPENDS ver_string )
225+
# creates scl_version_string.h using cmake script
226+
add_custom_command(OUTPUT ver_string ${CMAKE_CURRENT_BINARY_DIR}/include/scl_version_string.h
227+
COMMAND ${CMAKE_COMMAND} -DSOURCE_DIR=${SCL_SOURCE_DIR}
228+
-DBINARY_DIR=${SCL_BINARY_DIR}
229+
-P ${SCL_CMAKE_DIR}/scl_version_string.cmake)
230+
# scl_version_string.h is a generated file
231+
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/include/scl_version_string.h
232+
PROPERTIES GENERATED TRUE
233+
HEADER_FILE_ONLY TRUE )
234+
################
235+
233236
add_definitions( -pedantic -W -Wall -Wundef -Wfloat-equal -Wshadow -Winline -Wno-long-long )
234237

235238
include_directories(

cmake/scl_version_string.cmake

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#use git for a pretty commit id
2+
#uses 'git describe --tags', so tags are required in the repo
3+
#create a tag with 'git tag <name>' and 'git push --tags'
4+
5+
# http://stackoverflow.com/questions/3780667
6+
# http://www.cmake.org/pipermail/cmake/2009-February/027014.html
7+
8+
#scl_version_string.h defines scl_version() which returns a pretty commit description and a build timestamp.
9+
if( EXISTS ${SOURCE_DIR}/.git )
10+
find_package(Git QUIET)
11+
if(GIT_FOUND)
12+
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags RESULT_VARIABLE res_var OUTPUT_VARIABLE GIT_COM_ID )
13+
if( NOT ${res_var} EQUAL 0 )
14+
set( GIT_COMMIT_ID "unknown (no tags?)")
15+
message( WARNING "Git failed (invalid repo, or no tags). Build will not contain git revision info." )
16+
endif()
17+
string( REPLACE "\n" "" GIT_COMMIT_ID ${GIT_COM_ID} )
18+
else(GIT_FOUND)
19+
set( GIT_COMMIT_ID "unknown (git not found!)")
20+
message( WARNING "Git not found. Build will not contain git revision info." )
21+
endif(GIT_FOUND)
22+
else()
23+
set( GIT_COMMIT_ID "unknown (not a repository!)")
24+
message( WARNING "Git failed (.git not found). Build will not contain git revision info." )
25+
endif()
26+
27+
set( res_var 1 )
28+
IF (WIN32)
29+
EXECUTE_PROCESS(COMMAND "date" "/T" RESULT_VARIABLE res_var OUTPUT_VARIABLE TIMESTAMP )
30+
ELSEIF(UNIX)
31+
EXECUTE_PROCESS(COMMAND "date" RESULT_VARIABLE res_var OUTPUT_VARIABLE TIMESTAMP )
32+
ELSE()
33+
MESSAGE(WARNING "Unknown platform: date not included in version string")
34+
ENDIF()
35+
if( NOT ${res_var} EQUAL 0 )
36+
SET( TIMESTAMP "000000" )
37+
else()
38+
string( REPLACE "\n" "" TIMESTAMP ${TIMESTAMP} )
39+
endif()
40+
41+
set( vstring "//scl_version_string.h - written by cmake. Changes will be lost!\n"
42+
"#ifndef SCL_VERSION_STRING\n"
43+
"#define SCL_VERSION_STRING\n\n"
44+
"/*\n** Returns a string like \"test-1-g5e1fb47, built on <timestamp>\", where test is the\n"
45+
"** name of the last tagged git revision, 1 is the number of commits since that tag,\n"
46+
"** 'g' is unknown, 5e1fb47 is the first 7 chars of the git sha1 commit id, and\n"
47+
"** <timestamp> is the output of your system's 'date' command.\n*/\n\n"
48+
"const char* scl_version() {\n"
49+
" return \"git commit id ${GIT_COMMIT_ID}, built on ${TIMESTAMP}\"\;\n"
50+
"}\n\n"
51+
"#endif\n"
52+
)
53+
54+
file(WRITE scl_version_string.h.txt ${vstring} )
55+
# copy the file to the final header only if the version changes
56+
# reduces needless rebuilds
57+
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
58+
scl_version_string.h.txt ${BINARY_DIR}/include/scl_version_string.h)
59+
message("-- scl_version_string.h is up-to-date.")

data/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ FUNCTION(BUILD_A_SCHEMA SCHEMA_FILE)
8686
set_tests_properties( build_cpp_${PROJECT_NAME} PROPERTIES DEPENDS generate_cpp_${SCHEMA_SHORT_NAME} LABELS cpp_schema_build )
8787

8888
add_executable( p21read_${PROJECT_NAME} ${SCL_SOURCE_DIR}/src/test/p21read/p21read.cc )
89+
add_dependencies( p21read_${PROJECT_NAME} version_string )
8990
target_link_libraries( p21read_${PROJECT_NAME} ${PROJECT_NAME} )
9091
set_target_properties( p21read_${PROJECT_NAME} PROPERTIES COMPILE_FLAGS
9192
${${PROJECT_NAME}_COMPILE_FLAGS} )

include/scl_cf_cmake.h.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@
99
#cmakedefine HAVE_MEMMOVE 1
1010
#cmakedefine HAVE_SSIZE_T 1
1111

12-
//cmake updates this at build time
13-
#define SCL_COMMIT_ID "@GIT_COMMIT_ID@"

src/express/fedex.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
*/
7373

7474
#include "scl_cf.h"
75+
#include "scl_version_string.h"
7576
#include <stdlib.h>
7677
#include <stdio.h>
7778
#include <unistd.h>
@@ -91,7 +92,7 @@ char EXPRESSgetopt_options[256] = "Bbd:e:i:w:p:u:l:nrvz";
9192
int no_need_to_work = 0; /* TRUE if we can exit gracefully without doing any work */
9293

9394
void print_fedex_version( void ) {
94-
printf( "%s\nSCL version id (from git): %s\nhttp://github.com/mpictor/StepClassLibrary\n", EXPRESSprogram_name, SCL_COMMIT_ID );
95+
fprintf( stderr, "Build info for %s: %s\nhttp://github.com/mpictor/StepClassLibrary\n", EXPRESSprogram_name, scl_version() );
9596
no_need_to_work = 1;
9697
}
9798

src/fedex_plus/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ ENDIF()
3333

3434
SCL_ADDEXEC(fedex_plus "${fedex_plus_SOURCES}" "libexppp express")
3535

36+
add_dependencies( fedex_plus version_string )

src/fedex_plus/classes_misc.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ for the STEP Standard Data Access Interface as defined in document
1818
N350 ( August 31, 1993 ) of ISO 10303 TC184/SC4/WG7.
1919
*******************************************************************/
2020

21+
extern char* scl_version();
22+
2123
extern int multiple_inheritance;
2224
/*extern int corba_binding; */
2325

@@ -125,9 +127,9 @@ FILEcreate( const char * filename ) {
125127
}
126128

127129
fprintf( file, "#ifndef %s\n", fn = StrToConstant( filename ) );
128-
fprintf( file, "#define %s\n", fn );
130+
fprintf( file, "#define %s\n\n", fn );
129131

130-
fprintf( file, "// This file was generated by fedex_plus %s.\n", scl_version() );
132+
fprintf( file, "// This file was generated by fedex_plus,\n// %s.\n", scl_version() );
131133
fprintf( file, "// You probably don't want to edit it since your modifications\n" );
132134
fprintf( file, "// will be lost if fedex_plus is used to regenerate it.\n" );
133135
return ( file );

src/test/p21read/p21read.cc

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
extern void SchemaInit( class Registry & );
16+
#include "scl_version_string.h"
1617
#include <STEPfile.h>
1718
#include <sdai.h>
1819
#include <STEPattribute.h>
@@ -82,12 +83,18 @@ void checkSchemaName( Registry & reg, STEPfile & sf, bool ignoreErr ) {
8283
}
8384
}
8485

86+
void printVersion(const char * exe) {
87+
std::cout << exe << " build info: " << scl_version() << std::endl;
88+
}
89+
8590
void printUse(const char * exe) {
8691
std::cout << "p21read - read a STEP Part 21 exchange file using SCL, and write the data to another file." << std::endl;
8792
std::cout << "Syntax: " << exe << " [-i] [-s] infile [outfile]" << std::endl;
8893
std::cout << "Use '-i' to ignore a schema name mismatch." << std::endl;
8994
std::cout << "Use '-s' for strict interpretation (attributes that are \"missing and required\" will cause errors)." << std::endl;
95+
std::cout << "Use '-v' to print the version info below and exit." << std::endl;
9096
std::cout << "Use '--' as the last argument if a file name starts with a dash." << std::endl;
97+
printVersion(exe);
9198
exit( 1 );
9299
}
93100

@@ -98,31 +105,32 @@ int main( int argc, char * argv[] ) {
98105
if( argc > 4 || argc < 2 ) {
99106
printUse(argv[0]);
100107
}
101-
while( ( c = getopt( argc, argv, "is" ) ) != -1 ) {
108+
while( ( c = getopt( argc, argv, "isv" ) ) != -1 ) {
102109
switch( c ) {
103110
case 'i':
104111
ignoreErr = true;
105112
break;
106113
case 's':
107114
strict = true;
108115
break;
116+
case 'v':
117+
printVersion(argv[0]);
118+
exit(0);
109119
case '?':
110120
default:
111121
printUse(argv[0]);
112122
}
113123
}
114124

115125

116-
///////////////////////////////////////////////////////////////////////////////
117-
// You have to initialize the schema before you do anything else.
118-
// This initializes all of the registry information for the schema you
119-
// plan to use. The SchemaInit() function is generated by fedex_plus.
120-
// See the 'extern' stmt above.
121-
//
122-
// The registry is always going to be in memory.
123-
//
124-
///////////////////////////////////////////////////////////////////////////////
125-
126+
///////////////////////////////////////////////////////////////////////////////
127+
// You have to initialize the schema before you do anything else.
128+
// This initializes all of the registry information for the schema you
129+
// plan to use. The SchemaInit() function is generated by fedex_plus.
130+
// See the 'extern' stmt above.
131+
//
132+
// The registry is always going to be in memory.
133+
///////////////////////////////////////////////////////////////////////////////
126134
Registry registry( SchemaInit );
127135
InstMgr instance_list;
128136
STEPfile sfile( registry, instance_list, "", strict );

0 commit comments

Comments
 (0)