From 8f7beb277330068003df23b6d7e00d7799406909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Fri, 11 Jul 2025 15:57:28 +0000 Subject: [PATCH 01/13] Configure gazelle_cabal --- BUILD.bazel | 19 +++++++++++++++ WORKSPACE | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ nixpkgs.nix | 4 ++- 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/BUILD.bazel b/BUILD.bazel index dff62f0d..3573869a 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,5 +1,7 @@ exports_files(["nixpkgs.nix"]) +#gazelle:exclude {benchmarks,examples,jvm-streaming,jvm-batching} + load( "@rules_haskell//haskell:defs.bzl", "ghc_plugin", @@ -52,3 +54,20 @@ ghc_plugin( visibility = ["//visibility:public"], deps = [":inline-java"], ) + +load( + "@bazel_gazelle//:def.bzl", + "DEFAULT_LANGUAGES", + "gazelle", + "gazelle_binary", +) + +gazelle( + name = "gazelle", + gazelle = ":gazelle_binary", +) + +gazelle_binary( + name = "gazelle_binary", + languages = DEFAULT_LANGUAGES + ["@io_tweag_gazelle_cabal//gazelle_cabal"], +) diff --git a/WORKSPACE b/WORKSPACE index 35b389c0..b5365686 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -98,6 +98,10 @@ stack_snapshot( "ghc-boot-th", "pretty", "transformers", + # gazelle_cabal dependencies + "json", # keep + "path", # keep + "path-io", # keep ], extra_deps = { "zlib" : ["@zlib.dev//:zlib"] }, components_dependencies = { @@ -250,3 +254,69 @@ maven_install( "https://repo1.maven.org/maven2", ], ) + +# gazelle setup + +http_archive( + name = "rules_nixpkgs_go", + sha256 = "30271f7bd380e4e20e4d7132c324946c4fdbc31ebe0bbb6638a0f61a37e74397", + strip_prefix = "rules_nixpkgs-0.13.0/toolchains/go", + urls = ["https://github.com/tweag/rules_nixpkgs/releases/download/v0.13.0/rules_nixpkgs-0.13.0.tar.gz"], +) + +http_archive( + name = "io_bazel_rules_go", + sha256 = "f2dcd210c7095febe54b804bb1cd3a58fe8435a909db2ec04e31542631cf715c", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.31.0/rules_go-v0.31.0.zip", + "https://github.com/bazelbuild/rules_go/releases/download/v0.31.0/rules_go-v0.31.0.zip", + ], +) + +http_archive( + name = "bazel_gazelle", + sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz", + "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz", + ], +) + +load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository") + +############################################################ +# Define your own dependencies here using go_repository. +# Else, dependencies declared by rules_go/gazelle will be used. +# The first declaration of an external repository "wins". +############################################################ + +load("@rules_nixpkgs_go//:go.bzl", "nixpkgs_go_configure") + +nixpkgs_go_configure( + repository = "@nixpkgs", +) + +load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") + +go_rules_dependencies() + +gazelle_dependencies() + + +http_archive( + name = "io_tweag_gazelle_cabal", + strip_prefix = "gazelle_cabal-ca8f68e250bea33815fb373320f9610582c42083", + sha256 = "bd2ee67943007723b3425bf2fcbdb6b41b269c7bc03f01e40683d4b5f3984b3b", + url = "https://github.com/tweag/gazelle_cabal/archive/ca8f68e.zip", +) + +load("@rules_haskell//haskell:cabal.bzl", "stack_snapshot") +load("@io_tweag_gazelle_cabal//:defs.bzl", "gazelle_cabal_dependencies") +gazelle_cabal_dependencies() + +go_repository( + name = "org_golang_x_xerrors", + importpath = "golang.org/x/xerrors", + sum = "h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=", + version = "v0.0.0-20200804184101-5ec99f83aff1", +) diff --git a/nixpkgs.nix b/nixpkgs.nix index 794d0745..de81a1fc 100644 --- a/nixpkgs.nix +++ b/nixpkgs.nix @@ -3,7 +3,9 @@ # # by going to a commit before the one introducing the regression. args: -let pkgs = import (fetchTarball "https://github.com/tweag/nixpkgs/archive/73ad5f9e147.tar.gz") args; +let pkgs = import (fetchTarball { + sha256 = "01j7nhxbb2kjw38yk4hkjkkbmz50g3br7fgvad6b1cjpdvfsllds"; + url = "https://github.com/tweag/nixpkgs/archive/73ad5f9e147.tar.gz";}) args; stack_ignore_global_hints = pkgs.writeScriptBin "stack" '' #!${pkgs.stdenv.shell} # Skips the --global-hints parameter to stack. This is From 5bb2fea980a30cf6630b2481539db031e8e8d7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Fri, 11 Jul 2025 16:57:58 +0000 Subject: [PATCH 02/13] Fix formatting of BUILD files with gazelle --- BUILD.bazel | 13 +++++++------ jni/BUILD.bazel | 24 ++++++++++++++---------- jni/tests/BUILD.bazel | 10 +++++----- jvm-types/BUILD.bazel | 12 ++++++------ jvm-types/src/test/haskell/BUILD.bazel | 8 ++++---- jvm/BUILD.bazel | 14 +++++++------- jvm/benchmarks/BUILD.bazel | 10 +++++----- jvm/tests/BUILD.bazel | 14 +++++++------- tests/BUILD.bazel | 16 ++++++++-------- 9 files changed, 63 insertions(+), 58 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 3573869a..d2092fb0 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -10,8 +10,8 @@ load( cc_library( name = "bctable", - hdrs = ["cbits/bctable.h"], srcs = ["cbits/bctable.c"], + hdrs = ["cbits/bctable.h"], strip_include_prefix = "cbits", ) @@ -19,15 +19,16 @@ haskell_library( name = "inline-java", # cbits/bctable.h is included here so it can be found by Plugin.hs # at the same location when building with bazel as with stack. - srcs = glob(['src/**/*.hs', - 'src/**/*.hsc', - 'cbits/bctable.h', - ]), + srcs = glob([ + "src/**/*.hs", + "src/**/*.hsc", + "cbits/bctable.h", + ]), visibility = ["//visibility:public"], deps = [ + ":bctable", "//jni", "//jvm", - ":bctable", "@stackage//:Cabal", "@stackage//:base", "@stackage//:bytestring", diff --git a/jni/BUILD.bazel b/jni/BUILD.bazel index a96bfde6..a9e92185 100644 --- a/jni/BUILD.bazel +++ b/jni/BUILD.bazel @@ -29,13 +29,11 @@ load( # # define GET_FIELD(name, hs_rettype, c_rettype) \ # get####name####Field :: Coercible o (J a) => o -> JFieldID -> IO hs_rettype; -# +# genrule( name = "cat-tokens-sh", srcs = [], - executable = 1, outs = ["cat-tokens.sh"], - tools = ["@sed//:bin"], cmd = """ cat > $@ < $@ < Date: Fri, 11 Jul 2025 18:42:48 +0000 Subject: [PATCH 03/13] Add jni.cabal --- jni/Setup.hs | 2 + jni/{cat-tokens => cat-tokens.sh} | 0 jni/jni.cabal | 73 +++++++++++++++++++++++++++++++ shell-stack.nix | 35 +++++++++++++++ stack.yaml | 12 +++++ 5 files changed, 122 insertions(+) create mode 100644 jni/Setup.hs rename jni/{cat-tokens => cat-tokens.sh} (100%) create mode 100644 jni/jni.cabal create mode 100644 shell-stack.nix create mode 100644 stack.yaml diff --git a/jni/Setup.hs b/jni/Setup.hs new file mode 100644 index 00000000..9a994af6 --- /dev/null +++ b/jni/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/jni/cat-tokens b/jni/cat-tokens.sh similarity index 100% rename from jni/cat-tokens rename to jni/cat-tokens.sh diff --git a/jni/jni.cabal b/jni/jni.cabal new file mode 100644 index 00000000..45875574 --- /dev/null +++ b/jni/jni.cabal @@ -0,0 +1,73 @@ +name: jni +version: 0.8.0 +synopsis: Complete JNI raw bindings. +description: Please see README.md. +homepage: https://github.com/tweag/inline-java/tree/master/jni#readme +license: BSD3 +license-file: LICENSE +author: Tweag I/O +maintainer: m@tweag.io +copyright: 2015-2016 EURL Tweag. +category: FFI, JVM, Java +build-type: Simple +cabal-version: >=1.10 +extra-source-files: README.md cat-tokens.sh + +source-repository head + type: git + location: https://github.com/tweag/inline-java + subdir: jni + +library + hs-source-dirs: src/common src/linear-types + cc-options: -std=c11 + -- XXX issues with CPP make more convenient using + -- a custom preprocessor to deal with concatenation. + -- See note cat-tokens in jni/BUILD.bazel. + ghc-options: -F -pgmF./cat-tokens.sh + extra-libraries: jvm + exposed-modules: + Foreign.JNI + Foreign.JNI.Types + Foreign.JNI.String + Foreign.JNI.Internal + Foreign.JNI.Internal.BackgroundWorker + Foreign.JNI.Internal.RWLock + Foreign.JNI.NativeMethod + Foreign.JNI.Safe + Foreign.JNI.Types.Safe + Foreign.JNI.Unsafe + Foreign.JNI.Unsafe.Internal + Foreign.JNI.Unsafe.Internal.Introspection + build-depends: + async >=2.2.2, + base >=4.14 && <5, + bytestring >=0.10, + choice >=0.2.0, + containers >=0.5, + constraints >=0.8, + deepseq >=1.4.2, + inline-c >=0.6, + linear-base ==0.1.1, + singletons-base ==3.0, + stm >= 2.3, + text >= 1.2.3 + default-language: Haskell2010 + +test-suite spec + type: + exitcode-stdio-1.0 + hs-source-dirs: tests + main-is: Main.hs + other-modules: + Foreign.JNISpec + Spec + build-depends: + base, + hspec, + singletons-base, + jni + default-language: Haskell2010 + extra-libraries: pthread + ghc-options: -threaded + cpp-options: -DHSPEC_DISCOVER=hspec-discover diff --git a/shell-stack.nix b/shell-stack.nix new file mode 100644 index 00000000..1caa0666 --- /dev/null +++ b/shell-stack.nix @@ -0,0 +1,35 @@ +{ + pkgs ? import ./nixpkgs.nix { }, + ghcAttr ? "ghc902", +}: + +with pkgs; + +let + openjdk = openjdk11; + jvmlibdir = + if stdenv.isLinux + then "${openjdk}/lib/openjdk/lib/server" + else "${openjdk}/jre/lib/server"; + # XXX Workaround https://gitlab.haskell.org/ghc/ghc/-/issues/11042 + libHack = if stdenv.isDarwin then { + DYLD_LIBRARY_PATH = [jvmlibdir]; + } else { + LD_LIBRARY_PATH = [jvmlibdir]; + }; +in +haskell.lib.buildStackProject ({ + name = "inline-java"; + ghc = pkgs.haskell.compiler.${ghcAttr}; + buildInputs = [ + openjdk + git + # Needed to get correct locale for tests with encoding + glibcLocales + # to avoid CA certificate failures on macOS CI + cacert + ]; + # XXX Workaround https://gitlab.haskell.org/ghc/ghc/-/issues/11042 + extraArgs = ["--extra-lib-dirs=${jvmlibdir}"]; + LANG = "en_US.UTF-8"; +} // libHack) diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 00000000..0816921b --- /dev/null +++ b/stack.yaml @@ -0,0 +1,12 @@ +resolver: lts-19.30 + +packages: +- jni + +extra-deps: +- github: tweag/linear-base + commit: ec0b5aa6dc89f1d7c7e368b7387e363a5062e52d + +nix: + shell-file: ./shell-stack.nix + path: ["nixpkgs=./nixpkgs.nix"] From dd8b3c11871674a1fb3684ac7b05919b55441bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Fri, 11 Jul 2025 18:43:21 +0000 Subject: [PATCH 04/13] Update message in shell.nix hook --- shell.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell.nix b/shell.nix index 074fa36f..f8f7c551 100644 --- a/shell.nix +++ b/shell.nix @@ -40,7 +40,7 @@ mkShell { elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then ARCH="linux" fi - echo "[!] It looks like you are using a ''${ARCH} nix-based system. In order to build this project, you need to add the two following host_platform entries to your .bazelrc.local file." + echo "[!] It looks like you are using a ''${ARCH} nix-based system. In order to build this project, you need to add the following host_platform entry to your .bazelrc.local file." echo "" echo "build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host" fi From 284fdc1264c60719c5b221c471987cbe159c2f69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Fri, 11 Jul 2025 18:54:24 +0000 Subject: [PATCH 05/13] Allow gazelle cabal to resolve the jvm dependency --- BUILD.bazel | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/BUILD.bazel b/BUILD.bazel index d2092fb0..66e7fdf2 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,6 +1,7 @@ exports_files(["nixpkgs.nix"]) -#gazelle:exclude {benchmarks,examples,jvm-streaming,jvm-batching} +# gazelle:exclude {benchmarks,examples,jvm-streaming,jvm-batching} +# gazelle:resolve gazelle_cabal jvm @openjdk//:lib load( "@rules_haskell//haskell:defs.bzl", From ef005845a05b595ad0e502c19ecd3382ab5c503b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Fri, 11 Jul 2025 19:07:56 +0000 Subject: [PATCH 06/13] Generate build rules for jni from jni.cabal --- WORKSPACE | 5 ++++ jni/BUILD.bazel | 59 ++++++++++++++++++++++++++++++++++++++----- jni/jni.cabal | 6 +++-- jni/tests/BUILD.bazel | 23 ----------------- jni/tests/Spec.hs | 2 +- 5 files changed, 62 insertions(+), 33 deletions(-) delete mode 100644 jni/tests/BUILD.bazel diff --git a/WORKSPACE b/WORKSPACE index b5365686..5fb2dc26 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -64,6 +64,7 @@ stack_snapshot( "filepath", "ghc", "hspec", + "hspec-discover", "inline-c", "language-java", "monad-logger", @@ -113,6 +114,10 @@ stack_snapshot( "lib", "lib:attoparsec-internal", ], + "hspec-discover": [ + "lib", + "exe", + ], }, local_snapshot = "//:snapshot-9.0.2.yaml", # stack = "@stack_ignore_global_hints//:bin/stack" if ghc_version == "9.0.1" else None, diff --git a/jni/BUILD.bazel b/jni/BUILD.bazel index a9e92185..860270b8 100644 --- a/jni/BUILD.bazel +++ b/jni/BUILD.bazel @@ -1,6 +1,7 @@ load( "@rules_haskell//haskell:defs.bzl", "haskell_library", + "haskell_test", ) # Note cat-tokens @@ -45,19 +46,33 @@ END tools = ["@sed//:bin"], ) +# rule generated from jni/jni.cabal by gazelle_cabal haskell_library( name = "jni", - srcs = glob([ - "src/common/**/*.hs", - "src/common/**/*.hsc", - "src/linear-types/**/*.hs", - ]), - compiler_flags = [ + srcs = [ + "src/common/Foreign/JNI.hs", + "src/common/Foreign/JNI/Internal.hs", + "src/common/Foreign/JNI/Internal/BackgroundWorker.hs", + "src/common/Foreign/JNI/Internal/RWLock.hs", + "src/common/Foreign/JNI/NativeMethod.hsc", + "src/common/Foreign/JNI/String.hs", + "src/common/Foreign/JNI/Types.hs", + "src/common/Foreign/JNI/Unsafe.hs", + "src/common/Foreign/JNI/Unsafe/Internal.hs", + "src/common/Foreign/JNI/Unsafe/Internal/Introspection.hs", + "src/linear-types/Foreign/JNI/Safe.hs", + "src/linear-types/Foreign/JNI/Types/Safe.hs", + ], + extra_srcs = ["//jni:cat-tokens-sh"], + # keep + ghcopts = [ + "-DVERSION_jni=\"0.8.0\"", "-F", "-pgmF$(location cat-tokens-sh)", ], - extra_srcs = ["//jni:cat-tokens-sh"], + # keep tools = ["@sed//:bin"], + version = "0.8.0", visibility = ["//visibility:public"], deps = [ "@openjdk//:lib", @@ -75,3 +90,33 @@ haskell_library( "@stackage//:text", ], ) + +# rule generated from jni/jni.cabal by gazelle_cabal +haskell_test( + name = "spec", + timeout = "short", + srcs = [ + "tests/Foreign/JNISpec.hs", + "tests/Main.hs", + "tests/Spec.hs", + ], + # keep + extra_srcs = ["@openjdk//:rpath"], + ghcopts = [ + "-DVERSION_jni=\"0.8.0\"", + "-threaded", + "-lpthread", + "-DHSPEC_DISCOVER_HSPEC_DISCOVER_PATH=$(location @stackage-exe//hspec-discover)", + "-optl-Wl,@$(location @openjdk//:rpath)", # keep + ], + main_file = "tests/Main.hs", + tools = ["@stackage-exe//hspec-discover"], + version = "0.8.0", + visibility = ["//visibility:public"], + deps = [ + ":jni", + "@stackage//:base", + "@stackage//:hspec", + "@stackage//:singletons-base", + ], +) diff --git a/jni/jni.cabal b/jni/jni.cabal index 45875574..f6028548 100644 --- a/jni/jni.cabal +++ b/jni/jni.cabal @@ -10,7 +10,7 @@ maintainer: m@tweag.io copyright: 2015-2016 EURL Tweag. category: FFI, JVM, Java build-type: Simple -cabal-version: >=1.10 +cabal-version: >=2.0 extra-source-files: README.md cat-tokens.sh source-repository head @@ -70,4 +70,6 @@ test-suite spec default-language: Haskell2010 extra-libraries: pthread ghc-options: -threaded - cpp-options: -DHSPEC_DISCOVER=hspec-discover + cpp-options: -DHSPEC_DISCOVER_HSPEC_DISCOVER_PATH=hspec-discover + build-tool-depends: + hspec-discover:hspec-discover diff --git a/jni/tests/BUILD.bazel b/jni/tests/BUILD.bazel deleted file mode 100644 index 59a67259..00000000 --- a/jni/tests/BUILD.bazel +++ /dev/null @@ -1,23 +0,0 @@ -load( - "@rules_haskell//haskell:defs.bzl", - "haskell_test", -) - -haskell_test( - name = "spec", - timeout = "short", - srcs = glob(["**/*.hs"]), - compiler_flags = [ - "-optl-Wl,@$(location @openjdk//:rpath)", - "-DHSPEC_DISCOVER=$(location @hspec-discover//:bin)", - "-threaded", - ], - extra_srcs = ["@openjdk//:rpath"], - tools = ["@hspec-discover//:bin"], - deps = [ - "//jni", - "@stackage//:base", - "@stackage//:hspec", - "@stackage//:singletons-base", - ], -) diff --git a/jni/tests/Spec.hs b/jni/tests/Spec.hs index 753d8a2a..911f571f 100644 --- a/jni/tests/Spec.hs +++ b/jni/tests/Spec.hs @@ -1,2 +1,2 @@ {-# LANGUAGE CPP #-} -{-# OPTIONS_GHC -F -pgmF HSPEC_DISCOVER -optF --module-name=Spec #-} +{-# OPTIONS_GHC -F -pgmF HSPEC_DISCOVER_HSPEC_DISCOVER_PATH -optF --module-name=Spec #-} From a2175d0ef8ed33db63c517c3b803548345f9701e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Fri, 11 Jul 2025 21:40:10 +0000 Subject: [PATCH 07/13] Make cat-tokens into a Haskell binary --- WORKSPACE | 6 -- jni/BUILD.bazel | 65 +++++-------------- jni/cat-tokens.sh | 4 -- jni/cat-tokens/Main.hs | 52 +++++++++++++++ jni/jni.cabal | 22 +++++-- jni/src/common/Foreign/JNI/Unsafe/Internal.hs | 4 +- jni/src/linear-types/Foreign/JNI/Safe.hs | 4 +- 7 files changed, 90 insertions(+), 67 deletions(-) delete mode 100755 jni/cat-tokens.sh create mode 100644 jni/cat-tokens/Main.hs diff --git a/WORKSPACE b/WORKSPACE index 5fb2dc26..f8eb868a 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -153,12 +153,6 @@ haskell_register_ghc_nixpkgs( ], ) -nixpkgs_package( - name = "sed", - attribute_path = "gnused", - repository = "@nixpkgs", -) - nixpkgs_package( name = "hspec-discover", attribute_path = "haskellPackages.hspec-discover", diff --git a/jni/BUILD.bazel b/jni/BUILD.bazel index 860270b8..a24313d0 100644 --- a/jni/BUILD.bazel +++ b/jni/BUILD.bazel @@ -1,51 +1,10 @@ load( "@rules_haskell//haskell:defs.bzl", + "haskell_binary", "haskell_library", "haskell_test", ) -# Note cat-tokens -# -# This is a script to concatenate tokens after CPP preprocessing. -# -# On OSX we used to rely on cpphs to concatenate tokens in -# definitions like -# -# define GET_FIELD(name, hs_rettype, c_rettype) \ -# get/**/name/**/Field :: Coercible o (J a) => o -> JFieldID -> IO hs_rettype; -# -# The C preprocessor in OSX would otherwise replace the -# comments with whitespaces. -# -# Using cpphs, however, required a couple of hacks to workaround -# https://github.com/haskell/cabal/issues/4278 -# https://gitlab.haskell.org/ghc/ghc/-/issues/17185 -# -# And moreover, when using rules_haskell, ghc passes response files to cpphs, -# which are unsupported. -# https://github.com/tweag/rules_haskell/pull/836 -# -# Therefore, we currently resolve concatenation with a custom -# preprocessor to avoid the pile of hacks. The concatenator operator is ####. -# -# define GET_FIELD(name, hs_rettype, c_rettype) \ -# get####name####Field :: Coercible o (J a) => o -> JFieldID -> IO hs_rettype; -# -genrule( - name = "cat-tokens-sh", - srcs = [], - outs = ["cat-tokens.sh"], - cmd = """ -cat > $@ < \\$$3 -exit \\$$? -END - """, - executable = 1, - tools = ["@sed//:bin"], -) - # rule generated from jni/jni.cabal by gazelle_cabal haskell_library( name = "jni", @@ -63,15 +22,11 @@ haskell_library( "src/linear-types/Foreign/JNI/Safe.hs", "src/linear-types/Foreign/JNI/Types/Safe.hs", ], - extra_srcs = ["//jni:cat-tokens-sh"], - # keep ghcopts = [ "-DVERSION_jni=\"0.8.0\"", - "-F", - "-pgmF$(location cat-tokens-sh)", + "-DJNI_CAT_TOKENS_PATH=$(location :cat-tokens)", ], - # keep - tools = ["@sed//:bin"], + tools = [":cat-tokens"], version = "0.8.0", visibility = ["//visibility:public"], deps = [ @@ -120,3 +75,17 @@ haskell_test( "@stackage//:singletons-base", ], ) + +# rule generated from jni/jni.cabal by gazelle_cabal +haskell_binary( + name = "cat-tokens", + srcs = ["cat-tokens/Main.hs"], + ghcopts = ["-DVERSION_jni=\"0.8.0\""], + main_file = "cat-tokens/Main.hs", + version = "0.8.0", + visibility = ["//visibility:public"], + deps = [ + "@stackage//:base", + "@stackage//:bytestring", + ], +) diff --git a/jni/cat-tokens.sh b/jni/cat-tokens.sh deleted file mode 100755 index ece4547a..00000000 --- a/jni/cat-tokens.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -( echo "{-# LINE 1 \"$1\" #-}" ; sed 's/####//g' $2) > $3 -exit $? - diff --git a/jni/cat-tokens/Main.hs b/jni/cat-tokens/Main.hs new file mode 100644 index 00000000..4a38d519 --- /dev/null +++ b/jni/cat-tokens/Main.hs @@ -0,0 +1,52 @@ +-- | Takes three file paths, and copies the file at the second location to the +-- third location, replacing occurrences of #### with the empty string. +-- +-- The first location is used to prepend the output with a line pragma. +-- +-- > {-# LINE 1 "firstLocation" #-} +-- +-- +-- This is a script to concatenate tokens after CPP preprocessing. +-- +-- On OSX we used to rely on cpphs to concatenate tokens in +-- definitions like +-- +-- > define GET_FIELD(name, hs_rettype, c_rettype) \ +-- > get/**/name/**/Field :: Coercible o (J a) => o -> JFieldID -> IO hs_rettype; +-- +-- The C preprocessor in OSX would otherwise replace the +-- comments with whitespaces. +-- +-- Using cpphs, however, required a couple of hacks to workaround +-- https://github.com/haskell/cabal/issues/4278 +-- https://gitlab.haskell.org/ghc/ghc/-/issues/17185 +-- +-- And moreover, when using rules_haskell, ghc passes response files to cpphs, +-- which are unsupported. +-- https://github.com/tweag/rules_haskell/pull/836 +-- +-- Therefore, we currently resolve concatenation with a custom +-- preprocessor to avoid the pile of hacks. The concatenator operator is ####. +-- +-- > define GET_FIELD(name, hs_rettype, c_rettype) \ +-- > get####name####Field :: Coercible o (J a) => o -> JFieldID -> IO hs_rettype; +-- +module Main where + +import qualified Data.ByteString as BS +import qualified Data.ByteString.Char8 as C8 +import System.Environment +import System.IO + +main :: IO () +main = do + [srcF, inF, outF] <- getArgs + bs <- BS.readFile inF + hOut <- openBinaryFile outF WriteMode + hPutStrLn hOut $ "{-# LINE 1 \"" ++ srcF ++ "\" #-}"; + mapM_ (BS.hPut hOut) $ tokenise bs + hClose hOut + +tokenise :: BS.ByteString -> [BS.ByteString] +tokenise y = h : if BS.null t then [] else tokenise (BS.drop 4 t) + where (h, t) = BS.breakSubstring (C8.pack "####") y diff --git a/jni/jni.cabal b/jni/jni.cabal index f6028548..22829dd9 100644 --- a/jni/jni.cabal +++ b/jni/jni.cabal @@ -10,8 +10,8 @@ maintainer: m@tweag.io copyright: 2015-2016 EURL Tweag. category: FFI, JVM, Java build-type: Simple -cabal-version: >=2.0 -extra-source-files: README.md cat-tokens.sh +cabal-version: 2.0 +extra-source-files: README.md source-repository head type: git @@ -21,10 +21,6 @@ source-repository head library hs-source-dirs: src/common src/linear-types cc-options: -std=c11 - -- XXX issues with CPP make more convenient using - -- a custom preprocessor to deal with concatenation. - -- See note cat-tokens in jni/BUILD.bazel. - ghc-options: -F -pgmF./cat-tokens.sh extra-libraries: jvm exposed-modules: Foreign.JNI @@ -53,6 +49,20 @@ library stm >= 2.3, text >= 1.2.3 default-language: Haskell2010 + -- XXX issues with CPP make more convenient using + -- a custom preprocessor to deal with concatenation. + -- See jni/cat-tokens/Main.hs. + cpp-options: -DJNI_CAT_TOKENS_PATH=cat-tokens + build-tool-depends: + jni:cat-tokens + +executable cat-tokens + hs-source-dirs: cat-tokens + main-is: Main.hs + build-depends: + base, + bytestring + default-language: Haskell2010 test-suite spec type: diff --git a/jni/src/common/Foreign/JNI/Unsafe/Internal.hs b/jni/src/common/Foreign/JNI/Unsafe/Internal.hs index 28cd55f5..e64d2124 100644 --- a/jni/src/common/Foreign/JNI/Unsafe/Internal.hs +++ b/jni/src/common/Foreign/JNI/Unsafe/Internal.hs @@ -10,10 +10,10 @@ {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE ViewPatterns #-} -{-# OPTIONS_GHC -fno-warn-name-shadowing #-} +{-# OPTIONS_GHC -fno-warn-name-shadowing -F -pgmF JNI_CAT_TOKENS_PATH #-} -- XXX This file uses #### for concatenating tokens with a preprocessor in --- a portable way. See note cat-tokens in jni/BUILD.bazel. +-- a portable way. See note cat-tokens in jni/cat-tokens/Main.hs. module Foreign.JNI.Unsafe.Internal ( -- * JNI functions diff --git a/jni/src/linear-types/Foreign/JNI/Safe.hs b/jni/src/linear-types/Foreign/JNI/Safe.hs index 1f8687bc..7555aaf7 100644 --- a/jni/src/linear-types/Foreign/JNI/Safe.hs +++ b/jni/src/linear-types/Foreign/JNI/Safe.hs @@ -60,8 +60,10 @@ {-# LANGUAGE LinearTypes #-} {-# LANGUAGE NoImplicitPrelude #-} +{-# OPTIONS_GHC -fno-warn-name-shadowing -F -pgmF JNI_CAT_TOKENS_PATH #-} + -- XXX This file uses #### for concatenating tokens with a preprocessor in --- a portable way. See note cat-tokens in jni/BUILD.bazel. +-- a portable way. See cat-tokens/Main.hs. module Foreign.JNI.Safe ( module Foreign.JNI.Safe From 2237ca4159ccb8f2e4aef11ba19b7f773e951b4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Fri, 11 Jul 2025 23:18:05 +0000 Subject: [PATCH 08/13] Add CI job to build and test with stack --- .github/workflows/build.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2e48906b..467213e8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -37,6 +37,36 @@ jobs: ~/disk-cache key: repo-cache-${{ runner.os }}-nixpkgs-${{ env.cache-version }}-${{ github.run_id }}-${{ github.run_attempt }} + run-tests-with-stack: + name: Run Tests with stack + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: cachix/install-nix-action@v22 + - name: Configure + run: | + mkdir -p ~/repo-cache ~/disk-cache + echo build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host > .bazelrc.local + - name: Get Stack snapshot install directory + id: stack-snapshot + # NOTE: `stack path` must run at least once prior to caching to ensure the directory + # exists and is populated. + run: | + stack --nix path --snapshot-install-root + echo "dir=$(stack --nix path --snapshot-install-root)" > "${GITHUB_OUTPUT}" + - uses: actions/cache@v4 + with: + path: ${{ steps.stack-snapshot.outputs.dir }} + key: ${{ runner.os }}-stack-${{ hashFiles('**/*.cabal') }} + restore-keys: ${{ runner.os }}-stack- + - name: Build + run: | + stack --nix build + - name: Test + if: ${{ runner.os == 'Linux' }} + run: | + stack --nix test + run-tests-with-darwin: name: Run Tests with darwin runner runs-on: macos-11 From 3f8ec5ead00d502ce8a7db397f5a2915251d5f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Sat, 12 Jul 2025 00:00:15 +0000 Subject: [PATCH 09/13] Shorten job names --- .github/workflows/build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 467213e8..3e6a9454 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,7 +7,7 @@ env: jobs: run-tests-with-ubuntu: - name: Run Tests with ubuntu runner + name: with bazel runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -38,7 +38,7 @@ jobs: key: repo-cache-${{ runner.os }}-nixpkgs-${{ env.cache-version }}-${{ github.run_id }}-${{ github.run_attempt }} run-tests-with-stack: - name: Run Tests with stack + name: with stack runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -68,7 +68,7 @@ jobs: stack --nix test run-tests-with-darwin: - name: Run Tests with darwin runner + name: with bazel in darwin runs-on: macos-11 steps: From 9806b2456735d7cd7cd207d5978826a8a037e255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Sat, 12 Jul 2025 00:10:23 +0000 Subject: [PATCH 10/13] Add check for gazelle_cabal changes --- .github/workflows/build.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3e6a9454..56ec1548 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,6 +28,7 @@ jobs: restore-keys: | repo-cache-${{ runner.os }}-nixpkgs-${{ env.cache-version }}- - run: | + nix-shell --pure --run "bazel run $BAZEL_ARGS //:gazelle" nix-shell --pure --run "bazel test --test_output=all //... $BAZEL_ARGS" - uses: actions/cache/save@v3 if: github.ref == 'refs/heads/master' @@ -36,6 +37,15 @@ jobs: ~/repo-cache ~/disk-cache key: repo-cache-${{ runner.os }}-nixpkgs-${{ env.cache-version }}-${{ github.run_id }}-${{ github.run_attempt }} + - name: gazelle check + run: | + if ! git diff --exit-code + then + echo Repository files have changed + echo You might need to run: nix-shell --run "\"bazel run //:gazelle\"" + echo and commit the resulting changes. + exit 1 + fi run-tests-with-stack: name: with stack From c2f1f3f250c05d2d22f0d66a3a35c62e36ca38fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Sat, 12 Jul 2025 17:39:48 +0000 Subject: [PATCH 11/13] Add back jvm.cabal --- jvm/BUILD.bazel | 76 +++++++++++++++++++++++++++++++++--- jvm/Setup.hs | 2 + jvm/benchmarks/BUILD.bazel | 23 ----------- jvm/jvm.cabal | 80 ++++++++++++++++++++++++++++++++++++++ jvm/tests/BUILD.bazel | 28 ------------- jvm/tests/Spec.hs | 2 +- stack.yaml | 3 ++ 7 files changed, 156 insertions(+), 58 deletions(-) create mode 100644 jvm/Setup.hs delete mode 100644 jvm/benchmarks/BUILD.bazel create mode 100644 jvm/jvm.cabal delete mode 100644 jvm/tests/BUILD.bazel diff --git a/jvm/BUILD.bazel b/jvm/BUILD.bazel index 22ab1410..9e90d944 100644 --- a/jvm/BUILD.bazel +++ b/jvm/BUILD.bazel @@ -1,21 +1,28 @@ load( "@rules_haskell//haskell:defs.bzl", + "haskell_binary", "haskell_library", + "haskell_test", ) +# rule generated from jvm/jvm.cabal by gazelle_cabal haskell_library( name = "jvm", - srcs = glob([ - "src/common/**/*.hs", - "src/linear-types/**/*.hs", - ]), + srcs = [ + "src/common/Language/Java.hs", + "src/common/Language/Java/Internal.hs", + "src/common/Language/Java/Unsafe.hs", + "src/linear-types/Language/Java/Safe.hs", + ], + ghcopts = ["-DVERSION_jvm=\"0.6.0\""], + version = "0.6.0", visibility = ["//visibility:public"], deps = [ "//jni", "@stackage//:base", "@stackage//:bytestring", - "@stackage//:constraints", "@stackage//:choice", + "@stackage//:constraints", "@stackage//:distributed-closure", "@stackage//:exceptions", "@stackage//:linear-base", @@ -23,6 +30,63 @@ haskell_library( "@stackage//:template-haskell", "@stackage//:text", "@stackage//:vector", - ] + [ + ], +) + +# rule generated from jvm/jvm.cabal by gazelle_cabal +haskell_test( + name = "spec", + srcs = [ + "tests/Language/JavaSpec.hs", + "tests/Main.hs", + "tests/Spec.hs", + ], + # keep + extra_srcs = ["@openjdk//:rpath"], + ghcopts = [ + "-DVERSION_jvm=\"0.6.0\"", + "-threaded", + "-lpthread", + "-DHSPEC_DISCOVER_HSPEC_DISCOVER_PATH=$(location @stackage-exe//hspec-discover)", + "-optl-Wl,@$(location @openjdk//:rpath)", # keep + ], + main_file = "tests/Main.hs", + tools = ["@stackage-exe//hspec-discover"], + version = "0.6.0", + visibility = ["//visibility:public"], + deps = [ + ":jvm", + "//jni", + "@stackage//:QuickCheck", + "@stackage//:base", + "@stackage//:bytestring", + "@stackage//:hspec", + "@stackage//:quickcheck-text", + "@stackage//:text", + ], +) + +# rule generated from jvm/jvm.cabal by gazelle_cabal +haskell_binary( + name = "micro-benchmarks", + srcs = ["benchmarks/Main.hs"], + # keep + extra_srcs = ["@openjdk//:rpath"], + ghcopts = [ + "-DVERSION_jvm=\"0.6.0\"", + "-threaded", + "-optl-Wl,@$(location @openjdk//:rpath)", # keep + ], + main_file = "benchmarks/Main.hs", + version = "0.6.0", + visibility = ["//visibility:public"], + deps = [ + ":jvm", + "//jni", + "@stackage//:base", + "@stackage//:criterion", + "@stackage//:deepseq", + "@stackage//:singletons", + "@stackage//:text", ], ) diff --git a/jvm/Setup.hs b/jvm/Setup.hs new file mode 100644 index 00000000..9a994af6 --- /dev/null +++ b/jvm/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/jvm/benchmarks/BUILD.bazel b/jvm/benchmarks/BUILD.bazel deleted file mode 100644 index 056017ec..00000000 --- a/jvm/benchmarks/BUILD.bazel +++ /dev/null @@ -1,23 +0,0 @@ -load( - "@rules_haskell//haskell:defs.bzl", - "haskell_binary", -) - -haskell_binary( - name = "bench", - srcs = glob(["**/*.hs"]), - compiler_flags = [ - "-optl-Wl,@$(location @openjdk//:rpath)", - "-threaded", - ], - extra_srcs = ["@openjdk//:rpath"], - deps = [ - "//jni", - "//jvm", - "@stackage//:base", - "@stackage//:criterion", - "@stackage//:deepseq", - "@stackage//:singletons", - "@stackage//:text", - ], -) diff --git a/jvm/jvm.cabal b/jvm/jvm.cabal new file mode 100644 index 00000000..99951aef --- /dev/null +++ b/jvm/jvm.cabal @@ -0,0 +1,80 @@ +name: jvm +version: 0.6.0 +synopsis: Call JVM methods from Haskell. +description: Please see README.md. +homepage: http://github.com/tweag/inline-java/tree/master/jvm#readme +license: BSD3 +license-file: LICENSE +author: Tweag I/O +maintainer: m@tweag.io +copyright: 2015-2016 EURL Tweag. +category: FFI, JVM, Java +build-type: Simple +cabal-version: 2.0 +extra-source-files: README.md + +source-repository head + type: git + location: https://github.com/tweag/inline-java + subdir: jvm + +library + hs-source-dirs: src/common src/linear-types + exposed-modules: + Language.Java + Language.Java.Internal + Language.Java.Safe + Language.Java.Unsafe + build-depends: + base >=4.14 && <5, + bytestring >=0.10, + constraints >=0.8, + choice >=0.1, + distributed-closure >=0.3, + exceptions >=0.8, + jni >=0.8.0 && <0.9, + linear-base ==0.1.1, + singletons >=2.6, + text >=1.2, + template-haskell, + vector >=0.11 + default-language: Haskell2010 + +test-suite spec + type: + exitcode-stdio-1.0 + hs-source-dirs: tests + main-is: Main.hs + other-modules: + Language.JavaSpec + Spec + build-depends: + base, + bytestring, + hspec, + jni, + jvm, + QuickCheck, + quickcheck-text, + text + default-language: Haskell2010 + extra-libraries: pthread + ghc-options: -threaded + cpp-options: -DHSPEC_DISCOVER_HSPEC_DISCOVER_PATH=hspec-discover + build-tool-depends: + hspec-discover:hspec-discover + +benchmark micro-benchmarks + type: exitcode-stdio-1.0 + main-is: Main.hs + hs-source-dirs: benchmarks + build-depends: + base >=4.8 && <5, + criterion, + deepseq >=1.4.2, + jni, + jvm, + singletons, + text >=1.2 + default-language: Haskell2010 + ghc-options: -threaded diff --git a/jvm/tests/BUILD.bazel b/jvm/tests/BUILD.bazel deleted file mode 100644 index 7e1fd3d3..00000000 --- a/jvm/tests/BUILD.bazel +++ /dev/null @@ -1,28 +0,0 @@ -load( - "@rules_haskell//haskell:defs.bzl", - "haskell_test", -) - -haskell_test( - name = "spec", - timeout = "short", - srcs = glob(["**/*.hs"]), - compiler_flags = [ - "-optl-Wl,@$(location @openjdk//:rpath)", - "-DHSPEC_DISCOVER=$(location @hspec-discover//:bin)", - "-threaded", - ], - extra_srcs = ["@openjdk//:rpath"], - tools = ["@hspec-discover//:bin"], - deps = [ - "//jni", - "//jvm", - "@stackage//:QuickCheck", - "@stackage//:base", - "@stackage//:bytestring", - "@stackage//:hspec", - "@stackage//:quickcheck-text", - "@stackage//:quickcheck-unicode", - "@stackage//:text", - ], -) diff --git a/jvm/tests/Spec.hs b/jvm/tests/Spec.hs index 753d8a2a..911f571f 100644 --- a/jvm/tests/Spec.hs +++ b/jvm/tests/Spec.hs @@ -1,2 +1,2 @@ {-# LANGUAGE CPP #-} -{-# OPTIONS_GHC -F -pgmF HSPEC_DISCOVER -optF --module-name=Spec #-} +{-# OPTIONS_GHC -F -pgmF HSPEC_DISCOVER_HSPEC_DISCOVER_PATH -optF --module-name=Spec #-} diff --git a/stack.yaml b/stack.yaml index 0816921b..dac78f71 100644 --- a/stack.yaml +++ b/stack.yaml @@ -2,8 +2,11 @@ resolver: lts-19.30 packages: - jni +- jvm extra-deps: +- github: tweag/distributed-closure + commit: b92e75ec81e646703c7bde4f578a7352ee34f1ad - github: tweag/linear-base commit: ec0b5aa6dc89f1d7c7e368b7387e363a5062e52d From a46293d2e4fb9aa52837f3fe563882f1c6591cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Sat, 12 Jul 2025 18:14:17 +0000 Subject: [PATCH 12/13] Add back inline-java.cabal --- BUILD.bazel | 132 +++++++++++++++++++++++++++-------- Setup.hs | 2 + benchmarks/micro/BUILD.bazel | 25 ------- inline-java.cabal | 99 ++++++++++++++++++++++++++ stack.yaml | 1 + tests/BUILD.bazel | 31 -------- tests/SafeSpec.hs | 2 +- tests/Spec.hs | 2 +- 8 files changed, 207 insertions(+), 87 deletions(-) create mode 100644 Setup.hs delete mode 100644 benchmarks/micro/BUILD.bazel create mode 100644 inline-java.cabal delete mode 100644 tests/BUILD.bazel diff --git a/BUILD.bazel b/BUILD.bazel index 66e7fdf2..11c324dc 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -6,7 +6,9 @@ exports_files(["nixpkgs.nix"]) load( "@rules_haskell//haskell:defs.bzl", "ghc_plugin", + "haskell_binary", "haskell_library", + "haskell_test", ) cc_library( @@ -16,25 +18,63 @@ cc_library( strip_include_prefix = "cbits", ) +ghc_plugin( + name = "inline-java-plugin", + args = ["$(JAVABASE)/bin/javac"], + module = "Language.Java.Inline.Plugin", + toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"], + tools = ["@bazel_tools//tools/jdk:current_java_runtime"], + visibility = ["//visibility:public"], + deps = [":inline-java"], +) + +load( + "@bazel_gazelle//:def.bzl", + "DEFAULT_LANGUAGES", + "gazelle", + "gazelle_binary", +) + +gazelle( + name = "gazelle", + gazelle = ":gazelle_binary", +) + +gazelle_binary( + name = "gazelle_binary", + languages = DEFAULT_LANGUAGES + ["@io_tweag_gazelle_cabal//gazelle_cabal"], +) + +# rule generated from inline-java.cabal by gazelle_cabal haskell_library( name = "inline-java", # cbits/bctable.h is included here so it can be found by Plugin.hs # at the same location when building with bazel as with stack. - srcs = glob([ - "src/**/*.hs", - "src/**/*.hsc", - "cbits/bctable.h", - ]), + srcs = [ + "cbits/bctable.h", # keep + "src/GhcPlugins/Extras.hs", + "src/Language/Java/Inline.hs", + "src/Language/Java/Inline/Internal.hs", + "src/Language/Java/Inline/Internal/Magic.hsc", + "src/Language/Java/Inline/Internal/QQMarker.hs", + "src/Language/Java/Inline/Internal/QQMarker/Names.hs", + "src/Language/Java/Inline/Internal/QQMarker/Safe.hs", + "src/Language/Java/Inline/Plugin.hs", + "src/Language/Java/Inline/Safe.hs", + "src/Language/Java/Inline/Unsafe.hs", + ], + ghcopts = ["-DVERSION_inline_java=\"0.10.0\""], + hidden_modules = ["GhcPlugins.Extras"], + version = "0.10.0", visibility = ["//visibility:public"], deps = [ - ":bctable", + ":bctable", # keep "//jni", "//jvm", "@stackage//:Cabal", "@stackage//:base", "@stackage//:bytestring", "@stackage//:directory", - "@stackage//:filemanip", "@stackage//:filepath", "@stackage//:ghc", "@stackage//:language-java", @@ -47,29 +87,63 @@ haskell_library( ], ) -ghc_plugin( - name = "inline-java-plugin", - args = ["$(JAVABASE)/bin/javac"], - module = "Language.Java.Inline.Plugin", - toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"], - tools = ["@bazel_tools//tools/jdk:current_java_runtime"], +# rule generated from inline-java.cabal by gazelle_cabal +haskell_test( + name = "spec", + srcs = [ + "tests/Language/Java/Inline/SafeSpec.hs", + "tests/Language/Java/InlineSpec.hs", + "tests/Main.hs", + "tests/SafeSpec.hs", + "tests/Spec.hs", + ], + extra_srcs = ["@openjdk//:rpath"], # keep + ghcopts = [ + "-optl-Wl,@$(location @openjdk//:rpath)", # keep + "-DVERSION_inline_java=\"0.10.0\"", + "-threaded", + "-DHSPEC_DISCOVER_HSPEC_DISCOVER_PATH=$(location @stackage-exe//hspec-discover)", + ], + main_file = "tests/Main.hs", + plugins = [":inline-java-plugin"], + tools = ["@stackage-exe//hspec-discover"], + version = "0.10.0", visibility = ["//visibility:public"], - deps = [":inline-java"], -) - -load( - "@bazel_gazelle//:def.bzl", - "DEFAULT_LANGUAGES", - "gazelle", - "gazelle_binary", -) - -gazelle( - name = "gazelle", - gazelle = ":gazelle_binary", + deps = [ + "//:inline-java", # keep + "//jni", + "//jvm", + "@stackage//:QuickCheck", + "@stackage//:base", + "@stackage//:hspec", + "@stackage//:linear-base", + "@stackage//:quickcheck-unicode", + "@stackage//:text", + "@stackage//:vector", + ], ) -gazelle_binary( - name = "gazelle_binary", - languages = DEFAULT_LANGUAGES + ["@io_tweag_gazelle_cabal//gazelle_cabal"], +# rule generated from inline-java.cabal by gazelle_cabal +haskell_binary( + name = "micro-benchmarks", + srcs = ["benchmarks/micro/Main.hs"], + extra_srcs = ["@openjdk//:rpath"], # keep + ghcopts = [ + "-optl-Wl,@$(location @openjdk//:rpath)", # keep + "-DVERSION_inline_java=\"0.10.0\"", + "-threaded", + ], + main_file = "benchmarks/micro/Main.hs", + plugins = [":inline-java-plugin"], + version = "0.10.0", + visibility = ["//visibility:public"], + deps = [ + "//:inline-java", # keep + "//jni", + "//jvm", + "@stackage//:base", + "@stackage//:criterion", + "@stackage//:deepseq", + "@stackage//:singletons", + ], ) diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 00000000..9a994af6 --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/benchmarks/micro/BUILD.bazel b/benchmarks/micro/BUILD.bazel deleted file mode 100644 index 839e555e..00000000 --- a/benchmarks/micro/BUILD.bazel +++ /dev/null @@ -1,25 +0,0 @@ -load( - "@rules_haskell//haskell:defs.bzl", - "haskell_binary", -) - -haskell_binary( - name = "bench", - srcs = glob(['**/*.hs']), - extra_srcs = ["@openjdk//:rpath"], - compiler_flags = [ - "-optl-Wl,@$(location @openjdk//:rpath)", - "-threaded", - ], - deps = [ - "//jvm", - "//jni", - "//:inline-java", - "@stackage//:base", - "@stackage//:criterion", - "@stackage//:deepseq", - "@stackage//:singletons", - "@stackage//:text", - ], - plugins = ["//:inline-java-plugin"], -) diff --git a/inline-java.cabal b/inline-java.cabal new file mode 100644 index 00000000..c329c77e --- /dev/null +++ b/inline-java.cabal @@ -0,0 +1,99 @@ +name: inline-java +version: 0.10.0 +synopsis: Java interop via inline Java code in Haskell modules. +description: Please see README.md. +homepage: http://github.com/tweag/inline-java#readme +license: BSD3 +license-file: LICENSE +author: Tweag I/O +maintainer: m@tweag.io +copyright: 2015-2016 EURL Tweag. +category: FFI, JVM, Java +build-type: Simple +cabal-version: 2.0 +extra-source-files: + CHANGELOG.md + README.md + +source-repository head + type: git + location: https://github.com/tweag/inline-java + +library + hs-source-dirs: src + c-sources: cbits/bctable.c + cc-options: -std=c99 -Wall -Werror + include-dirs: cbits/ + includes: bctable.h + install-includes: bctable.h + exposed-modules: + Language.Java.Inline + Language.Java.Inline.Safe + Language.Java.Inline.Internal + Language.Java.Inline.Internal.Magic + Language.Java.Inline.Internal.QQMarker + Language.Java.Inline.Internal.QQMarker.Names + Language.Java.Inline.Internal.QQMarker.Safe + Language.Java.Inline.Plugin + Language.Java.Inline.Unsafe + other-modules: + GhcPlugins.Extras + build-depends: + base >=4.14.0.0 && <5, + bytestring >=0.10, + Cabal >=1.24.2, + directory >=1.2, + filepath >=1, + ghc >=9.0.2 && <9.2, + jni >=0.8 && <0.9, + jvm >=0.6 && <0.7, + language-java >=0.2, + linear-base ==0.1.1, + mtl >=2.2.1, + process >=1.2, + text >=1.2, + template-haskell >=2.10, + temporary >=1.2 + default-language: Haskell2010 + +test-suite spec + type: + exitcode-stdio-1.0 + hs-source-dirs: tests + main-is: Main.hs + other-modules: + SafeSpec + Spec + Language.Java.Inline.SafeSpec + Language.Java.InlineSpec + build-depends: + base, + jni, + jvm, + hspec, + inline-java, + linear-base, + QuickCheck, + quickcheck-unicode, + text, + vector + default-language: Haskell2010 + ghc-options: -threaded + cpp-options: -DHSPEC_DISCOVER_HSPEC_DISCOVER_PATH=hspec-discover + build-tool-depends: + hspec-discover:hspec-discover + +benchmark micro-benchmarks + type: exitcode-stdio-1.0 + main-is: Main.hs + hs-source-dirs: benchmarks/micro + build-depends: + base >=4.8 && <5, + criterion, + deepseq >=1.4.2, + inline-java, + jni, + jvm, + singletons + default-language: Haskell2010 + ghc-options: -threaded diff --git a/stack.yaml b/stack.yaml index dac78f71..185a4e6b 100644 --- a/stack.yaml +++ b/stack.yaml @@ -3,6 +3,7 @@ resolver: lts-19.30 packages: - jni - jvm +- . extra-deps: - github: tweag/distributed-closure diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel deleted file mode 100644 index bd46b137..00000000 --- a/tests/BUILD.bazel +++ /dev/null @@ -1,31 +0,0 @@ -load( - "@rules_haskell//haskell:defs.bzl", - "haskell_test", -) - -haskell_test( - name = "spec", - size = "small", - srcs = glob(["**/*.hs"]), - compiler_flags = [ - "-optl-Wl,@$(location @openjdk//:rpath)", - "-DHSPEC_DISCOVER=$(location @hspec-discover//:bin)", - "-threaded", - ], - extra_srcs = ["@openjdk//:rpath"], - plugins = ["//:inline-java-plugin"], - tools = ["@hspec-discover//:bin"], - deps = [ - "//:inline-java", - "//jni", - "//jvm", - "@stackage//:QuickCheck", - "@stackage//:base", - "@stackage//:hspec", - "@stackage//:linear-base", - "@stackage//:quickcheck-unicode", - "@stackage//:text", - "@stackage//:unix", - "@stackage//:vector", - ], -) diff --git a/tests/SafeSpec.hs b/tests/SafeSpec.hs index fec06403..695e0943 100644 --- a/tests/SafeSpec.hs +++ b/tests/SafeSpec.hs @@ -1,2 +1,2 @@ {-# LANGUAGE CPP #-} -{-# OPTIONS_GHC -F -pgmF HSPEC_DISCOVER -optF --module-name=SafeSpec #-} +{-# OPTIONS_GHC -F -pgmF HSPEC_DISCOVER_HSPEC_DISCOVER_PATH -optF --module-name=SafeSpec #-} diff --git a/tests/Spec.hs b/tests/Spec.hs index 753d8a2a..911f571f 100644 --- a/tests/Spec.hs +++ b/tests/Spec.hs @@ -1,2 +1,2 @@ {-# LANGUAGE CPP #-} -{-# OPTIONS_GHC -F -pgmF HSPEC_DISCOVER -optF --module-name=Spec #-} +{-# OPTIONS_GHC -F -pgmF HSPEC_DISCOVER_HSPEC_DISCOVER_PATH -optF --module-name=Spec #-} From 47526f6e4f3420bf73cc1797d3c2422a3b481a5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Sat, 12 Jul 2025 18:48:44 +0000 Subject: [PATCH 13/13] Have jni, jvm, and inline-java build with linear-base-0.1.0 --- inline-java.cabal | 2 +- jni/jni.cabal | 2 +- jni/src/linear-types/Foreign/JNI/Safe.hs | 5 +++-- jvm/jvm.cabal | 2 +- stack.yaml | 3 +-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/inline-java.cabal b/inline-java.cabal index c329c77e..0e9c6c72 100644 --- a/inline-java.cabal +++ b/inline-java.cabal @@ -48,7 +48,7 @@ library jni >=0.8 && <0.9, jvm >=0.6 && <0.7, language-java >=0.2, - linear-base ==0.1.1, + linear-base ==0.1.0, mtl >=2.2.1, process >=1.2, text >=1.2, diff --git a/jni/jni.cabal b/jni/jni.cabal index 22829dd9..5bb85d7e 100644 --- a/jni/jni.cabal +++ b/jni/jni.cabal @@ -44,7 +44,7 @@ library constraints >=0.8, deepseq >=1.4.2, inline-c >=0.6, - linear-base ==0.1.1, + linear-base ==0.1.0, singletons-base ==3.0, stm >= 2.3, text >= 1.2.3 diff --git a/jni/src/linear-types/Foreign/JNI/Safe.hs b/jni/src/linear-types/Foreign/JNI/Safe.hs index 7555aaf7..45b313cc 100644 --- a/jni/src/linear-types/Foreign/JNI/Safe.hs +++ b/jni/src/linear-types/Foreign/JNI/Safe.hs @@ -305,11 +305,12 @@ deleteGlobalRefNonFinalized o = liftPreludeIO (JNI.deleteGlobalRef o) -- run in a thread where the reference is not valid. newLocalRef :: (MonadIO m, Coercible o (J ty)) => o %1-> m (o, o) newLocalRef = Unsafe.toLinear $ \o -> - liftPreludeIO ((,) o . coerce . J <$> JNI.newLocalRef (unJ . coerce $ o)) + liftPreludeIO + ((,) o . coerce . J <$> JNI.newLocalRef (unJ . coerce Prelude.$ o)) deleteLocalRef :: (MonadIO m, Coercible o (J ty)) => o %1-> m () deleteLocalRef = Unsafe.toLinear $ \o -> - liftPreludeIO (JNI.deleteLocalRef (unJ . coerce $ o)) + liftPreludeIO (JNI.deleteLocalRef (unJ . coerce Prelude.$ o)) -- | Runs the given computation in a local frame, which ensures that -- if it throws an exception, all live local references created during diff --git a/jvm/jvm.cabal b/jvm/jvm.cabal index 99951aef..391c9641 100644 --- a/jvm/jvm.cabal +++ b/jvm/jvm.cabal @@ -33,7 +33,7 @@ library distributed-closure >=0.3, exceptions >=0.8, jni >=0.8.0 && <0.9, - linear-base ==0.1.1, + linear-base ==0.1.0, singletons >=2.6, text >=1.2, template-haskell, diff --git a/stack.yaml b/stack.yaml index 185a4e6b..87237745 100644 --- a/stack.yaml +++ b/stack.yaml @@ -8,8 +8,7 @@ packages: extra-deps: - github: tweag/distributed-closure commit: b92e75ec81e646703c7bde4f578a7352ee34f1ad -- github: tweag/linear-base - commit: ec0b5aa6dc89f1d7c7e368b7387e363a5062e52d +- linear-base-0.1.0 nix: shell-file: ./shell-stack.nix