Skip to content

Commit d81bfbd

Browse files
Changes to build chimera.
1. Addition of PCRE library 2. including the Chimera header files in JavaCppPreset
1 parent b0d5c9a commit d81bfbd

File tree

4 files changed

+210
-5
lines changed

4 files changed

+210
-5
lines changed

build.sh

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set -xeu
55
set -o pipefail
66

77
HYPERSCAN=5.4.0
8+
PCRE_VERSION=8.41
89
VECTORSCAN=af8c6d375cba32a50a9e575de83807d96a0fb503
910

1011
detect_platform() {
@@ -79,27 +80,31 @@ make -j $THREADS
7980
make install
8081
cd ..
8182

83+
curl -L -o pcre-$PCRE_VERSION.tar.gz https://sourceforge.net/projects/pcre/files/pcre/8.41/pcre-$PCRE_VERSION.tar.gz
84+
tar -zxf pcre-$PCRE_VERSION.tar.gz
85+
mv pcre-$PCRE_VERSION hyperscan-$HYPERSCAN
86+
8287
cd hyperscan-$HYPERSCAN
8388

8489
case $DETECTED_PLATFORM in
8590
linux-x86_64)
86-
CFLAGS='-O -fPIC' CC="gcc" CXX="g++ -std=c++11 -m64 -fPIC" cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/.." -DCMAKE_INSTALL_LIBDIR="lib" -DPCRE_SOURCE="." .
91+
CFLAGS='-O -fPIC' CC="gcc" CXX="g++ -std=c++11 -m64 -fPIC" cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/.." -DCMAKE_INSTALL_LIBDIR="lib" -DBUILD_STATIC_AND_SHARED=ON -DPCRE_SOURCE="pcre-$PCRE_VERSION" .
8792
make -j $THREADS hs hs_runtime hs_compile
8893
make install/strip
8994
;;
9095
macosx-x86_64)
91-
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/.." -DCMAKE_INSTALL_LIBDIR="lib" -DARCH_OPT_FLAGS='-Wno-error' -DPCRE_SOURCE="." .
96+
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/.." -DCMAKE_INSTALL_LIBDIR="lib" -DARCH_OPT_FLAGS='-Wno-error' -DBUILD_STATIC_AND_SHARED=ON -DPCRE_SOURCE="pcre-$PCRE_VERSION" .
9297
make -j $THREADS hs hs_runtime hs_compile
9398
make install/strip
9499
;;
95100
macosx-arm64)
96-
CFLAGS="-target arm64-apple-macos11" CXXFLAGS="-target arm64-apple-macos11" cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/.." -DCMAKE_INSTALL_LIBDIR="lib" -DARCH_OPT_FLAGS='-Wno-error' -DPCRE_SOURCE="." .
101+
CFLAGS="-target arm64-apple-macos11" CXXFLAGS="-target arm64-apple-macos11" cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/.." -DCMAKE_INSTALL_LIBDIR="lib" -DARCH_OPT_FLAGS='-Wno-error' -DBUILD_STATIC_AND_SHARED=ON -DPCRE_SOURCE="pcre-$PCRE_VERSION" .
97102
make -j $THREADS hs hs_runtime hs_compile
98103
make install/strip
99104
;;
100105
windows-x86_64)
101106
unset TEMP TMP # temp is defined in uppercase by bash and lowercase by windows, which causes problems with cmake + msbuild
102-
CXXFLAGS="/Wv:17" cmake -G "Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/.." -DCMAKE_INSTALL_LIBDIR="lib" -DARCH_OPT_FLAGS='' -DPCRE_SOURCE="." .
107+
CXXFLAGS="/Wv:17" cmake -G "Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/.." -DCMAKE_INSTALL_LIBDIR="lib" -DARCH_OPT_FLAGS='' -DBUILD_STATIC_AND_SHARED=ON -DPCRE_SOURCE="pcre-$PCRE_VERSION" .
103108
MSBuild.exe hyperscan.sln //p:Configuration=Release //p:Platform=x64
104109
cp -r src/* ../include/hs/
105110
cp lib/*.lib ../lib

src/main/java/com/gliwka/hyperscan/jni/JavaCppPreset.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
define = "NO_JNI_DETACH_THREAD", //JNI callback on same thread
3838
value = {"linux-x86_64", "macosx-x86_64", "macosx-arm64", "windows-x86_64"},
3939
compiler = "cpp11",
40-
include = {"hs/hs_common.h", "hs/hs_compile.h", "hs/hs_runtime.h", "hs/hs.h"},
40+
include = {
41+
"hs/hs_common.h", "hs/hs_compile.h", "hs/hs_runtime.h", "hs/hs.h",
42+
"hs/ch_common.h", "hs/ch_compile.h", "hs/ch_runtime.h", "hs/ch.h"
43+
},
4144
link = {"hs", "hs_runtime"}
4245
)
4346
},

src/sample/ChScanExample.java

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package sample;
2+
3+
4+
import com.gliwka.hyperscan.jni.ch_capture_t;
5+
import com.gliwka.hyperscan.jni.ch_compile_error_t;
6+
import com.gliwka.hyperscan.jni.ch_database_t;
7+
import com.gliwka.hyperscan.jni.ch_error_event_handler;
8+
import com.gliwka.hyperscan.jni.ch_match_event_handler;
9+
import com.gliwka.hyperscan.jni.ch_scratch_t;
10+
import com.gliwka.hyperscan.jni.hyperscan;
11+
import org.bytedeco.javacpp.BytePointer;
12+
import org.bytedeco.javacpp.IntPointer;
13+
import org.bytedeco.javacpp.Loader;
14+
import org.bytedeco.javacpp.Pointer;
15+
import org.bytedeco.javacpp.PointerPointer;
16+
import org.bytedeco.javacpp.annotation.Cast;
17+
import org.bytedeco.javacpp.annotation.Const;
18+
19+
import java.util.LinkedList;
20+
21+
import static com.gliwka.hyperscan.jni.hyperscan.HS_FLAG_SINGLEMATCH;
22+
import static com.gliwka.hyperscan.jni.hyperscan.HS_MODE_BLOCK;
23+
24+
25+
public class ChScanExample {
26+
27+
public static void main(String... args) {
28+
Loader.load(hyperscan.class);
29+
30+
String[] patterns = {"abc1", "asa", "dab"};
31+
ch_database_t database_t = null;
32+
ch_match_event_handler matchEventHandler = null;
33+
ch_error_event_handler errorEventHandler = null;
34+
ch_scratch_t scratchSpace = new ch_scratch_t();
35+
ch_compile_error_t compile_error_t;
36+
final LinkedList<long[]> matchedIds = new LinkedList<>();
37+
try (PointerPointer<ch_database_t> database_t_p = new PointerPointer<>(1);
38+
PointerPointer<ch_compile_error_t> compile_error_t_p = new PointerPointer<>(1);
39+
IntPointer compileFlags = new IntPointer(HS_FLAG_SINGLEMATCH, HS_FLAG_SINGLEMATCH, HS_FLAG_SINGLEMATCH);
40+
IntPointer patternIds = new IntPointer(1, 2, 3);
41+
PointerPointer expressionsPointer = new PointerPointer<BytePointer>(patterns);
42+
) {
43+
matchEventHandler = new ch_match_event_handler() {
44+
@Override
45+
public int call(@Cast("unsigned int") int id,
46+
@Cast("unsigned long long") long from,
47+
@Cast("unsigned long long") long to,
48+
@Cast("unsigned int") int flags,
49+
@Cast("unsigned int") int size,
50+
@Const ch_capture_t captured,
51+
Pointer ctx) {
52+
53+
System.out.println("id" + id + ": " + from + "-" + to);
54+
matchedIds.add(new long[]{from, to});
55+
return 0;
56+
}
57+
};
58+
59+
errorEventHandler = new ch_error_event_handler() {
60+
@Override
61+
public int call(@Cast("ch_error_event_t") int error_type,
62+
@Cast("unsigned int") int id, Pointer info,
63+
Pointer ctx) {
64+
65+
System.out.println("Error for id" + id + ":" + error_type);
66+
return 0;
67+
}
68+
};
69+
70+
int result = hyperscan.ch_compile_multi(
71+
expressionsPointer,
72+
compileFlags,
73+
patternIds,
74+
3,
75+
HS_MODE_BLOCK,
76+
null,
77+
database_t_p,
78+
compile_error_t_p
79+
);
80+
81+
database_t = new ch_database_t(database_t_p.get(0));
82+
compile_error_t = new ch_compile_error_t(compile_error_t_p.get(0));
83+
if (result != 0) {
84+
System.out.println(compile_error_t.message().getString());
85+
System.exit(1);
86+
}
87+
result = hyperscan.ch_alloc_scratch(database_t, scratchSpace);
88+
if (result != 0) {
89+
System.out.println("Error during scratch space allocation");
90+
System.exit(1);
91+
}
92+
93+
String textToSearch = "-21dasaaadabcaaa";
94+
95+
hyperscan.ch_scan(
96+
database_t,
97+
textToSearch,
98+
textToSearch.length(),
99+
0,
100+
scratchSpace,
101+
matchEventHandler,
102+
errorEventHandler,
103+
expressionsPointer
104+
);
105+
106+
System.out.println("Count: " + matchedIds.size());
107+
108+
} catch (Exception e) {
109+
throw new RuntimeException(e);
110+
} finally {
111+
hyperscan.ch_free_scratch(scratchSpace);
112+
if (database_t != null) {
113+
hyperscan.ch_free_database(database_t);
114+
}
115+
if (matchEventHandler != null) {
116+
matchEventHandler.close();
117+
}
118+
}
119+
}
120+
}

src/sample/HyperscanSample.java

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package sample;
2+
3+
import com.gliwka.hyperscan.jni.hs_compile_error_t;
4+
import com.gliwka.hyperscan.jni.hs_database_t;
5+
import com.gliwka.hyperscan.jni.hs_scratch_t;
6+
import com.gliwka.hyperscan.jni.hyperscan;
7+
import com.gliwka.hyperscan.jni.match_event_handler;
8+
import org.bytedeco.javacpp.BytePointer;
9+
import org.bytedeco.javacpp.IntPointer;
10+
import org.bytedeco.javacpp.Loader;
11+
import org.bytedeco.javacpp.Pointer;
12+
import org.bytedeco.javacpp.PointerPointer;
13+
import org.bytedeco.javacpp.annotation.Cast;
14+
15+
import static com.gliwka.hyperscan.jni.hyperscan.HS_FLAG_SINGLEMATCH;
16+
import static com.gliwka.hyperscan.jni.hyperscan.HS_MODE_BLOCK;
17+
18+
19+
public class HyperscanSample {
20+
21+
public static void main(String[] args) {
22+
Loader.load(hyperscan.class);
23+
24+
String[] patterns = {"abc1", "asa", "dab"};
25+
hs_database_t database_t = null;
26+
match_event_handler matchEventHandler = null;
27+
hs_scratch_t scratchSpace = new hs_scratch_t();
28+
hs_compile_error_t compile_error_t;
29+
30+
try (PointerPointer<hs_database_t> database_t_p = new PointerPointer<>(1);
31+
PointerPointer<hs_compile_error_t> compile_error_t_p = new PointerPointer<>(1);
32+
IntPointer compileFlags = new IntPointer(HS_FLAG_SINGLEMATCH, HS_FLAG_SINGLEMATCH, HS_FLAG_SINGLEMATCH);
33+
IntPointer patternIds = new IntPointer(1, 1, 1);
34+
PointerPointer<BytePointer> expressionsPointer = new PointerPointer<>(patterns)
35+
) {
36+
37+
matchEventHandler = new match_event_handler() {
38+
@Override
39+
public int call(@Cast("unsigned int") int id,
40+
@Cast("unsigned long long") long from,
41+
@Cast("unsigned long long") long to,
42+
@Cast("unsigned int") int flags, Pointer context) {
43+
System.out.println(from + "-" + to);
44+
System.out.println(id);
45+
return 0;
46+
}
47+
};
48+
49+
int result = hyperscan.hs_compile_multi(expressionsPointer, compileFlags, patternIds, 3, HS_MODE_BLOCK,
50+
null, database_t_p, compile_error_t_p);
51+
52+
database_t = new hs_database_t(database_t_p.get(0));
53+
compile_error_t = new hs_compile_error_t(compile_error_t_p.get(0));
54+
if (result != 0) {
55+
System.out.println(compile_error_t.message().getString());
56+
System.exit(1);
57+
}
58+
result = hyperscan.hs_alloc_scratch(database_t, scratchSpace);
59+
if (result != 0) {
60+
System.out.println("Error during scratch space allocation");
61+
System.exit(1);
62+
}
63+
64+
String textToSearch = "-21dasaaadabcaaa";
65+
hyperscan.hs_scan(database_t, textToSearch, textToSearch.length(), 0, scratchSpace, matchEventHandler, expressionsPointer);
66+
67+
} finally {
68+
hyperscan.hs_free_scratch(scratchSpace);
69+
if (database_t != null) {
70+
hyperscan.hs_free_database(database_t);
71+
}
72+
if (matchEventHandler != null) {
73+
matchEventHandler.close();
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)