From b560b8cabb55530526f913ec75b74254215355ae Mon Sep 17 00:00:00 2001 From: Janusz Jankowski Date: Mon, 21 Jan 2019 21:30:25 +0100 Subject: [PATCH] unit tests: update for cmake Signed-off-by: Janusz Jankowski --- developer_guides/unit_tests.rst | 103 +++++++++----------------------- 1 file changed, 27 insertions(+), 76 deletions(-) diff --git a/developer_guides/unit_tests.rst b/developer_guides/unit_tests.rst index aa2e4111..d39644de 100644 --- a/developer_guides/unit_tests.rst +++ b/developer_guides/unit_tests.rst @@ -6,86 +6,42 @@ Unit Tests Prerequisites ************* -You must use `cmocka `_ to run unit tests. +This guide assumes that you have proper setup and that you know how to build firmware. +If that's not true please follow :doc:`../getting_started/build-guide/build-from-scratch` first. -Use the package manager such as apt to build native libraries: +`Cmocka `_ is fetched and built automatically. +For successfull compilation it needs toolchain thats supports C stdlib. -.. code-block:: bash +Configuring for unit tests +************************** - sudo apt install libcmocka-dev +In order to build and run unit tests, just pass additional flag to +CMake **-DBUILD_UNIT_TESTS=ON**. -If you want to use the `custom version of cmocka `_ -(for example for cross-compilation), provide the path to cmocka -built for the chosen architecture using the **--with-cmocka-prefix** option. +Unit tests need valid config for used toolchain, so before building them you can +use default config like: -Enabling unit tests -******************* +.. code-block:: bash -In order to build and run unit tests, provide the path to cmocka using the -**--with-cmocka-prefix** option of the configure script. + make _defconfig -After the configuration is complete, build and run all unit tests by entering: +Then build and run all unit tests by entering: .. code-block:: bash - make check - -Reconfiguration is not necessary when the cmocka path is set in the -configuration script. Use **make** for building the normal APL Binary -and **make check** to build and run unit tests. + make -j4 && ctest -j8 Example: Running tests for APL ============================== -In order to build tests for the APL platform, use the `custom version of -cmocka `_. Run the **./configure** script -with the same parameters as when building APL binaries. You must also -add **--with-cmocka-prefix=**. For example: - .. code-block:: bash - ./autogen.sh - ./configure --with-arch=xtensa-smp --with-platform=apollolake \ - --with-dsp-core=$XTENSA_CORE --with-root-dir=$CONFIG_PATH/xtensa-elf \ - --host=xtensa-bxt-elf --with-meu=$MEU_PATH \ - --with-key=$PRIVATE_KEY_PATH CC=xt-xcc OBJCOPY=xt-objcopy \ - OBJDUMP=xt-objdump --with-cmocka-prefix=/home/admin/cminstall_apl_2017_8/ - make check - -Preparing cmocka package -************************ - -#. Build cmocka with the static library on: - - .. code-block:: bash - - cmake -DWITH_STATIC_LIB=ON - - In order to build cmocka with xt-xcc to link with a DSP binary code, - do the following: - - a. add another option -DCMAKE_C_COMPILER=xt-xcc - #. edit the cmocka build scripts to disable building of shared library + mkdir build_ut && cd build_ut + cmake -DTOOLCHAIN=xt -DROOT_DIR=$CONFIG_PATH/xtensa-elf -DBUILD_UNIT_TESTS=ON .. + make apollolake_defconfig + make -j4 && ctest -j8 -#. Create a directory for the package to be referenced by the main - **sof** build script and copy the required files there: - - .. code-block:: bash - - mkdir /home//cminstall - mkdir /home//cminstall/include - mkdir /home//cminstall/lib - - cp cmocka.h /home//cminstall/include - cp libcmocka.a /home//cminstall/lib - -#. Use the target location for the cmocka files when invoking the - **configure** script: - - .. code-block:: bash - - ./configure --with-cmocka-prefix=/home//cminstall ... Wrapping objects for unit tests ******************************* @@ -113,12 +69,14 @@ CFLAGS, however they should be passed in separate variables in the makefile. Example: -.. code-block:: bash +.. code-block:: cmake # some tests before ... - check_PROGRAMS += pipeline_connect_upstream - pipeline_connect_upstream_SOURCES = ../../src/audio/pipeline.c src/audio/pipeline/ pipeline_mocks.c src/audio/pipeline/pipeline_connect_upstream.c src/audio/pipeline/pipeline_mocks_rzalloc.c - pipeline_connect_upstream_CFLAGS = -Wl, --wrap=symbol_name + cmocka_test(pipeline_connect_upstream + pipeline_connect_upstream.c + ... + ) + target_link_libraries(pipeline_connect_upstream PRIVATE "-Wl,--wrap=symbol_name") Full information about wrapping can be found here: @@ -127,16 +85,9 @@ https://lwn.net/Articles/558106/ Notes ***** -#. Use the **make check -j** option while running tests that use xt-run +#. Use the **ctest -j** option while running tests that use xt-run (to speed up tests significantly) by running multiple instances of the xt-run simulator (it also speeds up build if you have many unit tests). -#. When you switch platforms, such as from native to APL, use **make - clean**; otherwise, **make** will not build binaries for the new - platform and your tests will fail. - -#. To speed up development of new unit tests, run specific tests such as: - - .. code-block:: bash - - make check check_PROGRAMS="testname1 testname2" +#. **ctest** only runs unit tests, to rebuild them you have to explicitly + run **make**.