CodeCompass enabling - shijunjing/edk2 GitHub Wiki

Enable the CodeCompass (https://github.com/Ericsson/CodeCompass, https://codecompass.net/) for edk2

CodeCompass Metrics: CodeCompass Codebites to see the callees and references: CodeCompass InfoTree to show the function basic info: CodeCompass Function call diagram:

Host machine directly build

Ubuntu 18.04 ("Bionic Beaver") LTS

sudo apt install git cmake make g++ gcc-7-plugin-dev libboost-all-dev \
  llvm-10-dev clang-10 libclang-10-dev \
  default-jdk libssl1.0-dev libgraphviz-dev libmagic-dev libgit2-dev ctags \
  libgtest-dev npm

Ubuntu 20.04 ("Focal Fossa") LTS

sudo apt install git cmake make g++ libboost-all-dev \
  llvm-10-dev clang-10 libclang-10-dev \
  odb libodb-dev thrift-compiler libthrift-dev \
  default-jdk libssl-dev libgraphviz-dev libmagic-dev libgit2-dev ctags \
  libgtest-dev npm

Build ODB with g++-5 :

jshi19@ub2-uefi-b01:~/CodeCompass$ sudo vim /etc/apt/sources.list (ubuntu20.04 only) 
append bionic distribution repo: deb http://archive.ubuntu.com/ubuntu/ bionic  main universe
jshi19@ub2-uefi-b01:~/CodeCompass$ sudo apt-get update
jshi19@ub2-uefi-b01:~/CodeCompass$ sudo apt-get install g++-5 gcc-5
select gcc-5 as the default compiler:
$ sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-5   200       auto mode
  1            /usr/bin/gcc-5   200       manual mode
  2            /usr/bin/gcc-8   100       manual mode
  3            /usr/bin/gcc-9   100       manual mode

jshi19@ub2-uefi-b01:~/CodeCompass$ wget https://download.build2.org/0.13.0/build2-install-0.13.0.sh
jshi19@ub2-uefi-b01:~/CodeCompass$ sh build2-install-0.13.0.sh --cxx g++-5 /home/jshi19/CodeCompass/build2_install

Use the Build2 tool-chain to build the ODB compiler and libraries with g++-5 :

jshi19@ub2-uefi-b01:~/CodeCompass$ export PATH=/home/jshi19/CodeCompass/build2_install/bin:$PATH
jshi19@ub2-uefi-b01:~/CodeCompass$ mkdir odb_build
jshi19@ub2-uefi-b01:~/CodeCompass$ cd odb_build/
jshi19@ub2-uefi-b01:~/CodeCompass/odb_build$ sudo apt install  gcc-5-plugin-dev
jshi19@ub2-uefi-b01:~/CodeCompass/odb_build$ bpkg create --quiet --jobs $(nproc) cc   config.cxx=g++-5   config.cc.coptions=-O3   config.bin.rpath=/home/jshi19/CodeCompass/odb_install/lib   config.install.root=/home/jshi19/CodeCompass/odb_install
jshi19@ub2-uefi-b01:~/CodeCompass/odb_build$ bpkg add https://pkg.cppget.org/1/beta --trust-yes
jshi19@ub2-uefi-b01:~/CodeCompass/odb_build$ bpkg fetch --trust-yes
jshi19@ub2-uefi-b01:~/CodeCompass/odb_build$ bpkg build odb --yes
jshi19@ub2-uefi-b01:~/CodeCompass/odb_build$ bpkg build libodb --yes
jshi19@ub2-uefi-b01:~/CodeCompass/odb_build$ bpkg build libodb-sqlite --yes
jshi19@ub2-uefi-b01:~/CodeCompass/odb_build$ bpkg build libodb-pgsql --yes
jshi19@ub2-uefi-b01:~/CodeCompass/odb_build$ bpkg install --all --recursive

Build Thrift :

jshi19@ub2-uefi-b01:~/CodeCompass$ wget "http://www.apache.org/dyn/mirrors/mirrors.cgi?action=download&filename=thrift/0.13.0/thrift-0.13.0.tar.gz"   -O thrift-0.13.0.tar.gz
jshi19@ub2-uefi-b01:~/CodeCompass$ tar -xvf ./thrift-0.13.0.tar.gz
jshi19@ub2-uefi-b01:~/CodeCompass$ cd thrift-0.13.0
jshi19@ub2-uefi-b01:~/CodeCompass/thrift-0.13.0$ ./configure --prefix=/home/jshi19/CodeCompass/thrift_install --silent --without-python   --enable-libtool-lock --enable-tutorial=no --enable-tests=no        --with-libevent --with-zlib --without-nodejs --without-lua          --without-ruby --without-csharp --without-erlang --without-perl     --without-php --without-php_extension --without-dart                --without-haskell --without-go --without-rs --without-haxe          --without-dotnetcore --without-d --without-qt4 --without-qt5        --without-java --without-c_glib
jshi19@ub2-uefi-b01:~/CodeCompass/thrift-0.13.0$ make install -j $(nproc)

Build GTest/Googletest :

jshi19@ub2-uefi-b01:~/CodeCompass$ mkdir gtest
jshi19@ub2-uefi-b01:~/CodeCompass$ cp -R /usr/src/googletest/* ./gtest
jshi19@ub2-uefi-b01:~/CodeCompass$ cd gtest/
jshi19@ub2-uefi-b01:~/CodeCompass/gtest$ mkdir build
jshi19@ub2-uefi-b01:~/CodeCompass/gtest$ cd build/
jshi19@ub2-uefi-b01:~/CodeCompass/gtest/build$ cmake .. -DCMAKE_INSTALL_PREFIX=~/CodeCompass/gtest_install

Build CodeCompass

jshi19@ub2-uefi-b01:~/CodeCompass$ export GTEST_ROOT=~/CodeCompass/gtest_install/
jshi19@ub2-uefi-b01:~/CodeCompass$ export CMAKE_PREFIX_PATH=~/CodeCompass/thrift_install/:$CMAKE_PREFIX_PATH
jshi19@ub2-uefi-b01:~/CodeCompass$ export CMAKE_PREFIX_PATH=~/CodeCompass/odb_install/:$CMAKE_PREFIX_PATH
jshi19@ub2-uefi-b01:~/CodeCompass$ export PATH=~/CodeCompass/thrift_install/bin:$PATH
jshi19@ub2-uefi-b01:~/CodeCompass$ export PATH=~/CodeCompass/odb_build/odb_install/bin:$PATH
jshi19@ub2-uefi-b01:~/CodeCompass/CodeCompass$ mkdir Build && cd Build
jshi19@ub2-uefi-b01:~/CodeCompass/CodeCompass/Build (master)$ cmake ..   -DCMAKE_INSTALL_PREFIX=~/CodeCompass/install   -DDATABASE=pgsql   -DCMAKE_BUILD_TYPE=RelWithDebInfo   -DLLVM_DIR=/usr/lib/llvm-10/cmake   -DClang_DIR=/usr/lib/cmake/clang-10
jshi19@ub2-uefi-b01:~/CodeCompass/CodeCompass/Build (master)$ make -j $(nproc)
jshi19@ub2-uefi-b01:~/CodeCompass/CodeCompass/Build (master)$ make install

Create compilation command database via build instrumentation:

jshi19@ub2-uefi-b01:~/wksp_efi$ sudo apt-get install build-essential git uuid-dev iasl nasm
jshi19@ub2-uefi-b01:~/wksp_efi$ git clone https://github.com/tianocore/edk2.git edk2-2
jshi19@ub2-uefi-b01:~/wksp_efi/edk2-2$ git submodule init
jshi19@ub2-uefi-b01:~/wksp_efi/edk2-2$ git submodule sync --recursive
jshi19@ub2-uefi-b01:~/wksp_efi/edk2-2$ git submodule update --recursive
jshi19@ub2-uefi-b01:~/wksp_efi/edk2-2$ export CLANG_BIN=~/llvm/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/
jshi19@ub2-uefi-b01:~/wksp_efi/edk2-2$ source edksetup.sh
jshi19@ub2-uefi-b01:~/wksp_efi/edk2-2$ make -C BaseTools/
jshi19@ub2-uefi-b01:~/wksp_efi/edk2-2$ ~/CodeCompass/install/bin/CodeCompass_logger build_commands.json "build -p OvmfPkg/OvmfPkgIa32X64.dsc -a IA32 -a X64 -t CLANGPDB"

Install the postgresql database

jshi19@ub2-uefi-b01:~$ sudo apt-get install postgresql-all
Success. You can now start the database server using:

    pg_ctlcluster 12 main start

Ver Cluster Port Status Owner    Data directory              Log file
12  main    5433 down   postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log

Please note the postgresql default port 5432 already be used in my ubuntu 20.04, so it uses the 5433 as default port! Add new PG DB user by psql on port 5433 with 'postgres' owner

jshi19@ub2-uefi-b01:~$ sudo su postgres
postgres@ub2-uefi-b01:/home/jshi19$ psql -p 5433
postgres=# CREATE USER compass WITH SUPERUSER LOGIN PASSWORD '123456';
CREATE ROLE
\q
postgres@ub2-uefi-b01:/home/jshi19$ exit

Parse and store the results of the project in a PostgreSQL database:

jshi19@ub2-uefi-b01:~$ ~/CodeCompass/install/bin/CodeCompass_parser -d "pgsql:host=localhost;port=5433;user=compass;password=123456;database=mydatabase" -w /home/jshi19/CodeCompass/workspace -n edk2 -i /home/jshi19/wksp_efi/edk2-2/build_commands.json -i /home/jshi19/wksp_efi/edk2-2 -j 16 -f --label src=/home/jshi19/wksp_efi/edk2-2/

Start the web server:

jshi19@ub2-uefi-b01:~$ ~/CodeCompass/install/bin/CodeCompass_webserver   -w /home/jshi19/CodeCompass/workspace/   -p 6251

Use web browser to access your host machine IP:6251 port http://xxx.xxx.xxx.xxx:6251

Run CodeCompass_webserver application as daemons

jshi19@ub2-uefi-b01:~/CodeCompass$ sudo supervisorctl status
CodeCompass_webserver            RUNNING   pid 645683, uptime 3 days, 3:45:48

Add configure file for CodeCompass_webserver as below:

sudo apt-get install supervisor
jshi19@ub2-uefi-b01:~/CodeCompass$ cat /etc/supervisor/conf.d/CodeCompass_webserver_edk2.conf
[program:CodeCompass_webserver]
command=/home/jshi19/CodeCompass/install/bin/CodeCompass_webserver -w /home/jshi19/CodeCompass/workspace/ -p 6251
process_name=CodeCompass_webserver
directory=/home/jshi19/CodeCompass/workspace/
autostart=true
autorestart=true
stderr_logfile=/home/jshi19/CodeCompass/workspace/test_stderr.log
stdout_logfile=/home/jshi19/CodeCompass/workspace/test_stdout.log

Start the CodeCompass_webserver:

jshi19@ub2-uefi-b01:~/CodeCompass$ sudo supervisord -c /etc/supervisor/supervisord.conf
jshi19@ub2-uefi-b01:~/CodeCompass$ sudo supervisorctl restart all

Try the Docker image:

https://github.com/Ericsson/CodeCompass/tree/master/docker

Set the docker proxy: https://docs.docker.com/config/daemon/systemd/#httphttps-proxy

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf
jshi19@ub2-uefi-b01:~$ cat /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://xxx:[email protected]:xxx"
Environment="HTTPS_PROXY=http://xxx:[email protected]:xxx"

Clone the CodeCompass repo:

jshi19@ub2-uefi-b01:~/CodeCompass$ git clone https://github.com/Ericsson/CodeCompass.git
jshi19@ub2-uefi-b01:~/CodeCompass$ cd CodeCompass

Add the proxy in the docker file:

jshi19@ub2-uefi-b01:~/CodeCompass/CodeCompass (master)$ git diff
diff --git a/docker/dev/Dockerfile b/docker/dev/Dockerfile
index 6d8c8da..94b41c7 100644
--- a/docker/dev/Dockerfile
+++ b/docker/dev/Dockerfile
@@ -6,7 +6,10 @@ FROM ubuntu:20.04
 ARG DEBIAN_FRONTEND=noninteractive

 # CodeCompass development dependencies.
-RUN set -x && apt-get update -qq \
+RUN set -x && \
+  export http_proxy=http://xxx-xxx.intel.com:xxx && \
+  export https_proxy=http://xxx-xxx.intel.com:xxx && \
+  apt-get update -qq \
   && apt-get -y install --no-install-recommends \
   cmake make \
   default-jdk \

Build the development environment image:

jshi19@ub2-uefi-b01:~/CodeCompass/CodeCompass (master)$ docker build -t codecompass:dev --file docker/dev/Dockerfile .
... ...
Successfully built 065dc218961f
Successfully tagged codecompass:dev

start the docker container from the image:

jshi19@ub2-uefi-b01:~$ docker run --rm -ti --env DATABASE=sqlite --env BUILD_TYPE=Release --volume ~/CodeCompass:/CodeCompass --volume ~/project:/projects/myproject -p 8001:8080 codecompass:dev /bin/bash

Build, install and test CodeCompass:

root@14d1a26dcc63:/# cd CodeCompass/CodeCompass/docker/dev/
root@14d1a26dcc63:/CodeCompass/CodeCompass/docker/dev# codecompass-build.sh -j16
root@14d1a26dcc63:/CodeCompass/CodeCompass/docker/dev# codecompass-build.sh install
root@14d1a26dcc63:/CodeCompass/CodeCompass/docker/dev# codecompass-build.sh test
... ...
Running tests...
Test project /CodeCompass/build
    Start 1: cppservice
1/2 Test #1: cppservice .......................   Passed    3.83 sec
    Start 2: cppparser
2/2 Test #2: cppparser ........................   Passed    2.68 sec

100% tests passed, 0 tests failed out of 2

Total Test time (real) =   6.51 sec
make: Leaving directory '/CodeCompass/build'

CodeCompass links

CodeCompass (https://github.com/Ericsson/CodeCompass, https://codecompass.net/)