|
| 1 | +# Wrapper for integrating PHP sources with CMake |
| 2 | +# |
| 3 | +# This file uses the FetchContent module to download the PHP source code (from |
| 4 | +# a tarball), integrate additional CMake files, and apply necessary patches to |
| 5 | +# enable building PHP with CMake. |
| 6 | +# |
| 7 | +# While not part of the CMake-based build system itself, this file serves as a |
| 8 | +# wrapper to bridge the upstream PHP source code with the CMake-based build |
| 9 | +# system in this repository, streamlining the integration process. |
| 10 | + |
| 11 | +cmake_minimum_required(VERSION 3.25...3.31) |
| 12 | + |
| 13 | +if(CMAKE_SOURCE_DIR PATH_EQUAL CMAKE_BINARY_DIR) |
| 14 | + message( |
| 15 | + FATAL_ERROR |
| 16 | + "In-source builds are disabled. Please, set the build directory.\n" |
| 17 | + "For example:\n" |
| 18 | + " cmake -B php-build\n" |
| 19 | + " cmake --build php-build -j" |
| 20 | + ) |
| 21 | +endif() |
| 22 | + |
| 23 | +project( |
| 24 | + PhpBuildSystem |
| 25 | + VERSION 8.3 |
| 26 | + DESCRIPTION "CMake-based PHP build system" |
| 27 | + HOMEPAGE_URL "https://github.com/petk/php-build-system" |
| 28 | + LANGUAGES NONE |
| 29 | +) |
| 30 | + |
| 31 | +set(phpVersion "${PhpBuildSystem_VERSION}-dev") |
| 32 | +set(phpBranch "PHP-${PhpBuildSystem_VERSION}") |
| 33 | +set(phpSourceDir "${CMAKE_CURRENT_BINARY_DIR}/php-src") |
| 34 | +set(phpBinaryDir "${CMAKE_CURRENT_BINARY_DIR}/php") |
| 35 | + |
| 36 | +include(FetchContent) |
| 37 | + |
| 38 | +FetchContent_Declare( |
| 39 | + php |
| 40 | + URL https://github.com/php/php-src/archive/refs/heads/${phpBranch}.tar.gz |
| 41 | + SOURCE_DIR ${phpSourceDir} |
| 42 | + BINARY_DIR ${phpBinaryDir} |
| 43 | + PATCH_COMMAND |
| 44 | + ${CMAKE_COMMAND} |
| 45 | + -P |
| 46 | + ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/PhpBuildSystem/patch.cmake |
| 47 | +) |
| 48 | + |
| 49 | +# Create script for the FetchContent_Declare(PATCH_COMMAND). |
| 50 | +file( |
| 51 | + CONFIGURE |
| 52 | + OUTPUT CMakeFiles/PhpBuildSystem/patch.cmake |
| 53 | + CONTENT [=[ |
| 54 | + message(STATUS "Adding CMake files to PHP sources") |
| 55 | + file(COPY "@CMAKE_CURRENT_SOURCE_DIR@/cmake/" DESTINATION "@phpSourceDir@") |
| 56 | + |
| 57 | + string(REGEX MATCH [[^([0-9]+\.[0-9]+)]] _ "@PhpBuildSystem_VERSION@") |
| 58 | + file( |
| 59 | + GLOB |
| 60 | + patches |
| 61 | + "@CMAKE_CURRENT_SOURCE_DIR@/patches/${CMAKE_MATCH_1}/*.patch" |
| 62 | + ) |
| 63 | + |
| 64 | + if(NOT patches) |
| 65 | + return() |
| 66 | + endif() |
| 67 | + |
| 68 | + find_package(Git QUIET) |
| 69 | + if(NOT Git_FOUND) |
| 70 | + # See: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10164 |
| 71 | + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.32) |
| 72 | + find_package(Patch QUIET) |
| 73 | + else() |
| 74 | + find_program(Patch_EXECUTABLE patch) |
| 75 | + if(Patch_EXECUTABLE) |
| 76 | + set(Patch_FOUND TRUE) |
| 77 | + endif() |
| 78 | + endif() |
| 79 | + endif() |
| 80 | + |
| 81 | + if(NOT Git_FOUND AND NOT Patch_FOUND) |
| 82 | + message(WARNING "Patches not applied. Install Git or the patch command.") |
| 83 | + return() |
| 84 | + endif() |
| 85 | + |
| 86 | + message(STATUS "Applying patches to PHP sources\n") |
| 87 | + |
| 88 | + if(Git_FOUND) |
| 89 | + # Add .git directory to be able to apply patches. |
| 90 | + execute_process( |
| 91 | + COMMAND ${GIT_EXECUTABLE} init |
| 92 | + WORKING_DIRECTORY "@phpSourceDir@" |
| 93 | + RESULT_VARIABLE result |
| 94 | + OUTPUT_VARIABLE output |
| 95 | + ERROR_VARIABLE error |
| 96 | + ERROR_STRIP_TRAILING_WHITESPACE |
| 97 | + OUTPUT_QUIET |
| 98 | + ) |
| 99 | + if(NOT result EQUAL 0) |
| 100 | + message(WARNING "Failed to create .git directory:\n${output}\n${error}") |
| 101 | + endif() |
| 102 | + |
| 103 | + execute_process( |
| 104 | + COMMAND ${GIT_EXECUTABLE} apply --ignore-whitespace ${patches} |
| 105 | + WORKING_DIRECTORY "@phpSourceDir@" |
| 106 | + RESULT_VARIABLE result |
| 107 | + OUTPUT_VARIABLE output |
| 108 | + ERROR_VARIABLE error |
| 109 | + ERROR_STRIP_TRAILING_WHITESPACE |
| 110 | + OUTPUT_QUIET |
| 111 | + ) |
| 112 | + if(NOT result EQUAL 0) |
| 113 | + message(WARNING "Failed to apply patches:\n${output}\n${error}.") |
| 114 | + endif() |
| 115 | + |
| 116 | + # Clean temporary .git directory. Checks are done as safeguards. |
| 117 | + if( |
| 118 | + IS_DIRECTORY "@phpSourceDir@/.git" |
| 119 | + AND EXISTS "@phpSourceDir@/main/php_version.h" |
| 120 | + AND EXISTS "@phpSourceDir@/CMakeLists.txt" |
| 121 | + ) |
| 122 | + file(REMOVE_RECURSE "@phpSourceDir@/.git/") |
| 123 | + endif() |
| 124 | + elseif(Patch_FOUND) |
| 125 | + foreach(patch IN LISTS patches) |
| 126 | + execute_process( |
| 127 | + COMMAND ${Patch_EXECUTABLE} -p1 -i "${patch}" |
| 128 | + WORKING_DIRECTORY "@phpSourceDir@" |
| 129 | + RESULT_VARIABLE result |
| 130 | + OUTPUT_VARIABLE output |
| 131 | + ERROR_VARIABLE error |
| 132 | + ERROR_STRIP_TRAILING_WHITESPACE |
| 133 | + OUTPUT_QUIET |
| 134 | + ) |
| 135 | + if(NOT result EQUAL 0) |
| 136 | + cmake_path(GET patch FILENAME filename) |
| 137 | + message(WARNING "Patch ${filename} failed:\n${output}\n${error}\n") |
| 138 | + endif() |
| 139 | + endforeach() |
| 140 | + endif() |
| 141 | + ]=] |
| 142 | + @ONLY |
| 143 | +) |
| 144 | + |
| 145 | +cmake_path( |
| 146 | + RELATIVE_PATH |
| 147 | + phpSourceDir |
| 148 | + BASE_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 149 | + OUTPUT_VARIABLE phpSourceDirRelative |
| 150 | +) |
| 151 | +message(STATUS "Downloading PHP ${phpVersion} to ${phpSourceDirRelative}") |
| 152 | +FetchContent_MakeAvailable(php) |
0 commit comments