Ruby 3.5.0dev (2025-08-16 revision 87c4ebd00115b9424f04813b4dd5f3d087a14eb9)
version.c (87c4ebd00115b9424f04813b4dd5f3d087a14eb9)
1/**********************************************************************
2
3 version.c -
4
5 $Author$
6 created at: Thu Sep 30 20:08:01 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#include "internal/cmdlineopt.h"
13#include "internal/parse.h"
14#include "internal/gc.h"
15#include "ruby/ruby.h"
16#include "version.h"
17#include "vm_core.h"
18#include "yjit.h"
19#include <stdio.h>
20
21#ifndef EXIT_SUCCESS
22#define EXIT_SUCCESS 0
23#endif
24
25#ifdef RUBY_REVISION
26# if RUBY_PATCHLEVEL == -1
27# define RUBY_API_VERSION_NAME "master"
28# ifndef RUBY_BRANCH_NAME
29# define RUBY_BRANCH_NAME RUBY_API_VERSION_NAME
30# endif
31# define RUBY_REVISION_STR " "RUBY_BRANCH_NAME" "RUBY_REVISION
32# else
33# define RUBY_API_VERSION_NAME RUBY_API_VERSION_STR
34# define RUBY_REVISION_STR " revision "RUBY_REVISION
35# endif
36#else
37# define RUBY_REVISION "HEAD"
38# define RUBY_REVISION_STR ""
39#endif
40#if !defined RUBY_RELEASE_DATETIME || RUBY_PATCHLEVEL != -1
41# undef RUBY_RELEASE_DATETIME
42# define RUBY_RELEASE_DATETIME RUBY_RELEASE_DATE
43#endif
44
45#define PRINT(type) puts(ruby_##type)
46#define MKSTR(type) rb_obj_freeze(rb_usascii_str_new_static(ruby_##type, sizeof(ruby_##type)-1))
47#define MKINT(name) INT2FIX(ruby_##name)
48
49#define RUBY_API_VERSION_STR \
50 STRINGIZE(RUBY_API_VERSION_MAJOR) "." \
51 STRINGIZE(RUBY_API_VERSION_MINOR) "." \
52 ""
58#define RUBY_VERSION \
59 STRINGIZE(RUBY_VERSION_MAJOR) "." \
60 STRINGIZE(RUBY_VERSION_MINOR) "." \
61 STRINGIZE(RUBY_VERSION_TEENY) ""
62#ifndef RUBY_FULL_REVISION
63# define RUBY_FULL_REVISION RUBY_REVISION
64#endif
65#ifdef YJIT_SUPPORT
66#define YJIT_DESCRIPTION " +YJIT " STRINGIZE(YJIT_SUPPORT)
67#else
68#define YJIT_DESCRIPTION " +YJIT"
69#endif
70#ifdef ZJIT_SUPPORT
71#define ZJIT_DESCRIPTION " +ZJIT " STRINGIZE(ZJIT_SUPPORT)
72#else
73#define ZJIT_DESCRIPTION " +ZJIT"
74#endif
75#if USE_MODULAR_GC
76#define GC_DESCRIPTION " +GC"
77#else
78#define GC_DESCRIPTION ""
79#endif
80const char ruby_version[] = RUBY_VERSION;
81const char ruby_revision[] = RUBY_FULL_REVISION;
82const char ruby_release_date[] = RUBY_RELEASE_DATE;
83const char ruby_platform[] = RUBY_PLATFORM;
84const int ruby_patchlevel = RUBY_PATCHLEVEL;
85const char ruby_api_version_name[] = RUBY_API_VERSION_NAME;
86const char ruby_description[] =
87 "ruby " RUBY_VERSION RUBY_PATCHLEVEL_STR " "
88 "(" RUBY_RELEASE_DATETIME RUBY_REVISION_STR ") "
89 "[" RUBY_PLATFORM "]";
90static const int ruby_description_opt_point =
91 (int)(sizeof(ruby_description) - sizeof(" [" RUBY_PLATFORM "]"));
92
93const char ruby_copyright[] = "ruby - Copyright (C) "
94 RUBY_BIRTH_YEAR_STR "-" RUBY_RELEASE_YEAR_STR " "
96const char ruby_engine[] = "ruby";
97
98// Might change after initialization
99const char *rb_dynamic_description = ruby_description;
100
101static inline void
102define_ruby_const(VALUE mod, const char *name, VALUE value, bool toplevel)
103{
104 if (toplevel) {
105 rb_define_global_const(name, value);
106 name += rb_strlen_lit("RUBY_");
107 }
108 rb_define_const(mod, name, value);
109}
110
111/* RDoc needs rb_define_const */
112#define rb_define_const(mod, name, value) \
113 define_ruby_const(mod, (mod == mRuby ? "RUBY_" name : name), value, (mod == mRuby))
114
116void
117Init_version(void)
118{
119 /*
120 * The Ruby[rdoc-ref:Ruby] module that contains portable information among
121 * implementations.
122 *
123 * The constants defined here are aliased in the toplevel with
124 * +RUBY_+ prefix.
125 */
126 VALUE mRuby = rb_define_module("Ruby");
127
128 enum {ruby_patchlevel = RUBY_PATCHLEVEL};
129 VALUE version = MKSTR(version);
130 VALUE ruby_engine_name = MKSTR(engine);
131 // MKSTR macro is a marker for fake.rb
132
133 /*
134 * The running version of ruby
135 */
136 rb_define_const(mRuby, "VERSION", /* MKSTR(version) */ version);
137 /*
138 * The date this ruby was released
139 */
140 rb_define_const(mRuby, "RELEASE_DATE", MKSTR(release_date));
141 /*
142 * The platform for this ruby
143 */
144 rb_define_const(mRuby, "PLATFORM", MKSTR(platform));
145 /*
146 * The patchlevel for this ruby. If this is a development build of ruby
147 * the patchlevel will be -1
148 */
149 rb_define_const(mRuby, "PATCHLEVEL", MKINT(patchlevel));
150 /*
151 * The GIT commit hash for this ruby.
152 */
153 rb_define_const(mRuby, "REVISION", MKSTR(revision));
154 /*
155 * The copyright string for ruby
156 */
157 rb_define_const(mRuby, "COPYRIGHT", MKSTR(copyright));
158 /*
159 * The engine or interpreter this ruby uses.
160 */
161 rb_define_const(mRuby, "ENGINE", /* MKSTR(engine) */ ruby_engine_name);
162 ruby_set_script_name(ruby_engine_name);
163 /*
164 * The version of the engine or interpreter this ruby uses.
165 */
166 rb_define_const(mRuby, "ENGINE_VERSION", /* MKSTR(version) */ version);
167
168 rb_provide("ruby2_keywords.rb");
169}
170
171#if USE_YJIT
172#define YJIT_OPTS_ON opt->yjit
173#else
174#define YJIT_OPTS_ON 0
175#endif
176
177#if USE_ZJIT
178#define ZJIT_OPTS_ON opt->zjit
179#else
180#define ZJIT_OPTS_ON 0
181#endif
182
183int ruby_mn_threads_enabled;
184
185#ifndef RB_DEFAULT_PARSER
186#define RB_DEFAULT_PARSER RB_DEFAULT_PARSER_PRISM
187#endif
188static ruby_default_parser_enum default_parser = RB_DEFAULT_PARSER;
189
190ruby_default_parser_enum
191rb_ruby_default_parser(void)
192{
193 return default_parser;
194}
195
196void
197rb_ruby_default_parser_set(ruby_default_parser_enum parser)
198{
199 default_parser = parser;
200}
201
202static void
203define_ruby_description(const char *const jit_opt)
204{
205#define JIT_DESCRIPTION YJIT_DESCRIPTION ZJIT_DESCRIPTION
206
207 static char desc[
208 sizeof(ruby_description)
209 + rb_strlen_lit(JIT_DESCRIPTION)
210 + rb_strlen_lit(" +MN")
211 + rb_strlen_lit(" +PRISM")
212#if USE_MODULAR_GC
213 + rb_strlen_lit(GC_DESCRIPTION)
214 // Assume the active GC name can not be longer than 20 chars
215 // so that we don't have to use strlen and remove the static
216 // qualifier from desc.
217 + RB_GC_MAX_NAME_LEN + 3
218#endif
219
220 ];
221
222 int n = ruby_description_opt_point;
223 memcpy(desc, ruby_description, n);
224# define append(s) (n += (int)strlcpy(desc + n, s, sizeof(desc) - n))
225 if (*jit_opt) append(jit_opt);
226 RUBY_ASSERT(n <= ruby_description_opt_point + (int)rb_strlen_lit(JIT_DESCRIPTION));
227 if (ruby_mn_threads_enabled) append(" +MN");
228 if (rb_ruby_prism_p()) append(" +PRISM");
229#if USE_MODULAR_GC
230 append(GC_DESCRIPTION);
231 if (rb_gc_modular_gc_loaded_p()) {
232 append("[");
233 append(rb_gc_active_gc_name());
234 append("]");
235 }
236#endif
237 append(ruby_description + ruby_description_opt_point);
238# undef append
239
240 VALUE mRuby = rb_path2class("Ruby");
241 VALUE description = rb_obj_freeze(rb_usascii_str_new_static(desc, n));
242 rb_dynamic_description = desc;
243
244 /*
245 * The full ruby version string, like <tt>ruby -v</tt> prints
246 */
247 rb_define_const(mRuby, "DESCRIPTION", /* MKSTR(description) */ description);
248#undef JIT_DESCRIPTION
249}
250
251void
252Init_ruby_description(ruby_cmdline_options_t *opt)
253{
254 const char *const jit_opt =
255 YJIT_OPTS_ON ? YJIT_DESCRIPTION :
256 ZJIT_OPTS_ON ? ZJIT_DESCRIPTION :
257 "";
258 define_ruby_description(jit_opt);
259}
260
261void
262ruby_set_yjit_description(void)
263{
264 VALUE mRuby = rb_path2class("Ruby");
265 rb_const_remove(rb_cObject, rb_intern("RUBY_DESCRIPTION"));
266 rb_const_remove(mRuby, rb_intern("DESCRIPTION"));
267 define_ruby_description(YJIT_DESCRIPTION);
268}
269
270void
272{
273 puts(rb_dynamic_description);
274
275#ifdef RUBY_LAST_COMMIT_TITLE
276 fputs("last_commit=" RUBY_LAST_COMMIT_TITLE, stdout);
277#endif
278#ifdef HAVE_MALLOC_CONF
279 if (malloc_conf) printf("malloc_conf=%s\n", malloc_conf);
280#endif
281 fflush(stdout);
282}
283
284void
286{
287 PRINT(copyright);
288 fflush(stdout);
289}
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1592
void ruby_set_script_name(VALUE name)
Identical to ruby_script(), except it takes the name as a Ruby String instance.
Definition ruby.c:3056
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition object.c:1297
void ruby_show_copyright(void)
Prints the copyright notice of the CRuby interpreter to stdout.
Definition version.c:285
void ruby_show_version(void)
Prints the version information of the CRuby interpreter to stdout.
Definition version.c:271
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
Definition load.c:767
#define rb_strlen_lit(str)
Length of a string literal.
Definition string.h:1692
VALUE rb_usascii_str_new_static(const char *ptr, long len)
Identical to rb_str_new_static(), except it generates a string of "US ASCII" encoding instead of "bin...
Definition string.c:1170
VALUE rb_path2class(const char *path)
Resolves a Q::W::E::R-style path string to the actual class it points.
Definition variable.c:492
VALUE rb_const_remove(VALUE space, ID name)
Identical to rb_mod_remove_const(), except it takes the name as ID instead of VALUE.
Definition variable.c:3713
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
Definition variable.c:4171
const int ruby_api_version[3]
API versions, in { major, minor, teeny } order.
Definition version.c:53
const char ruby_engine[]
This is just "ruby" for us.
Definition version.c:96
#define RUBY_API_VERSION_TEENY
Teeny version.
Definition version.h:76
const char ruby_platform[]
Target platform identifier, in a C string.
Definition version.c:83
const char ruby_version[]
Stringised version.
Definition version.c:80
#define RUBY_API_VERSION_MAJOR
Major version.
Definition version.h:64
#define RUBY_API_VERSION_MINOR
Minor version.
Definition version.h:70
#define RUBY_AUTHOR
Author of this project.
Definition version.h:32
const char ruby_copyright[]
Copyright notice.
Definition version.c:93
const char ruby_release_date[]
Date of release, in a C string.
Definition version.c:82
const int ruby_patchlevel
This is a monotonic increasing integer that describes specific "patch" level.
Definition version.c:84
const char ruby_description[]
This is what ruby -v prints to the standard error.
Definition version.c:86
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40