Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The document was converted from Japanese by https://www.deepl.com/.
- Loading branch information
Showing
6 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# mruby-bin-utils | ||
|
||
This is a command line program that serves as a wrapper to compile and link source code requiring libmruby. | ||
|
||
The name is a play on GNU Binutils. | ||
|
||
## How to build and use it | ||
|
||
Add this `mruby-bin-utils` to your build configuration file. | ||
|
||
```ruby | ||
# in your build configuration file | ||
|
||
MRuby::Build.new do |conf| | ||
... | ||
conf.gem core: "mruby-bin-utils" | ||
... | ||
end | ||
``` | ||
|
||
Building with this configuration will generate several executable programs. | ||
To each of those programs, you can add switches depending on the corresponding source code and compiler. | ||
|
||
For example, `mruby-cc` takes C source code and generates an executable file. | ||
If the toolchain used to build mruby is "gcc", you can use the gcc switches, and if the toolchain is "visualcpp", you can use the VisualC++ switches. | ||
|
||
For mruby built with the gcc toolchain, you can use `mruby-cc` as follows | ||
|
||
```console | ||
$ bin/mruby-cc yourcode.c # Compile to generate a.out | ||
$ bin/mruby-cc -o exec yourcode.c # Compile to generate exec | ||
$ bin/mruby-cc -o exec yourcode.c -lzip # Compile and link with libzip to generate exec | ||
``` | ||
|
||
## Generated programs and corresponding files | ||
|
||
| Program name | File type | ||
| ----------------- | -------- | ||
| `bin/mruby-cc` | C source file | ||
| `bin/mruby-c++` | C++ source file | ||
| `bin/mruby-as` | Assembly source file | ||
| `bin/mruby-objc` | Objective-C source file | ||
| `bin/mruby-ld` | Object file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "pathname" | ||
|
||
cc_command = File.join(ENV["BUILD_DIR"], "bin/mruby-cc") | ||
testdata = Pathname(__dir__).parent + "testdata" | ||
|
||
assert "C compile" do | ||
system *%W(#{cc_command} #{testdata + "test1.c"}) | ||
assert_true $?.success? | ||
end | ||
|
||
assert "C compile (failing)" do | ||
system *%W(#{cc_command} #{testdata + "test2.c"}), err: File::NULL | ||
assert_false $?.success? | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
MRuby::Gem::Specification.new("mruby-bin-utils") do |spec| | ||
spec.summary = "one-shot mruby C/C++ compiler and utilities" | ||
spec.license = "MIT" | ||
spec.author = "mruby developers" | ||
spec.add_dependency "mruby-bin-config", core: "mruby-bin-config" | ||
|
||
if ENV["OS"] =~ /windows/i | ||
msvc = build.toolchains.find { |e| e == "visualcpp" } ? true : false | ||
binext = ".bat" | ||
bingen = ->(bin, cflags, ldflags) { | ||
(cflags, ldflags) = [cflags, ldflags].map { |flags| | ||
flags.map { |f| | ||
varname = %(mrbc_#{f.sub(/^--/, "").gsub(/[^\w]+/, "_")}) | ||
extractor = %(for /f "usebackq delims=" %%i in (`%mrbconfig% #{f}`) do set #{varname}=%%i) | ||
["%#{varname}%", extractor] | ||
} | ||
} | ||
|
||
withlink = "/link " if msvc && bin != "mruby-ld" | ||
|
||
<<~CODE | ||
@echo off | ||
if "%1" == "" ( | ||
\techo %~n0: no files given 1>&2 | ||
\texit /b 1 | ||
) | ||
set mrbconfig="%~dp0.\\mruby-config.bat" | ||
#{cflags.map { |v, e| e }.join("\n")} | ||
#{ldflags.map { |v, e| e }.join("\n")} | ||
call #{cflags.map { |v, e| v }.join(" ")} %* #{withlink}#{ldflags.map { |v, e| v }.join(" ")} | ||
CODE | ||
} | ||
else | ||
binext = "" | ||
bingen = ->(bin, cflags, ldflags) { | ||
<<~CODE | ||
#!/bin/sh | ||
if [ "$#" -lt 1 ] | ||
then | ||
\techo "$(basename "$0"): no files given" 1>&2 | ||
\texit 1 | ||
fi | ||
mrbconfig="$(dirname "$0")/mruby-config" | ||
$("$mrbconfig" #{cflags.join(" ")}) "$@" $("$mrbconfig" #{ldflags.join(" ")}) | ||
CODE | ||
} | ||
end | ||
|
||
if spec.build.kind_of?(MRuby::CrossBuild) | ||
destdir = "#{build.build_dir}/host-bin" | ||
addbin = ->(dest) { build.products << dest } | ||
else | ||
destdir = "#{build.build_dir}/bin" | ||
addbin = ->(dest) { build.bins << File.basename(dest) } | ||
end | ||
|
||
[ | ||
["mruby-cc", %w(--cc --cflags), %w(--ldflags --ldflags-before-libs --libs)], | ||
["mruby-c++", %w(--cxx --cxxflags), %w(--ldflags --ldflags-before-libs --libs)], | ||
["mruby-as", %w(--as --asflags), %w(--ldflags --ldflags-before-libs --libs)], | ||
["mruby-objc", %w(--objc --objcflags), %w(--ldflags --ldflags-before-libs --libs)], | ||
["mruby-ld", %w(--ld), %w(--ldflags --ldflags-before-libs --libs)], | ||
].each do |bin, cflags, ldflags| | ||
destpath = File.join(destdir, "#{bin}#{binext}") | ||
|
||
addbin.call(destpath) | ||
|
||
file destpath => __FILE__ do |t| | ||
_pp "GEN", destpath.relative_path | ||
mkdir_p destdir | ||
|
||
# NOTE: No line break code is specified, so text appropriate to the environment is output | ||
File.write(destpath, bingen.call(bin, cflags, ldflags), perm: 0755) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <mruby.h> | ||
|
||
int | ||
main(int argc, char **argv) | ||
{ | ||
mrb_state *mrb = mrb_open(); | ||
mrb_close(mrb); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#error "INTENTIONAL ERROR FOR FAILING!" |