folly README

The project's README from github.com/facebook/folly. As one markdown file: README.md.

Folly: Facebook Open-source Library

What is folly?

Logo Folly

Folly (an acronym loosely after Facebook Open Source Library) is a library of C++20 components designed with practicality and efficiency in mind. Folly contains a variety of core library components used extensively at Facebook. In particular, it's often a dependency of Facebook's other open source C++ efforts and place where those projects can share code.

It complements (as opposed to competing against) offerings such as Boost and of course std. In fact, we embark on defining our own component only when something we need is either not available, or does not meet the needed performance profile. We endeavor to remove things from folly if or when std or Boost obsoletes them.

Performance concerns permeate much of Folly, sometimes leading to designs that are more idiosyncratic than they would otherwise be (see e.g. PackedSyncPtr.h, SmallLocks.h). Good performance at large scale is a unifying theme in all of Folly.

Check it out in the intro video

Explain Like I’m 5: Folly

Logical Design

Folly is a collection of relatively independent components, some as simple as a few symbols. There is no restriction on internal dependencies, meaning that a given folly module may use any other folly components.

All symbols are defined in the top-level namespace folly, except of course macros. Macro names are ALL_UPPERCASE and should be prefixed with FOLLY_. Namespace folly defines other internal namespaces such as internal or detail. User code should not depend on symbols in those namespaces.

Physical Design

At the top level Folly uses the classic "stuttering" scheme folly/folly used by Boost and others. The first directory serves as an installation root of the library (with possible versioning a la folly-1.0/), and the second is to distinguish the library when including files, e.g. #include <folly/FBString.h>.

The directory structure is flat (mimicking the namespace structure), i.e. we don't have an elaborate directory hierarchy (it is possible this will change in future versions). The subdirectory experimental contains files that are used inside folly and possibly at Facebook but not considered stable enough for client use. Your code should not use files in folly/experimental lest it may break when you update Folly.

The folly/folly/test subdirectory includes the unittests for all components, usually named ComponentXyzTest.cpp for each ComponentXyz.*. The folly/folly/docs directory contains documentation.

What's in it?

Because of folly's fairly flat structure, the best way to see what's in it is to look at the headers in top level folly/ directory. You can also check the docs folder for documentation, starting with the overview.

Folly is published on GitHub at https://github.com/facebook/folly.

Build Notes

Because folly does not provide any ABI compatibility guarantees from commit to commit, we generally recommend building folly as a static library.

folly supports gcc (5.1+), clang, or MSVC. It should run on Linux (x86-32, x86-64, and ARM), iOS, macOS, and Windows (x86-64). The CMake build is only tested on some of these platforms; at a minimum, we aim to support macOS and Linux (on the latest Ubuntu LTS release or newer.)

getdeps.py

This script is used by many of Meta's OSS tools. It will download and build all of the necessary dependencies first, and will then invoke cmake etc to build folly. This will help ensure that you build with relevant versions of all of the dependent libraries, taking into account what versions are installed locally on your system.

It's written in python so you'll need python3.6 or later on your PATH. It works on Linux, macOS and Windows.

The settings for folly's cmake build are held in its getdeps manifest build/fbcode_builder/manifests/folly, which you can edit locally if desired.

Dependencies

If on Linux or MacOS (with homebrew installed) you can install system dependencies to save building them:

# Clone the repo
git clone https://github.com/facebook/folly
# Install dependencies
cd folly
sudo ./build/fbcode_builder/getdeps.py install-system-deps --recursive

If you'd like to see the packages before installing them:

./build/fbcode_builder/getdeps.py install-system-deps --dry-run --recursive

On other platforms or if on Linux and without system dependencies getdeps.py will mostly download and build them for you during the build step.

Some of the dependencies getdeps.py uses and installs are:

Build

This script will download and build all of the necessary dependencies first, and will then invoke cmake etc to build folly. This will help ensure that you build with relevant versions of all of the dependent libraries, taking into account what versions are installed locally on your system.

getdeps.py currently requires python 3.6+ to be on your path.

getdeps.py will invoke cmake etc.

# Clone the repo
git clone https://github.com/facebook/folly
cd folly
# Build, using system dependencies if available
python3 ./build/fbcode_builder/getdeps.py --allow-system-packages build

It puts output in its scratch area:

You can also specify a --scratch-path argument to control the location of the scratch directory used for the build. You can find the default scratch install location from logs or with python3 ./build/fbcode_builder/getdeps.py show-inst-dir.

There are also --install-dir and --install-prefix arguments to provide some more fine-grained control of the installation directories. However, given that folly provides no compatibility guarantees between commits we generally recommend building and installing the libraries to a temporary location, and then pointing your project's build at this temporary location, rather than installing folly in the traditional system installation directories. e.g., if you are building with CMake you can use the CMAKE_PREFIX_PATH variable to allow CMake to find folly in this temporary installation directory when building your project.

If you want to invoke cmake again to iterate, there is a helpful run_cmake.py script output in the scratch build directory. You can find the scratch build directory from logs or with python3 ./build/fbcode_builder/getdeps.py show-build-dir.

Run tests

By default getdeps.py will build the tests for folly. To run them:

cd folly
python3 ./build/fbcode_builder/getdeps.py --allow-system-packages test

build.sh/build.bat wrapper

build.sh can be used on Linux and MacOS, on Windows use the build.bat script instead. Its a wrapper around getdeps.py.

Build with cmake directly

If you don't want to let getdeps invoke cmake for you then by default, building the tests is disabled as part of the CMake all target. To build the tests, specify -DBUILD_TESTS=ON to CMake at configure time.

NB if you want to invoke cmake again to iterate on a getdeps.py build, there is a helpful run_cmake.py script output in the scratch-path build directory. You can find the scratch build directory from logs or with python3 ./build/fbcode_builder/getdeps.py show-build-dir.

Running tests with ctests also works if you cd to the build dir, e.g. (cd $(python3 ./build/fbcode_builder/getdeps.py show-build-dir) && ctest)

Finding dependencies in non-default locations

If you have boost, gtest, or other dependencies installed in a non-default location, you can use the CMAKE_INCLUDE_PATH and CMAKE_LIBRARY_PATH variables to make CMAKE look also look for header files and libraries in non-standard locations. For example, to also search the directories /alt/include/path1 and /alt/include/path2 for header files and the directories /alt/lib/path1 and /alt/lib/path2 for libraries, you can invoke cmake as follows:

cmake \
  -DCMAKE_INCLUDE_PATH=/alt/include/path1:/alt/include/path2 \
  -DCMAKE_LIBRARY_PATH=/alt/lib/path1:/alt/lib/path2 ...

Ubuntu LTS, CentOS Stream, Fedora

Use the getdeps.py approach above. We test in CI on Ubuntu LTS, and occasionally on other distros.

If you find the set of system packages is not quite right for your chosen distro, you can specify distro version specific overrides in the dependency manifests (e.g. https://github.com/facebook/folly/blob/main/build/fbcode_builder/manifests/boost ). You could probably make it work on most recent Ubuntu/Debian or Fedora/Redhat derived distributions.

At time of writing (Dec 2021) there is a build break on GCC 11.x based systems in lang_badge_test. If you don't need badge functionality you can work around by commenting it out from CMakeLists.txt (unfortunately fbthrift does need it)

Windows (Vcpkg)

Note that many tests are disabled for folly Windows builds, you can see them in the log from the cmake configure step, or by looking for WINDOWS_DISABLED in CMakeLists.txt

That said, getdeps.py builds work on Windows and are tested in CI.

If you prefer, you can try Vcpkg. folly is available in Vcpkg and releases may be built via vcpkg install folly:x64-windows.

You may also use vcpkg install folly:x64-windows --head to build against main.

macOS

getdeps.py builds work on macOS and are tested in CI, however if you prefer, you can try one of the macOS package managers

Homebrew

folly is available as a Formula and releases may be built via brew install folly.

You may also use folly/build/bootstrap-osx-homebrew.sh to build against main:

  ./folly/build/bootstrap-osx-homebrew.sh

This will create a build directory _build in the top-level.

MacPorts

Install the required packages from MacPorts:

  sudo port install \
    boost \
    cmake \
    gflags \
    git \
    google-glog \
    libevent \
    libtool \
    lz4 \
    lzma \
    openssl \
    snappy \
    xz \
    zlib

Download and install folly with the parameters listed below:

  git clone https://github.com/facebook/folly.git
  cd folly
  mkdir _build
  cd _build
  cmake ..
  make
  sudo make install

folly (facebook/folly)

Searching Database…

30.6K ·GitHub
这是什么东西?Folly(Facebook Open Source Library)是一个 C++20 组件库,以实用和高效为设计目标,在 Facebook 内部被广泛使用,也常常是 Facebook 其他开源 C++ 项目的依赖。
它是一整个大库,还是各自独立的部件集合?它是一批相对独立的组件,统一放在顶层命名空间 folly 之下——有的组件只包含少数几个符号,各个模块内部也可以随意互相使用。
怎么构建和安装?克隆仓库后运行 python3 ./build/fbcodebuilder/getdeps.py --allow-system-packages build,它会自动拉取依赖并调用 CMake。也有预编译版本:brew install folly 或 vcpkg install folly:x64-windows。
构建之前需要先装好什么?PATH 里要有 Python 3.6+。其余的由 getdeps.py 自己下载并构建,包括一个 C++14 版本的 Boost 和 googletest;在 Linux/macOS 上也可以先用它的 install-system-deps 命令预装依赖包。
依赖它之前有什么坑要注意?不同提交之间没有 ABI 兼容性保证,所以建议把它构建成静态库放到一个临时目录,再让你的项目指向那里(比如通过 CMAKE_PREFIX_PATH),而不是装到系统里。
既然 std 和 Boost 都有了,我为什么还要用 Folly?它是补充而不是竞争:只有当所需功能在现有库里缺失、或者性能达不到要求时,Folly 才加入自己的组件;一旦 std 或 Boost 能满足需求,这些组件就会被淘汰。
支持哪些平台和编译器?支持 Linux(x86-32/64、ARM)、iOS、macOS 和 Windows x86-64,可用 gcc 5.1+、clang 或 MSVC。CMake 构建主要在 macOS 和 Linux 上测试——需要最新 Ubuntu LTS 或更新版本。
为什么 Folly 的一些组件看起来那么另类?性能是贯穿整个库的核心考量,而在大规模场景下追求高性能有时会催生很特别的设计——README 里就举了 PackedSyncPtr.h 和 SmallLocks.h 作为例子。
我可以包含 folly/experimental 里的头文件吗?最好不要:这些组件虽然在 folly 内部和 Facebook 有使用,但并不保证对外稳定,依赖它们的代码在升级 Folly 时可能会编译不过。
怎么运行 Folly 的测试?getdeps.py 默认就会构建测试——运行 python3 ./build/fbcodebuilder/getdeps.py --allow-system-packages test 即可。需要 googletest,而且 Windows 构建里很多测试是禁用的。
构建完成后库文件放在哪里?放在 getdeps 的临时目录下,路径是 installed/folly/lib/libfolly.a,是一个静态库。可以用 --scratch-path 改位置,用 show-inst-dir 查看位置。
Creator
facebook
Size
61.2 MB
Stars
30.6K
Age
14y
Commits · 90 Days