添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

単体テスト自動化のために GoogleTestを使いたいので調査した結果を導入手順として記載することにしました。
Linuxクロス開発環境です!

Raspberry Pi 3 Model B + OpenWrt 19.07.3環境で動作するGoogleTestライブラリをビルドする手順

Ubuntu 18.04(VM上)で、クロスコンパイルしてGoogleTestライブラリを生成します。
因みに、Raspberry Pi 3Bのアーキテクチャは armです。
OpenWrt 64bit版を構築したので、x86-arm64コンパイラを使います。(OpenWrt 32bit版も構築可能)

  • OpenWrt 19.07.3のビルド方法は[ https://www.emb-se.com/?page_id=1004 ] を参照して下さい。私は64bit版でビルドしました。
  • CMakeを使用するのでインストールします。
  • $ sudo apt-get install cmake

    GitHubから GoogleTestソース一式を取得

    $ pwd
    /home/develop
    $ git clone https://github.com/google/googletest.git pi_GoogleTest
    ※ GoogleTest最新版で良ければ、下記コマンドは不要です
    ※ OpenWrtが32bit版の場合、1.8.0版が無難(GoogleTestでコンパイルエラーが発生しない)。
    $ git fetch --tags
    $ git tag -l
    release-1.0.0
    release-1.0.1
    release-1.1.0
    release-1.10.0
    release-1.2.0
    release-1.2.1
    release-1.3.0
    release-1.4.0
    release-1.5.0
    release-1.6.0
    release-1.7.0
    release-1.8.0
    release-1.8.1
    v1.10.x
    $ git checkout release-1.8.0

    GoogleTestのcmakeパラメータファイルを作る

    pi_GoogleTestフォルダ下に cmakeパラメータファイルを生成します

    $ pwd
    /home/develop
    $ cd pi_GoogleTest
    $ vi OpenWrt-toolchain-arm64.cmake
  • OpenWrt-toolchain-arm64.cmake の内容
    ※フルパスを指定しないとcmakeでエラーが発生します。ホームを示す~/も使えません。ダサいですねぇ。
  • set(CMAKE_SYSTEM_NAME Linux)
    set(CMAKE_CROSSCOMPILING TRUE)
    set(CMAKE_C_COMPILER /home/develop/pi_OpenWrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-10.1.0_glibc/bin/aarch64-openwrt-linux-gcc)
    set(CMAKE_CXX_COMPILER /home/develop/pi_OpenWrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-10.1.0_glibc/bin/aarch64-openwrt-linux-g++)
    set(THREADS_PTHREAD_ARG /home/develop/pi_OpenWrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-10.1.0_glibc)
    set(CMAKE_FIND_ROOT_PATH /home/develop/pi_OpenWrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-10.1.0_glibc)
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(BUILD_GTEST ON)
    set(BUILD_GMOCK OFF)

    GoogleTestのcmakeを実行します。

    $ pwd
    /home/develop/pi_GoogleTest
    $ mkdir build
    $ cd build
    $ cmake .. -DCMAKE_TOOLCHAIN_FILE=../OpenWrt-toolchain-arm64.cmake
    -- The C compiler identification is GNU 10.1.0
    -- The CXX compiler identification is GNU 10.1.0
    -- Check for working C compiler: /home/develop/pi_OpenWrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-10.1.0_glibc/bin/aarch64-openwrt-linux-gcc
    -- Check for working C compiler: /home/develop/pi_OpenWrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-10.1.0_glibc/bin/aarch64-openwrt-linux-gcc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /home/develop/pi_OpenWrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-10.1.0_glibc/bin/aarch64-openwrt-linux-g++
    -- Check for working CXX compiler: /home/develop/pi_OpenWrt/staging_dir/toolchain-aarch64_cortex-a53_gcc-10.1.0_glibc/bin/aarch64-openwrt-linux-g++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Found PythonInterp: /usr/bin/python (found version "2.7.17") 
    -- Looking for pthread.h
    -- Looking for pthread.h - found
    -- Looking for pthread_create
    -- Looking for pthread_create - not found
    -- Looking for pthread_create in pthreads
    -- Looking for pthread_create in pthreads - not found
    -- Looking for pthread_create in pthread
    -- Looking for pthread_create in pthread - found
    -- Found Threads: TRUE  
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/develop/pi_GoogleTest/build

    GoogleTestの静的ライブラリ(libgtest.a)を生成します。

    使えるか不明ですが、libgmock.aも作られます。
    ※ 環境変数 STAGING_DIR が未定義だと言われてますが問題ありません。

    $ pwd
    /home/develop/pi_GoogleTest/build
    $ make
    Scanning dependencies of target gtest
    [ 12%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
    aarch64-openwrt-linux-g++: warning: environment variable 'STAGING_DIR' not defined
    [ 25%] Linking CXX static library ../lib/libgtest.a
    [ 25%] Built target gtest
    Scanning dependencies of target gmock
    [ 37%] Building CXX object googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
    aarch64-openwrt-linux-g++: warning: environment variable 'STAGING_DIR' not defined
    [ 50%] Linking CXX static library ../lib/libgmock.a
    [ 50%] Built target gmock
    Scanning dependencies of target gmock_main
    [ 62%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
    aarch64-openwrt-linux-g++: warning: environment variable 'STAGING_DIR' not defined
    [ 75%] Linking CXX static library ../lib/libgmock_main.a
    [ 75%] Built target gmock_main
    Scanning dependencies of target gtest_main
    [ 87%] Building CXX object googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
    aarch64-openwrt-linux-g++: warning: environment variable 'STAGING_DIR' not defined
    [100%] Linking CXX static library ../lib/libgtest_main.a
    [100%] Built target gtest_main
    $ cd ..
    $ find . -name libgtest.a
    ./build/lib/libgtest.a
    $ find . -name gtest.h
    ./googletest/include/gtest/gtest.h

    ※ 開発環境にコピーする場合、以下フォルダを丸っとコピーする事。テストアプリのmakefileでlibとincludeパスに追加します。
    /home/develop/pi_GoogleTest/build/lib
    /home/develop/pi_GoogleTest/googletest/include

    次のステップは、GoogleTest(テスト自動化)のお試し実装です。