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