Skip to content

Commit f008ac7

Browse files
committed
SCP: PCRE2 support.
Add PCRE2 support as an alternative to the original PCRE. PCRE2 is actively maintained and has a cleaner API than its predecessor. - Use a function interface around regular expressions to isolate and reduce the preprocessor #if/#endif forest. Abide by a single function declaration/implementation approach, vs. multiple function implentations within an #if/#endif forest. - Clean up the code so that work isn't recreated or duplicated, such as compiling regexps. Platform notes: - cmake: PCRE2 is the default RegEx support. To revert to PCRE, add "-DPREFER_PCRE" to the cmake configuration command line. - makefile: PCRE remains the default RegEx support out of respect for tradition (cue Chaim Topol singing "Tradition".) Enable PCRE2 by defining "USE_PCRE2=<some value>" from the make command line or shell environment. - Visual Studio Projects: Untouched. Externally built libraries provide regular expression support residing in a specific place in the file system, which is outside the control of SIMH proper. sim_defs.h: - Change EXPECT::size from int32 to size_t for increased cross-platform/64-bit compatibility (i.e., array indices and offsets should be size_t, where practicable.) - Add typedefs for EXPECT regex support independence: sim_regex_t, sim_re_capture_t. - Add regular expression context to EXPTAB that maintains state when processing the captured matches, making the function interface clean. Tracks intermediate state while processing captured matches.
1 parent 2437b13 commit f008ac7

File tree

10 files changed

+344
-111
lines changed

10 files changed

+344
-111
lines changed

.travis/deps.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ install_linux() {
2727
sudo apt-get install -ym libegl1-mesa-dev libgles2-mesa-dev
2828
sudo apt-get install -ym libsdl2-dev libfreetype6-dev libsdl2-ttf-dev
2929
sudo apt-get install -ym libpcap-dev libvdeplug-dev
30+
sudo apt-get install -ym libpcre2-dev libpcre2-8-0
3031
sudo apt-get install -ym cmake cmake-data
3132
}
3233

@@ -37,6 +38,7 @@ install_mingw64() {
3738
mingw-w64-x86_64-gcc \
3839
mingw-w64-x86_64-make \
3940
mingw-w64-x86_64-pcre \
41+
mingw-w64-x86_64-pcre2 \
4042
mingw-w64-x86_64-freetype \
4143
mingw-w64-x86_64-SDL2 \
4244
mingw-w64-x86_64-SDL2_ttf \
@@ -50,6 +52,7 @@ install_ucrt64() {
5052
mingw-w64-ucrt-x86_64-gcc \
5153
mingw-w64-ucrt-x86_64-make \
5254
mingw-w64-ucrt-x86_64-pcre \
55+
mingw-w64-ucrt-x86_64-pcre2 \
5356
mingw-w64-ucrt-x86_64-freetype \
5457
mingw-w64-ucrt-x86_64-SDL2 \
5558
mingw-w64-ucrt-x86_64-SDL2_ttf \

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ option(WITH_REGEX
183183
TRUE)
184184
option(PREFER_PCRE
185185
"Prefer (=1)/ignore (=0) Prefer PCRE over PCRE2 (def: ignore)"
186-
TRUE)
186+
FALSE)
187187
option(WITH_NETWORK
188188
"Enable (=1)/disable (=0) simulator networking support. (def: enabled)"
189189
TRUE)

README-CMake.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ following the table.
830830
| `BUILD_SHARED_DEPS` | platform-specific | Build dependencies as shared libraries/DLLs on Windows. Does nothing on Linux/macOS. Disabled by default on Windows to ensure that the simulators link against static libraries. |
831831
| `WITH_ASYNC` | enabled | Asynchronous I/O and threading support. |
832832
| `WITH_REGEX` | enabled | PCRE regular expression support. |
833+
| `PREFER_PCRE` | disabled | Prefer the original PCRE regular expression library over PCRE2. The default is PCRE2, which is more actively maintained. |
833834
| `WITH_NETWORK` | enabled | Simulator networking support. `WITH_PCAP`, `WITH_SLIRP`, `WITH_VDE` and `WITH_TAP` only have meaning if `WITH_NETWORK` is enabled. |
834835
| `WITH_PCAP` | enabled | libpcap (packet capture) support. |
835836
| `WITH_SLIRP` | enabled | SLIRP UDP network support. |

cmake/CMake-Maintainers.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ The top-level `CMakeLists.txt` drives CMake's configure/generate phase, which ha
221221
is separate for the time being for functional clarity.
222222
- `thread_lib` (`pthreads-dep.cmake`): Platform threading support. Usually empty for Linux and macOS
223223
platforms, adds the _PThreads4W_ external dependency for Windows native and XP platforms.
224-
- `simh_regexp`: Regular expression support, if the `WITH_REGEX` option is `True`. PCRE is effectively
225-
the only regular expression library that ends up in the `simh_regexp` interface library at the
226-
moment. There is support for PCRE2, but it requires changes to `scp.c` to actually make it useful.
224+
- `simh_regexp`: Regular expression support, if the `WITH_REGEX` option is `True`.
227225
- `simh_video`: Graphics, input controller and sound support, based on [SDL2][libsdl2], if the
228226
`WITH_VIDEO` option is `True`. Empty interface library if `WITH_VIDEO` is `False`. `simh_video` also
229227
pulls in `libpng` and `SDL_ttf`, along with their dependencies.
@@ -352,9 +350,7 @@ SIMH includes six `find_package` scripts:
352350
capture library; `libpcap` and it's Win32 equivalent are dynamically loaded at run time. The headers are
353351
only needed for `libpcap`'s function prototypes.
354352

355-
- `FindPCRE.cmake`, `FindPCRE2.cmake`: Locate PCRE and PCRE2 libraries and headers. SIMH does not support
356-
PCRE2 yet, however, finding the PCRE2 headers and library are included to make PCRE2 support easier at
357-
some point in the future.
353+
- `FindPCRE.cmake`, `FindPCRE2.cmake`: Locate PCRE and PCRE2 libraries and headers.
358354

359355
- `FindPTW.cmake`: "PTW" is shorthand for the Windows PThreads4w POSIX threads compatibility library.
360356

cmake/dep-link.cmake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,8 @@ if (WITH_REGEX)
195195
ELSEIF (NOT PREFER_PCRE AND PCRE2_FOUND)
196196
target_compile_definitions(simh_regexp INTERFACE HAVE_PCRE2_H)
197197
target_include_directories(simh_regexp INTERFACE ${PCRE2_INCLUDE_DIRS})
198-
if (NOT WIN32)
199198
target_link_libraries(simh_regexp INTERFACE ${PCRE2_LIBRARY})
200-
else ()
199+
if (WIN32)
201200
## Use static linkage (vice DLL) on Windows:
202201
target_compile_definitions(simh_regexp INTERFACE PCRE2_STATIC)
203202
endif ()

cmake/dep-locate.cmake

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ if (WITH_REGEX)
1919
find_package(PCRE)
2020
endif ()
2121
else ()
22+
## There isn't a difference between the vcpkg and LEGACY strategy.
23+
## SIMH provides its own FindPCRE2.cmake module...
2224
find_package(PCRE2)
2325
endif ()
2426
endif ()
@@ -111,7 +113,7 @@ include (ExternalProject)
111113

112114
# Source URLs (to make it easy to update versions):
113115
set(ZLIB_SOURCE_URL "https://github.com/madler/zlib/archive/v1.2.13.zip")
114-
set(PCRE2_SOURCE_URL "https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.40/pcre2-10.40.zip")
116+
set(PCRE2_SOURCE_URL "https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.42/pcre2-10.42.zip")
115117
## PCRE needs multiple URLs to chase a working SF mirror:
116118
list(APPEND PCRE_SOURCE_URL
117119
"https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.zip/download?use_mirror=cytranet"
@@ -178,10 +180,17 @@ IF (WITH_REGEX AND NOT (PCRE_FOUND OR PCRE2_FOUND OR TARGET unofficial::pcre::pc
178180
if (NOT PREFER_PCRE)
179181
set(PCRE_URL ${PCRE2_SOURCE_URL})
180182
list(APPEND PCRE_CMAKE_ARGS
181-
-DPCRE2_BUILD_PCREGREP:Bool=Off
183+
-DPCRE2_BUILD_PCRE2_8:Bool=On
184+
-DPCRE2_BUILD_PCRE2GREP:Bool=Off
185+
-DPCRE2_STATIC_RUNTIME:Bool=On
182186
-DPCRE2_SUPPORT_LIBEDIT:Bool=Off
183187
-DPCRE2_SUPPORT_LIBREADLINE:Bool=Off
184188
)
189+
if (WIN32)
190+
list(APPEND PCRE_CMAKE_ARGS
191+
-DBUILD_STATIC_LIBS:Bool=On
192+
)
193+
endif ()
185194

186195
# IF(MSVC)
187196
# list(APPEND PCRE_CMAKE_ARGS -DINSTALL_MSVC_PDB=On)

makefile

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,39 @@ ifeq (${WIN32},) #*nix Environments (&& cygwin)
603603
LIBEXT = $(LIBEXTSAVE)
604604
endif
605605
endif
606-
# Find PCRE RegEx library.
607-
ifneq (,$(call find_include,pcre))
608-
ifneq (,$(call find_lib,pcre))
609-
OS_CCDEFS += -DHAVE_PCRE_H
610-
OS_LDFLAGS += -lpcre
611-
$(info using libpcre: $(call find_lib,pcre) $(call find_include,pcre))
612-
ifeq ($(LD_SEARCH_NEEDED),$(call need_search,pcre))
613-
OS_LDFLAGS += -L$(dir $(call find_lib,pcre))
606+
## RegEx: Prefer the PCRE library over PCRE2 for historical reasons. Only detect
607+
## and use PCRE2 if USE_PCRE2 is set as a command line variable or in the
608+
## environment.
609+
FALLBACK_PCRE=yes
610+
ifdef USE_PCRE2
611+
# Find the PCRE2 RegEx library
612+
ifneq (,$(call find_include,pcre2))
613+
PCRE2_LIB=pcre2-8
614+
ifneq (,$(call find_lib,${PCRE2_LIB}))
615+
OS_CCDEFS += -DHAVE_PCRE2_H
616+
OS_LDFLAGS += -l${PCRE2_LIB}
617+
$(info using libpcre2-8: $(call find_lib,${PCRE2_LIB}) $(call find_include,pcre2))
618+
ifeq ($(LD_SEARCH_NEEDED),$(call need_search,${PCRE2_LIB}))
619+
OS_LDFLAGS += -L$(dir $(call find_lib,${{PCRE2_LIB}}))
620+
endif
621+
FALLBACK_PCRE=
622+
endif
623+
endif
624+
ifneq ($(FALLBACK_PCRE),)
625+
$(info *** Info *** PCRE2 not detected, falling back to PCRE)
626+
endif
627+
endif
628+
# Find PCRE RegEx library, either because we didn't want PCRE2 or we didn't
629+
# find PCRE2.
630+
ifneq (${FALLBACK_PCRE},)
631+
ifneq (,$(call find_include,pcre))
632+
ifneq (,$(call find_lib,pcre))
633+
OS_CCDEFS += -DHAVE_PCRE_H
634+
OS_LDFLAGS += -lpcre
635+
$(info using libpcre: $(call find_lib,pcre) $(call find_include,pcre))
636+
ifeq ($(LD_SEARCH_NEEDED),$(call need_search,pcre))
637+
OS_LDFLAGS += -L$(dir $(call find_lib,pcre))
638+
endif
614639
endif
615640
endif
616641
endif

0 commit comments

Comments
 (0)