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