zstd/build/cmake
Yann Collet dcf784a04b
Merge pull request #3760 from jcelerier/patch-1
[x-compile] Fix cross-compiling for AArch64 with lld
2024-01-13 11:41:30 -08:00
..
CMakeModules Disable linker flag detection on MSVC/ClangCL. 2023-03-22 22:13:57 +01:00
contrib Coalesce Almost All Copyright Notices to Standard Phrasing 2022-12-20 12:52:34 -05:00
lib Merge pull request #3760 from jcelerier/patch-1 2024-01-13 11:41:30 -08:00
programs Fixed zstd cmake shared build on windows 2023-08-27 16:51:24 +02:00
tests External matchfinder API (#3333) 2022-12-28 16:45:14 -05:00
.gitignore fixed VS2017Community build script 2018-10-03 18:42:44 -07:00
CMakeLists.txt [cmake] Remove code for compatibility with CMake < 3.0 2023-11-01 20:46:47 +01:00
README.md Add target_include_directories because windows and macos need it for me 2023-10-23 17:38:09 +02:00
zstdConfig.cmake Generate a better CMake Package Config file 2020-04-09 17:16:12 -04:00

Cmake contributions

Contributions to the cmake build configurations are welcome. Please use case sensitivity that matches modern (i.e. cmake version 2.6 and above) conventions of using lower-case for commands, and upper-case for variables.

How to build

As cmake doesn't support command like cmake clean, it's recommended to perform an "out of source build". To do this, you can create a new directory and build in it:

cd build/cmake
mkdir builddir
cd builddir
cmake ..
make

Then you can clean all cmake caches by simply delete the new directory:

rm -rf build/cmake/builddir

And of course, you can directly build in build/cmake:

cd build/cmake
cmake
make

To show cmake build options, you can:

cd build/cmake/builddir
cmake -LH ..

Bool options can be set to ON/OFF with -D[option]=[ON/OFF]. You can configure cmake options like this:

cd build/cmake/builddir
cmake -DZSTD_BUILD_TESTS=ON -DZSTD_LEGACY_SUPPORT=OFF ..
make

how to use it with CMake FetchContent

For all options available, you can see it on https://github.com/facebook/zstd/blob/dev/build/cmake/lib/CMakeLists.txt

include(FetchContent)

set(ZSTD_BUILD_STATIC ON)
set(ZSTD_BUILD_SHARED OFF)

FetchContent_Declare(
    zstd
    URL "https://github.com/facebook/zstd/releases/download/v1.5.5/zstd-1.5.5.tar.gz"
    DOWNLOAD_EXTRACT_TIMESTAMP TRUE
    SOURCE_SUBDIR build/cmake
)

FetchContent_MakeAvailable(zstd)

target_link_libraries(
    ${PROJECT_NAME}
    PRIVATE
    libzstd_static
)

# On windows and macos this is needed
target_include_directories(
    ${PROJECT_NAME}
    PRIVATE
    ${zstd_SOURCE_DIR}/lib
)

referring

Looking for a 'cmake clean' command to clear up CMake output

CMake Style Recommendations

Indent all code correctly, i.e. the body of

  • if/else/endif
  • foreach/endforeach
  • while/endwhile
  • macro/endmacro
  • function/endfunction

Use spaces for indenting, 2, 3 or 4 spaces preferably. Use the same amount of spaces for indenting as is used in the rest of the file. Do not use tabs.

Upper/lower casing

Most important: use consistent upper- or lowercasing within one file !

In general, the all-lowercase style is preferred.

So, this is recommended:

add_executable(foo foo.c)

These forms are discouraged

ADD_EXECUTABLE(bar bar.c)
Add_Executable(hello hello.c)
aDd_ExEcUtAbLe(blub blub.c)

End commands

To make the code easier to read, use empty commands for endforeach(), endif(), endfunction(), endmacro() and endwhile(). Also, use empty else() commands.

For example, do this:

if(FOOVAR)
   some_command(...)
else()
   another_command(...)
endif()

and not this:

if(BARVAR)
   some_other_command(...)
endif(BARVAR)

Other resources for best practices

https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html#modules