Slide 1

Slide 1 text

༻ mruby ိሜލฏ୆޻۩ John Lin (@johnlinvc)

Slide 2

Slide 2 text

᮫ԙզ • John Lin • Twitter: @johnlinvc • Senior Solution Architect, DevOps 
 @ West Pharmaceutical Service (୆ᖯҖ๏෰຿)

Slide 3

Slide 3 text

Devops ᕝᕝత Golang • ༗ᴍԀ᷺ྃ if err != nil • ૝ཁ௅ፌ༻ Ruby ိሜ֤छ޻۩ɻ • ୠੋ Ruby ༗Ұᴍ೉ᚙ䆋੒ᄸҰࣥߦ䈕ɻ

Slide 4

Slide 4 text

େߝ • ॄኄੋ mrubyɻ • mruby త࢖༻ํ๏ɻ • ༻ mruby-cli ိሜލฏ୆ mruby ఔࣜɻ • mruby-cli ӡ࡞ݪཧɻ

Slide 5

Slide 5 text

ॄኄੋ mruby • mruby ᢛ Ruby త᮫܎ • mruby తಛ৭ • mrbgems

Slide 6

Slide 6 text

mruby • ओཁ։ᚙऀੋ Ruby తᚙ໌ਓ matz • ໨લ࠷৽൛ຊੋ 3.0 • https://mruby.org

Slide 7

Slide 7 text

mruby ᢛ Ruby త᮫܎ • Ruby ༗Ұݸ ISO ඪ४ ISO/IEC 30170:2012 • େՈ࠷ৗ༻త Ruby ੋ MRIɼଖଞؐ༗ JRuby ౳౳ɻ • mruby ੋଖதҰݸመ࡞ɻ

Slide 8

Slide 8 text

mruby తಛ৭ • ओཁ໨ඪੋ༻ိቕೖࡏଖଞఔࣜதɻ • هԱᱪ༻ྔֱগɼ୞ཁ 100kbɻ(IRB ཁ 17 mb) • େ෦෼త㚎ݐവࣜݿ౎ੋબ഑ɻ

Slide 9

Slide 9 text

mrbgems • mruby ༻త౟݅؅ཧثɻ • ౟݅త㚎༰ՄҎੋ mruby ҃ੋ C extension ɻ • धཁࡏฤᩄ mruby త࣌ީबࢦఆ૝ཁత౟݅ɻ

Slide 10

Slide 10 text

҆᧋mruby • ಁա ruby-build ҆᧋ɻ • ኺݪ࢝ᛰฤᩄɻ • ။༗ࡾݸओཁࣥߦ䈕ɼmruby , mrbc, mirb

Slide 11

Slide 11 text

ಁա ruby-build ҆᧋ • ཁઌ᧋޷ฤᩄث (clang / gcc) • ruby-build mruby-3.0.0 ~/.local/mruby-3.0.0

Slide 12

Slide 12 text

ኺݪ࢝ᛰฤᩄ • ໵ཁઌ᧋޷ฤᩄث (clang / gcc) $ git clone https://github.com/mruby/mruby.git $ cd mruby $ rake

Slide 13

Slide 13 text

mruby త࢖༻ํ๏ • REPL • Source code • Bytecode • Bytecode in c

Slide 14

Slide 14 text

Hello world puts "hello world"

Slide 15

Slide 15 text

REPL (read-eval-print-loop) $ mirb mirb - Embeddable Interactive Ruby Shell > puts "hello world" hello world => nil

Slide 16

Slide 16 text

REPL • ༏ᴍ • ํศଌࢼ • ᠍ᴍ • جຊ্ෆೳᚙ䆋څଖଞผਓ༻ɻ

Slide 17

Slide 17 text

Source code $ cat > hello.rb <

Slide 18

Slide 18 text

Source code • ༏ᴍ • ࠷ख़ࣩత։ᚙྲྀఔ • ᠍ᴍ • mruby ࿨ఔࣜᛰੋ෼։తɼ༗Ұᴍ೉ᚙ෍ɻ • धཁ೺ఔࣜᛰڅ࢖༻ऀɻ

Slide 19

Slide 19 text

Bytecode $ cat > hello.rb <

Slide 20

Slide 20 text

Bytecode • ༏ᴍ • ఔࣜᛰෆ༻څผਓ • ᠍ᴍ • ൺֱෳᯑత։ᚙྲྀఔ • ؐੋཁᚙ෍ .mrb ࿨ mruby

Slide 21

Slide 21 text

Bytecode in C $ cat > hello.rb <

Slide 22

Slide 22 text

hello.c #include #ifdef __cplusplus extern const uint8_t hello_symbol[]; #endif const uint8_t hello_symbol[] = { 0x52,0x49,0x54,0x45,0x30,0x32,0x30,0x30,0x00,0x00,0x00,0x5c,0x4d,0x41,0x54,0x5a , 0x30,0x30,0x30,0x30,0x49,0x52,0x45,0x50,0x00,0x00,0x00,0x40,0x30,0x33,0x30,0x30 , 0x00,0x00,0x00,0x34,0x00,0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x0c,0x14,0x01, 0x51,0x02,0x00,0x2f,0x01,0x00,0x01,0x39,0x01,0x6b,0x00,0x01,0x00,0x00,0x0b,0x68, 0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,0x6c,0x64,0x00,0x00,0x01,0x00,0x04,0x70, 0x75,0x74,0x73,0x00,0x45,0x4e,0x44,0x00,0x00,0x00,0x00,0x08, };

Slide 23

Slide 23 text

Bytecode in C $ cat > hello_bin.c < #include #include "hello.c" int main(void) { mrb_state *mrb = mrb_open(); if (!mrb) { /* handle error */ } mrb_load_irep(mrb, hello_symbol); mrb_close(mrb); return 0; } HEAD

Slide 24

Slide 24 text

Bytecode in C $ gcc -std=c99 -Iinclude hello_bin.c -o hello build/host/lib/libmruby.a -lm $ ./hello hello world

Slide 25

Slide 25 text

Bytecode in C • ༏ᴍ • ෆ༻څݪ࢝ᛰ • ᄸҰࣥߦ䈕 • ᠍ᴍ • ྲྀఔෳᯑ • 㑌࣍౎ཁॏ৽ฤᩄ

Slide 26

Slide 26 text

mruby-cli • ॄኄੋ mruby-cli • ҆᧋ • ৽⃧ሢҊ • ฤᩄࣥߦ䈕 • ࢖༻ mrbgems • mirb, mtest ౳ีॿޭೳ

Slide 27

Slide 27 text

mruby-cli • ༝ Heroku త Terence Lee ։ᚙత mruby ฤᩄ޻۩ɻ • ՄҎҰ࣍ฤᩄग़ଟݸฏ୆తᄸҰࣥߦ䈕ɻ • ೥ٱࣦमɼ࠷ޙҰ࣍ commit ੋ 2017ɻ • զ೺ሏత૬ґ౟݅౎ߋ৽੒ 2021 ൛తɻݱࡏຢ။ಈྃɻ • https://github.com/johnlinvc/mruby-cli

Slide 28

Slide 28 text

mruby-cli ߋ৽႔ • https://github.com/johnlinvc/mruby-cli • ኺ mruby 1.2 ঋڃҝ mruby 3.0.0 • ࢧԉ࠷৽ macOS 14

Slide 29

Slide 29 text

҆᧋ • ௚઀ኺ GitHub ্Լࡌ ሣጯฏ୆తࣥߦ䈕ɻ $ ./mruby-cli --help mruby-cli [switches] [arguments] mruby-cli -h, --help : show this message mruby-cli -s, --setup= : setup your app mruby-cli -v, --version : print mruby-cli version

Slide 30

Slide 30 text

৽⃧ሢҊ $ mruby-cli -s hello-world create .gitignore create mrbgem.rake create build_con fi g.rb create Rake fi le create docker-compose.yml create tools/ create tools/hello-world/ create tools/hello-world/hello-world.c create mrblib/ create mrblib/hello-world.rb create mrblib/hello-world/ create mrblib/hello-world/version.rb create bintest/ create bintest/hello-world.rb create test/ create test/test_hello-world.rb

Slide 31

Slide 31 text

ሢҊ݁ߏ $ tree hello-world hello-world ᵓ── Rake fi le ᵓ── bintest │ └── hello-world.rb ᵓ── build_con fi g.rb ᵓ── docker-compose.yml ᵓ── mrbgem.rake ᵓ── mrblib │ ᵓ── hello-world │ │ └── version.rb │ └── hello-world.rb ᵓ── test │ └── test_hello-world.rb └── tools └── hello-world └── hello-world.c

Slide 32

Slide 32 text

ॏཁత䈕Ҋ • mrblib/hello-world.rb • mrbgem.rake

Slide 33

Slide 33 text

mrblib/hello-world.rb def __main__(argv) if argv[1] == "version" puts "v#{HelloWorld::VERSION}" else puts "Hello World" end end

Slide 34

Slide 34 text

ฤᩄ $ docker-compose run compile ... $ mruby/build/x86_64-apple-darwin21.1/bin/hello-world hello world

Slide 35

Slide 35 text

mrbgems • ՄҎ࢖༻ଖଞਓሜతവࣜݿɻ • ೺ֹ֎తവࣜݿՃਐ dependency बՄҎྃɻ • ෆաҰᒬཁॏ৽ฤᩄ

Slide 36

Slide 36 text

mrbgem.rake require_relative 'mrblib/hello-world/version' spec = MRuby::Gem::Speci fi cation.new('hello-world') do |spec| spec.bins = ['hello-world'] spec.add_dependency 'mruby-print', :core => 'mruby-print' spec.add_dependency 'mruby-mtest', :mgem => 'mruby-mtest' end spec.license = 'MIT' spec.author = 'MRuby Developer' spec.summary = 'hello-world' spec.version = HelloWorld::VERSION

Slide 37

Slide 37 text

࢖༻ mruby-env • mruby-env ఏڙྃ ENV class తመ࡞ɻ • ࡏ dependency Ճೖ spec.add_dependency 'mruby-env', :mgem => 'mruby-env' • ࡏ hello-world.rb Ճೖ puts "hello #{ENV['user']}" • ฤᩄɼࣥߦ

Slide 38

Slide 38 text

mrbgem.rake require_relative 'mrblib/hello-world/version' spec = MRuby::Gem::Speci fi cation.new('hello-world') do |spec| spec.bins = ['hello-world'] spec.add_dependency 'mruby-print', :core => 'mruby-print' spec.add_dependency 'mruby-mtest', :mgem => 'mruby-mtest' spec.add_dependency 'mruby-env', :mgem => 'mruby-env' end spec.license = 'MIT' spec.author = 'MRuby Developer' spec.summary = 'hello-world' spec.version = HelloWorld::VERSION

Slide 39

Slide 39 text

mrblib/hello-world.rb def __main__(argv) if argv[1] == "version" puts "v#{HelloWorld::VERSION}" else puts "Hello World" puts "hello #{ENV['user']}" end end

Slide 40

Slide 40 text

ฤᩄ $ docker-compose run compile ... $ user="coscup" mruby/build/x86_64-apple-darwin21.1/bin/hello-world hello world hello coscup

Slide 41

Slide 41 text

ีॿޭೳ • docker-compose run mirb ။ࣥߦ mirbɼ༻ိଌࢼ৽త mgem ኷ํศɻ • docker-compose run mtest ။ࣥߦ mtestɻ။䋯 mtest, mruby త test toolɻ • docker-compose run shell ။䋯 shellɻ
 ༻ိ debug ฤᩄࡨޡ኷ํศ (T_T)

Slide 42

Slide 42 text

mruby-cli ݪཧ • ӡ࡞ྲྀఔ • cross-compile • docker-compose

Slide 43

Slide 43 text

ӡ࡞ྲྀఔ 1. 㗞ੜฤᩄ૬᮫తઃఆɼ૾ੋ Rake fi le, build_con fi g.rb ౳౳ 2. Լࡌ mruby ݪ࢝ᛰ 3. ಁա docker-compose ɼ
 ༻㗞ੜग़ိతઃఆ cross compile ग़ᄸҰࣥߦ䈕ɻ

Slide 44

Slide 44 text

cross compile • 㑌ݸ࡞ۀܥ౷࿨CPUՍߏ౎༗ෆಉతࣥߦ䈕֨ࣜɻ • ୠ࠷ޙ౎ੋ 0 ࿨ 1ɻ • ୞ཁ஌ಓ֨ࣜबՄҎࡏҰݸ؀ڥԼฤᩄग़ଖଞ؀ڥతࣥߦ䈕ɻ

Slide 45

Slide 45 text

cross compile ྲྀఔ • ࢖༻ՄҎฤᩄग़໨ඪ؀ڥతฤᩄث • ࢦఆ target triplet (x86_64-apple-darwin, arm-pc-linux) • ౥഑໨ඪ؀ڥతܥ౷വࣜݿ (glibc , macOSSDK)

Slide 46

Slide 46 text

cross compile for darwin • ࢖༻ osxcross https://github.com/tpoechtrager/osxcross • ኺ Xcode தநऔग़ macOS SDKɻ • ฤᩄ cctool/ld64

Slide 47

Slide 47 text

cross compile for Windows • ࢖༻ mingw • 㚎ؚՄҎฤᩄग़windows ࣥߦ䈕త gcc • 㚎ؚ windows ༻త glibc • 32 / 64 bit ੋෆಉత package

Slide 48

Slide 48 text

docker-compose • Docker తෟሱ޻۩ɻಛ৭ੋՄҎ༻ docker-compose.yaml ိऔ୅ჩᏐɻ • ࢖༻༬ઌ҆᧋޷લड़ cross compile tool త linux imageɻ • ೺ሢҊֻࡌਐ containerɻ • ࣥߦ༬ઌఆٛ޷తࢦྩɻ

Slide 49

Slide 49 text

݁࿦ • mruby ՄҎ༻ိሜधཁᄸҰࣥߦ䈕తఔࣜ • mruby-cli ՄҎ༻ိઅল cross compile mruby తൟᯑख᠃ • Ұىိ⃧Ճ mruby తࢢ㟯཰㠧

Slide 50

Slide 50 text

ະိల๬ • ࢧԉ arm • ೺ docker-compose ෧᧋ࡏ mruby-cli build ࢦྩཫɻ • ೺ mruby ݪ࢝ᛰแਐ mruby-cli త docker image ཫɻ

Slide 51

Slide 51 text

Q&A QR code John Lin Twitter: @johnlinvc ౤Өย QR code ໢ᅿɿ