[libnbd PATCH] golang: Optionally use gofmt on generated .go files
Combine my recent work on improving the generated Go output with Tage's work on using a canonical formatter for Rust. If gofmt is available during the build, then the generated .go files will now use TAB indents and have proper columnar alignment; if it is not available, the project still compiles. Signed-off-by: Eric Blake <eblake@redhat.com> --- Followup to this earlier series: https://listman.redhat.com/archives/libguestfs/2023-July/032133.html I still need to fold in the 'make check' verification of non-generated .go files, plus Dan's idea of enabling CI coverage... configure.ac | 1 + generator/config.mli | 1 + generator/utils.mli | 1 + generator/config.ml.in | 3 ++- generator/generator.ml | 15 ++++++++++----- generator/utils.ml | 12 +++++++++++- 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/configure.ac b/configure.ac index 3e8b9142..b475dccf 100644 --- a/configure.ac +++ b/configure.ac @@ -596,6 +596,7 @@ AC_ARG_ENABLE([golang], [enable_golang=yes]) AS_IF([test "x$enable_golang" != "xno"],[ AC_CHECK_PROG([GOLANG],[go],[go],[no]) + AC_CHECK_PROG([GOFMT],[gofmt],[gofmt],[no]) AS_IF([test "x$GOLANG" != "xno"],[ AC_MSG_CHECKING([if $GOLANG is usable]) AS_IF([ ( diff --git a/generator/config.mli b/generator/config.mli index 5f1a46a6..22c61dad 100644 --- a/generator/config.mli +++ b/generator/config.mli @@ -17,4 +17,5 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *) +val gofmt : string val rustfmt : string diff --git a/generator/utils.mli b/generator/utils.mli index d97d43ac..9f7824b2 100644 --- a/generator/utils.mli +++ b/generator/utils.mli @@ -54,6 +54,7 @@ val (** Type of code formatter. *) type formatter = + | Gofmt | Rustfmt (** Redirect stdout to a file. Possibly formatting the code. *) diff --git a/generator/config.ml.in b/generator/config.ml.in index 7ac5237c..48d0d4a4 100644 --- a/generator/config.ml.in +++ b/generator/config.ml.in @@ -18,4 +18,5 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *) -let rustfmt = "@RUSTFMT@" +let gofmt = "@GOFMT@" +let rustfmt = "@RUSTFMT@" diff --git a/generator/generator.ml b/generator/generator.ml index c62b0c4f..8c9a585b 100644 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -57,10 +57,15 @@ let output_to "ocaml/NBD.ml" OCaml.generate_ocaml_nbd_ml; output_to "ocaml/nbd-c.c" OCaml.generate_ocaml_nbd_c; - output_to "golang/bindings.go" GoLang.generate_golang_bindings_go; - output_to "golang/closures.go" GoLang.generate_golang_closures_go; - output_to "golang/wrappers.go" GoLang.generate_golang_wrappers_go; + output_to ~formatter:(Some Gofmt) "golang/bindings.go" + GoLang.generate_golang_bindings_go; + output_to ~formatter:(Some Gofmt) "golang/closures.go" + GoLang.generate_golang_closures_go; + output_to ~formatter:(Some Gofmt) "golang/wrappers.go" + GoLang.generate_golang_wrappers_go; output_to "golang/wrappers.h" GoLang.generate_golang_wrappers_h; - output_to ~formatter:(Some Rustfmt) "rust/libnbd-sys/src/generated.rs" RustSys.generate_rust_sys_bindings; - output_to ~formatter:(Some Rustfmt) "rust/src/bindings.rs" Rust.generate_rust_bindings; + output_to ~formatter:(Some Rustfmt) "rust/libnbd-sys/src/generated.rs" + RustSys.generate_rust_sys_bindings; + output_to ~formatter:(Some Rustfmt) "rust/src/bindings.rs" + Rust.generate_rust_bindings; diff --git a/generator/utils.ml b/generator/utils.ml index 3302b309..443ac6df 100644 --- a/generator/utils.ml +++ b/generator/utils.ml @@ -420,6 +420,7 @@ let | i -> failwithf "%s: failed with error code %d" cmd i type formatter = + | Gofmt | Rustfmt let output_to ?(formatter = None) filename k = @@ -431,6 +432,15 @@ let close_out c; chan := NoOutput; (match formatter with + | Some Gofmt -> + if Config.gofmt <> "no" then ( + let cmd = sprintf "%s -w %s" Config.gofmt filename_new in + match system cmd with + | WEXITED 0 -> () + | WEXITED i -> failwithf "gofmt failed with exit code %d" i + | WSIGNALED i | WSTOPPED i -> + failwithf "gofmt was killed or stopped by signal %d" i + ) | Some Rustfmt -> if Config.rustfmt <> "no" then ( let cmd = sprintf "%s %s" Config.rustfmt filename_new in @@ -439,7 +449,7 @@ let | WEXITED i -> failwithf "rustfmt failed with exit code %d" i | WSIGNALED i | WSTOPPED i -> failwithf "rustfmt was killed or stopped by signal %d" i - ); + ) | None -> ()); (* Is the new file different from the current file? *) if Sys.file_exists filename && files_equal filename filename_new then -- 2.41.0
On Fri, Aug 04, 2023 at 09:29:48AM -0500, Eric Blake wrote:
Combine my recent work on improving the generated Go output with Tage's work on using a canonical formatter for Rust. If gofmt is available during the build, then the generated .go files will now use TAB indents and have proper columnar alignment; if it is not available, the project still compiles.
Signed-off-by: Eric Blake <eblake@redhat.com> ---
Followup to this earlier series: https://listman.redhat.com/archives/libguestfs/2023-July/032133.html
Now in as 9cb10b39, after rebasing to yet more rustfmt changes in the meantime.
I still need to fold in the 'make check' verification of non-generated .go files, plus Dan's idea of enabling CI coverage...
This still remains on my todo list. -- Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
On Mon, Aug 07, 2023 at 04:11:15PM -0500, Eric Blake wrote:
On Fri, Aug 04, 2023 at 09:29:48AM -0500, Eric Blake wrote:
Combine my recent work on improving the generated Go output with Tage's work on using a canonical formatter for Rust. If gofmt is available during the build, then the generated .go files will now use TAB indents and have proper columnar alignment; if it is not available, the project still compiles.
Signed-off-by: Eric Blake <eblake@redhat.com> ---
Followup to this earlier series: https://listman.redhat.com/archives/libguestfs/2023-July/032133.html
Now in as 9cb10b39, after rebasing to yet more rustfmt changes in the meantime.
This did seem to cause trouble with the CI: https://gitlab.com/nbdkit/libnbd/-/pipelines/958658174 On FreeBSD only, the error is: Error: Error on dynamically loaded library: ./../dllmlnbd.so: /usr/local/lib/libgnutls.so.30: Undefined symbol "strverscmp@FBSD_1.7" This makes no sense to me, could be a coincidental problem with FreeBSD, gnutls or gnulib. Rich.
I still need to fold in the 'make check' verification of non-generated .go files, plus Dan's idea of enabling CI coverage...
This still remains on my todo list.
-- Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org _______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
participants (2)
-
Eric Blake -
Richard W.M. Jones