せろひきの練習NEO

パソコン&スマートデバイス&サーバーテクノロジー

Windows 10 のWSLでmicropythonをビルドする

micropythonは主にARM環境(Cortex M)で使用されるpython処理系です。ほぼほぼpython 3.4 + 一部のpython 3.5機能が実装されています。

https://micropython.org/

f:id:bunniesfield:20200627071628p:plain

micropython.org トップページ

ところで、micropythonの github を見てみると、Linux環境でのビルド方法が紹介されています。

https://github.com/micropython/micropython/tree/master

これって普通にWSLでビルドできそう、と思ったので実際にやってみました。

使用した環境は Windows 10 Pro version 1909 (64bit)、PCはThinkPad T460s です。

1. Windows 10にWSLをインストール

Microsoft Storeアプリから、Ubuntu 18.04 LTSを選択してインストールします。(いわゆるWSL1ですね。)

2. aptデータベースを更新

cello@TP460S-WIN10:~$ sudo apt update
[sudo] password for cello:
Get:1 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease [4003 B]
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:3 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:5 https://packages.microsoft.com/ubuntu/18.04/prod bionic/main amd64 Packages [119 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:7 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [761 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [982 kB]
Get:9 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [241 kB]
Get:10 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [62.1 kB]
Get:11 http://security.ubuntu.com/ubuntu bionic-security/restricted Translation-en [14.1 kB]
Get:12 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [674 kB]
Get:13 http://security.ubuntu.com/ubuntu bionic-security/universe Translation-en [224 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/main Translation-en [333 kB]
Get:15 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [7696 B]
Get:16 http://security.ubuntu.com/ubuntu bionic-security/multiverse Translation-en [2792 B]
Get:17 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [69.7 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic-updates/restricted Translation-en [15.8 kB]
Get:19 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1086 kB]
Get:20 http://archive.ubuntu.com/ubuntu bionic-updates/universe Translation-en [337 kB]
Get:21 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [11.3 kB]
Get:22 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse Translation-en [4804 B]
Fetched 5200 kB in 14s (373 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
136 packages can be upgraded. Run 'apt list --upgradable' to see them.
cello@TP460S-WIN10:~$

3. 必要なaptパッケージをインストール。ドキュメントには build-essential (includes toolchain and make), libffi-dev, and pkg-config が必要と書いてあります。 

cello@TP460S-WIN10:~$ sudo apt install build-essential
[sudo] password for cello:
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version (12.4ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 71 not upgraded.

cello@TP460S-WIN10:~$ sudo apt install pkg-config
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
pkg-config
0 upgraded, 1 newly installed, 0 to remove and 71 not upgraded.
Need to get 45.0 kB of archives.
After this operation, 185 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 pkg-config amd64 0.29.1-0ubuntu2 [45.0 kB]
Fetched 45.0 kB in 1s (31.8 kB/s)
Selecting previously unselected package pkg-config.
(Reading database ... 39332 files and directories currently installed.)
Preparing to unpack .../pkg-config_0.29.1-0ubuntu2_amd64.deb ...
Unpacking pkg-config (0.29.1-0ubuntu2) ...
Setting up pkg-config (0.29.1-0ubuntu2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

cello@TP460S-WIN10:~$ sudo apt install libffi-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
libffi-dev
0 upgraded, 1 newly installed, 0 to remove and 71 not upgraded.
Need to get 156 kB of archives.
After this operation, 362 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 libffi-dev amd64 3.2.1-8 [156 kB]
Fetched 156 kB in 2s (97.3 kB/s)
Selecting previously unselected package libffi-dev:amd64.
(Reading database ... 39348 files and directories currently installed.)
Preparing to unpack .../libffi-dev_3.2.1-8_amd64.deb ...
Unpacking libffi-dev:amd64 (3.2.1-8) ...
Setting up libffi-dev:amd64 (3.2.1-8) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
cello@TP460S-WIN10:~$  

ここにはちゃんとは書いてありませんが、python3が必要です。

cello@TP460S-WIN10:~$ which python3
/usr/bin/python3

cello@TP460S-WIN10:~$ python3 --version
Python 3.6.9
cello@TP460S-WIN10:~$

あと当たり前ですが git も必要ですね。この辺りはお任せします。

 4. micropython を clone

現在のディレクトリにmicropythonをcloneします。必要に応じてcd します。

cello@TP460S-WIN10:~$ cd
cello@TP460S-WIN10:~$ git clone https://github.com/micropython/micropython.git
Cloning into 'micropython'...
remote: Enumerating objects: 81027, done.
remote: Total 81027 (delta 0), reused 0 (delta 0), pack-reused 81027
Receiving objects: 100% (81027/81027), 44.59 MiB | 8.08 MiB/s, done.
Resolving deltas: 100% (58697/58697), done.
Checking out files: 100% (3664/3664), done.
cello@TP460S-WIN10:~$ cd micropython/
cello@TP460S-WIN10:~/micropython$ ls
ACKNOWLEDGEMENTS CODEOFCONDUCT.md LICENSE docs examples lib mpy-cross py tools
CODECONVENTIONS.md CONTRIBUTING.md README.md drivers extmod logo ports tests
cello@TP460S-WIN10:~/micropython$ ls ports
bare-arm esp32 javascript minimal pic16bit qemu-arm stm32 unix zephyr
cc3200 esp8266 mimxrt nrf powerpc samd teensy windows
cello@TP460S-WIN10:~/micropython$  

 5 mpy-crossツールをビルド

このためにpython3が必要?

cello@TP460S-WIN10:~/micropython$ cd mpy-cross/
cello@TP460S-WIN10:~/micropython/mpy-cross$ make
Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
mkdir -p build/genhdr
GEN build/genhdr/mpversion.h
GEN build/genhdr/moduledefs.h
GEN build/genhdr/qstr.i.last
GEN build/genhdr/qstr.split
GEN build/genhdr/qstrdefs.collected.h
QSTR updated
GEN build/genhdr/qstrdefs.generated.h
mkdir -p build/lib/utils/
mkdir -p build/py/
CC ../py/mpstate.c
CC ../py/nlr.c
CC ../py/nlrx86.c
CC ../py/nlrx64.c

(中略)

CC ../py/repl.c
CC ../py/smallint.c
CC ../py/frozenmod.c
CC main.c
CC gccollect.c
CC ../lib/utils/gchelper_generic.c
LINK mpy-cross
text data bss dec hex filename
299214 17248 872 317334 4d796 mpy-cross
cello@TP460S-WIN10:~/micropython/mpy-cross$ cd ..
cello@TP460S-WIN10:~/micropython$ 

6 unix版micropythonをビルド

cello@TP460S-WIN10:~/micropython$ cd ports/unix
cello@TP460S-WIN10:~/micropython/ports/unix$ ls
Makefile coverage.c main.c modmachine.c moduselect.c mpthreadport.c
alloc.c fatfs_port.c manifest.py modos.c modusocket.c mpthreadport.h
btstack_usb.c gccollect.c manifest_coverage.py modtermios.c mpconfigport.h qstrdefsport.h
coverage-frzmpy input.c modffi.c modtime.c mpconfigport.mk unix_mphal.c
coverage-frzstr input.h modjni.c moduos_vfs.c mphalport.h variants
cello@TP460S-WIN10:~/micropython/ports/unix$ make submodules
Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
Updating submodules: lib/axtls lib/berkeley-db-1.xx lib/libffi
Submodule 'lib/axtls' (https://github.com/pfalcon/axtls) registered for path '../../lib/axtls'
Submodule 'lib/berkeley-db-1.xx' (https://github.com/pfalcon/berkeley-db-1.xx) registered for path '../../lib/berkeley-db-1.xx'
Submodule 'lib/libffi' (https://github.com/atgreen/libffi) registered for path '../../lib/libffi'
Cloning into '/home/cello/micropython/lib/axtls'...
Cloning into '/home/cello/micropython/lib/berkeley-db-1.xx'...
Cloning into '/home/cello/micropython/lib/libffi'...
Submodule path '../../lib/axtls': checked out '43a6e6bd3bbc03dc501e16b89fba0ef042ed3ea0'
Submodule path '../../lib/berkeley-db-1.xx': checked out '35aaec4418ad78628a3b935885dd189d41ce779b'
Submodule path '../../lib/libffi': checked out 'e9de7e35f2339598b16cbb375f9992643ed81209'
cello@TP460S-WIN10:~/micropython/ports/unix$ make
Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
mkdir -p build-standard/genhdr
GEN build-standard/genhdr/mpversion.h
GEN build-standard/genhdr/moduledefs.h
GEN build-standard/genhdr/qstr.i.last
GEN build-standard/genhdr/qstr.split
GEN build-standard/genhdr/qstrdefs.collected.h
QSTR updated
GEN build-standard/genhdr/qstrdefs.generated.h
mkdir -p build-standard/build-standard/
mkdir -p build-standard/extmod/
mkdir -p build-standard/lib/axtls/crypto/
mkdir -p build-standard/lib/axtls/ssl/
mkdir -p build-standard/lib/berkeley-db-1.xx/btree/
mkdir -p build-standard/lib/berkeley-db-1.xx/mpool/
mkdir -p build-standard/lib/embed/
mkdir -p build-standard/lib/mp-readline/
mkdir -p build-standard/lib/timeutils/
mkdir -p build-standard/lib/utils/
mkdir -p build-standard/py/
CC ../../py/mpstate.c
CC ../../py/nlr.c
CC ../../py/nlrx86.c
CC ../../py/nlrx64.c

(中略)

MPY upip.py
MPY upip_utarfile.py
GEN build-standard/frozen_content.c
CC build-standard/frozen_content.c
CC main.c
CC gccollect.c
CC unix_mphal.c
CC mpthreadport.c
CC input.c
CC modmachine.c
CC modos.c
CC moduos_vfs.c
CC modtime.c
CC moduselect.c
CC alloc.c
CC coverage.c
CC fatfs_port.c
CC btstack_usb.c
CC ../../lib/axtls/ssl/asn1.c
CC ../../lib/axtls/ssl/loader.c
CC ../../lib/axtls/ssl/tls1.c
CC ../../lib/axtls/ssl/tls1_svr.c
CC ../../lib/axtls/ssl/tls1_clnt.c
CC ../../lib/axtls/ssl/x509.c
CC ../../lib/axtls/crypto/aes.c
CC ../../lib/axtls/crypto/bigint.c
CC ../../lib/axtls/crypto/crypto_misc.c
CC ../../lib/axtls/crypto/hmac.c
CC ../../lib/axtls/crypto/md5.c
CC ../../lib/axtls/crypto/rsa.c
CC ../../lib/axtls/crypto/sha1.c
CC ../../extmod/modbtree.c
CC ../../lib/berkeley-db-1.xx/btree/bt_close.c
CC ../../lib/berkeley-db-1.xx/btree/bt_conv.c
CC ../../lib/berkeley-db-1.xx/btree/bt_debug.c
CC ../../lib/berkeley-db-1.xx/btree/bt_delete.c
CC ../../lib/berkeley-db-1.xx/btree/bt_get.c
CC ../../lib/berkeley-db-1.xx/btree/bt_open.c
CC ../../lib/berkeley-db-1.xx/btree/bt_overflow.c
CC ../../lib/berkeley-db-1.xx/btree/bt_page.c
CC ../../lib/berkeley-db-1.xx/btree/bt_put.c
CC ../../lib/berkeley-db-1.xx/btree/bt_search.c
CC ../../lib/berkeley-db-1.xx/btree/bt_seq.c
CC ../../lib/berkeley-db-1.xx/btree/bt_split.c
CC ../../lib/berkeley-db-1.xx/btree/bt_utils.c
CC ../../lib/berkeley-db-1.xx/mpool/mpool.c
CC modtermios.c
CC modusocket.c
CC modffi.c
CC ../../lib/mp-readline/readline.c
CC ../../lib/timeutils/timeutils.c
CC ../../lib/utils/gchelper_generic.c
LINK micropython
text data bss dec hex filename
445351 55344 2112 502807 7ac17 micropython
cello@TP460S-WIN10:~/micropython/ports/unix$ ls -l ./micropython
-rwxrwxrwx 1 cello cello 505744 Jun 27 06:59 micropython
cello@TP460S-WIN10:~/micropython/ports/unix$ file ./micropython
./micropython: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=d95cd37b4e2e95465f6ff5aceb2e115c9d3ed383, stripped
cello@TP460S-WIN10:~/micropython/ports/unix

7. 実行してみる

cello@TP460S-WIN10:~/micropython/ports/unix$ ./micropython
MicroPython v1.12-590-g9f911d822 on 2020-06-27; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> list(5 * x + y for x in range(10) for y in [4, 2, 1])
[4, 2, 1, 9, 7, 6, 14, 12, 11, 19, 17, 16, 24, 22, 21, 29, 27, 26, 34, 32, 31, 39, 37, 36, 44, 42, 41, 49, 47, 46]
>>> (Control-Dを入力)
cello@TP460S-WIN10:~/micropython/ports/unix$ ./micropython -h
usage: ./micropython [<opts>] [-X <implopt>] [-c <command> | -m <module> | <filename>]
Options:
-h : print this help message
-i : enable inspection via REPL after running command/module/file
-v : verbose (trace various operations); can be multiple
-O[N] : apply bytecode optimizations of level N

Implementation specific options (-X):
compile-only -- parse and compile only
emit={bytecode,native,viper} -- set the default code emitter
heapsize=<n>[w][K|M] -- set the heap size for the GC (default 2097152)
cello@TP460S-WIN10:~/micropython/ports/unix$ ./micropython -m upip install micropython-pystone
Installing to: /home/cello/.micropython/lib/
Warning: micropython.org SSL certificate is not validated
Installing micropython-pystone 3.4.2.post2 from https://micropython.org/pi/pystone/pystone-3.4.2.post2.tar.gz
cello@TP460S-WIN10:~/micropython/ports/unix$ ./micropython -m pystone
Pystone(1.2) time for 50000 passes = 0.438
This machine benchmarks at 114155 pystones/second
cello@TP460S-WIN10:~/micropython/ports/unix$

ということでビルドできました。

 8. インストールしておく

cello@TP460S-WIN10:~/micropython/ports/unix$ sudo make install
[sudo] password for cello:
Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
install -d /usr/local/bin
install micropython /usr/local/bin/micropython
cello@TP460S-WIN10:~/micropython/ports/unix$ cd
cello@TP460S-WIN10:~$ which micropython
/usr/local/bin/micropython
cello@TP460S-WIN10:~$ micropython -m pystone
Pystone(1.2) time for 50000 passes = 0.515
This machine benchmarks at 97087.4 pystones/second
cello@TP460S-WIN10:~$cello@TP460S-WIN10:~$

ということで問題なさそうです。(micropythonライブラリはユーザーの ~/.micropythonフォルダにインストールされています。)