{"id":19454586,"url":"https://github.com/p4lang/p4c","last_synced_at":"2026-03-02T05:07:18.043Z","repository":{"id":37579922,"uuid":"55433859","full_name":"p4lang/p4c","owner":"p4lang","description":"P4_16 reference compiler","archived":false,"fork":false,"pushed_at":"2025-05-14T00:33:18.000Z","size":264707,"stargazers_count":763,"open_issues_count":422,"forks_count":464,"subscribers_count":66,"default_branch":"main","last_synced_at":"2025-05-14T01:27:18.911Z","etag":null,"topics":["p4","p4c","p4lang","p4language"],"latest_commit_sha":null,"homepage":"https://p4.org/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/p4lang.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-04-04T18:12:32.000Z","updated_at":"2025-05-12T06:48:24.000Z","dependencies_parsed_at":"2023-02-17T12:30:39.133Z","dependency_job_id":"257bc3a6-9b69-4aee-b767-f61e05c2450b","html_url":"https://github.com/p4lang/p4c","commit_stats":null,"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4lang%2Fp4c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4lang%2Fp4c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4lang%2Fp4c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4lang%2Fp4c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/p4lang","download_url":"https://codeload.github.com/p4lang/p4c/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254159836,"owners_count":22024564,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["p4","p4c","p4lang","p4language"],"created_at":"2024-11-10T17:10:20.940Z","updated_at":"2026-03-02T05:07:18.011Z","avatar_url":"https://github.com/p4lang.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!--!\n\\page getting_started Getting Started                                    \n--\u003e\n\u003c!--!\n\\internal\n--\u003e\n# P4C\n\u003c!--!\n\\endinternal\n--\u003e\n\u003c!--!\n[TOC]\n--\u003e\n[![Main Build](https://github.com/p4lang/p4c/actions/workflows/ci-test-debian.yml/badge.svg)](https://github.com/p4lang/p4c/actions/workflows/ci-test-debian.yml)\n[![Main Build](https://github.com/p4lang/p4c/actions/workflows/ci-test-fedora.yml/badge.svg)](https://github.com/p4lang/p4c/actions/workflows/ci-test-fedora.yml)\n[![Main Build](https://github.com/p4lang/p4c/actions/workflows/ci-test-mac.yml/badge.svg)](https://github.com/p4lang/p4c/actions/workflows/ci-test-mac.yml)\n[![Bazel Build](https://github.com/p4lang/p4c/actions/workflows/ci-bazel.yml/badge.svg)](https://github.com/p4lang/p4c/actions/workflows/ci-bazel.yml)\n[![Validation](https://github.com/p4lang/p4c/actions/workflows/ci-validation-nightly.yml/badge.svg)](https://github.com/p4lang/p4c/actions/workflows/ci-validation-nightly.yml)\n[![Docker Container](https://github.com/p4lang/p4c/actions/workflows/ci-container-image.yml/badge.svg)](https://github.com/p4lang/p4c/actions/workflows/ci-container-image.yml)\n\n\u003c!--!\n\\internal\n--\u003e\n* [Sample Backends in P4C](#sample-backends-in-p4c)\n* [Getting started](#getting-started)\n   * [Installing packaged versions of P4C](#installing-packaged-versions-of-p4c)\n   * [Installing P4C from source](#installing-p4c-from-source)\n* [Dependencies](#dependencies)\n   * [Ubuntu dependencies](#ubuntu-dependencies)\n   * [Fedora dependencies](#fedora-dependencies)\n   * [macOS dependencies](#macos-dependencies)\n   * [Garbage collector](#garbage-collector)\n   * [Crash dumps](#crash-dumps)\n* [Development tools](#development-tools)\n   * [Git setup](#git-setup)\n* [Docker](#docker)\n* [Bazel](#bazel)\n* [Build system](#build-system)\n   * [Defining new CMake targets](#defining-new-cmake-targets)\n* [Known issues](#known-issues)\n   * [Frontend](#frontend)\n   * [Backends](#backends)\n* [How to Contribute](#how-to-contribute)\n* [P4 Compiler Onboarding](#p4-compiler-onboarding)\n* [Contact](#contact)\n\u003c!--!\n\\endinternal\n--\u003e\nP4C is a reference compiler for the P4 programming language.\nIt supports both P4-14 and P4-16; you can find more information about P4\n[here](http://p4.org) and the specifications for both versions of the language\n[here](https://p4.org/specs).\nOne fact attesting to the level of quality and completeness of P4C's\ncode is that its front-end code, mid-end code, and P4C-graphs back end\nare used as the basis for at least one commercially supported P4\ncompiler.\n\nP4C is modular; it provides a standard frontend and midend which can be combined\nwith a target-specific backend to create a complete P4 compiler. The goal is to\nmake adding new backends easy.\n\n\u003c!--!\n\\include{doc} \"../docs/doxygen/01_overview.md\"\n--\u003e\n## Sample Backends in P4C\nP4C includes seven sample backends, catering to different target architectures and use cases:\n* p4c-bm2-ss: can be used to target the P4 `simple_switch` written using\n  the [BMv2 behavioral model](https://github.com/p4lang/behavioral-model),\n* p4c-dpdk: can be used to target the [DPDK software switch (SWX) pipeline](https://doc.dpdk.org/guides/rel_notes/release_20_11.html),\n* p4c-ebpf: can be used to generate C code which can be compiled to [eBPF](https://en.wikipedia.org/wiki/Berkeley_Packet_Filter)\n  and then loaded in the Linux kernel. The eBPF backend currently implements three architecture models:\n  [ebpf_model.p4 for packet filtering, xdp_model.p4 for XDP](./backends/ebpf/README.md) and\n  [the fully-featured PSA (Portable Switch Architecture) model](./backends/ebpf/psa/README.md).\n* p4test: a source-to-source P4 translator which can be used for\n  testing, learning compiler internals and debugging,\n* p4c-graphs: can be used to generate visual representations of a P4 program;\n  for now it only supports generating graphs of top-level control flows, and\n* p4c-ubpf: can be used to generate eBPF code that runs in user-space.\n* p4tools: a platform for P4 test utilities, including a test-case generator for P4 programs.\nSample command lines:\n\nCompile P4_16 or P4_14 source code.  If your program successfully\ncompiles, the command will create files with the same base name as the\nP4 program you supplied, and the following suffixes instead of the\n`.p4`:\n\n+ a file with suffix `.p4i`, which is the output from running the\n  preprocessor on your P4 program.\n+ a file with suffix `.json` that is the JSON file format expected by\n  BMv2 behavioral model `simple_switch`.\n\n```bash\np4c --target bmv2 --arch v1model my-p4-16-prog.p4\np4c --target bmv2 --arch v1model --std p4-14 my-p4-14-prog.p4\n```\n\nBy adding the option `--p4runtime-files \u003cfilename\u003e.txt` as shown in\nthe example commands below, P4C will also create a file\n`\u003cfilename\u003e.txt`.  This is a text format \"P4Info\" file, containing a\ndescription of the tables and other objects in your P4 program that\nhave an auto-generated control plane API.\n\n```\np4c --target bmv2 --arch v1model --p4runtime-files my-p4-16-prog.p4info.txt my-p4-16-prog.p4\np4c --target bmv2 --arch v1model --p4runtime-files my-p4-14-prog.p4info.txt --std p4-14 my-p4-14-prog.p4\n```\n\nAll of these commands take the `--help` argument to show documentation\nof supported command line options.  `p4c --target-help` shows the\nsupported \"target, arch\" pairs.\n\n```bash\np4c --help\np4c --target-help\n```\n\nAuto-translate P4_14 source to P4_16 source:\n\n```bash\np4test --std p4-14 my-p4-14-prog.p4 --pp auto-translated-p4-16-prog.p4\n```\n\nCheck syntax of P4_16 or P4_14 source code, without limitations that\nmight be imposed by any particular compiler back end.  There is no\noutput for these commands other than error and/or warning messages.\n\n```bash\np4test my-p4-16-prog.p4\np4test --std p4-14 my-p4-14-prog.p4\n```\n\nGenerate GraphViz \".dot\" files for parsers and controls of a P4_16 or\nP4_14 source program.\n\n```bash\np4c-graphs my-p4-16-prog.p4\np4c-graphs --std p4-14 my-p4-14-prog.p4\n```\n\nGenerate PDF of parser instance named \"ParserImpl\" generated by the\n`p4c-graphs` command above (search for graphviz below for its install\ninstructions):\n\n```bash\ndot -Tpdf ParserImpl.dot \u003e ParserImpl.pdf\n```\n\n## Getting started\n\n### Installing packaged versions of P4C\n\nP4C has package support for several Ubuntu and Debian distributions.\n\n#### Ubuntu\n\nA P4C package is available in the following repositories for Ubuntu 20.04 and newer.\n\n```bash\nsource /etc/lsb-release\necho \"deb http://download.opensuse.org/repositories/home:/p4lang/xUbuntu_${DISTRIB_RELEASE}/ /\" | sudo tee /etc/apt/sources.list.d/home:p4lang.list\ncurl -fsSL https://download.opensuse.org/repositories/home:p4lang/xUbuntu_${DISTRIB_RELEASE}/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_p4lang.gpg \u003e /dev/null\nsudo apt-get update\nsudo apt install p4lang-p4c\n```\n\n#### Debian\n\nFor Debian 11 (Bullseye) it can be installed as follows:\n\n```bash\necho 'deb https://download.opensuse.org/repositories/home:/p4lang/Debian_11/ /' | sudo tee /etc/apt/sources.list.d/home:p4lang.list\ncurl -fsSL https://download.opensuse.org/repositories/home:p4lang/Debian_11/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_p4lang.gpg \u003e /dev/null\nsudo apt update\nsudo apt install p4lang-p4c\n```\n\nIf you cannot use a repository to install P4C, you can download the `.deb` file\nfor your release and install it manually. You need to download a new file each\ntime you want to upgrade P4C.\n\n1. Go to [p4lang-p4c package page on OpenSUSE Build Service](https://build.opensuse.org/package/show/home:p4lang/p4lang-p4c), click on\n\"Download package\" and choose your operating system version.\n\n2. Install P4C, changing the path below to the path where you downloaded the package.\n\n```bash\nsudo dpkg -i /path/to/package.deb\n```\n\n### Installing P4C from source\n1.  Clone the repository. It includes submodules, so be sure to use\n    `--recursive` to pull them in:\n    ```\n    git clone --recursive https://github.com/p4lang/p4c.git\n    ```\n    If you forgot `--recursive`, you can update the submodules at any time using:\n    ```\n    git submodule update --init --recursive\n    ```\n\n2.  Install [dependencies](#dependencies). You can find specific instructions\n    for Ubuntu 22.04 [here](#ubuntu-dependencies) and for macOS 11\n    [here](#macos-dependencies).  You can also look at the\n    [CI installation script](https://github.com/p4lang/p4c/blob/main/tools/ci-build.sh).\n\n3.  Build. Building should also take place in a subdirectory named `build`.\n    ```\n    mkdir build\n    cd build\n    cmake .. \u003coptional arguments\u003e\n    make -j4\n    make -j4 check\n    ```\n    The cmake command takes the following optional arguments to\n    further customize the build (see file `CMakeLists.txt` for the full list):\n     - `-DCMAKE_BUILD_TYPE=Release|Debug` -- set CMAKE_BUILD_TYPE to\n      Release or Debug to build with optimizations or with debug\n      symbols to run in gdb. Default is Release.\n     - `-DCMAKE_INSTALL_PREFIX=\u003cpath\u003e` -- set the directory where\n       `make install` installs the compiler. Defaults to /usr/local.\n     - `-DENABLE_BMV2=ON|OFF`. Enable [the bmv2 backend](backends/bmv2/README.md). Default ON.\n     - `-DENABLE_EBPF=ON|OFF`. Enable [the ebpf backend](backends/ebpf/README.md). Default ON.\n     - `-DENABLE_P4TC=ON|OFF`. Enable [the TC backend](backends/tc/README.md). Default ON.\n     - `-DENABLE_UBPF=ON|OFF`. Enable [the ubpf backend](backends/ubpf/README.md). Default ON.\n     - `-DENABLE_DPDK=ON|OFF`. Enable [the DPDK backend](backends/dpdk/README.md). Default ON.\n     - `-DENABLE_P4C_GRAPHS=ON|OFF`. Enable [the p4c-graphs backend](backends/graphs/README.md). Default ON.\n     - `-DENABLE_P4FMT=ON|OFF`. Enable [the p4fmt backend](backends/p4fmt/README.md). Default ON.\n     - `-DENABLE_P4TEST=ON|OFF`. Enable [the p4test backend](backends/p4test/README.md). Default ON.\n     - `-DENABLE_TEST_TOOLS=ON|OFF`. Enable [the p4tools backend](backends/p4tools/README.md). Default OFF.\n     - `-DENABLE_DOCS=ON|OFF`. Build documentation. Default is OFF.\n     - `-DENABLE_GC=ON|OFF`. Enable the use of the garbage collection\n       library. Default is ON.\n     - `-DENABLE_GTESTS=ON|OFF`. Enable building and running GTest unit tests.\n       Default is ON.\n     - `-DP4C_USE_PREINSTALLED_ABSEIL=ON|OFF`. Try to find a system version of Abseil instead of a fetched one. Default is OFF.\n     - `-DP4C_USE_PREINSTALLED_PROTOBUF=ON|OFF`. Try to find a system version of Protobuf instead of a CMake version. Default is OFF.\n     - `-DENABLE_ABSEIL_STATIC=ON|OFF`. Enable the use of static abseil libraries. Default is ON. Only has an effect when `P4C_USE_PREINSTALLED_ABSEIL` is enabled.\n     - `-DENABLE_PROTOBUF_STATIC=ON|OFF`. Enable the use of static protobuf libraries. Default is ON.\n       Only has an effect when `P4C_USE_PREINSTALLED_PROTOBUF` is enabled.\n     - `-DENABLE_MULTITHREAD=ON|OFF`. Use multithreading.  Default is\n       OFF.\n     - `-DBUILD_LINK_WITH_GOLD=ON|OFF`. Use Gold linker for build if available.\n     - `-DBUILD_LINK_WITH_LLD=ON|OFF`. Use LLD linker for build if available (overrides `BUILD_LINK_WITH_GOLD`).\n     - `-DENABLE_LTO=ON|OFF`. Use Link Time Optimization (LTO).  Default is OFF.\n     - `-DENABLE_WERROR=ON|OFF`. Treat warnings as errors.  Default is OFF.\n     - `-DCMAKE_UNITY_BUILD=ON|OFF `. Enable [unity builds](https://cmake.org/cmake/help/latest/prop_tgt/UNITY_BUILD.html) for faster compilation.  Default is OFF.\n\n    If adding new targets to this build system, please see\n    [instructions](#defining-new-cmake-targets).\n\n4.  (Optional) Install the compiler and the P4 shared headers globally.\n    ```\n    sudo make install\n    ```\n    The compiler driver `p4c` and binaries for each of the backends are\n    installed in `/usr/local/bin` by default; the P4 headers are placed in\n    `/usr/local/share/p4c`.\n\n5.  You're ready to go! You should be able to compile a P4-16 program for BMV2\n    using:\n    ```\n    p4c -b bmv2-ss-p4org program.p4 -o program.bmv2.json\n    ```\n\nIf you plan to contribute to P4C, you'll find more useful information\n[here](#development-tools).\n\n## Dependencies\n\nUbuntu 22.04 is the officially supported platform for P4C. There's also\nunofficial support for macOS 11. Other platforms are untested; you can try to\nuse them, but YMMV.\n\n- A C++17 compiler. GCC 9.1 or later or Clang 6.0 or later is required.\n\n- `git` for version control\n\n- CMake 3.16.3 or higher\n\n- Boehm-Weiser garbage-collector C++ library\n\n- GNU Bison and Flex for the parser and lexical analyzer generators.\n\n- Google Protocol Buffers v3.25.3 or higher for control plane API generation\n\n- C++ boost library\n\n- Python 3 for scripting and running tests\n\n- Optional: Documentation generation requires Doxygen (1.13.2) and Graphviz (2.38.0 or higher).\n\nBackends may have additional dependencies. The dependencies for the backends\nincluded with `P4C` are documented here:\n  * [BMv2](backends/bmv2/README.md)\n  * [eBPF](backends/ebpf/README.md)\n  * [graphs](backends/graphs/README.md)\n\n### Ubuntu dependencies\n\nMost dependencies can be installed using `apt-get install`:\n\n```bash\nsudo apt-get install cmake g++ git automake libtool libgc-dev bison flex \\\nlibfl-dev libboost-dev libboost-iostreams-dev \\\nlibboost-graph-dev llvm pkg-config python3 python3-pip \\\ntcpdump\n\npip3 install --user -r requirements.txt\n```\n\n**For documentation building:**\n\n**Tools**\n- Download the Doxygen 1.13.2 binary \n```bash\nwget https://github.com/doxygen/doxygen/releases/download/Release_1_13_0/doxygen-1.13.2.linux.bin.tar.gz\n```\n- Extract and install Doxygen \n```bash\ntar xzvf doxygen-1.13.2.linux.bin.tar.gz\ncd doxygen-1.13.2\nsudo make install\ncd .. \n```\n- Install Graphviz\n```bash\nsudo apt-get install -y graphviz\n```\n**Theme** \n```bash\ngit clone --depth 1 -b v2.3.4 https://github.com/jothepro/doxygen-awesome-css ./docs/doxygen/awesome_css\n```\n\n`P4C` also depends on Google Protocol Buffers (Protobuf). `P4C` requires version\n3.0 or higher, so the packaged version provided in Ubuntu 22.04 **should**\nwork. However, P4C typically installs its own version of Protobuf using CMake's `FetchContent` module\n(at the moment, 3.25.3). If you are experiencing issues with the Protobuf version shipped with your OS distribution, we recommend that to install Protobuf 3.25.3 from source. You can find instructions\n[here](https://github.com/protocolbuffers/protobuf/blob/v3.25.3/src/README.md).\nAfter cloning Protobuf and before you build, check-out version 3.25.3:\n\n`git checkout v3.25.3`\n\nPlease note that while all Protobuf versions newer than 3.0 should work for\nP4C itself, you may run into trouble with Abseil, some extensions and other p4lang\nprojects unless you install version 3.25.3.\n\nP4C also depends on Google Abseil library. This library is also a pre-requisite for Protobuf of any version newer than 3.21. Therefore the use of Protobuf of suitable version automatically fulfils Abseil dependency. P4C typically installs its own version of Abseil using CMake's `FetchContent` module (Abseil LTS 20240116.1 at the moment).\n\n#### CMake\nP4C requires a CMake version of at least 3.16.3 or higher. On older systems, a newer version of CMake can be installed using `pip3 install --user cmake==3.16.3`. We have a CI test on Ubuntu 18.04 that uses this option, but there is no guarantee that this will lead to a successful build.\n\n### Fedora dependencies\n\n```bash\nsudo dnf install -y cmake g++ git automake libtool gc-devel bison flex \\\nlibfl-devel gmp-devel boost-devel boost-iostreams boost-graph llvm pkg-config \\\npython3 python3-pip tcpdump\n\nsudo pip3 install -r requirements.txt\n```\n\n**For documentation building:**\n\n**Tools**\n- Download the Doxygen 1.13.2 binary\n```bash \nwget https://github.com/doxygen/doxygen/releases/download/Release_1_13_0/doxygen-1.13.2.linux.bin.tar.gz\n```\n- Extract and install Doxygen\n```bash\ntar xzvf doxygen-1.13.2.linux.bin.tar.gz\ncd doxygen-1.13.2\nsudo make install\ncd ..\n```\n- Install Graphviz\n```bash\nsudo dnf install -y graphviz\n```\n**Theme**\n```bash\ngit clone --depth 1 -b v2.3.4 https://github.com/jothepro/doxygen-awesome-css ./docs/doxygen/awesome_css\n```\n\nYou can also look at the [dependencies installation script](https://github.com/p4lang/p4c/blob/main/tools/install_fedora_deps.sh)\nfor a fresh Fedora instance.\n\n### macOS dependencies\n\nInstalling on macOS:\n\n- Enable XCode's command-line tools:\n  ```\n  xcode-select --install\n  ```\n\n- Install Homebrew:\n  ```\n  /usr/bin/ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"\n  ```\n  Be sure to add `/usr/local/bin/` to your `$PATH`.\n\n- Install dependencies using Homebrew:\n  ```\n  brew install autoconf automake libtool bdw-gc boost bison pkg-config\n  ```\n  or with MacPorts\n  ```\n  sudo port install autoconf automake coreutils libtool boehmgc boost bison pkg-config\n  ```\n\n  By default, Homebrew doesn't link programs into `/usr/local/bin` if\n  they would conflict with a version provided by the base system. This\n  includes Bison, since an older version ships with macOS. `make\n  check` depends on the newer Bison we just installed from Homebrew\n  (see [#83](http://github.com/p4lang/p4c/issues/83)), so you'll want\n  to add it to your `$PATH` one way or another. One simple way to do\n  that is to request that Homebrew link it into `/usr/local/bin`:\n  ```\n  brew link --force bison\n  ```\n\n  **Optional documentation building tools:**\n  -  Download and install the Doxygen 1.13.2 DMG file from [here](https://github.com/doxygen/doxygen/releases/download/Release_1_13_0/Doxygen-1.13.2.dmg).\n  - Install Graphviz\n  ```\n  brew install graphviz\n  ```\n  **Optional Documentation theme:** \n  ```\n  git clone --depth 1 -b v2.3.4 https://github.com/jothepro/doxygen-awesome-css ./docs/doxygen/awesome_css\n  ```\n\n  Homebrew offers a `protobuf` formula. It installs version 3.2, which should\n  work for P4C itself but may cause problems with some extensions. It's\n  preferable to use the version of Protobuf which is supplied with CMake's fetchcontent (3.25.3).\n\n  The `protobuf` formula requires the following CMake variables to be set,\n  otherwise CMake does not find the libraries or fails in linking. It is likely\n  that manually installed Protobuf will require similar treatment.\n\n  ```\n  PB_PREFIX=\"$(brew --prefix --installed protobuf)\"\n  ./bootstrap.sh \\\n    -DProtobuf_INCLUDE_DIR=\"${PB_PREFIX}/include/\" \\\n    -DProtobuf_LIBRARY=\"${PB_PREFIX}/lib/libprotobuf.dylib\" \\\n    -DENABLE_PROTOBUF_STATIC=OFF\n  ```\n\n### Garbage collector\n\nP4c relies on [BDW garbage collector](https://github.com/ivmai/bdwgc)\nto manage its memory.  By default, the P4C executables are linked with\nthe garbage collector library.  When the GC causes problems, this can\nbe disabled by setting `ENABLE_GC` cmake option to `OFF`.  However,\nthis will dramatically increase the memory usage by the compiler, and\nmay become impractical for compiling large programs.  **Do not disable\nthe GC**, unless you really have to.  We have noticed that this may be\na problem on MacOS.\n\n### Crash dumps\n\nP4c will use [libbacktrace](https://github.com/ianlancetaylor/libbacktrace.git)\nto produce readable crash dumps if it is available.  This is an optional\ndependency; if it is not available everything should build just fine, but\ncrash dumps will not be very readable.\n\n## Development tools\n\nThere is a variety of design and development documentation [here](docs/README.md).\n\nWe recommend using `clang++` with no optimizations for speeding up\ncompilation and simplifying debugging.\n\nWe recommend installing a new version of [gdb](http://ftp.gnu.org/gnu/gdb),\nbecause older gdb versions do not always handle C++11 or newer correctly.\n\nWe recommend exuberant ctags for navigating source code in Emacs and vi.  `sudo\napt-get install exuberant-ctags.` The Makefile targets `make ctags` and `make\netags` generate tags for vi and Emacs respectively.  (Make sure that you are\nusing the correct version of ctags; there are several competing programs with\nthe same name in existence.)\n\nTo build code documentation, after installing Doxygen and the other\nrequired packages:\n\n```bash\n# Starting from root directory of your copy of p4c repo\ncd docs/doxygen\ndoxygen doxygen.cfg\n```\nThe HTML output is available in\n`docs/doxygen/build/html/index.html`.\n\n### Git setup\n\nOccasionally formatting commits are applied to P4C. These pollute the git history. To ignore these commits in git blame, run this command\n```git config blame.ignoreRevsFile .git-blame-ignore-revs```\n\nThe P4C code base is subject to a series of linter checks which are checked by CI. To avoid failing these checks and wasting unnecessary CI cycles and resources, you can install [git commit hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) by running\n```./tools/install_git_hooks.sh```\nThese commit hooks will run on every commit and check the files you are planning to commit with cpplint and clang-format.\n\n## Docker\n\nA Dockerfile is included. You can generate an image which contains a copy of P4C\nin `/p4c/build` by running:\n\n```\ndocker build -t p4c .\n```\n\nOn some platforms Docker limits the memory usage of any container, even\ncontainers used during the `docker build` process. On macOS in particular the\ndefault is 2GB, which is not enough to build P4C. Increase the memory limit to\nat least 4GB via Docker preferences or you are likely to see \"internal compiler\nerrors\" from GCC which are caused by low memory.\n\n## Bazel\n[![Bazel Build](https://github.com/p4lang/p4c/actions/workflows/ci-bazel.yml/badge.svg)](https://github.com/p4lang/p4c/actions/workflows/ci-bazel.yml)\n\nThe project can also be build using [Bazel](https://bazel.build):\n```sh\nbazel build //...\n```\nWe run continuous integration to ensure this works with the latest version of\nBazel.\n\nWe also provide a [`p4_library` rule](https://github.com/p4lang/p4c/blob/main/bazel/p4_library.bzl) for invoking\nP4C during the build process of 3rd party Bazel projects.\n\nSee [bazel/example](https://github.com/p4lang/p4c/tree/main/bazel/example) for an example of how to use or extend P4C in\nyour own Bazel project. You may use it as a template to get you started.\n\n## Build system\n\nThe build system is based on cmake.  This section describes how it can be customized.\n\n### Defining new CMake targets\n\nWhen building a new backend target, add it into the development tree in the\nextensions subdirectory. The cmake-based build system will automatically include\nit if it contains a CMakeLists.txt file.\n\nFor a new backend, the cmake file should contain the following rules:\n\n#### IR definition files\n\nBackend specific IR definition files should be added to the global list\nof IR_DEF_FILES as they are processed together with the core IR files.\nUse the following rule:\n\n```\nset (IR_DEF_FILES ${IR_DEF_FILES} ${MY_IR_DEF_FILES} PARENT_SCOPE)\n```\nwhere `MY_IR_DEF_FILES` is a list of file names with absolute path\n(for example, use `${CMAKE_CURRENT_SOURCE_DIR}`).\n\nIf in addition you have additional supporting source files, they\nshould be added to the ir sources, as follows:\n\n```\nset(EXTENSION_IR_SOURCES ${EXTENSION_IR_SOURCES} ${MY_IR_SRCS} PARENT_SCOPE)\n```\nAgain, `MY_IR_SRCS` is a list of file names with absolute path.\n\n#### Source files\n\nSources (.cpp and .h) should be added to the cpplint and clang-format target using the following rule:\n\n```\nadd_cpplint_files (${CMAKE_CURRENT_SOURCE_DIR} \"${MY_SOURCES_AND_HEADERS}\")\nadd_clang_format_files (${CMAKE_CURRENT_SOURCE_DIR} \"${MY_SOURCES_AND_HEADERS}\")\n```\n\nPython files should be added to the black and isort target using the following rule:\n```\nadd_black_files (${CMAKE_CURRENT_SOURCE_DIR} \"${MY_SOURCES_AND_HEADERS}\")\n```\n\nThe P4C CMakeLists.txt will use that name to figure the full path of the files to lint.\n\nclang-format, black, and isort need to be installed before the linter can be used. They can be installed with the following command:\n```\npip3 install --user \"clang-format==18.1.0\" \"black==24.3.0\" \"isort==5.13.2\"\n```\nclang-format can be checked using the `make clang-format` command. Complaints can be fixed by running `make clang-format-fix-errors`. black and isort can be checked using the `make black` or `make isort` command respectively. Complaints can be fixed by running `make black-fix-errors` or `make isort-fix-errors`.\n\ncpplint, clang-format, and black/isort run as checks as port of P4C's continuous integration process. To make sure that these tests pass, we recommend installing the appropriate git hooks. This can be done by running\n```\n./tools/install_git_hooks.sh\n```\nclang-format, cpplint, and black/isort checks will be enforced on every branch commit. In cases where checks are failing but the commit is sound, one can bypass the hook enforcement using `git commit --no-verify`.\n\n#### Target\n\nDefine a target for your executable. The target should link against\nthe core `P4C_LIBRARIES` and `P4C_LIB_DEPS`.  `P4C_LIB_DEPS` are\npackage dependencies. If you need additional libraries for your\nproject, add them to `P4C_LIB_DEPS`.\n\nIn addition, your target should depend on the `genIR` target, since\nyou need all the IR generation to happen before you start compiling\nyour backend. If you chose to have your backend as a library (seem the\nbackends/bmv2 example), the library should depend on `genIR`, and\nthere is no longer necessary for your executable to depend on it.\n\n```\nadd_executable(p4c-mybackend ${MY_SOURCES})\ntarget_link_libraries (p4c-mybackend ${P4C_LIBRARIES} ${P4C_LIB_DEPS})\nadd_dependencies(p4c-mybackend genIR)\n```\n\n#### Tests\n\nWe implemented support equivalent to the automake `make check` rules.\nAll tests should be included in `make check` and in addition, we support\n`make check-*` rules. To enable this support, add the following rules:\n\n```\nset(MY_DRIVER \u003cdriver or compiler executable\u003e)\n\nset (MY_TEST_SUITES\n  ${P4C_SOURCE_DIR}/testdata/p4_16_samples/*.p4\n  ${P4C_SOURCE_DIR}/testdata/p4_16_errors/*.p4\n  )\nset (MY_XFAIL_TESTS\n  testdata/p4_16_errors/this_test_fails.p4\n )\np4c_add_tests(\"mybackend\" ${MY_DRIVER} \"${MY_TEST_SUITES}\" \"${MY_XFAIL_TESTS}\")\n```\n\nIn addition, you can add individual tests to a suite using the following macro:\n```\nset(isXFail FALSE)\nset(SWITCH_P4 testdata/p4_14_samples/switch_20160512/switch.p4)\n\np4c_add_test_with_args (\"mybackend\" ${MY_DRIVER} ${isXFail}\n  \"switch_with_custom_profile\" ${SWITCH_P4} \"-DCUSTOM_PROFILE\")\n```\n\nSee the documentation for\n[`p4c_add_test_with_args`](cmake/P4CUtils.cmake) and\n[`p4c_add_tests`](cmake/P4CUtils.cmake) for more information on the\narguments to these macros.\n\nTo pass custom arguments to P4C, you can set the environment variable `P4C_ARGS`:\n```\nmake check P4C_ARGS=\"-Xp4c=MY_CUSTOM_FLAG\"\n```\n\nWhen making changes to P4C, it is sometimes useful to be able to run\nthe tests while overwriting the expected output files that are saved\nin this repository.  One such situation is when your changes to P4C\ncause the names of compiler-generated local variables to change.  To\nforce the expected output files to be rewritten while running the\ntests, assign a value to the shell environment variable\n`P4TEST_REPLACE`.  Here is one example Bash command to do so:\n\n```\nP4TEST_REPLACE=1 make check\n```\n\n#### Installation\n\nDefine rules to install your backend. Typically you need to install\nthe binary, the additional architecture headers, and the configuration\nfile for the P4C driver.\n\n```\ninstall (TARGETS p4c-mybackend\n  RUNTIME DESTINATION ${P4C_RUNTIME_OUTPUT_DIRECTORY})\ninstall (DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/p4include\n  DESTINATION ${P4C_ARTIFACTS_OUTPUT_DIRECTORY})\ninstall (FILES ${CMAKE_CURRENT_SOURCE_DIR}/driver/p4c.mybackend.cfg\n  DESTINATION ${P4C_ARTIFACTS_OUTPUT_DIRECTORY}/p4c_src)\n```\n\n\u003c!--!\n\\include{doc} \"../lib/README.md\"\n--\u003e\n## Known issues\n\nIssues with the compiler are tracked on\n[GitHub](https://github.com/p4lang/p4c/issues). Before opening a new\nissue, please check whether a similar issue is already opened. Opening\nissues and submitting a pull request with fixes for those issues is\nmuch appreciated.\n\nIn addition to the list of issues on Github, there are a number of\ncurrently unsupported features listed below:\n\n### Frontend\n\n#### P4_14 features not supported in P4_16\n\n* extern/blackbox attributes -- there is support for carrying them in\nthe IR, but they are lost if P4_16 code is output.  Backends can\naccess them from the IR\n\n* Nonstandard extension primitives from P4_14\n  * Execute_meter extra arguments\n  * Recirculate/clone/resubmit variants\n  * Bypass_egress\n  * Sample_ primitives\n  * invalidate\n\n* No support for P4_14 parser exceptions.\n\n### Backends\n\n#### Bmv2 Backend\n\n* Tables with multiple apply calls\n\nSee also [unsupported P4_16 language features](backends/bmv2/README.md#unsupported-p4_16-language-features).\n\n## How to Contribute\n\nWe welcome and appreciate new contributions. Please take a moment to review our [Contribution Guidelines](CONTRIBUTING.md) to get started.\n\n## P4 Compiler Onboarding\nEducational material on P4: \n\n- General hands-on [tutorials](https://github.com/p4lang/tutorials).\n- [Technical documentation on P4-related topics](https://github.com/jafingerhut/p4-guide?tab=readme-ov-file#introduction).\n- Motivating P4: [IEEE ICC 2018 // Keynote: Nick McKeown, Programmable Forwarding Planes Are Here To Stay](https://www.youtube.com/watch?v=8ie0FcsN07U)\n- Introducing P4-16 in detail: \n  - Part 1: [Introduction to P4_16. Part 1](https://www.youtube.com/watch?v=GslseT4hY1w)\n  - Part 2: [Introduction to P4_16. Part 2](https://www.youtube.com/watch?v=yqxpypXIOtQ)\n- Material on the official P4 compiler: \n  - [Understanding the Open-Source P4-16 Compiler - February 15, 2022 - Mihai Budiu](https://www.youtube.com/watch?v=Rx5AQ0IF6eU)\n  - [Understanding P4-16 Open-Source Compiler, Part 2 - March 1, 2022 - Mihai Budiu](https://www.youtube.com/watch?v=YnPHPaPSmpU)\n  - [Compiler Design - Implementation Architecture](https://github.com/p4lang/p4c/blob/main/docs/compiler-design.pdf).\n- Introduction to P4Runtime: [Next-Gen SDN Tutorial - Session 1: P4 and P4Runtime Basics](https://www.youtube.com/watch?v=KRx92qSLgo4)\n\n## Contact\nWe appreciate your contributions and look forward to working with you to improve the P4 Compiler Project (P4C)!\n- For further assistance or questions regarding contributions, reach out to us in our [community chat](https://p4lang.zulipchat.com/).  [Joining link](https://p4lang.zulipchat.com/join/kjtv2reafrylssget425wx6c/) .\n- For general P4-related questions, use the [P4 forum](https://forum.p4.org/).\n- For other communication channels click [here](https://p4.org/join/).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp4lang%2Fp4c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fp4lang%2Fp4c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp4lang%2Fp4c/lists"}