2020年8月30日日曜日

CLI アプリケーションな gem を作った

CLI アプリケーションな gem を作った

テストでは test-unitsimplecov を使用。

手順を記録していなかったので、思い出しながらまとめ直し。

ひな形作成

$ bundle gem newgem
Creating gem 'newgem'...
Do you want to generate tests with your gem?
Type 'rspec' or 'minitest' to generate those test files now and in the future. rspec/minitest/(none): minitest
Do you want to license your code permissively under the MIT license?
This means that any other developer or company will be legally allowed to use your code for free as long as they admit you created it. You can read more about the MIT license at https://choosealicense.com/licenses/mit. y/(n): n
Do you want to include a code of conduct in gems you generate?
Codes of conduct can increase contributions to your project by contributors who prefer collaborative, safe spaces. You can read more about the code of conduct at contributor-covenant.org. Having a code of conduct means agreeing to the responsibility of enforcing it, so be sure that you are prepared to do that. Be sure that your email address is specified as a contact in the generated code of conduct so that people know who to contact in case of a violation. For suggestions about how to enforce codes of conduct, see https://bit.ly/coc-enforcement. y/(n): n
      create  newgem/Gemfile
      create  newgem/lib/newgem.rb
      create  newgem/lib/newgem/version.rb
      create  newgem/newgem.gemspec
      create  newgem/Rakefile
      create  newgem/README.md
      create  newgem/bin/console
      create  newgem/bin/setup
      create  newgem/.gitignore
      create  newgem/.travis.yml
      create  newgem/test/test_helper.rb
      create  newgem/test/newgem_test.rb
Initializing git repo in /work/newgem
Gem 'newgem' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html

基本情報更新

  1. newgem/newgem.gemspec を更新
    • spec.author
    • spec.email
    • spec.summary
    • spec.description
    • spec.homepage
    • spec.metadata["source_code_uri"]
    • spec.metadata["changelog_uri"]
  2. spec.metadata["allowed_push_host" = "TODO: Set to 'http://mygemserver.com'" を削除
    • rubygems.org に push できるようにするため

test-unit and simplecov を使うための設定

  1. newgem/Gemfile から minitest の行を削除
  2. newgem/newgem.gemspec に依存関係定義を追加
    • spec.add_development_dependency 'test-unit'
    • spec.add_development_dependency 'simplecov'
  3. newgem/test/test_helper.rb の更新
    • よくわからないけどこんな感じになった

      $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
      
      require 'simplecov'
      require "test/unit"
      
      SimpleCov.start do
        enable_coverage :branch
        add_filter 'test'
      end
      
      require "newgem"
  4. newgem/test/newgem_test.rb の更新
    • 更新というか、テストに合わせて新規作成する感じ。ひな形は以下のような感じ。

      require "test_helper"
      
      require_relative '../lib/test/target/file.rb'
      
      class TestTargetClass < Test::Unit::TestCase
        sub_test_case "method_name" do
          test "test perspective" do
      
            expected = "dummy"
            actual = ...(snip)
      
            assert_equal(expected, actual)
          end
        end
      end

開発手順

依存パッケージ取得

bundle install

テスト

$ bundle exec rake test

ビルド

$ bundle exec rake build

リリース

$ gem signin
$ bundle exec rake release

多分こんな感じ…。 以上。

参考資料

gcc の std オプションのデフォルト値を調べる

調べてみました。

gcc

$ echo | gcc -dM -E - | grep -F __STDC_V
#define __STDC_VERSION__ 201112L
  • -E : コンパイル・リンクを行わない(プリプロセッサ処理だけ行う)
  • -dM : 有効なマクロ定義を出力

g++

$ echo | g++ -dM -E -x c++ - | grep -F __cplusplus
#define __cplusplus 201402L
  • -x : 言語設定
    • 標準入力だと .c or .cpp で判定できないので、明示的に .cpp であることを知らせる

ちなみに、サポートしている std オプションの一覧は以下で取得できる。

gcc -v --help 2>/dev/null | grep -E "^\s+\-std=.*$"
g++ -v --help 2>/dev/null | grep -E "^\s+\-std=.*$"

参考資料

2020年8月23日日曜日

Debian 10 で TOPPERS/ASP3 in Zig を動かす

前提

  • OS: Windoes 10 Pro 1909
  • Docker :Docker version 19.03.12, build 48a66213fe
  • Docker Image: debian:buster-slim
  • asp3_in_zig: 3d0d9f6bb7bdafaafb5a76529cc8af3ade577e77

docker run -it --rm debian:buster-slim してからの作業。

環境構築

apt-get update
apt-get install -y git curl bzip2 xz-utils make ruby qemu-system-arm gcc-arm-none-eabi

# zig 0.6.0+4e63cae36 を /opt/zig-linux-x86_64-0.6.0+4e63cae36 にインストール
curl -L https://ziglang.org/builds/zig-linux-x86_64-0.6.0+4e63cae36.tar.xz -O
tar xf zig-linux-x86_64-0.6.0+4e63cae36.tar.xz -C /opt
export PATH=$PATH:/opt/zig-linux-x86_64-0.6.0+4e63cae36

# tecsgen 1.7.0 のインストール
curl -L https://www.toppers.jp/download.cgi/tecsgen-1.7.0.tgz -O
tar xf tecsgen-1.7.0.tgz -C /opt

asp3_in_zig の取得とビルド

WORK_DIR=/work
ASP3_DIR=$WORK_DIR/asp3_in_zig

mkdir -p $WORK_DIR
cd $WORK_DIR
git clone https://github.com/toppers/asp3_in_zig.git

cd $ASP3_DIR
git checkout 3d0d9f6bb7bdafaafb5a76529cc8af3ade577e77

mkdir $ASP3_DIR/OBJ-ARM
cd $ASP3_DIR/OBJ-ARM
../configure.rb -G /opt/tecsgen-1.7.0/tecsgen/tecsgen.rb -T ct11mpcore_gcc -O "-DTOPPERS_USE_QEMU"
make

sample1 の実行

root@e5307855eb8c:/work/asp3_in_zig/OBJ-ARM# qemu-system-arm -M realview-eb-mpcore -semihosting -m 128M -nographic -kernel asp
pulseaudio: pa_context_connect() failed
pulseaudio: Reason: Connection refused
pulseaudio: Failed to initialize PA contextaudio: Could not init `pa' audio driver
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM default
alsa: Could not initialize DAC
alsa: Failed to open `default':
alsa: Reason: No such file or directory
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM default
alsa: Could not initialize DAC
alsa: Failed to open `default':
alsa: Reason: No such file or directory
audio: Failed to create voice `lm4549.out'

TOPPERS/ASP3 Kernel in Zig Release 3.6.0 for ARM CT11MPCore (Aug 23 2020, 12:34:09)
Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
                            Toyohashi Univ. of Technology, JAPAN
Copyright (C) 2004-2020 by Embedded and Real-Time Systems Laboratory
               Graduate School of Informatics, Nagoya Univ., JAPAN

no time event is processed in hrt interrupt.
System logging task is started.
Sample program starts (exinf = 0).
task1 is running (001).   |
task1 is running (002).   |
task1 is running (003).   |
...(snip)

以上。

参考資料