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

用 mruby 來寫跨平台工具

用 mruby 來寫跨平台工具

coscup 2021

3f7d9611fc919c98512b779cde637dfc?s=128

Johnlin

July 16, 2021
Tweet

Transcript

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

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

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

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

    ိሜލฏ୆ mruby ఔࣜɻ • mruby-cli ӡ࡞ݪཧɻ
  5. ॄኄੋ mruby • mruby ᢛ Ruby త᮫܎ • mruby తಛ৭

    • mrbgems
  6. mruby • ओཁ։ᚙऀੋ Ruby తᚙ໌ਓ matz • ໨લ࠷৽൛ຊੋ 3.0 •

    https://mruby.org
  7. mruby ᢛ Ruby త᮫܎ • Ruby ༗Ұݸ ISO ඪ४ ISO/IEC

    30170:2012 • େՈ࠷ৗ༻త Ruby ੋ MRIɼଖଞؐ༗ JRuby ౳౳ɻ • mruby ੋଖதҰݸመ࡞ɻ
  8. mruby తಛ৭ • ओཁ໨ඪੋ༻ိቕೖࡏଖଞఔࣜதɻ • هԱᱪ༻ྔֱগɼ୞ཁ 100kbɻ(IRB ཁ 17 mb)

    • େ෦෼త㚎ݐവࣜݿ౎ੋબ഑ɻ
  9. mrbgems • mruby ༻త౟݅؅ཧثɻ • ౟݅త㚎༰ՄҎੋ mruby ҃ੋ C extension

    ɻ • धཁࡏฤᩄ mruby త࣌ީबࢦఆ૝ཁత౟݅ɻ
  10. ҆᧋mruby • ಁա ruby-build ҆᧋ɻ • ኺݪ࢝ᛰฤᩄɻ • ။༗ࡾݸओཁࣥߦ䈕ɼmruby ,

    mrbc, mirb
  11. ಁա ruby-build ҆᧋ • ཁઌ᧋޷ฤᩄث (clang / gcc) • ruby-build

    mruby-3.0.0 ~/.local/mruby-3.0.0
  12. ኺݪ࢝ᛰฤᩄ • ໵ཁઌ᧋޷ฤᩄث (clang / gcc) $ git clone https://github.com/mruby/mruby.git

    $ cd mruby $ rake
  13. mruby త࢖༻ํ๏ • REPL • Source code • Bytecode •

    Bytecode in c
  14. Hello world puts "hello world"

  15. REPL (read-eval-print-loop) $ mirb mirb - Embeddable Interactive Ruby Shell

    > puts "hello world" hello world => nil
  16. REPL • ༏ᴍ • ํศଌࢼ • ᠍ᴍ • جຊ্ෆೳᚙ䆋څଖଞผਓ༻ɻ

  17. Source code $ cat > hello.rb <<HEAD puts "hello world"

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

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

    $ mrbc hello.rb $ mruby -b hello.mrb hello world
  20. Bytecode • ༏ᴍ • ఔࣜᛰෆ༻څผਓ • ᠍ᴍ • ൺֱෳᯑత։ᚙྲྀఔ •

    ؐੋཁᚙ෍ .mrb ࿨ mruby
  21. Bytecode in C $ cat > hello.rb <<HEAD puts "hello

    world" HEAD $ mrbc -Bhello_symbol hello.rb # 㗞ੜ hello.c
  22. 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, };
  23. 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
  24. Bytecode in C $ gcc -std=c99 -Iinclude hello_bin.c -o hello

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

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

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

    • ՄҎҰ࣍ฤᩄग़ଟݸฏ୆తᄸҰࣥߦ䈕ɻ • ೥ٱࣦमɼ࠷ޙҰ࣍ commit ੋ 2017ɻ • զ೺ሏత૬ґ౟݅౎ߋ৽੒ 2021 ൛తɻݱࡏຢ။ಈྃɻ • https://github.com/johnlinvc/mruby-cli
  28. mruby-cli ߋ৽႔ • https://github.com/johnlinvc/mruby-cli • ኺ mruby 1.2 ঋڃҝ mruby

    3.0.0 • ࢧԉ࠷৽ macOS 14
  29. ҆᧋ • ௚઀ኺ 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
  30. ৽⃧ሢҊ $ 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
  31. ሢҊ݁ߏ $ 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
  32. ॏཁత䈕Ҋ • mrblib/hello-world.rb • mrbgem.rake

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

    puts "Hello World" end end
  34. ฤᩄ $ docker-compose run compile ... $ mruby/build/x86_64-apple-darwin21.1/bin/hello-world hello world

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

  36. 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
  37. ࢖༻ mruby-env • mruby-env ఏڙྃ ENV class తመ࡞ɻ • ࡏ

    dependency Ճೖ spec.add_dependency 'mruby-env', :mgem => 'mruby-env' • ࡏ hello-world.rb Ճೖ puts "hello #{ENV['user']}" • ฤᩄɼࣥߦ
  38. 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
  39. 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
  40. ฤᩄ $ docker-compose run compile ... $ user="coscup" mruby/build/x86_64-apple-darwin21.1/bin/hello-world hello

    world hello coscup
  41. ีॿޭೳ • docker-compose run mirb ။ࣥߦ mirbɼ༻ိଌࢼ৽త mgem ኷ํศɻ •

    docker-compose run mtest ။ࣥߦ mtestɻ။䋯 mtest, mruby త test toolɻ • docker-compose run shell ။䋯 shellɻ
 ༻ိ debug ฤᩄࡨޡ኷ํศ (T_T)
  42. mruby-cli ݪཧ • ӡ࡞ྲྀఔ • cross-compile • docker-compose

  43. ӡ࡞ྲྀఔ 1. 㗞ੜฤᩄ૬᮫తઃఆɼ૾ੋ Rake fi le, build_con fi g.rb ౳౳

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

    ୞ཁ஌ಓ֨ࣜबՄҎࡏҰݸ؀ڥԼฤᩄग़ଖଞ؀ڥతࣥߦ䈕ɻ
  45. cross compile ྲྀఔ • ࢖༻ՄҎฤᩄग़໨ඪ؀ڥతฤᩄث • ࢦఆ target triplet (x86_64-apple-darwin,

    arm-pc-linux) • ౥഑໨ඪ؀ڥతܥ౷വࣜݿ (glibc , macOSSDK)
  46. cross compile for darwin • ࢖༻ osxcross https://github.com/tpoechtrager/osxcross • ኺ

    Xcode தநऔग़ macOS SDKɻ • ฤᩄ cctool/ld64
  47. cross compile for Windows • ࢖༻ mingw • 㚎ؚՄҎฤᩄग़windows ࣥߦ䈕త

    gcc • 㚎ؚ windows ༻త glibc • 32 / 64 bit ੋෆಉత package
  48. docker-compose • Docker తෟሱ޻۩ɻಛ৭ੋՄҎ༻ docker-compose.yaml ိऔ୅ჩᏐɻ • ࢖༻༬ઌ҆᧋޷લड़ cross compile

    tool త linux imageɻ • ೺ሢҊֻࡌਐ containerɻ • ࣥߦ༬ઌఆٛ޷తࢦྩɻ
  49. ݁࿦ • mruby ՄҎ༻ိሜधཁᄸҰࣥߦ䈕తఔࣜ • mruby-cli ՄҎ༻ိઅল cross compile mruby

    తൟᯑख᠃ • Ұىိ⃧Ճ mruby తࢢ㟯཰㠧
  50. ະိల๬ • ࢧԉ arm • ೺ docker-compose ෧᧋ࡏ mruby-cli build

    ࢦྩཫɻ • ೺ mruby ݪ࢝ᛰแਐ mruby-cli త docker image ཫɻ
  51. Q&A QR code John Lin Twitter: @johnlinvc ౤Өย QR code

    ໢ᅿɿ