Skip to content

Commit 970d30f

Browse files
committed
Rebuild project targets when needed
This is initial solution to bring all project targets to updated state when PHP is not found on the system, while still enabling development with the built PHP itself.
1 parent 0151a93 commit 970d30f

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

cmake/cmake/Bootstrap.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ include(cmake/Configuration.cmake)
4646
# Check requirements.
4747
include(cmake/Requirements.cmake)
4848

49+
# Rebuild all targets when needed.
50+
if(NOT PHPSystem_EXECUTABLE)
51+
include(PHP/Rebuild)
52+
endif()
53+
4954
message("
5055
Running system checks
5156
---------------------

cmake/cmake/modules/PHP/PositionIndependentCode.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Check whether to enable the `POSITION_INDEPENDENT_CODE` or not.
44
https://cmake.org/cmake/help/latest/variable/CMAKE_POSITION_INDEPENDENT_CODE.html
55
#]=============================================================================]
66

7+
include_guard(GLOBAL)
8+
79
if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_SIZEOF_VOID_P EQUAL 4)
810
# On 32-bit *nix (Linux and FreeBSD at least) when using Clang, the PIC is
911
# required.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#[=============================================================================[
2+
Rebuild all project targets.
3+
4+
When PHP is not found on the system, PHP generates some files during development
5+
using the php_cli target itself, which can bring cyclic dependencies among
6+
targets if custom commands would depend on the php_cli target. Although not a
7+
good practice, this helps bringing all targets to updated state.
8+
#]=============================================================================]
9+
10+
include_guard(GLOBAL)
11+
12+
# Store a list of all targets inside the given <dir> into the <result> variable.
13+
function(_php_get_all_targets result dir)
14+
get_property(targets DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS)
15+
get_property(subdirs DIRECTORY ${dir} PROPERTY SUBDIRECTORIES)
16+
17+
foreach(subdir ${subdirs})
18+
cmake_language(CALL ${CMAKE_CURRENT_FUNCTION} subdirTargets ${subdir})
19+
list(APPEND targets ${subdirTargets})
20+
endforeach()
21+
22+
set(${result} ${targets} PARENT_SCOPE)
23+
endfunction()
24+
25+
# Ensure all project targets are rebuilt as needed.
26+
function(_php_rebuild)
27+
_php_get_all_targets(targets ${CMAKE_CURRENT_SOURCE_DIR})
28+
list(REMOVE_ITEM targets "php_rebuild")
29+
30+
add_custom_command(
31+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/php_rebuild.timestamp
32+
COMMAND
33+
${CMAKE_COMMAND}
34+
-E cmake_echo_color --magenta --bold " Updating targets"
35+
COMMAND
36+
${CMAKE_COMMAND}
37+
-E touch ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/php_rebuild.timestamp
38+
COMMAND
39+
${CMAKE_COMMAND}
40+
--build . --target php_rebuild -j
41+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
42+
DEPENDS ${targets}
43+
)
44+
45+
add_custom_target(
46+
php_update_targets ALL
47+
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/php_rebuild.timestamp
48+
)
49+
50+
add_custom_target(
51+
php_rebuild
52+
COMMAND
53+
${CMAKE_COMMAND}
54+
-E rm -f ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/php_rebuild.timestamp
55+
DEPENDS ${targets}
56+
)
57+
endfunction()
58+
59+
# Run at the end of the configuration.
60+
cmake_language(
61+
DEFER
62+
DIRECTORY ${PROJECT_SOURCE_DIR}
63+
CALL _php_rebuild
64+
)

0 commit comments

Comments
 (0)