From 8d24c4fdb9d453025c5acb84409ae5b74c56bd65 Mon Sep 17 00:00:00 2001 From: Kate F Date: Mon, 19 Apr 2021 17:17:04 -0700 Subject: [PATCH 1/9] Initial import for Cargo. --- .gitignore | 4 +++- Cargo.toml | 13 +++++++++++++ Makefile | 15 +++++++++++++++ src/libfsm/fsm.c | 4 ++++ src/libfsm/lib.rs | 12 ++++++++++++ src/libfsm/libfsm.syms | 4 ++++ src/libfsm/libfsm_rs.h | 6 ++++++ 7 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Cargo.toml create mode 100644 src/libfsm/lib.rs create mode 100644 src/libfsm/libfsm_rs.h diff --git a/.gitignore b/.gitignore index 567609b12..65d6036f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -build/ +/build +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 000000000..f443424c4 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "libfsm-rs" +version = "0.1.0" +authors = ["Kate F "] +edition = "2018" + +[lib] +path = "src/libfsm/lib.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + diff --git a/Makefile b/Makefile index d5c14b3cc..053fb0363 100644 --- a/Makefile +++ b/Makefile @@ -110,3 +110,18 @@ STAGE_BUILD := ${STAGE_BUILD:Nbin/cvtpcre} grep FAIL ${BUILD}/tests/*/res*; [ $$? -ne 0 ] .endif +.if !${CC:T:Memcc*} + +.for prog in ${PROG} +${BUILD}/bin/${prog}: ./target/debug/liblibfsm_rs.a +.endfor + +# naming for kmkf prog.mk +./target/debug/liblibfsm_rs.a: ./target/debug/liblibfsm_rs.rlib + ln -sf ${.ALLSRC:T} ${.TARGET} + +./target/debug/liblibfsm_rs.rlib: + cargo build + +.endif + diff --git a/src/libfsm/fsm.c b/src/libfsm/fsm.c index 60f748184..99848c603 100644 --- a/src/libfsm/fsm.c +++ b/src/libfsm/fsm.c @@ -22,6 +22,7 @@ #include "internal.h" #include "capture.h" #include "endids.h" +#include "libfsm_rs.h" void free_contents(struct fsm *fsm) @@ -47,6 +48,9 @@ fsm_new(const struct fsm_options *opt) static const struct fsm_options defaults; struct fsm *new, f; + /* just to prove linking works */ + fsm_noop(); + if (opt == NULL) { opt = &defaults; } diff --git a/src/libfsm/lib.rs b/src/libfsm/lib.rs new file mode 100644 index 000000000..def4ba84e --- /dev/null +++ b/src/libfsm/lib.rs @@ -0,0 +1,12 @@ +// just here to prove linking works +#[no_mangle] +pub extern "C" fn fsm_noop() { +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} diff --git a/src/libfsm/libfsm.syms b/src/libfsm/libfsm.syms index 947012fd0..667aac301 100644 --- a/src/libfsm/libfsm.syms +++ b/src/libfsm/libfsm.syms @@ -126,3 +126,7 @@ fsm_capture_set_path fsm_capture_rebase_capture_id fsm_capture_alloc fsm_capture_dump + +# from rust +fsm_noop + diff --git a/src/libfsm/libfsm_rs.h b/src/libfsm/libfsm_rs.h new file mode 100644 index 000000000..d81f33857 --- /dev/null +++ b/src/libfsm/libfsm_rs.h @@ -0,0 +1,6 @@ +#ifndef LIBFSM_RS_H +#define LIBFSM_RS_H + +void fsm_noop(void); + +#endif From a643036c6bb26bfbc82f024934589c4fd1c6868f Mon Sep 17 00:00:00 2001 From: Kate F Date: Mon, 19 Apr 2021 17:33:39 -0700 Subject: [PATCH 2/9] Special cases for tests with their own build stuff. --- tests/aho_corasick/Makefile | 4 ++++ tests/capture/Makefile | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/tests/aho_corasick/Makefile b/tests/aho_corasick/Makefile index 5748ddd5c..89524ed3b 100644 --- a/tests/aho_corasick/Makefile +++ b/tests/aho_corasick/Makefile @@ -16,6 +16,10 @@ ${TEST_OUTDIR.tests/aho_corasick}/actest: ${TEST_OUTDIR.tests/aho_corasick}/acte ${TEST_OUTDIR.tests/aho_corasick}/actest: ${BUILD}/lib/${lib:R}.a .endfor +.if !${CC:T:Memcc*} +${TEST_OUTDIR.tests/aho_corasick}/actest: ./target/debug/liblibfsm_rs.a +.endif + AC_TEST=${TEST_OUTDIR.tests/aho_corasick}/actest ${AC_TEST}: diff --git a/tests/capture/Makefile b/tests/capture/Makefile index 53d63ff2b..9117ebb2c 100644 --- a/tests/capture/Makefile +++ b/tests/capture/Makefile @@ -9,8 +9,13 @@ test:: ${TEST_OUTDIR.tests/capture}/res${n} SRC += ${TEST_SRCDIR.tests/capture}/capture${n}.c CFLAGS.${TEST_SRCDIR.tests/capture}/capture${n}.c = -UNDEBUG +.if ${CC:T:Memcc*} ${TEST_OUTDIR.tests/capture}/run${n}: ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ${CC} ${CFLAGS} -o ${TEST_OUTDIR.tests/capture}/run${n} ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ${BUILD}/lib/libfsm.a +.else +${TEST_OUTDIR.tests/capture}/run${n}: ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ./target/debug/liblibfsm_rs.rlib + ${CC} ${CFLAGS} -o ${TEST_OUTDIR.tests/capture}/run${n} ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ${BUILD}/lib/libfsm.a ./target/debug/liblibfsm_rs.a +.endif ${TEST_OUTDIR.tests/capture}/res${n}: ${TEST_OUTDIR.tests/capture}/run${n} ( ${TEST_OUTDIR.tests/capture}/run${n} 1>&2 && echo PASS || echo FAIL ) > ${TEST_OUTDIR.tests/capture}/res${n} From 00c33dfb2a88ee061fc7dc8ae65f4e11d847dd10 Mon Sep 17 00:00:00 2001 From: Kate F Date: Mon, 19 Apr 2021 17:53:56 -0700 Subject: [PATCH 3/9] An attempt to bring in Cargo-generated dependencies. --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Makefile b/Makefile index 053fb0363..4025c0c4f 100644 --- a/Makefile +++ b/Makefile @@ -123,5 +123,10 @@ ${BUILD}/bin/${prog}: ./target/debug/liblibfsm_rs.a ./target/debug/liblibfsm_rs.rlib: cargo build +./target/debug/liblibfsm_rs.d: ./target/debug/liblibfsm_rs.rlib +.if exists(./target/debug/liblibfsm_rs.d) +.include "./target/debug/liblibfsm_rs.d" +.endif + .endif From 977208935f45e0fed00e4bd0aedf340b547f3b62 Mon Sep 17 00:00:00 2001 From: Kate F Date: Mon, 19 Apr 2021 17:54:17 -0700 Subject: [PATCH 4/9] Run cargo test. --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 4025c0c4f..2f5cfcb72 100644 --- a/Makefile +++ b/Makefile @@ -123,6 +123,9 @@ ${BUILD}/bin/${prog}: ./target/debug/liblibfsm_rs.a ./target/debug/liblibfsm_rs.rlib: cargo build +test:: + cargo test + ./target/debug/liblibfsm_rs.d: ./target/debug/liblibfsm_rs.rlib .if exists(./target/debug/liblibfsm_rs.d) .include "./target/debug/liblibfsm_rs.d" From 6e05435ba6fd21d3cd53919731e9a49c425bcf3b Mon Sep 17 00:00:00 2001 From: Kate F Date: Mon, 19 Apr 2021 17:58:59 -0700 Subject: [PATCH 5/9] Switch to `staticlib` rather than symlinking for an archive. --- Cargo.toml | 1 + Makefile | 8 ++------ tests/capture/Makefile | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f443424c4..48f6a50a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Kate F "] edition = "2018" [lib] +crate-type = ["staticlib"] path = "src/libfsm/lib.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/Makefile b/Makefile index 2f5cfcb72..b1bbc3e5b 100644 --- a/Makefile +++ b/Makefile @@ -116,17 +116,13 @@ STAGE_BUILD := ${STAGE_BUILD:Nbin/cvtpcre} ${BUILD}/bin/${prog}: ./target/debug/liblibfsm_rs.a .endfor -# naming for kmkf prog.mk -./target/debug/liblibfsm_rs.a: ./target/debug/liblibfsm_rs.rlib - ln -sf ${.ALLSRC:T} ${.TARGET} - -./target/debug/liblibfsm_rs.rlib: +./target/debug/liblibfsm_rs.a: cargo build test:: cargo test -./target/debug/liblibfsm_rs.d: ./target/debug/liblibfsm_rs.rlib +./target/debug/liblibfsm_rs.d: ./target/debug/liblibfsm_rs.a .if exists(./target/debug/liblibfsm_rs.d) .include "./target/debug/liblibfsm_rs.d" .endif diff --git a/tests/capture/Makefile b/tests/capture/Makefile index 9117ebb2c..bf8734df7 100644 --- a/tests/capture/Makefile +++ b/tests/capture/Makefile @@ -13,7 +13,7 @@ CFLAGS.${TEST_SRCDIR.tests/capture}/capture${n}.c = -UNDEBUG ${TEST_OUTDIR.tests/capture}/run${n}: ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ${CC} ${CFLAGS} -o ${TEST_OUTDIR.tests/capture}/run${n} ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ${BUILD}/lib/libfsm.a .else -${TEST_OUTDIR.tests/capture}/run${n}: ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ./target/debug/liblibfsm_rs.rlib +${TEST_OUTDIR.tests/capture}/run${n}: ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ./target/debug/liblibfsm_rs.a ${CC} ${CFLAGS} -o ${TEST_OUTDIR.tests/capture}/run${n} ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ${BUILD}/lib/libfsm.a ./target/debug/liblibfsm_rs.a .endif From 34e8b493f4f01999759e06457c033ece8bd7b06d Mon Sep 17 00:00:00 2001 From: Kate F Date: Mon, 19 Apr 2021 21:07:51 -0700 Subject: [PATCH 6/9] Remove support for building shared libraries. This is a shame, but gets in the way of porting to Rust. --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index b1bbc3e5b..251e95288 100644 --- a/Makefile +++ b/Makefile @@ -92,7 +92,6 @@ INCDIR += include .include .include .include -.include .include .include .include From b572d09632199cf9b0935c43a4de9214396ec7c4 Mon Sep 17 00:00:00 2001 From: Kate F Date: Mon, 19 Apr 2021 21:44:22 -0700 Subject: [PATCH 7/9] Add `-undefined dynamic_lookup` for symbols provided by Cargo-built libraries. --- Makefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Makefile b/Makefile index 251e95288..b8920ad5b 100644 --- a/Makefile +++ b/Makefile @@ -126,5 +126,12 @@ test:: .include "./target/debug/liblibfsm_rs.d" .endif +# for symbols provided by libraries built by cargo instead +.if ${SYSTEM} == Darwin +.for part in ${PART} +LDRFLAGS.${part} += -undefined dynamic_lookup +.endfor +.endif + .endif From bb83fd4a68fe1bf889068bc553aa3e422a1afc71 Mon Sep 17 00:00:00 2001 From: Kate F Date: Tue, 20 Apr 2021 02:05:34 -0700 Subject: [PATCH 8/9] Switch to ld -r linking the cargo-generated .a archive when producing the partially linked .o files. This hopefully avoids an ld bug where `-undefined dynamic_lookup` doesn't work on MacOS, and also removes the need for programs to link both the C and Rust libraries directly. --- Makefile | 11 +++-------- tests/aho_corasick/Makefile | 4 ---- tests/capture/Makefile | 5 ----- 3 files changed, 3 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index b8920ad5b..9982082e6 100644 --- a/Makefile +++ b/Makefile @@ -111,10 +111,6 @@ STAGE_BUILD := ${STAGE_BUILD:Nbin/cvtpcre} .if !${CC:T:Memcc*} -.for prog in ${PROG} -${BUILD}/bin/${prog}: ./target/debug/liblibfsm_rs.a -.endfor - ./target/debug/liblibfsm_rs.a: cargo build @@ -126,12 +122,11 @@ test:: .include "./target/debug/liblibfsm_rs.d" .endif -# for symbols provided by libraries built by cargo instead -.if ${SYSTEM} == Darwin .for part in ${PART} -LDRFLAGS.${part} += -undefined dynamic_lookup +${BUILD}/lib/${part}.o: ./target/debug/liblibfsm_rs.a +# hijacking LDRFLAGS here because the target only expects .o sources +LDRFLAGS.${part} += ./target/debug/liblibfsm_rs.a .endfor -.endif .endif diff --git a/tests/aho_corasick/Makefile b/tests/aho_corasick/Makefile index 89524ed3b..5748ddd5c 100644 --- a/tests/aho_corasick/Makefile +++ b/tests/aho_corasick/Makefile @@ -16,10 +16,6 @@ ${TEST_OUTDIR.tests/aho_corasick}/actest: ${TEST_OUTDIR.tests/aho_corasick}/acte ${TEST_OUTDIR.tests/aho_corasick}/actest: ${BUILD}/lib/${lib:R}.a .endfor -.if !${CC:T:Memcc*} -${TEST_OUTDIR.tests/aho_corasick}/actest: ./target/debug/liblibfsm_rs.a -.endif - AC_TEST=${TEST_OUTDIR.tests/aho_corasick}/actest ${AC_TEST}: diff --git a/tests/capture/Makefile b/tests/capture/Makefile index bf8734df7..53d63ff2b 100644 --- a/tests/capture/Makefile +++ b/tests/capture/Makefile @@ -9,13 +9,8 @@ test:: ${TEST_OUTDIR.tests/capture}/res${n} SRC += ${TEST_SRCDIR.tests/capture}/capture${n}.c CFLAGS.${TEST_SRCDIR.tests/capture}/capture${n}.c = -UNDEBUG -.if ${CC:T:Memcc*} ${TEST_OUTDIR.tests/capture}/run${n}: ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ${CC} ${CFLAGS} -o ${TEST_OUTDIR.tests/capture}/run${n} ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ${BUILD}/lib/libfsm.a -.else -${TEST_OUTDIR.tests/capture}/run${n}: ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ./target/debug/liblibfsm_rs.a - ${CC} ${CFLAGS} -o ${TEST_OUTDIR.tests/capture}/run${n} ${TEST_OUTDIR.tests/capture}/capture${n}.o ${TEST_OUTDIR.tests/capture}/captest.o ${BUILD}/lib/libfsm.a ./target/debug/liblibfsm_rs.a -.endif ${TEST_OUTDIR.tests/capture}/res${n}: ${TEST_OUTDIR.tests/capture}/run${n} ( ${TEST_OUTDIR.tests/capture}/run${n} 1>&2 && echo PASS || echo FAIL ) > ${TEST_OUTDIR.tests/capture}/res${n} From 677279d321cdd26c928dbd222ba597b73c81851e Mon Sep 17 00:00:00 2001 From: Kate F Date: Tue, 20 Apr 2021 08:36:51 -0700 Subject: [PATCH 9/9] Only the one `${part}` depends on libfsm_rs. --- Makefile | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 9982082e6..a8c560455 100644 --- a/Makefile +++ b/Makefile @@ -122,11 +122,9 @@ test:: .include "./target/debug/liblibfsm_rs.d" .endif -.for part in ${PART} -${BUILD}/lib/${part}.o: ./target/debug/liblibfsm_rs.a +${BUILD}/lib/libfsm.o: ./target/debug/liblibfsm_rs.a # hijacking LDRFLAGS here because the target only expects .o sources -LDRFLAGS.${part} += ./target/debug/liblibfsm_rs.a -.endfor +LDRFLAGS.libfsm += ./target/debug/liblibfsm_rs.a .endif