This repository delves into the core of the PHP build system, elucidating the intricacies of how to build PHP with CMake.
# Prerequisites for Debian-based distributions:
sudo apt install cmake gcc g++ bison libsqlite3-dev
# Prerequisites for Fedora-based distributions:
sudo dnf install cmake gcc gcc-c++ bison sqlite-devel
Click here for more platforms
# Prerequisites for macOS:
xcode-select --install # XCode command line tools
brew install cmake bison # See https://brew.sh how to install Homebrew
# Prerequisites for Alpine Linux:
sudo apk add --no-cache cmake make gcc g++ musl-dev bison sqlite-dev
# Prerequisites for BSD-based systems:
sudo pkg install cmake bison sqlite3
# Prerequisites for Haiku:
pkgman install cmake bison sqlite_devel
# Prerequisites for Solaris/illumos-based systems:
sudo pkg install cmake bison sqlite-3
git clone https://github.com/petk/php-build-system
cd php-build-system
cmake -B php-build
cmake --build php-build -j
After build is complete, a PHP binary should be available to run:
./php-build/php/sapi/cli/php -v
PHP developers typically opt for convenient methods to set up PHP on their machines, such as utilizing prebuilt Linux packages available in their Linux distribution repositories, deploying Docker images, or relying on user-friendly stacks that bundle PHP, its extensions, web server, and database into a unified installation package.
# Debian-based distributions:
sudo apt install php...
# Fedora-based distributions:
sudo dnf install php...
In contrast, the practice of building PHP from source code is primarily reserved for specific purposes, such as PHP source code development or extensive customization of PHP configurations on a particular system. This approach is less commonly employed by everyday PHP developers due to its intricate and time-consuming nature.
In the realm of software development, a build system is a collection of tools and files that automate the process of compiling, linking, and assembling the project's source code into its final form, ready to be executed. It helps developers with repetitive tasks and ensures consistency and correctness in the build process for various platforms and hardware out there.
A key function of a build system in the context of C/C++ software development is to establish a structured framework that guides how code should be written. Beyond its primary role of compiling source files into executable programs, the build system plays a pivotal educational role, imparting best practices and coding standards to developers. By enforcing consistency and adherence to coding conventions, it fosters the creation of high-quality code, ultimately enhancing software maintainability and reliability.
Additionally, the build system aims to enable developers to work efficiently by abstracting away system-specific details, allowing them to focus on the logic and usability of their code. When adding a new source file or making minor modifications, developers shouldn't have to delve into the inner workings of the build system, sift through extensive build system documentation or extensively explore the complexities of the underlying system.
There are numerous well-known build systems available, ranging from the veteran GNU Autotools and the widely adopted CMake, to the efficient Ninja, versatile SCons, adaptable Meson, nimble xmake, cutting-edge Zig build system, and even the simplest manual usage of Make.
To understand the PHP source code better, it would be beneficial to grasp its directory structure. PHP is developed at the php-src GitHub repository.
After cloning the repository:
git clone https://github.com/php/php-src
cd php-src
there is a large monolithic repository consisting of C source code files, PHP tests and other associated files:
π <php-src>
ββπ .git # Git configuration and source directory
ββπ benchmark # Benchmark some common applications in CI
ββπ build # *nix build system files
ββπ docs # PHP internals documentation
ββπ ext # PHP core extensions
ββπ bcmath # The bcmath PHP extension
ββπ libbcmath # The bcmath library forked and maintained in php-src
ββπ tests # *.phpt test files for extension
ββπ bcmath.stub.php # A stub file for the bcmath extension functions
ββπ ...
ββπ curl # The curl PHP extension
ββπ sync-constants.php # The curl symbols checker
ββπ ...
ββπ date # The date/time PHP extension
ββπ lib # Bundled datetime library https://github.com/derickr/timelib
ββπ ...
ββπ ...
ββπ dl_test # Extension for testing dl()
ββπ dom
ββπ lexbor # https://github.com/lexbor/lexbor
ββπ ...
ββπ ffi # The FFI PHP extension
ββπ ffi_parser.c # Generated by https://github.com/dstogov/llk
ββπ ...
ββπ fileinfo # The fileinfo PHP extension
ββπ libmagic # Modified libmagic https://github.com/file/file
ββπ data_file.c # Generated by `ext/fileinfo/create_data_file.php`
ββπ libmagic.patch # Modifications patch from upstream libmagic
ββπ magicdata.patch # Modifications patch from upstream libmagic
ββπ ...
ββπ gd # The GD PHP extension
ββπ libgd # Bundled and modified GD library https://github.com/libgd/libgd
ββπ ...
ββπ mbstring # The Multibyte string PHP extension
ββπ libmbfl # Forked and maintained in php-src
ββπ unicode_data.h # Generated by `ext/mbstring/ucgendat/ucgendat.php`
ββπ ...
ββπ opcache # The OPcache PHP extension
ββπ jit # OPcache Jit
ββπ ir # Bundled part of IR framework https://github.com/dstogov/ir
ββπ dynasm # DynASM encoding engine
ββπ minilua.c # Customized Lua scripting language to build LuaJIT
ββπ ...
ββπ gen_ir_fold_hash # IR folding engine generator created at build
ββπ ir_emit_<arch>.h # IR folding engine rules generated by minilua
ββπ minilua # Executable tool created at build
ββπ ...
ββπ pcre # The PCRE PHP extension
ββπ pcre2lib # https://www.pcre.org/
ββπ ...
ββπ skeleton # Skeleton for new extensions using `ext/ext_skel.php`
ββπ standard # Always enabled core extension
ββπ html_tables
ββπ mappings # https://www.unicode.org/Public/MAPPINGS/
ββπ ...
ββπ credits_ext.h # Generated by `scripts/dev/credits`
ββπ credits_sapi.h # Generated by `scripts/dev/credits`
ββπ html_tables.h # Generated by `ext/standard/html_tables/html_table_gen.php`
ββπ ...
ββπ tokenizer # The tokenizer PHP extension
ββπ tokenizer_data.c # Generated by `ext/tokenizer/tokenizer_data_gen.php`
ββπ tokenizer_data_stub.php # Generated by `ext/tokenizer/tokenizer_data_gen.php`
ββπ ...
ββπ zend_test # For testing internal APIs. Not needed for regular builds
ββπ ...
ββπ zip/ # Bundled https://github.com/pierrejoye/php_zip
ββπ ...
ββπ ...
ββπ ext_skel.php # Helper script that creates a new PHP extension
ββπ main # Binding that ties extensions, SAPIs, and Zend Engine together
ββπ streams # Streams layer subsystem
ββπ debug_gdb_scripts.c # Generated by `scripts/gdb/debug_gdb_scripts_gen.php`
ββπ ...
ββπ modules # Shared libraries, created when building PHP
ββπ pear # PEAR installation
ββπ sapi # PHP SAPI (Server API) modules
ββπ cli # Command-line PHP SAPI module
ββπ mime_type_map.h # Generated by `sapi/cli/generate_mime_type_map.php`
ββπ ...
ββπ ...
ββπ scripts # php-config, phpize and internal development scripts
ββπ tests # Core features tests
ββπ TSRM # Thread Safe Resource Manager
ββπ Zend # Zend Engine
ββπ asm # Bundled from src/asm in https://github.com/boostorg/context
ββπ Optimizer # For faster PHP execution through opcode caching and optimization
ββπ tests # PHP tests *.phpt files for Zend Engine
ββπ zend_vm_execute.h # Generated by `Zend/zend_vm_gen.php`
ββπ zend_vm_opcodes.c # Generated by `Zend/zend_vm_gen.php`
ββπ zend_vm_opcodes.h # Generated by `Zend/zend_vm_gen.php`
ββπ ...
ββπ win32 # Windows build files
ββπ ...
At the time of writing, CMake is actively developed, and many developers may already be familiar with it, making C code more appealing to new contributors. Numerous IDEs offer excellent CMake integration for C/C++ projects.
CMake shares many similarities with Autotools, which simplifies the learning curve for those already accustomed to building C code using existing systems.
Notably, CMake features better out-of-the-box support on Windows systems, where Autotools may encounter issues without additional adaptations and adjustments in the build process.
Despite Autotools potentially seeming complex and arcane to new developers, unfamiliar with it, it remains a robust and solid build system option for C/C++ projects on *nix systems. Many large open-source projects use Autotools, and some even incorporate it alongside CMake.