1 2# Contributing to Fruit 3 4This file contains various information and documentation for Fruit contributors. 5If you only want to use Fruit, see the [wiki](https://github.com/google/fruit/wiki); 6you can find instructions for building Fruit manually 7[here](https://github.com/google/fruit/wiki/install#building-fruit-manually). 8 9If you actually want to change Fruit itself, that's great! Read on. 10 11### Basics 12 13#### Build systems 14 15Fruit supports two build systems: CMake (configured in `CMakeLists.txt` files) and 16[Bazel](https://www.bazel.io) (configured in `BUILD` files). 17 18This means that when you build/test Fruit code you have a choice of what build system you want to use, 19but also that for larger changes (typically, if you add new files) you might need changes in both 20`CMakeLists.txt` and `BUILD` files, to make sure that Fruit keeps building (and passing its tests) under both build 21systems. 22Both build systems are tested in Travis CI (see below). 23 24Example commands to build a development version of Fruit using CMake (with all assertions enabled) and run the tests: 25 26```bash 27cd $PATH_TO_FRUIT 28mkdir build-debug 29cd build-debug 30cmake .. -DCMAKE_BUILD_TYPE=Debug -DFRUIT_ENABLE_CLANG_TIDY=TRUE -DCMAKE_CXX_FLAGS="-Werror -DFRUIT_DEBUG=1 -DFRUIT_EXTRA_DEBUG=1 -D_GLIBCXX_DEBUG=1" 31make -j 16 32cd tests 33py.test-3 -n auto 34``` 35 36### Continuous Integration (CI) 37 38Fruit uses Travis CI for continuous integration. You can see the latest CI runs in Travis CI 39[here](https://travis-ci.org/google/fruit/builds). The CI configuration is defined in 40`extras/scripts/travis_yml_generator.py`, that generates a `.travis.yml` file (which must also be checked in, due to the 41way Travis CI is configured). 42 43When editing the `travis_yml_generator.py` script you should also update the `.travis.yml` file (in the same commit) 44by running: 45 46```bash 47cd $PATH_TO_FRUIT 48extras/scripts/travis_yml_generator.py >.travis.yml 49``` 50 51Fruit tests run in Travis CI in various configurations/environments, notably: 52 53* In Linux or OS X 54* In various Ubuntu versions 55* Using GCC or Clang 56* Optionally running under Valgrind 57* Optionally running with ASan/UBSan 58* Using CMake or Bazel 59 60These tests run after every commit in master and for every pull request (as soon as the pull request is sent). 61 62Linux tests run in Docker, using a set of images built for this purpose 63([list of images](https://hub.docker.com/r/polettimarco/fruit-basesystem/tags/)). 64 65If a test fails in Travis CI in some configuration, look at the beginning of the Travis CI Job log for a line such as: 66 67```bash 68export OS=linux; export COMPILER='clang-3.9'; export STL='libstdc++'; export UBUNTU='16.04'; extras/scripts/postsubmit.sh DebugValgrind 69``` 70 71You can then run the same command locally (from your fruit directory) to reproduce the issue. Running this 72`postsubmit.sh` script will run the tests under Docker to ensure repeatability of the results. 73 74For example, even if the failure only happens with an old Ubuntu/GCC version you don't have installed, it will download 75a Docker image containing that old Ubuntu/GCC and then run the tests inside a VM started from that image. 76 77Once `postsubmit.sh` completes, if you want you can attach to the stopped VM used to run the tests by running: 78 79```bash 80docker attach fruit 81``` 82 83This is often very useful to e.g. re-run a compilation manually with additional debug flags. 84 85When running `postsubmit.sh` manually in this way, it will run using the latest changes in your fruit directory, even if 86they aren't staged/committed yet. This allows to do a quicker edit/test cycle. 87 88To speed up the execution of `postsubmit.sh` you can also set the `NJOBS` variable, e.g.: 89 90```bash 91export NJOBS=16; export OS=linux; export COMPILER='clang-3.9'; export STL='libstdc++'; export UBUNTU='16.04'; extras/scripts/postsubmit.sh DebugValgrind 92``` 93 94The default number of jobs (used in Travis CI) is 2. 95 96### How to run Fruit tests on Windows 97 98You can import Fruit in Visual Studio (2017 and later) as a CMake project. You need to set the relevant CMake flags in 99the `CMakeSettings.json` file that Visual Studio will create. 100For example, if you installed Boost in `C:\boost\boost_1_62_0`, you can put this configuration in your 101`CMakeSettings.json`: 102 103 { 104 // See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file. 105 "configurations": [ 106 { 107 "name": "x64-Debug", 108 "generator": "Visual Studio 16 2019 Win64", 109 "configurationType": "Debug", 110 "buildRoot": "${projectDir}\\out\\build\\${name}", 111 "cmakeCommandArgs": "-DBoost_INCLUDE_DIR=C:\\boost\\boost_1_62_0 -DCMAKE_BUILD_TYPE=Debug -DFRUIT_ADDITIONAL_CXX_FLAGS=/Z7", 112 "buildCommandArgs": "-m -v:minimal", 113 "intelliSenseMode": "windows-msvc-x64" 114 }, 115 { 116 "name": "x64-Debug-noboost", 117 "generator": "Visual Studio 16 2019 Win64", 118 "configurationType": "Debug", 119 "buildRoot": "${projectDir}\\out\\build\\${name}", 120 "cmakeCommandArgs": "-DFRUIT_USES_BOOST=False -DCMAKE_BUILD_TYPE=Debug -DFRUIT_ADDITIONAL_CXX_FLAGS=/Z7", 121 "buildCommandArgs": "-m -v:minimal", 122 "intelliSenseMode": "windows-msvc-x64" 123 } 124 125 ] 126 } 127 128The `/Z7` flag instructs Visual Studio to use the C7 format for debugging information, which allows Fruit's tests to run in parallel without interfering with each other. 129 130If you don't want to use Boost, you can replace the `-DBoost_INCLUDE_DIR=...` flags above with `-DFRUIT_USES_BOOST=False`. 131 132You can now run CMake within Visual Studio (from the menu: CMake -> Cache -> Generate -> CMakeLists.txt) and build Fruit (from the menu: CMake -> Build All). 133 134You can also run tests, but *only* from the command-line (after building Fruit from Visual Studio), running tests from Visual Studio doesn't work. 135 136To do that, you'll need python3 installed (you can download it [here](https://www.python.org/downloads/)). 137 138You'll also some Python packages. You can install them with: 139 140 pip install absl-py 141 pip install pytest 142 pip install pytest-xdist 143 144To do so: 145 146* Open the Start menu 147* From there, open the "Native Tools Command Prompt for VS 2017" shell for the chosen architecture. For example, "x64 Native Tools Command Prompt for VS 2017". 148* In Visual Studio, open the Output view (from the menu: View -> Output) and select "CMake" in the "Show output from:" dropdown menu. 149* Scroll to the beginning of that view. You should see two lines starting with "Command line" and "Working directory" respectively. 150* Cd to that working directory in the shell. For example, if the path in the "Working directory" line is `C:\Users\Marco\AppData\Local\CMakeBuild\fa17dda0-4eec-6438-a358-e1253b7e86ff\build\x64-Debug`, you can run `cd "C:\Users\Marco\AppData\Local\CMakeBuild\fa17dda0-4eec-6438-a358-e1253b7e86ff\build\x64-Debug"`. 151* Cd to the "tests" subdirectory ("cd tests"). 152* Then run pytest, e.g. `py.test -n auto`. 153 154### Sending pull requests 155 156If you send a pull request, you should make sure that these CI tests are passing. They will run automatically on your 157pull request as soon as you send it. 158 159As an exception, if the current master also failed the last CI run feel free to send the pull request anyway (you can go 160[here](https://travis-ci.org/google/fruit) to check if that's the case). 161 162If a test fails, see the CI section above for informations on how to reproduce. 163 164You should also make sure that your code: 165 166* Is formatted correctly ([more details here](#code-style)) 167* Has appropriate tests (if your change is user-visible, or if you're introducing new branches that should be tested) 168 169### What to install in order to develop Fruit code 170 171In addition to 172[the compiler you need to install to build Fruit](https://github.com/google/fruit/wiki/install#dependencies), 173when developing Fruit code you might need some of the following software. Note that depending on your change you may or 174may not need all of these; you might want to go ahead without these and then only install additional things if you get 175an error about a missing tool. 176 177* CMake 178* Bazel ([installation instructions](https://www.bazel.io/docs/install.html)) 179* Valgrind 180* Docker 181 182## Useful command for fast edit/rebuild/retest cycles 183 184This command uses Bazel to run the tests (so you need to have it installed in order to use this). 185Bazel has a much more fine-grained picture of what tests depend on what source files, so it will often avoid running 186tests that have passed before when it knows that they will pass (unlike py.test that runs the entire test suite every 187time). This is especially relevant for incremental builds when only test sources have changed (e.g. after adjusting an 188expectation in a test or fixing a bug in the test); there is little difference when changing `src/` or `include/` 189because all tests will be re-run anyway. 190 191```bash 192cd $PATH_TO_FRUIT/extras/bazel_root 193bazel test --test_output=errors \ 194 --test_summary=terse \ 195 //third_party/fruit/... 196``` 197 198## Checking test coverage 199 200Fruit's test suite supports collecting test coverage (only when building with GCC on Linux using CMake). 201Example commands: 202 203```bash 204cd $PATH_TO_FRUIT 205mkdir build-coverage 206cd build-coverage 207CXX=g++-6 cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo -DFRUIT_ENABLE_COVERAGE=ON 208make -j 10 209(cd tests; py.test-3 -n auto) 210lcov --rc lcov_branch_coverage=1 --capture --directory . --output-file coverage.info 211lcov --rc lcov_branch_coverage=1 --remove coverage.info '/usr/include/*' '/tmp/*' -o coverage-filtered.info 212genhtml --no-function-coverage --rc lcov_branch_coverage=1 --rc genhtml_hi_limit=100 coverage-filtered.info --output-directory html 213google-chrome html/index.html 214``` 215 216The important figures for each file are: 217* Percentage of lines covered 218* Percentage of branches covered 219 220Ideally, they should both be 100%. The `LCOV_EXCL_LINE` and `LCOV_EXCL_BR_LINE` markers can be used to mark lines and 221branches (respectively) that can't be covered and therefore should be excluded. 222 223Note that the "percentage of **functions** covered" metric is not meaningful for Fruit, since it considers each 224instantiation of a template function/method as separate (even if they share the same source lines). 225 226## Code style 227 228C++ code in Fruit should be indented using clang-format (a `.clang-format` file is provided in the Fruit root 229directory). You can re-indent all code using this command: 230 231```bash 232$ clang-format -i $(git ls-files | egrep '\.cpp|\.h' ) 233``` 234 235## Reporting vulnerabilities 236 237In case of a security vulnerability in Fruit, please contact [poletti.marco@gmail.com](mailto:poletti.marco@gmail.com) directly instead of using the public issue tracker. 238