Debian Codegraph @

ext/prism/extconf.rb

raw symbols SCIP
1 # frozen_string_literal: true
2
3 require "rbconfig"
4
5 if ARGV.delete("--help")
6 print(<<~TEXT)
7 USAGE: ruby #{$PROGRAM_NAME} [options]
8
9 Flags that are always valid:
10
11 --enable-build-debug
12 Enable debug build.
13 You may also set the PRISM_BUILD_DEBUG environment variable.
14
15 --enable-build-minimal
16 Enable minimal build.
17 You may also set the PRISM_BUILD_MINIMAL environment variable.
18
19 --help
20 Display this message.
21
22 Environment variables used:
23
24 PRISM_BUILD_DEBUG
25 Equivalent to `--enable-build-debug` when set, even if nil or blank.
26
27 PRISM_BUILD_MINIMAL
28 Equivalent to `--enable-build-minimal` when set, even if nil or blank.
29
30 TEXT
31 exit!(0)
32 end
33
34 # If this gem is being build from a git source, then we need to run
35 # templating if it hasn't been run yet. In normal packaging, we would have
36 # shipped the templated files with the gem, so this wouldn't be necessary.
37 def generate_templates
38 Dir.chdir(File.expand_path("../..", __dir__)) do
39 if !File.exist?("include/prism/ast.h") && Dir.exist?(".git")
40 system(RbConfig.ruby, "templates/template.rb", exception: true)
41 end
42 end
43 end
44
45 # Runs `make` in the root directory of the project. Note that this is the
46 # `Makefile` for the overall project, not the `Makefile` that is being generated
47 # by this script.`
48 def make(env, target)
49 puts "Running make #{target} with #{env.inspect}"
50 Dir.chdir(File.expand_path("../..", __dir__)) do
51 system(
52 env,
53 RUBY_PLATFORM.match?(/openbsd|freebsd/) ? "gmake" : "make",
54 target,
55 exception: true
56 )
57 end
58 end
59
60 # On non-CRuby we only need the shared library since we'll interface with it
61 # through FFI, so we'll build only that and not the C extension. We also avoid
62 # `require "mkmf"` as that prepends the GraalVM LLVM toolchain to PATH on TruffleRuby < 24.0,
63 # but we want to use the system toolchain here since libprism is run natively.
64 if RUBY_ENGINE != "ruby"
65 generate_templates
66 soext = RbConfig::CONFIG["SOEXT"]
67 # Pass SOEXT to avoid an extra subprocess just to query that
68 make({ "SOEXT" => soext }, "build/libprism.#{soext}")
69 File.write("Makefile", "all install clean:\n\t@#{RbConfig::CONFIG["NULLCMD"]}\n")
70 return
71 end
72
73 require "mkmf"
74
75 # First, ensure that we can find the header for the prism library.
76 generate_templates # Templates should be generated before find_header.
77 unless find_header("prism.h", File.expand_path("../../include", __dir__))
78 raise "prism.h is required"
79 end
80
81 # Next, ensure we can find the header for the C extension. Explicitly look for
82 # the extension header in the parent directory because we want to consistently
83 # look for `prism/extension.h` in our source files to line up with our mirroring
84 # in CRuby.
85 unless find_header("prism/extension.h", File.expand_path("..", __dir__))
86 raise "prism/extension.h is required"
87 end
88
89 # If `--enable-build-debug` is passed to this script or the
90 # `PRISM_BUILD_DEBUG` environment variable is defined, we'll build with the
91 # `PRISM_BUILD_DEBUG` macro defined. This causes parse functions to
92 # duplicate their input so that they have clearly set bounds, which is useful
93 # for finding bugs that cause the parser to read off the end of the input.
94 if enable_config("build-debug", ENV["PRISM_BUILD_DEBUG"] || false)
95 append_cflags("-DPRISM_BUILD_DEBUG")
96 end
97
98 # If `--enable-build-minimal` is passed to this script or the
99 # `PRISM_BUILD_MINIMAL` environment variable is defined, we'll build with the
100 # set of defines that comprise the minimal set. This causes the parser to be
101 # built with minimal features, necessary for stripping out functionality when
102 # the size of the final built artifact is a concern.
103 if enable_config("build-minimal", ENV["PRISM_BUILD_MINIMAL"] || false)
104 append_cflags("-DPRISM_BUILD_MINIMAL")
105 end
106
107 # By default, all symbols are hidden in the shared library.
108 append_cflags("-fvisibility=hidden")
109
110 def src_list(path)
111 srcdir = path.dup
112 RbConfig.expand(srcdir) # mutates srcdir :-/
113 Dir[File.join(srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
114 end
115
116 def add_libprism_source(path)
117 $VPATH << path
118 src_list path
119 end
120
121 $srcs = src_list("$(srcdir)") +
122 add_libprism_source("$(srcdir)/../../src") +
123 add_libprism_source("$(srcdir)/../../src/util")
124
125 # Finally, we'll create the `Makefile` that is going to be used to configure and
126 # build the C extension.
127 create_makefile("prism/prism")