Skip to content

Commit f2967be

Browse files
committed
Download re2c and Bison with FetchContent
This uses FetchContent and ExternalProject modules together. FetchContent integrates better with find_package() calls, and ExternalProject takes care of isolation. Changes and notes: - Redundant local FindBISON overrided module removed and configuration moved to Bison module itself. - The SOURCE_SUBDIR option pointing to non-existing relative path ensures that package is not added via add_subdirectory() to not mess the cache variables, and package can be still downloaded via FetchContent_MakeAvailable() command.
1 parent 8154ec4 commit f2967be

File tree

3 files changed

+87
-80
lines changed

3 files changed

+87
-80
lines changed

cmake/cmake/modules/FindBISON.cmake

Lines changed: 0 additions & 21 deletions
This file was deleted.

cmake/cmake/modules/PHP/Bison.cmake

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ php_bison(foo foo.y foo.c OPTIONS -Wall --debug)
145145
# bison -Wall --debug foo.y --output foo.c
146146
```
147147
148-
### Specifying options
148+
### Example: Specifying options
149149
150150
This module provides some default options when using the `ADD_DEFAULT_OPTIONS`:
151151
@@ -227,7 +227,9 @@ _php_bison_config()
227227

228228
include_guard(GLOBAL)
229229

230+
include(ExternalProject)
230231
include(FeatureSummary)
232+
include(FetchContent)
231233

232234
# Configuration after find_package() in this module.
233235
macro(_php_bison_config_options)
@@ -317,6 +319,12 @@ function(php_bison name input output)
317319
AND NOT _PHP_BISON_DOWNLOAD
318320
)
319321
find_package(BISON ${PHP_BISON_VERSION} ${quiet})
322+
set_package_properties(
323+
BISON
324+
PROPERTIES
325+
URL "https://www.gnu.org/software/bison/"
326+
DESCRIPTION "General-purpose parser generator"
327+
)
320328
endif()
321329

322330
get_property(role GLOBAL PROPERTY CMAKE_ROLE)
@@ -655,8 +663,8 @@ function(_php_bison_download)
655663
)
656664

657665
# Target created by ExternalProject:
658-
if(TARGET bison)
659-
add_dependencies(Bison::Bison bison)
666+
if(TARGET BISON-install)
667+
add_dependencies(Bison::Bison BISON-install)
660668
endif()
661669

662670
# Move dependency to PACKAGES_FOUND.
@@ -676,22 +684,35 @@ function(_php_bison_download)
676684
_PHP_BISON_DOWNLOAD
677685
TRUE
678686
CACHE INTERNAL
679-
"Internal marker whether the Bison will be downloaded."
687+
"Internal marker whether Bison is downloaded."
680688
)
681689

682690
return(PROPAGATE BISON_FOUND BISON_VERSION)
683691
endfunction()
684692

685693
# Downloads GNU Bison.
686694
function(_php_bison_download_gnu)
687-
message(STATUS "GNU Bison ${BISON_VERSION} will be downloaded at build phase")
688-
689-
include(ExternalProject)
695+
message(
696+
STATUS
697+
"Downloading GNU Bison ${BISON_VERSION} from https://ftp.gnu.org/gnu/bison"
698+
)
690699

691-
ExternalProject_Add(
692-
bison
700+
FetchContent_Declare(
701+
BISON
693702
URL https://ftp.gnu.org/gnu/bison/bison-${BISON_VERSION}.tar.gz
703+
SOURCE_SUBDIR non-existing
694704
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
705+
OVERRIDE_FIND_PACKAGE
706+
)
707+
708+
FetchContent_MakeAvailable(BISON)
709+
710+
ExternalProject_Add(
711+
BISON
712+
STEP_TARGETS build install
713+
SOURCE_DIR ${bison_SOURCE_DIR}
714+
BINARY_DIR ${bison_BINARY_DIR}
715+
INSTALL_DIR ${FETCHCONTENT_BASE_DIR}/bison-install
695716
CONFIGURE_COMMAND
696717
<SOURCE_DIR>/configure
697718
--disable-dependency-tracking
@@ -701,37 +722,32 @@ function(_php_bison_download_gnu)
701722
LOG_INSTALL TRUE
702723
)
703724

704-
ExternalProject_Get_Property(bison INSTALL_DIR)
725+
ExternalProject_Get_Property(BISON INSTALL_DIR)
705726

706727
set_property(CACHE BISON_EXECUTABLE PROPERTY VALUE ${INSTALL_DIR}/bin/bison)
707728
endfunction()
708729

709-
# Downloads https://github.com/lexxmark/winflexbison.
730+
# Downloads winflexbison.
710731
function(_php_bison_download_windows)
711732
message(
712733
STATUS
713-
"Downloading win_bison ${BISON_VERSION} (${PHP_BISON_WIN_VERSION_DOWNLOAD})"
734+
"Downloading win_bison ${BISON_VERSION} from "
735+
"https://github.com/lexxmark/winflexbison "
736+
"(${PHP_BISON_WIN_VERSION_DOWNLOAD})"
714737
)
715738

716-
get_directory_property(dir EP_BASE)
717-
if(NOT dir)
718-
set(dir "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles")
719-
endif()
720-
721-
set(file "${dir}/win_flex_bison.zip")
722-
723-
file(
724-
DOWNLOAD
725-
"https://github.com/lexxmark/winflexbison/releases/download/v${PHP_BISON_WIN_VERSION_DOWNLOAD}/win_flex_bison-${PHP_BISON_WIN_VERSION_DOWNLOAD}.zip"
726-
${file}
727-
SHOW_PROGRESS
739+
FetchContent_Declare(
740+
BISON
741+
URL https://github.com/lexxmark/winflexbison/releases/download/v${PHP_BISON_WIN_VERSION_DOWNLOAD}/win_flex_bison-${PHP_BISON_WIN_VERSION_DOWNLOAD}.zip
742+
SOURCE_SUBDIR non-existing
743+
OVERRIDE_FIND_PACKAGE
728744
)
729745

730-
file(ARCHIVE_EXTRACT INPUT "${file}" DESTINATION "${dir}/win_flex_bison")
746+
FetchContent_MakeAvailable(BISON)
731747

732748
set_property(
733749
CACHE
734750
BISON_EXECUTABLE
735-
PROPERTY VALUE "${dir}/win_flex_bison/win_bison.exe"
751+
PROPERTY VALUE "${bison_SOURCE_DIR}/win_bison.exe"
736752
)
737753
endfunction()

cmake/cmake/modules/PHP/Re2c.cmake

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ _php_re2c_config()
212212

213213
include_guard(GLOBAL)
214214

215+
include(ExternalProject)
215216
include(FeatureSummary)
217+
include(FetchContent)
216218

217219
option(PHP_RE2C_COMPUTED_GOTOS "Enable computed goto GCC extension with re2c")
218220
mark_as_advanced(PHP_RE2C_COMPUTED_GOTOS)
@@ -618,60 +620,70 @@ function(_php_re2c_download)
618620
)
619621
endif()
620622

621-
message(STATUS "Re2c ${RE2C_VERSION} will be downloaded at build phase")
623+
message(
624+
STATUS
625+
"Downloading re2c ${RE2C_VERSION} from https://github.com/skvadrik/re2c"
626+
)
627+
628+
FetchContent_Declare(
629+
RE2C
630+
URL https://github.com/skvadrik/re2c/archive/refs/tags/${RE2C_VERSION}.tar.gz
631+
SOURCE_SUBDIR non-existing
632+
OVERRIDE_FIND_PACKAGE
633+
)
622634

623-
include(ExternalProject)
635+
FetchContent_MakeAvailable(RE2C)
624636

625637
# Configure re2c build.
638+
set(options -DRE2C_BUILD_RE2GO=OFF -DRE2C_BUILD_RE2RUST=OFF)
639+
626640
if(RE2C_VERSION VERSION_GREATER_EQUAL 4)
627-
set(
628-
re2cOptions
629-
-DRE2C_BUILD_RE2D=OFF
630-
-DRE2C_BUILD_RE2HS=OFF
631-
-DRE2C_BUILD_RE2JAVA=OFF
632-
-DRE2C_BUILD_RE2JS=OFF
633-
-DRE2C_BUILD_RE2OCAML=OFF
634-
-DRE2C_BUILD_RE2PY=OFF
635-
-DRE2C_BUILD_RE2V=OFF
636-
-DRE2C_BUILD_RE2ZIG=OFF
637-
-DRE2C_BUILD_TESTS=OFF
641+
list(
642+
APPEND
643+
options
644+
-DRE2C_BUILD_RE2D=OFF
645+
-DRE2C_BUILD_RE2HS=OFF
646+
-DRE2C_BUILD_RE2JAVA=OFF
647+
-DRE2C_BUILD_RE2JS=OFF
648+
-DRE2C_BUILD_RE2OCAML=OFF
649+
-DRE2C_BUILD_RE2PY=OFF
650+
-DRE2C_BUILD_RE2V=OFF
651+
-DRE2C_BUILD_RE2ZIG=OFF
652+
-DRE2C_BUILD_TESTS=OFF
638653
)
639654
else()
640-
set(
641-
re2cOptions
642-
-DCMAKE_DISABLE_FIND_PACKAGE_Python3=TRUE
643-
-DPython3_VERSION=3.7
655+
list(
656+
APPEND
657+
options
658+
-DCMAKE_DISABLE_FIND_PACKAGE_Python3=TRUE
659+
-DPython3_VERSION=3.7
644660
)
645661
endif()
646662

647663
if(RE2C_VERSION VERSION_GREATER_EQUAL 4.2)
648-
list(APPEND re2cOptions -DRE2C_BUILD_RE2SWIFT=OFF)
664+
list(APPEND options -DRE2C_BUILD_RE2SWIFT=OFF)
649665
endif()
650666

651667
ExternalProject_Add(
652-
re2c
653-
URL
654-
https://github.com/skvadrik/re2c/archive/refs/tags/${RE2C_VERSION}.tar.gz
655-
CMAKE_ARGS
656-
-DRE2C_BUILD_RE2GO=OFF
657-
-DRE2C_BUILD_RE2RUST=OFF
658-
${re2cOptions}
668+
RE2C
669+
STEP_TARGETS build
670+
SOURCE_DIR ${re2c_SOURCE_DIR}
671+
BINARY_DIR ${re2c_BINARY_DIR}
672+
CMAKE_ARGS ${options}
659673
INSTALL_COMMAND ""
660674
)
661675

662-
ExternalProject_Get_Property(re2c BINARY_DIR)
663-
set(re2c ${BINARY_DIR}/re2c)
664-
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
665-
string(APPEND re2c ".exe")
666-
endif()
667-
set_property(CACHE RE2C_EXECUTABLE PROPERTY VALUE ${re2c})
676+
set_property(
677+
CACHE RE2C_EXECUTABLE
678+
PROPERTY VALUE ${re2c_BINARY_DIR}/re2c${CMAKE_EXECUTABLE_SUFFIX}
679+
)
668680

669681
add_executable(RE2C::RE2C IMPORTED GLOBAL)
670682
set_target_properties(
671683
RE2C::RE2C
672684
PROPERTIES IMPORTED_LOCATION ${RE2C_EXECUTABLE}
673685
)
674-
add_dependencies(RE2C::RE2C re2c)
686+
add_dependencies(RE2C::RE2C RE2C-build)
675687

676688
# Move dependency to PACKAGES_FOUND.
677689
block()
@@ -690,7 +702,7 @@ function(_php_re2c_download)
690702
_PHP_RE2C_DOWNLOAD
691703
TRUE
692704
CACHE INTERNAL
693-
"Internal marker whether the re2c will be downloaded."
705+
"Internal marker whether the re2c is downloaded."
694706
)
695707

696708
return(PROPAGATE RE2C_FOUND RE2C_VERSION)

0 commit comments

Comments
 (0)