Ruby: version.c Source File

Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)

version.c
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 "ruby/ruby.h"
14#include "version.h"
15#include "vm_core.h"
16#include "rjit.h"
17#include "yjit.h"
18#include <stdio.h>
19
20#ifndef EXIT_SUCCESS
21#define EXIT_SUCCESS 0
22#endif
23
24#ifdef RUBY_REVISION
25# if RUBY_PATCHLEVEL == -1
26# ifndef RUBY_BRANCH_NAME
27# define RUBY_BRANCH_NAME "master"
28# endif
29# define RUBY_REVISION_STR " "RUBY_BRANCH_NAME" "RUBY_REVISION
30# else
31# define RUBY_REVISION_STR " revision "RUBY_REVISION
32# endif
33#else
34# define RUBY_REVISION "HEAD"
35# define RUBY_REVISION_STR ""
36#endif
37#if !defined RUBY_RELEASE_DATETIME || RUBY_PATCHLEVEL != -1
38# undef RUBY_RELEASE_DATETIME
39# define RUBY_RELEASE_DATETIME RUBY_RELEASE_DATE
40#endif
41
42#define PRINT(type) puts(ruby_##type)
43#define MKSTR(type) rb_obj_freeze(rb_usascii_str_new_static(ruby_##type, sizeof(ruby_##type)-1))
44#define MKINT(name) INT2FIX(ruby_##name)
45
46 const int ruby_api_version[] = {
47 RUBY_API_VERSION_MAJOR,
48 RUBY_API_VERSION_MINOR,
49 RUBY_API_VERSION_TEENY,
50};
51#define RUBY_VERSION \
52 STRINGIZE(RUBY_VERSION_MAJOR) "." \
53 STRINGIZE(RUBY_VERSION_MINOR) "." \
54 STRINGIZE(RUBY_VERSION_TEENY) ""
55#ifndef RUBY_FULL_REVISION
56# define RUBY_FULL_REVISION RUBY_REVISION
57#endif
58#ifdef YJIT_SUPPORT
59#define YJIT_DESCRIPTION " +YJIT " STRINGIZE(YJIT_SUPPORT)
60#else
61#define YJIT_DESCRIPTION " +YJIT"
62#endif
63 const char ruby_version[] = RUBY_VERSION;
64const char ruby_revision[] = RUBY_FULL_REVISION;
65 const char ruby_release_date[] = RUBY_RELEASE_DATE;
66 const char ruby_platform[] = RUBY_PLATFORM;
67 const int ruby_patchlevel = RUBY_PATCHLEVEL;
68 const char ruby_description[] =
69 "ruby " RUBY_VERSION RUBY_PATCHLEVEL_STR " "
70 "(" RUBY_RELEASE_DATETIME RUBY_REVISION_STR ") "
71 "[" RUBY_PLATFORM "]";
72static const int ruby_description_opt_point =
73 (int)(sizeof(ruby_description) - sizeof(" [" RUBY_PLATFORM "]"));
74
75 const char ruby_copyright[] = "ruby - Copyright (C) "
76 RUBY_BIRTH_YEAR_STR "-" RUBY_RELEASE_YEAR_STR " "
77 RUBY_AUTHOR;
78 const char ruby_engine[] = "ruby";
79
80// Might change after initialization
81const char *rb_dynamic_description = ruby_description;
82
84void
85Init_version(void)
86{
87 enum {ruby_patchlevel = RUBY_PATCHLEVEL};
88 VALUE version = MKSTR(version);
89 VALUE ruby_engine_name = MKSTR(engine);
90 // MKSTR macro is a marker for fake.rb
91
92 /*
93 * The running version of ruby
94 */
95 rb_define_global_const("RUBY_VERSION", /* MKSTR(version) */ version);
96 /*
97 * The date this ruby was released
98 */
99 rb_define_global_const("RUBY_RELEASE_DATE", MKSTR(release_date));
100 /*
101 * The platform for this ruby
102 */
103 rb_define_global_const("RUBY_PLATFORM", MKSTR(platform));
104 /*
105 * The patchlevel for this ruby. If this is a development build of ruby
106 * the patchlevel will be -1
107 */
108 rb_define_global_const("RUBY_PATCHLEVEL", MKINT(patchlevel));
109 /*
110 * The GIT commit hash for this ruby.
111 */
112 rb_define_global_const("RUBY_REVISION", MKSTR(revision));
113 /*
114 * The copyright string for ruby
115 */
116 rb_define_global_const("RUBY_COPYRIGHT", MKSTR(copyright));
117 /*
118 * The engine or interpreter this ruby uses.
119 */
120 rb_define_global_const("RUBY_ENGINE", /* MKSTR(engine) */ ruby_engine_name);
121 ruby_set_script_name(ruby_engine_name);
122 /*
123 * The version of the engine or interpreter this ruby uses.
124 */
125 rb_define_global_const("RUBY_ENGINE_VERSION", /* MKSTR(version) */ version);
126
127 rb_provide("ruby2_keywords.rb");
128}
129
130#if USE_RJIT
131#define RJIT_OPTS_ON opt->rjit.on
132#else
133#define RJIT_OPTS_ON 0
134#endif
135
136#if USE_YJIT
137#define YJIT_OPTS_ON opt->yjit
138#else
139#define YJIT_OPTS_ON 0
140#endif
141
142int ruby_mn_threads_enabled;
143
144bool * rb_ruby_prism_ptr(void);
145
146static void
147define_ruby_description(const char *const jit_opt)
148{
149 static char desc[
150 sizeof(ruby_description)
151 + rb_strlen_lit(YJIT_DESCRIPTION)
152 + rb_strlen_lit(" +MN")
153 + rb_strlen_lit(" +PRISM")
154 ];
155
156 const char *const threads_opt = ruby_mn_threads_enabled ? " +MN" : "";
157 const char *const parser_opt = (*rb_ruby_prism_ptr()) ? " +PRISM" : "";
158
159 int n = snprintf(desc, sizeof(desc),
160 "%.*s"
161 "%s" // jit_opt
162 "%s" // threads_opts
163 "%s" // parser_opt
164 "%s",
165 ruby_description_opt_point, ruby_description,
166 jit_opt,
167 threads_opt,
168 parser_opt,
169 ruby_description + ruby_description_opt_point);
170
171 VALUE description = rb_obj_freeze(rb_usascii_str_new_static(desc, n));
172 rb_dynamic_description = desc;
173
174 /*
175 * The full ruby version string, like <tt>ruby -v</tt> prints
176 */
177 rb_define_global_const("RUBY_DESCRIPTION", /* MKSTR(description) */ description);
178}
179
180void
181Init_ruby_description(ruby_cmdline_options_t *opt)
182{
183 const char *const jit_opt =
184 RJIT_OPTS_ON ? " +RJIT" :
185 YJIT_OPTS_ON ? YJIT_DESCRIPTION :
186 "";
187 define_ruby_description(jit_opt);
188}
189
190void
191ruby_set_yjit_description(void)
192{
193 rb_const_remove(rb_cObject, rb_intern("RUBY_DESCRIPTION"));
194 define_ruby_description(YJIT_DESCRIPTION);
195}
196
197void
198ruby_show_version(void)
199{
200 puts(rb_dynamic_description);
201
202#ifdef RUBY_LAST_COMMIT_TITLE
203 fputs("last_commit=" RUBY_LAST_COMMIT_TITLE, stdout);
204#endif
205#ifdef HAVE_MALLOC_CONF
206 if (malloc_conf) printf("malloc_conf=%s\n", malloc_conf);
207#endif
208 fflush(stdout);
209}
210
211void
212ruby_show_copyright(void)
213{
214 PRINT(copyright);
215 fflush(stdout);
216}
ruby_set_script_name
void ruby_set_script_name(VALUE name)
Identical to ruby_script(), except it takes the name as a Ruby String instance.
Definition ruby.c:2883
ruby_show_copyright
void ruby_show_copyright(void)
Prints the copyright notice of the CRuby interpreter to stdout.
Definition version.c:212
ruby_show_version
void ruby_show_version(void)
Prints the version information of the CRuby interpreter to stdout.
Definition version.c:198
rb_provide
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
Definition load.c:714
rb_strlen_lit
#define rb_strlen_lit(str)
Length of a string literal.
Definition string.h:1692
rb_usascii_str_new_static
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:1003
rb_const_remove
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:3244
rb_define_global_const
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:3702
ruby_api_version
const int ruby_api_version[3]
API versions, in { major, minor, teeny } order.
Definition version.c:46
ruby_engine
const char ruby_engine[]
This is just "ruby" for us.
Definition version.c:78
RUBY_API_VERSION_TEENY
#define RUBY_API_VERSION_TEENY
Teeny version.
Definition version.h:76
ruby_platform
const char ruby_platform[]
Target platform identifier, in a C string.
Definition version.c:66
ruby_version
const char ruby_version[]
Stringised version.
Definition version.c:63
RUBY_API_VERSION_MAJOR
#define RUBY_API_VERSION_MAJOR
Major version.
Definition version.h:64
RUBY_API_VERSION_MINOR
#define RUBY_API_VERSION_MINOR
Minor version.
Definition version.h:70
RUBY_AUTHOR
#define RUBY_AUTHOR
Author of this project.
Definition version.h:32
ruby_copyright
const char ruby_copyright[]
Copyright notice.
Definition version.c:75
ruby_release_date
const char ruby_release_date[]
Date of release, in a C string.
Definition version.c:65
ruby_patchlevel
const int ruby_patchlevel
This is a monotonic increasing integer that describes specific "patch" level.
Definition version.c:67
ruby_description
const char ruby_description[]
This is what ruby -v prints to the standard error.
Definition version.c:68
ruby.h
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40