Skip to content

Commit aa143b5

Browse files
committed
Include Git revision in version info.
CMakeLists.txt contains now the version numbers for DMD and (next) LDC release. If a .git folder is found then the LDC version is replaced by the first 6 chars of the Git revision. Possible improvements: - If the build is not done at the master branch then it could be useful to check for a tag and use the tag instead of the revision. (for release builds) - Maybe it is useful to include the branch name. This fixes issue #366.
1 parent 31d85d0 commit aa143b5

8 files changed

+219
-7
lines changed

CMakeLists.txt

+12-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ include(GetLinuxDistribution)
2929
# Main configuration.
3030
#
3131

32+
# Version information
33+
set(LDC_VERSION "0.12.0") # May be overridden by git hash tag
34+
set(DMD_VERSION "2.63.1")
35+
3236
# Generally, we want to install everything into CMAKE_INSTALL_PREFIX, but when
3337
# it is /usr, put the config files into /etc to meet common practice.
3438
if(NOT DEFINED SYSCONF_INSTALL_DIR)
@@ -154,6 +158,12 @@ set(LDC_GENERATED
154158
#
155159
# Gather source files.
156160
#
161+
include(GetGitRevisionDescription)
162+
get_git_head_revision(REFSPEC HASH)
163+
if(NOT HASH STREQUAL "GITDIR-NOTFOUND")
164+
string(SUBSTRING "${HASH}" 0 6 LDC_VERSION)
165+
endif()
166+
configure_file(driver/ldc-version.cpp.in driver/ldc-version.cpp)
157167

158168
# Also add the header files to the build so that they are available in IDE
159169
# project files generated via CMake.
@@ -171,11 +181,13 @@ set(DRV_SRC
171181
driver/tool.cpp
172182
driver/linker.cpp
173183
driver/main.cpp
184+
${CMAKE_BINARY_DIR}/driver/ldc-version.cpp
174185
)
175186
set(DRV_HDR
176187
driver/linker.h
177188
driver/cl_options.h
178189
driver/configfile.h
190+
driver/ldc-version.h
179191
driver/target.h
180192
driver/toobj.h
181193
driver/tool.h
@@ -267,7 +279,6 @@ add_definitions(
267279
-DOPAQUE_VTBLS
268280
-DLDC_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}"
269281
-DLDC_LLVM_VER=${LDC_LLVM_VER}
270-
-DLDC_LLVM_VERSION_STRING="${LLVM_VERSION_STRING}"
271282
)
272283

273284
if(UNIX)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# - Returns a version string from Git
2+
#
3+
# These functions force a re-configure on each git commit so that you can
4+
# trust the values of the variables in your build system.
5+
#
6+
# get_git_head_revision(<refspecvar> <hashvar> [<additional arguments to git describe> ...])
7+
#
8+
# Returns the refspec and sha hash of the current head revision
9+
#
10+
# git_describe(<var> [<additional arguments to git describe> ...])
11+
#
12+
# Returns the results of git describe on the source tree, and adjusting
13+
# the output so that it tests false if an error occurs.
14+
#
15+
# git_get_exact_tag(<var> [<additional arguments to git describe> ...])
16+
#
17+
# Returns the results of git describe --exact-match on the source tree,
18+
# and adjusting the output so that it tests false if there was no exact
19+
# matching tag.
20+
#
21+
# Requires CMake 2.6 or newer (uses the 'function' command)
22+
#
23+
# Original Author:
24+
# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
25+
# http://academic.cleardefinition.com
26+
# Iowa State University HCI Graduate Program/VRAC
27+
#
28+
# Copyright Iowa State University 2009-2010.
29+
# Distributed under the Boost Software License, Version 1.0.
30+
# (See accompanying file LICENSE_1_0.txt or copy at
31+
# http://www.boost.org/LICENSE_1_0.txt)
32+
33+
if(__get_git_revision_description)
34+
return()
35+
endif()
36+
set(__get_git_revision_description YES)
37+
38+
# We must run the following at "include" time, not at function call time,
39+
# to find the path to this module rather than the path to a calling list file
40+
get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
41+
42+
function(get_git_head_revision _refspecvar _hashvar)
43+
set(GIT_PARENT_DIR "${CMAKE_SOURCE_DIR}")
44+
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
45+
while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
46+
set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
47+
get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
48+
if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
49+
# We have reached the root directory, we are not in git
50+
set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
51+
set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
52+
return()
53+
endif()
54+
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
55+
endwhile()
56+
set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
57+
if(NOT EXISTS "${GIT_DATA}")
58+
file(MAKE_DIRECTORY "${GIT_DATA}")
59+
endif()
60+
61+
if(NOT EXISTS "${GIT_DIR}/HEAD")
62+
return()
63+
endif()
64+
set(HEAD_FILE "${GIT_DATA}/HEAD")
65+
configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
66+
67+
configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
68+
"${GIT_DATA}/grabRef.cmake"
69+
@ONLY)
70+
include("${GIT_DATA}/grabRef.cmake")
71+
72+
set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
73+
set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
74+
endfunction()
75+
76+
function(git_describe _var)
77+
if(NOT GIT_FOUND)
78+
find_package(Git QUIET)
79+
endif()
80+
get_git_head_revision(refspec hash)
81+
if(NOT GIT_FOUND)
82+
set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
83+
return()
84+
endif()
85+
if(NOT hash)
86+
set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
87+
return()
88+
endif()
89+
90+
# TODO sanitize
91+
#if((${ARGN}" MATCHES "&&") OR
92+
# (ARGN MATCHES "||") OR
93+
# (ARGN MATCHES "\\;"))
94+
# message("Please report the following error to the project!")
95+
# message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
96+
#endif()
97+
98+
#message(STATUS "Arguments to execute_process: ${ARGN}")
99+
100+
execute_process(COMMAND
101+
"${GIT_EXECUTABLE}"
102+
describe
103+
${hash}
104+
${ARGN}
105+
WORKING_DIRECTORY
106+
"${CMAKE_SOURCE_DIR}"
107+
RESULT_VARIABLE
108+
res
109+
OUTPUT_VARIABLE
110+
out
111+
ERROR_QUIET
112+
OUTPUT_STRIP_TRAILING_WHITESPACE)
113+
if(NOT res EQUAL 0)
114+
set(out "${out}-${res}-NOTFOUND")
115+
endif()
116+
117+
set(${_var} "${out}" PARENT_SCOPE)
118+
endfunction()
119+
120+
function(git_get_exact_tag _var)
121+
git_describe(out --exact-match ${ARGN})
122+
set(${_var} "${out}" PARENT_SCOPE)
123+
endfunction()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#
2+
# Internal file for GetGitRevisionDescription.cmake
3+
#
4+
# Requires CMake 2.6 or newer (uses the 'function' command)
5+
#
6+
# Original Author:
7+
# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
8+
# http://academic.cleardefinition.com
9+
# Iowa State University HCI Graduate Program/VRAC
10+
#
11+
# Copyright Iowa State University 2009-2010.
12+
# Distributed under the Boost Software License, Version 1.0.
13+
# (See accompanying file LICENSE_1_0.txt or copy at
14+
# http://www.boost.org/LICENSE_1_0.txt)
15+
16+
set(HEAD_HASH)
17+
18+
file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024)
19+
20+
string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS)
21+
if(HEAD_CONTENTS MATCHES "ref")
22+
# named branch
23+
string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}")
24+
if(EXISTS "@GIT_DIR@/${HEAD_REF}")
25+
configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
26+
elseif(EXISTS "@GIT_DIR@/logs/${HEAD_REF}")
27+
configure_file("@GIT_DIR@/logs/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
28+
set(HEAD_HASH "${HEAD_REF}")
29+
endif()
30+
else()
31+
# detached HEAD
32+
configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY)
33+
endif()
34+
35+
if(NOT HEAD_HASH)
36+
file(READ "@GIT_DATA@/head-ref" HEAD_HASH LIMIT 1024)
37+
string(STRIP "${HEAD_HASH}" HEAD_HASH)
38+
endif()

dmd2/mars.c

-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ void Global::init()
103103
compiler.vendor = "Digital Mars D";
104104
#endif
105105
#if IN_LLVM
106-
version = "v2.063";
107-
ldc_version = "trunk";
108-
llvm_version = "LLVM " LDC_LLVM_VERSION_STRING;
109106
compiler.vendor = "LDC";
110107
#endif
111108

dmd2/mars.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ struct Global
341341

342342
const char *version;
343343
#if IN_LLVM
344-
char *ldc_version;
345-
char *llvm_version;
344+
const char *ldc_version;
345+
const char *llvm_version;
346346

347347
bool inExtraInliningSemantic;
348348
#endif

driver/ldc-version.cpp.in

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- driver/ldc-version.c - ----------------------------------*- C++ -*-===//
2+
//
3+
// LDC – the LLVM D compiler
4+
//
5+
// This file is distributed under the BSD-style LDC license. See the LICENSE
6+
// file for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "driver/ldc-version.h"
11+
12+
namespace ldc {
13+
14+
const char * const ldc_version = "@LDC_VERSION@";
15+
const char * const dmd_version = "@DMD_VERSION@";
16+
const char * const llvm_version = "@LLVM_VERSION_STRING@";
17+
18+
}

driver/ldc-version.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- driver/ldc-version.h - ----------------------------------*- C++ -*-===//
2+
//
3+
// LDC – the LLVM D compiler
4+
//
5+
// This file is distributed under the BSD-style LDC license. See the LICENSE
6+
// file for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LDC_DRIVER_LDC_VERSION_H
11+
#define LDC_DRIVER_LDC_VERSION_H
12+
13+
namespace ldc {
14+
15+
extern const char * const ldc_version;
16+
extern const char * const dmd_version;
17+
extern const char * const llvm_version;
18+
19+
}
20+
21+
#endif

driver/main.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "dmd2/target.h"
1818
#include "driver/cl_options.h"
1919
#include "driver/configfile.h"
20+
#include "driver/ldc-version.h"
2021
#include "driver/linker.h"
2122
#include "driver/target.h"
2223
#include "driver/toobj.h"
@@ -86,7 +87,7 @@ static cl::list<std::string, StringsAdapter> debuglibs("debuglib",
8687

8788
void printVersion() {
8889
printf("LDC - the LLVM D compiler (%s):\n", global.ldc_version);
89-
printf(" based on DMD %s and %s\n", global.version, global.llvm_version);
90+
printf(" based on DMD v%s and LLVM %s\n", global.version, global.llvm_version);
9091
printf(" Default target: %s\n", llvm::sys::getDefaultTargetTriple().c_str());
9192
std::string CPU = llvm::sys::getHostCPUName();
9293
if (CPU == "generic") CPU = "(unknown)";
@@ -165,6 +166,9 @@ int main(int argc, char** argv)
165166
int status = EXIT_SUCCESS;
166167

167168
global.init();
169+
global.version = ldc::dmd_version;
170+
global.ldc_version = ldc::ldc_version;
171+
global.llvm_version = ldc::llvm_version;
168172

169173
// Set some default values
170174
#if _WIN32

0 commit comments

Comments
 (0)