Ruby  3.4.0dev (2024-11-05 revision 348a53415339076afc4a02fcd09f3ae36e9c4c61)
version.c (348a53415339076afc4a02fcd09f3ae36e9c4c61)
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 "ruby/ruby.h"
15 #include "version.h"
16 #include "vm_core.h"
17 #include "rjit.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 
47 const int ruby_api_version[] = {
51 };
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 const char ruby_version[] = RUBY_VERSION;
65 const char ruby_revision[] = RUBY_FULL_REVISION;
66 const char ruby_release_date[] = RUBY_RELEASE_DATE;
67 const char ruby_platform[] = RUBY_PLATFORM;
68 const int ruby_patchlevel = RUBY_PATCHLEVEL;
69 const char ruby_description[] =
70  "ruby " RUBY_VERSION RUBY_PATCHLEVEL_STR " "
71  "(" RUBY_RELEASE_DATETIME RUBY_REVISION_STR ") "
72  "[" RUBY_PLATFORM "]";
73 static const int ruby_description_opt_point =
74  (int)(sizeof(ruby_description) - sizeof(" [" RUBY_PLATFORM "]"));
75 
76 const char ruby_copyright[] = "ruby - Copyright (C) "
77  RUBY_BIRTH_YEAR_STR "-" RUBY_RELEASE_YEAR_STR " "
79 const char ruby_engine[] = "ruby";
80 
81 // Might change after initialization
82 const char *rb_dynamic_description = ruby_description;
83 
85 void
86 Init_version(void)
87 {
88  enum {ruby_patchlevel = RUBY_PATCHLEVEL};
89  VALUE version = MKSTR(version);
90  VALUE ruby_engine_name = MKSTR(engine);
91  // MKSTR macro is a marker for fake.rb
92 
93  /*
94  * The running version of ruby
95  */
96  rb_define_global_const("RUBY_VERSION", /* MKSTR(version) */ version);
97  /*
98  * The date this ruby was released
99  */
100  rb_define_global_const("RUBY_RELEASE_DATE", MKSTR(release_date));
101  /*
102  * The platform for this ruby
103  */
104  rb_define_global_const("RUBY_PLATFORM", MKSTR(platform));
105  /*
106  * The patchlevel for this ruby. If this is a development build of ruby
107  * the patchlevel will be -1
108  */
109  rb_define_global_const("RUBY_PATCHLEVEL", MKINT(patchlevel));
110  /*
111  * The GIT commit hash for this ruby.
112  */
113  rb_define_global_const("RUBY_REVISION", MKSTR(revision));
114  /*
115  * The copyright string for ruby
116  */
117  rb_define_global_const("RUBY_COPYRIGHT", MKSTR(copyright));
118  /*
119  * The engine or interpreter this ruby uses.
120  */
121  rb_define_global_const("RUBY_ENGINE", /* MKSTR(engine) */ ruby_engine_name);
122  ruby_set_script_name(ruby_engine_name);
123  /*
124  * The version of the engine or interpreter this ruby uses.
125  */
126  rb_define_global_const("RUBY_ENGINE_VERSION", /* MKSTR(version) */ version);
127 
128  rb_provide("ruby2_keywords.rb");
129 }
130 
131 #if USE_RJIT
132 #define RJIT_OPTS_ON opt->rjit.on
133 #else
134 #define RJIT_OPTS_ON 0
135 #endif
136 
137 #if USE_YJIT
138 #define YJIT_OPTS_ON opt->yjit
139 #else
140 #define YJIT_OPTS_ON 0
141 #endif
142 
143 int ruby_mn_threads_enabled;
144 
145 #ifndef RB_DEFAULT_PARSER
146 #define RB_DEFAULT_PARSER RB_DEFAULT_PARSER_PRISM
147 #endif
148 static ruby_default_parser_enum default_parser = RB_DEFAULT_PARSER;
149 
150 ruby_default_parser_enum
151 rb_ruby_default_parser(void)
152 {
153  return default_parser;
154 }
155 
156 void
157 rb_ruby_default_parser_set(ruby_default_parser_enum parser)
158 {
159  default_parser = parser;
160 }
161 
162 static void
163 define_ruby_description(const char *const jit_opt)
164 {
165  static char desc[
166  sizeof(ruby_description)
167  + rb_strlen_lit(YJIT_DESCRIPTION)
168  + rb_strlen_lit(" +MN")
169  + rb_strlen_lit(" +PRISM")
170  ];
171 
172  int n = ruby_description_opt_point;
173  memcpy(desc, ruby_description, n);
174 # define append(s) (n += (int)strlcpy(desc + n, s, sizeof(desc) - n))
175  if (*jit_opt) append(jit_opt);
176  RUBY_ASSERT(n <= ruby_description_opt_point + (int)rb_strlen_lit(YJIT_DESCRIPTION));
177  if (ruby_mn_threads_enabled) append(" +MN");
178  if (rb_ruby_prism_p()) append(" +PRISM");
179  append(ruby_description + ruby_description_opt_point);
180 # undef append
181 
182  VALUE description = rb_obj_freeze(rb_usascii_str_new_static(desc, n));
183  rb_dynamic_description = desc;
184 
185  /*
186  * The full ruby version string, like <tt>ruby -v</tt> prints
187  */
188  rb_define_global_const("RUBY_DESCRIPTION", /* MKSTR(description) */ description);
189 }
190 
191 void
192 Init_ruby_description(ruby_cmdline_options_t *opt)
193 {
194  const char *const jit_opt =
195  RJIT_OPTS_ON ? " +RJIT" :
196  YJIT_OPTS_ON ? YJIT_DESCRIPTION :
197  "";
198  define_ruby_description(jit_opt);
199 }
200 
201 void
202 ruby_set_yjit_description(void)
203 {
204  rb_const_remove(rb_cObject, rb_intern("RUBY_DESCRIPTION"));
205  define_ruby_description(YJIT_DESCRIPTION);
206 }
207 
208 void
210 {
211  puts(rb_dynamic_description);
212 
213 #ifdef RUBY_LAST_COMMIT_TITLE
214  fputs("last_commit=" RUBY_LAST_COMMIT_TITLE, stdout);
215 #endif
216 #ifdef HAVE_MALLOC_CONF
217  if (malloc_conf) printf("malloc_conf=%s\n", malloc_conf);
218 #endif
219  fflush(stdout);
220 }
221 
222 void
224 {
225  PRINT(copyright);
226  fflush(stdout);
227 }
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition: assert.h:219
void ruby_set_script_name(VALUE name)
Identical to ruby_script(), except it takes the name as a Ruby String instance.
Definition: ruby.c:3021
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition: object.c:1258
void ruby_show_copyright(void)
Prints the copyright notice of the CRuby interpreter to stdout.
Definition: version.c:223
void ruby_show_version(void)
Prints the version information of the CRuby interpreter to stdout.
Definition: version.c:209
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
Definition: load.c:714
#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:1123
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:3267
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition: symbol.c:823
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:3727
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:79
#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:67
const char ruby_version[]
Stringised version.
Definition: version.c:64
#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:76
const char ruby_release_date[]
Date of release, in a C string.
Definition: version.c:66
const int ruby_patchlevel
This is a monotonic increasing integer that describes specific "patch" level.
Definition: version.c:68
const char ruby_description[]
This is what ruby -v prints to the standard error.
Definition: version.c:69
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40