1# Cmake contributions 2 3Contributions to the cmake build configurations are welcome. Please 4use case sensitivity that matches modern (i.e. cmake version 2.6 and above) 5conventions of using lower-case for commands, and upper-case for 6variables. 7 8## How to build 9 10As cmake doesn't support command like `cmake clean`, it's recommended to perform an "out of source build". 11To do this, you can create a new directory and build in it: 12```sh 13cd build/cmake 14mkdir builddir 15cd builddir 16cmake .. 17make 18``` 19Then you can clean all cmake caches by simply delete the new directory: 20```sh 21rm -rf build/cmake/builddir 22``` 23 24And of course, you can directly build in build/cmake: 25```sh 26cd build/cmake 27cmake 28make 29``` 30 31To show cmake build options, you can: 32```sh 33cd build/cmake/builddir 34cmake -LH .. 35``` 36 37Bool options can be set to `ON/OFF` with `-D[option]=[ON/OFF]`. You can configure cmake options like this: 38```sh 39cd build/cmake/builddir 40cmake -DZSTD_BUILD_TESTS=ON -DZSTD_LEGACY_SUPPORT=OFF .. 41make 42``` 43 44### how to use it with CMake FetchContent 45 46For all options available, you can see it on <https://github.com/facebook/zstd/blob/dev/build/cmake/lib/CMakeLists.txt> 47```cmake 48include(FetchContent) 49 50set(ZSTD_BUILD_STATIC ON) 51set(ZSTD_BUILD_SHARED OFF) 52 53FetchContent_Declare( 54 zstd 55 URL "https://github.com/facebook/zstd/releases/download/v1.5.5/zstd-1.5.5.tar.gz" 56 DOWNLOAD_EXTRACT_TIMESTAMP TRUE 57 SOURCE_SUBDIR build/cmake 58) 59 60FetchContent_MakeAvailable(zstd) 61 62target_link_libraries( 63 ${PROJECT_NAME} 64 PRIVATE 65 libzstd_static 66) 67 68# On windows and macos this is needed 69target_include_directories( 70 ${PROJECT_NAME} 71 PRIVATE 72 ${zstd_SOURCE_DIR}/lib 73) 74``` 75 76### referring 77[Looking for a 'cmake clean' command to clear up CMake output](https://stackoverflow.com/questions/9680420/looking-for-a-cmake-clean-command-to-clear-up-cmake-output) 78 79## CMake Style Recommendations 80 81### Indent all code correctly, i.e. the body of 82 83 * if/else/endif 84 * foreach/endforeach 85 * while/endwhile 86 * macro/endmacro 87 * function/endfunction 88 89Use spaces for indenting, 2, 3 or 4 spaces preferably. Use the same amount of 90spaces for indenting as is used in the rest of the file. Do not use tabs. 91 92### Upper/lower casing 93 94Most important: use consistent upper- or lowercasing within one file ! 95 96In general, the all-lowercase style is preferred. 97 98So, this is recommended: 99 100``` 101add_executable(foo foo.c) 102``` 103 104These forms are discouraged 105 106``` 107ADD_EXECUTABLE(bar bar.c) 108Add_Executable(hello hello.c) 109aDd_ExEcUtAbLe(blub blub.c) 110``` 111 112### End commands 113To make the code easier to read, use empty commands for endforeach(), endif(), 114endfunction(), endmacro() and endwhile(). Also, use empty else() commands. 115 116For example, do this: 117 118``` 119if(FOOVAR) 120 some_command(...) 121else() 122 another_command(...) 123endif() 124``` 125 126and not this: 127 128``` 129if(BARVAR) 130 some_other_command(...) 131endif(BARVAR) 132``` 133 134### Other resources for best practices 135 136https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html#modules 137