Upgrade to Pro — share decks privately, control downloads, hide ads and more …

用 mruby 來寫跨平台工具

用 mruby 來寫跨平台工具

coscup 2021

Johnlin

July 16, 2021
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. ᮫ԙզ • John Lin • Twitter: @johnlinvc • Senior Solution

    Architect, DevOps 
 @ West Pharmaceutical Service (୆ᖯҖ๏෰຿)
  2. Devops ᕝᕝత Golang • ༗ᴍԀ᷺ྃ if err != nil •

    ૝ཁ௅ፌ༻ Ruby ိሜ֤छ޻۩ɻ • ୠੋ Ruby ༗Ұᴍ೉ᚙ䆋੒ᄸҰࣥߦ䈕ɻ
  3. େߝ • ॄኄੋ mrubyɻ • mruby త࢖༻ํ๏ɻ • ༻ mruby-cli

    ိሜލฏ୆ mruby ఔࣜɻ • mruby-cli ӡ࡞ݪཧɻ
  4. mruby ᢛ Ruby త᮫܎ • Ruby ༗Ұݸ ISO ඪ४ ISO/IEC

    30170:2012 • େՈ࠷ৗ༻త Ruby ੋ MRIɼଖଞؐ༗ JRuby ౳౳ɻ • mruby ੋଖதҰݸመ࡞ɻ
  5. mrbgems • mruby ༻త౟݅؅ཧثɻ • ౟݅త㚎༰ՄҎੋ mruby ҃ੋ C extension

    ɻ • धཁࡏฤᩄ mruby త࣌ީबࢦఆ૝ཁత౟݅ɻ
  6. Source code $ cat > hello.rb <<HEAD puts "hello world"

    HEAD $ mruby hello.rb hello world
  7. Source code • ༏ᴍ • ࠷ख़ࣩత։ᚙྲྀఔ • ᠍ᴍ • mruby

    ࿨ఔࣜᛰੋ෼։తɼ༗Ұᴍ೉ᚙ෍ɻ • धཁ೺ఔࣜᛰڅ࢖༻ऀɻ
  8. Bytecode $ cat > hello.rb <<HEAD puts "hello world" HEAD

    $ mrbc hello.rb $ mruby -b hello.mrb hello world
  9. Bytecode in C $ cat > hello.rb <<HEAD puts "hello

    world" HEAD $ mrbc -Bhello_symbol hello.rb # 㗞ੜ hello.c
  10. hello.c #include <stdint.h> #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, };
  11. Bytecode in C $ cat > hello_bin.c <<HEAD #include <mruby.h>

    #include <mruby/irep.h> #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
  12. Bytecode in C $ gcc -std=c99 -Iinclude hello_bin.c -o hello

    build/host/lib/libmruby.a -lm $ ./hello hello world
  13. Bytecode in C • ༏ᴍ • ෆ༻څݪ࢝ᛰ • ᄸҰࣥߦ䈕 •

    ᠍ᴍ • ྲྀఔෳᯑ • 㑌࣍౎ཁॏ৽ฤᩄ
  14. mruby-cli • ॄኄੋ mruby-cli • ҆᧋ • ৽⃧ሢҊ • ฤᩄࣥߦ䈕

    • ࢖༻ mrbgems • mirb, mtest ౳ีॿޭೳ
  15. mruby-cli • ༝ Heroku త Terence Lee ։ᚙత mruby ฤᩄ޻۩ɻ

    • ՄҎҰ࣍ฤᩄग़ଟݸฏ୆తᄸҰࣥߦ䈕ɻ • ೥ٱࣦमɼ࠷ޙҰ࣍ commit ੋ 2017ɻ • զ೺ሏత૬ґ౟݅౎ߋ৽੒ 2021 ൛తɻݱࡏຢ။ಈྃɻ • https://github.com/johnlinvc/mruby-cli
  16. ҆᧋ • ௚઀ኺ GitHub ্Լࡌ ሣጯฏ୆తࣥߦ䈕ɻ $ ./mruby-cli --help mruby-cli

    [switches] [arguments] mruby-cli -h, --help : show this message mruby-cli -s<name>, --setup=<name> : setup your app mruby-cli -v, --version : print mruby-cli version
  17. ৽⃧ሢҊ $ 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
  18. ሢҊ݁ߏ $ 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
  19. 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
  20. ࢖༻ mruby-env • mruby-env ఏڙྃ ENV class తመ࡞ɻ • ࡏ

    dependency Ճೖ spec.add_dependency 'mruby-env', :mgem => 'mruby-env' • ࡏ hello-world.rb Ճೖ puts "hello #{ENV['user']}" • ฤᩄɼࣥߦ
  21. 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
  22. ีॿޭೳ • docker-compose run mirb ။ࣥߦ mirbɼ༻ိଌࢼ৽త mgem ኷ํศɻ •

    docker-compose run mtest ။ࣥߦ mtestɻ။䋯 mtest, mruby త test toolɻ • docker-compose run shell ။䋯 shellɻ
 ༻ိ debug ฤᩄࡨޡ኷ํศ (T_T)
  23. ӡ࡞ྲྀఔ 1. 㗞ੜฤᩄ૬᮫తઃఆɼ૾ੋ Rake fi le, build_con fi g.rb ౳౳

    2. Լࡌ mruby ݪ࢝ᛰ 3. ಁա docker-compose ɼ
 ༻㗞ੜग़ိతઃఆ cross compile ग़ᄸҰࣥߦ䈕ɻ
  24. cross compile • 㑌ݸ࡞ۀܥ౷࿨CPUՍߏ౎༗ෆಉతࣥߦ䈕֨ࣜɻ • ୠ࠷ޙ౎ੋ 0 ࿨ 1ɻ •

    ୞ཁ஌ಓ֨ࣜबՄҎࡏҰݸ؀ڥԼฤᩄग़ଖଞ؀ڥతࣥߦ䈕ɻ
  25. cross compile for Windows • ࢖༻ mingw • 㚎ؚՄҎฤᩄग़windows ࣥߦ䈕త

    gcc • 㚎ؚ windows ༻త glibc • 32 / 64 bit ੋෆಉత package
  26. ະိల๬ • ࢧԉ arm • ೺ docker-compose ෧᧋ࡏ mruby-cli build

    ࢦྩཫɻ • ೺ mruby ݪ࢝ᛰแਐ mruby-cli త docker image ཫɻ