Skip to content

Commit

Permalink
Auto-generate the release date on version.h from git CommitDate (#6382)
Browse files Browse the repository at this point in the history
* Auto-generate the release date on version.h

from git CommitDate

* Generate revision.h on mswin
  • Loading branch information
k0kubun committed Sep 17, 2022
1 parent ca4cbe5 commit 38a7a13
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
3 changes: 2 additions & 1 deletion common.mk
Expand Up @@ -1214,7 +1214,7 @@ $(BUILTIN_RB_INCS): $(top_srcdir)/tool/mk_builtin_loader.rb
$(srcdir)/revision.h:
$(srcdir)/revision.h$(gnumake:yes=-nongnumake):
$(Q)$(RM) $(@F)
$(Q)$(NULLCMD) > $@ || $(NULLCMD) > $(@F)
$(NULLCMD) > $(@F)

revision.tmp::
$(Q) $(NULLCMD) > $@
Expand Down Expand Up @@ -8275,6 +8275,7 @@ load.$(OBJEXT): {$(VPATH)}vm_core.h
load.$(OBJEXT): {$(VPATH)}vm_opts.h
loadpath.$(OBJEXT): $(hdrdir)/ruby/ruby.h
loadpath.$(OBJEXT): $(hdrdir)/ruby/version.h
loadpath.$(OBJEXT): $(top_srcdir)/revision.h
loadpath.$(OBJEXT): $(top_srcdir)/version.h
loadpath.$(OBJEXT): {$(VPATH)}assert.h
loadpath.$(OBJEXT): {$(VPATH)}backward/2/assume.h
Expand Down
10 changes: 8 additions & 2 deletions tool/file2lastrev.rb
Expand Up @@ -66,7 +66,13 @@ def self.output=(output)
new_vcs["."]
end
}
exit unless vcs
unless vcs
# Output only release_date when .git is missing
if @output == :revision_h
puts VCS.release_date(Time.now - 10) # same as make-snapshot
end
exit
end

@output =
case @output
Expand All @@ -76,7 +82,7 @@ def self.output=(output)
}
when :revision_h
Proc.new {|last, changed, modified, branch, title|
vcs.revision_header(last, modified, branch, title, limit: @limit)
vcs.revision_header(last, modified, modified, branch, title, limit: @limit)
}
when :doxygen
Proc.new {|last, changed|
Expand Down
22 changes: 18 additions & 4 deletions tool/lib/vcs.rb
Expand Up @@ -95,6 +95,15 @@ def self.define_options(parser, opts = {})
opts
end

def self.release_date(time)
t = time.utc
[
t.strftime('#define RUBY_RELEASE_YEAR %Y'),
t.strftime('#define RUBY_RELEASE_MONTH %-m'),
t.strftime('#define RUBY_RELEASE_DAY %-d'),
]
end

attr_reader :srcdir

def initialize(path)
Expand Down Expand Up @@ -204,7 +213,8 @@ def short_revision(rev)
revision_handler(rev).short_revision(rev)
end

def revision_header(last, modified = nil, branch = nil, title = nil, limit: 20)
# make-snapshot generates only release_date whereas file2lastrev generates both release_date and release_datetime
def revision_header(last, release_date, release_datetime = nil, branch = nil, title = nil, limit: 20)
short = short_revision(last)
if /[^\x00-\x7f]/ =~ title and title.respond_to?(:force_encoding)
title = title.dup.force_encoding("US-ASCII")
Expand All @@ -225,10 +235,11 @@ def revision_header(last, modified = nil, branch = nil, title = nil, limit: 20)
title = title.dump.sub(/\\#/, '#')
code << "#define RUBY_LAST_COMMIT_TITLE #{title}"
end
if modified
t = modified.utc
if release_datetime
t = release_datetime.utc
code << t.strftime('#define RUBY_RELEASE_DATETIME "%FT%TZ"')
end
code += VCS.release_date(release_date)
code
end

Expand Down Expand Up @@ -386,7 +397,10 @@ def commit
end

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

def cmd_args(cmds, srcdir = nil)
Expand Down
2 changes: 1 addition & 1 deletion tool/make-snapshot
Expand Up @@ -347,7 +347,7 @@ def package(vcs, rev, destdir, tmp = nil)
end

File.open("#{v}/revision.h", "wb") {|f|
f.puts vcs.revision_header(revision)
f.puts vcs.revision_header(revision, modified)
}
version ||= (versionhdr = IO.read("#{v}/version.h"))[RUBY_VERSION_PATTERN, 1]
version ||=
Expand Down
13 changes: 5 additions & 8 deletions version.h
Expand Up @@ -13,13 +13,12 @@
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL -1

#define RUBY_RELEASE_YEAR 2022
#define RUBY_RELEASE_MONTH 9
#define RUBY_RELEASE_DAY 17

#include "ruby/version.h"
#include "ruby/internal/abi.h"

#ifndef RUBY_REVISION
#include "revision.h"

#ifndef TOKEN_PASTE
#define TOKEN_PASTE(x,y) x##y
#endif
Expand All @@ -38,6 +37,8 @@
#define RUBY_RELEASE_DAY_STR STRINGIZE(RUBY_RELEASE_DAY)
#endif

#endif

#ifdef RUBY_ABI_VERSION
# define RUBY_ABI_VERSION_SUFFIX "+"STRINGIZE(RUBY_ABI_VERSION)
#else
Expand All @@ -61,8 +62,4 @@
#define RUBY_PATCHLEVEL_STR ""
#endif

#ifndef RUBY_REVISION
# include "revision.h"
#endif

#endif /* RUBY_TOPLEVEL_VERSION_H */
2 changes: 2 additions & 0 deletions win32/Makefile.sub
Expand Up @@ -1244,6 +1244,8 @@ $(RCFILES): $(RBCONFIG) $(srcdir)/revision.h $(srcdir)/win32/resource.rb
-so_name=$(RUBY_SO_NAME) \
. $(icondirs) $(win_srcdir)

$(srcdir)/revision.h: $(REVISION_H)

update-benchmark-driver:
$(GIT) clone https://github.com/benchmark-driver/benchmark-driver $(srcdir)/benchmark/benchmark-driver || \
$(GIT) -C $(srcdir)/benchmark/benchmark-driver pull origin master
Expand Down
1 change: 1 addition & 0 deletions win32/ifchange.bat
Expand Up @@ -89,6 +89,7 @@ if exist %dest% (
)
)
for %%I in (%1) do echo %%~I updated
del /f %dest%
copy %src% %dest% > nul
del %src%

Expand Down

0 comments on commit 38a7a13

Please sign in to comment.