Skip to content

Commit 38a7a13

Browse files
authored
Auto-generate the release date on version.h from git CommitDate (#6382)
* Auto-generate the release date on version.h from git CommitDate * Generate revision.h on mswin
1 parent ca4cbe5 commit 38a7a13

File tree

7 files changed

+37
-16
lines changed

7 files changed

+37
-16
lines changed

common.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ $(BUILTIN_RB_INCS): $(top_srcdir)/tool/mk_builtin_loader.rb
12141214
$(srcdir)/revision.h:
12151215
$(srcdir)/revision.h$(gnumake:yes=-nongnumake):
12161216
$(Q)$(RM) $(@F)
1217-
$(Q)$(NULLCMD) > $@ || $(NULLCMD) > $(@F)
1217+
$(NULLCMD) > $(@F)
12181218

12191219
revision.tmp::
12201220
$(Q) $(NULLCMD) > $@
@@ -8275,6 +8275,7 @@ load.$(OBJEXT): {$(VPATH)}vm_core.h
82758275
load.$(OBJEXT): {$(VPATH)}vm_opts.h
82768276
loadpath.$(OBJEXT): $(hdrdir)/ruby/ruby.h
82778277
loadpath.$(OBJEXT): $(hdrdir)/ruby/version.h
8278+
loadpath.$(OBJEXT): $(top_srcdir)/revision.h
82788279
loadpath.$(OBJEXT): $(top_srcdir)/version.h
82798280
loadpath.$(OBJEXT): {$(VPATH)}assert.h
82808281
loadpath.$(OBJEXT): {$(VPATH)}backward/2/assume.h

tool/file2lastrev.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ def self.output=(output)
6666
new_vcs["."]
6767
end
6868
}
69-
exit unless vcs
69+
unless vcs
70+
# Output only release_date when .git is missing
71+
if @output == :revision_h
72+
puts VCS.release_date(Time.now - 10) # same as make-snapshot
73+
end
74+
exit
75+
end
7076

7177
@output =
7278
case @output
@@ -76,7 +82,7 @@ def self.output=(output)
7682
}
7783
when :revision_h
7884
Proc.new {|last, changed, modified, branch, title|
79-
vcs.revision_header(last, modified, branch, title, limit: @limit)
85+
vcs.revision_header(last, modified, modified, branch, title, limit: @limit)
8086
}
8187
when :doxygen
8288
Proc.new {|last, changed|

tool/lib/vcs.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ def self.define_options(parser, opts = {})
9595
opts
9696
end
9797

98+
def self.release_date(time)
99+
t = time.utc
100+
[
101+
t.strftime('#define RUBY_RELEASE_YEAR %Y'),
102+
t.strftime('#define RUBY_RELEASE_MONTH %-m'),
103+
t.strftime('#define RUBY_RELEASE_DAY %-d'),
104+
]
105+
end
106+
98107
attr_reader :srcdir
99108

100109
def initialize(path)
@@ -204,7 +213,8 @@ def short_revision(rev)
204213
revision_handler(rev).short_revision(rev)
205214
end
206215

207-
def revision_header(last, modified = nil, branch = nil, title = nil, limit: 20)
216+
# make-snapshot generates only release_date whereas file2lastrev generates both release_date and release_datetime
217+
def revision_header(last, release_date, release_datetime = nil, branch = nil, title = nil, limit: 20)
208218
short = short_revision(last)
209219
if /[^\x00-\x7f]/ =~ title and title.respond_to?(:force_encoding)
210220
title = title.dup.force_encoding("US-ASCII")
@@ -225,10 +235,11 @@ def revision_header(last, modified = nil, branch = nil, title = nil, limit: 20)
225235
title = title.dump.sub(/\\#/, '#')
226236
code << "#define RUBY_LAST_COMMIT_TITLE #{title}"
227237
end
228-
if modified
229-
t = modified.utc
238+
if release_datetime
239+
t = release_datetime.utc
230240
code << t.strftime('#define RUBY_RELEASE_DATETIME "%FT%TZ"')
231241
end
242+
code += VCS.release_date(release_date)
232243
code
233244
end
234245

@@ -386,7 +397,10 @@ def commit
386397
end
387398

388399
class GIT < self
389-
register(".git") {|path, dir| File.exist?(File.join(path, dir))}
400+
register(".git") do |path, dir|
401+
File.exist?(File.join(path, dir)) &&
402+
(`#{COMMAND} -v` rescue false) # make sure git command exists
403+
end
390404
COMMAND = ENV["GIT"] || 'git'
391405

392406
def cmd_args(cmds, srcdir = nil)

tool/make-snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def package(vcs, rev, destdir, tmp = nil)
347347
end
348348

349349
File.open("#{v}/revision.h", "wb") {|f|
350-
f.puts vcs.revision_header(revision)
350+
f.puts vcs.revision_header(revision, modified)
351351
}
352352
version ||= (versionhdr = IO.read("#{v}/version.h"))[RUBY_VERSION_PATTERN, 1]
353353
version ||=

version.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
1414
#define RUBY_PATCHLEVEL -1
1515

16-
#define RUBY_RELEASE_YEAR 2022
17-
#define RUBY_RELEASE_MONTH 9
18-
#define RUBY_RELEASE_DAY 17
19-
2016
#include "ruby/version.h"
2117
#include "ruby/internal/abi.h"
2218

19+
#ifndef RUBY_REVISION
20+
#include "revision.h"
21+
2322
#ifndef TOKEN_PASTE
2423
#define TOKEN_PASTE(x,y) x##y
2524
#endif
@@ -38,6 +37,8 @@
3837
#define RUBY_RELEASE_DAY_STR STRINGIZE(RUBY_RELEASE_DAY)
3938
#endif
4039

40+
#endif
41+
4142
#ifdef RUBY_ABI_VERSION
4243
# define RUBY_ABI_VERSION_SUFFIX "+"STRINGIZE(RUBY_ABI_VERSION)
4344
#else
@@ -61,8 +62,4 @@
6162
#define RUBY_PATCHLEVEL_STR ""
6263
#endif
6364

64-
#ifndef RUBY_REVISION
65-
# include "revision.h"
66-
#endif
67-
6865
#endif /* RUBY_TOPLEVEL_VERSION_H */

win32/Makefile.sub

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,8 @@ $(RCFILES): $(RBCONFIG) $(srcdir)/revision.h $(srcdir)/win32/resource.rb
12441244
-so_name=$(RUBY_SO_NAME) \
12451245
. $(icondirs) $(win_srcdir)
12461246

1247+
$(srcdir)/revision.h: $(REVISION_H)
1248+
12471249
update-benchmark-driver:
12481250
$(GIT) clone https://github.com/benchmark-driver/benchmark-driver $(srcdir)/benchmark/benchmark-driver || \
12491251
$(GIT) -C $(srcdir)/benchmark/benchmark-driver pull origin master

win32/ifchange.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ if exist %dest% (
8989
)
9090
)
9191
for %%I in (%1) do echo %%~I updated
92+
del /f %dest%
9293
copy %src% %dest% > nul
9394
del %src%
9495

0 commit comments

Comments
 (0)