Using kwwrap plus kwinject to generate a build specification
Using kwwrap plus kwinject to generate a build specificationIf you cannot use kwinject to integrate with your build (for example, if you run distributed builds ), kwwrap can be integrated into your organization's build process to generate a build trace. The build trace is used as input to kwinject to create a build specification. Editing the makefileIn the simplest scenario for a kwwrap-based build integration, you are doing a make build, and your makefile defines several widely used variations on the name of your C compiler, C++ compiler, and linker/librarian. Inserting kwwrap into your makefile causes kwwrap to execute for every compile and link command in your build, and generate a build trace while your build runs. A build trace is a file that contains the sequence of process calls that took place during your build (for example, the make command). Note that you must always specify the full path to the trace file, so that kwwrap is always writing to the same file. In addition, if you run the kwwrap-enabled build more than once with the same settings, delete the previously generated build trace before running the build again. Otherwise, the trace file will contain commands from multiple builds. Note: For distributed builds, you must run the following procedure on all build machines and merge the resultant build trace files.
To integrate kwwrap with your build:
ExampleZlib is a small open-source project that uses GNU make to build on UNIX. The makefile defines the environment variables CC, AR and LDSHARED to define the tools used to compile and generate static and dynamic libraries. You can edit the makefile to use a wrapped version of the compiler and linker by adding kwwrap:
$ cd /path/to/zlib-1.2.2 ... configure and generate Makefile $ ./configure ... now edit the Makefile -- file: zlib-1.2.2/Makefile ... CC=kwwrap -o /path/to/kwwrap.trace gcc LDSHARED=kwwrap -o /path/to/kwwrap.trace gcc AR=kwwrap -o /path/to/kwwrap.trace ar rc ... -- cut ... run the build using wrapped compiler/linker; kwwrap.trace will be generated $ make ... convert the raw build trace to a build specification $ kwinject -t /path/to/kwwrap.trace -o kwinject.out If you cannot substitute your compiler with a command plus optionsFor situations where the compiler must be substituted with a single command (that is, without options such as -o), on Windows, you can create a configuration file for kwwrap. The file must be named kwwrap.conf, and it must be located in the same directory as kwwrap. Each line of the kwwrap configuration file has the following format:<executable_name>=<options_list> where
Notes
Example kwwrap.conf file cl.exe=-o "C:\My traces\mozilla.trace" "C:\Program Files\Microsoft Visual Studio .NET\Vc7\bin\cl.exe" link.exe=-o "C:\My traces\mozilla.trace" "C:\Program Files\Microsoft Visual Studio .NET\Vc7\bin\link.exe" When editing the makefile is not an option
Using environment variablesIf changing the original makefile is not an option, you can use environment variables to override the values set by the makefile. You can then use the --environment-overrides (-e) option of GNU make or MS nmake. UNIX example, using make and gcc ... building on UNIX $ cd zlib-1.2.2 ... build $ ./configure ... $ CC="kwwrap -o /path/to/kwwrap.trace gcc" $ AR="kwwrap -o /path/to/kwwrap.trace ar rc" $ LDSHARED="kwwrap -o /path/to/kwwrap.trace gcc" $ export CC AR LDSHARED ... build using wrapped compiler/linker; kwwrap.trace will be generated $ make -e ... convert build trace to build specification $ kwinject -t /path/to/kwwrap.trace -o kwinject.out Windows example On the Windows platform, zlib is built with Microsoft's nmake and cl-compiler. The Microsoft-specific makefile uses a slightly different set of variables: CC, AR and LD. ... building on Windows > cd d:\path\to\zlib-1.2.2 ... set up environment variables > set CC=kwwrap -o d:\path\to\kwwrap.trace cl > set AR=kwwrap -o d:\path\to\kwwrap.trace lib > set LD=kwwrap -o d:\path\to\kwwrap.trace link ... build uses wrapped compiler/linker; kwwrap.trace will be generated > nmake -e -f win32\Makefile.msc ... convert build trace to a build specification > kwinject -t d:\path\to\kwwrap.trace -o kwinject.out
Creating wrapper scriptsxerces is a medium-sized open-source project that uses GNU make to build on UNIX. The simple approach of editing the makefile does not apply here, because xerces uses a custom configuration script that does not accept a compiler name that includes spaces, such as "kwwrap gcc". Instead, we can create wrapper scripts. Example $ cd /path/to/xerces-c-src_2_6_0 ... create a directory for shell-wrappers $ mkdir kw-wrappers ... create several simple wrapper scripts and save them into the kw-wrappers directory: -- file: kw-wrappers/kwwrap-gcc #!/bin/sh exec kwwrap -o /path/to/kwwrap.trace gcc "$@" -- file: kw-wrappers/kwwrap-g++ #!/bin/sh exec kwwrap -o /path/to/kwwrap.trace g++ "$@" -- file: kw-wrappers/kwwrap-ar #!/bin/sh exec kwwrap -o /path/to/kwwrap.trace ar "$@" -- cut ... don’t forget to set the execute permissions on the created scripts $ chmod +x kw-wrappers/* ... set up PATH and other environment variables $ PATH=$PWD/kw-wrappers:$PATH $ CC=kwwrap-gcc $ CXX=kwwrap-g++ $ AR=kwwrap-ar $ export CC CXX AR ... configure xerces $ XERCESCROOT=$PWD $ cd $XERCESCROOT/src/xercesc $ ./runConfigure -p linux -c $CC -x $CXX ... build using the wrapper scripts; build trace will be generated $ make ... generate a build specification from the build trace $ kwinject -t /path/to/kwwrap/trace -o kwinject.out Use kwwrap with CMakeIf you use CMake to manage your build process, you can modify your CMakeLists.txt file to generate a build trace file that can be used as input to kwinject to generate your build specification file. You can use this method if you have System Integrity Protection (SIP) enabled on Mac OS X 10.10 or later. set(KWWRAP_PATH "<path to Klocwork install directory>/bin/kwwrap") # Specify kwwrap full path set(KWWRAP_TRACE_PATH "${PROJECT_BINARY_DIR}/kw.trace") # Specify path to Klocwork trace file set(KWWRAP_TARGET "${KWWRAP_PATH} -o ${KWWRAP_TRACE_PATH}") set(CMAKE_C_COMPILE_OBJECT "${KWWRAP_TARGET} ${CMAKE_C_COMPILE_OBJECT}") set(CMAKE_C_LINK_EXECUTABLE "${KWWRAP_TARGET} ${CMAKE_C_LINK_EXECUTABLE}") set(CMAKE_C_CREATE_SHARED_LIBRARY "${KWWRAP_TARGET} ${CMAKE_C_CREATE_SHARED_LIBRARY}") set(CMAKE_C_CREATE_SHARED_MODULE "${KWWRAP_TARGET} ${CMAKE_C_CREATE_SHARED_MODULE}") set(CMAKE_C_CREATE_STATIC_LIBRARY "${KWWRAP_TARGET} ${CMAKE_C_CREATE_STATIC_LIBRARY}") set(CMAKE_CXX_COMPILE_OBJECT "${KWWRAP_TARGET} ${CMAKE_CXX_COMPILE_OBJECT}") set(CMAKE_CXX_LINK_EXECUTABLE "${KWWRAP_TARGET} ${CMAKE_CXX_LINK_EXECUTABLE}") set(CMAKE_CXX_CREATE_SHARED_LIBRARY "${KWWRAP_TARGET} ${CMAKE_CXX_CREATE_SHARED_LIBRARY}") set(CMAKE_CXX_CREATE_SHARED_MODULE "${KWWRAP_TARGET} ${CMAKE_CXX_CREATE_SHARED_MODULE}") set(CMAKE_CXX_CREATE_STATIC_LIBRARY "${KWWRAP_TARGET} ${CMAKE_CXX_CREATE_STATIC_LIBRARY}") You can run a "pretend" build as a testThe --pretend option of kwwrap can be used to run a "pretend build"--a build in which your compiler and linker are never executed. With this option, kwwrap writes the supplied command line to a build trace, but does not run the command. A "pretend" build generally takes less time than a real build, and the generated build trace is not accurate. This approach therefore only makes sense for simple projects, and projects where an accurate build specification is not critical. Note: The fact that your compiler and linker are not being executed could break the build, so you may want to set your build command to ignore errors (for example, with the -i option of make).
Here is an example using zlib. This example uses environment variables to override makefile settings as described in Using environment variables. $ cd zlib-1.2.2 ... build $ ./configure ... set up environment variables $ CC="kwwrap -p -o /path/to/kwwrap.trace gcc" $ AR="kwwrap -p -o /path/to/kwwrap.trace ar rc" $ LDSHARED="kwwrap -p -o /path/to/kwwrap.trace gcc" $ export CC AR LDSHARED ... "pretend" build using wrappers; compiler/linker are not executed $ make -e -i ... convert the build trace to a build specification $ kwinject -t /path/to/kwwrap.trace -o kwinject.out What's next?
Prerequisites The Klocwork Extensibility Interface is installed as part of the Klocwork command line feature from the Klocwork Product Portal. Mac users: You must have a working version of the GCC compiler to create custom checkers. |