Skip to content

Commit

Permalink
Add loader based on dune-site.plugin and refine test deps.
Browse files Browse the repository at this point in the history
  • Loading branch information
paurkedal committed Feb 10, 2024
1 parent a853536 commit 31b9396
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 104 deletions.
7 changes: 7 additions & 0 deletions caqti-driver-mariadb/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
(optional)
(library_flags (:standard -linkall))
(libraries caqti caqti.platform caqti.platform.unix mariadb))

(plugin
(package caqti-driver-mariadb)
(name caqti-driver-mariadb)
(optional)
(libraries caqti-driver-mariadb)
(site (caqti plugins)))
6 changes: 6 additions & 0 deletions caqti-driver-pgx/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@
(enabled_if (>= %{ocaml_version} "4.10"))
(library_flags (:standard -linkall))
(libraries caqti caqti.platform domain-name ipaddr pgx))

(plugin
(package caqti-driver-pgx)
(name caqti-driver-pgx)
(libraries caqti-driver-pgx)
(site (caqti plugins)))
6 changes: 6 additions & 0 deletions caqti-driver-postgresql/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
(public_name caqti-driver-postgresql)
(library_flags (:standard -linkall))
(libraries caqti caqti.platform caqti.platform.unix postgresql))

(plugin
(package caqti-driver-postgresql)
(name caqti-driver-postgresql)
(libraries caqti-driver-postgresql)
(site (caqti plugins)))
6 changes: 6 additions & 0 deletions caqti-driver-sqlite3/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
(flags (:standard -thread))
(library_flags (:standard -linkall))
(libraries caqti caqti.platform caqti.platform.unix sqlite3))

(plugin
(package caqti-driver-sqlite3)
(name caqti-driver-sqlite3)
(libraries caqti-driver-sqlite3)
(site (caqti plugins)))
6 changes: 6 additions & 0 deletions caqti-tls-async/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@
caqti-tls
tls
tls-async))

(plugin
(package caqti-tls-async)
(name caqti-tls-async)
(libraries caqti-tls-async)
(site (caqti plugins)))
6 changes: 6 additions & 0 deletions caqti-tls-eio/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@
caqti-tls
tls
tls-eio))

(plugin
(package caqti-tls-eio)
(name caqti-tls-eio)
(libraries caqti-tls-eio)
(site (caqti plugins)))
6 changes: 6 additions & 0 deletions caqti-tls-lwt/lib-unix/dune
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@
caqti-tls
tls
tls-lwt))

(plugin
(package caqti-tls-lwt)
(name caqti-tls-lwt-unix)
(libraries caqti-tls-lwt.unix)
(site (caqti plugins)))
1 change: 1 addition & 0 deletions caqti.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ depends: [
"cmdliner" {with-test & >= "1.1.0"}
"domain-name" {>= "0.2.0"}
"dune" {>= "3.9"}
"dune-site"
"ipaddr" {>= "3.0.0"}
"logs"
"lwt-dllist"
Expand Down
29 changes: 29 additions & 0 deletions caqti/lib-plugin/caqti_plugin.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(* Copyright (C) 2024 Petter A. Urkedal <[email protected]>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version, with the LGPL-3.0 Linking Exception.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* and the LGPL-3.0 Linking Exception along with this library. If not, see
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
*)

let () =
let load pkg =
let pkg = String.map (function '.' -> '-' | c -> c) pkg in
if List.mem pkg (Sites.Plugins.Plugins.list ()) then
begin
Sites.Plugins.Plugins.load pkg;
Ok ()
end
else
Error ("Package " ^ pkg ^ " is not avaliable.")
in
Caqti_platform.Connector.define_loader load
9 changes: 9 additions & 0 deletions caqti/lib-plugin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(library
(name caqti_plugin)
(public_name caqti.plugin)
(library_flags (:standard -linkall))
(libraries caqti.platform dune-site dune-site.plugins))

(generate_sites_module
(module sites)
(plugins (caqti plugins)))
17 changes: 17 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
(lang dune 3.9)
(using dune_site 0.1)
(name caqti)

(package (name caqti) (sites (lib plugins)))
(package (name caqti-async) (allow_empty))
(package (name caqti-driver-mariadb) (allow_empty))
(package (name caqti-driver-pgx) (allow_empty))
(package (name caqti-driver-postgresql) (allow_empty))
(package (name caqti-driver-sqlite3) (allow_empty))
(package (name caqti-dynload) (allow_empty))
(package (name caqti-eio) (allow_empty))
(package (name caqti-lwt) (allow_empty))
(package (name caqti-mirage) (allow_empty))
(package (name caqti-tls-async) (allow_empty))
(package (name caqti-tls-eio) (allow_empty))
(package (name caqti-tls-lwt) (allow_empty))
(package (name caqti-tls) (allow_empty))
(package (name caqti-type-calendar) (allow_empty))
10 changes: 6 additions & 4 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
(rule
(alias runtest)
(package caqti-lwt)
(deps (:test bikereg.exe) (alias ../testsuite/drivers) ../testsuite/uris.conf)
(deps
(:test bikereg.exe)
(:u %{env:CAQTI_TEST_URIS_FILE=../testsuite/uris.conf})
(include ../testsuite/dune.uri-deps-lwt)
(env_var CAQTI_TEST_X509_AUTHENTICATOR))
(locks /db/bikereg)
(action
(setenv OCAMLPATH ../testsuite
(run %{test} -U ../testsuite/uris.conf))))
(action (run %{test} -U %{u})))
90 changes: 90 additions & 0 deletions testsuite/deps_of_uris.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
(* Copyright (C) 2024 Petter A. Urkedal <[email protected]>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version, with the LGPL-3.0 Linking Exception.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* and the LGPL-3.0 Linking Exception along with this library. If not, see
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
*)

open Testlib
module String_set = Set.Make (String)

let (^/) p q = if p = "." then q else if q = "." then p else Filename.concat p q
let (%) f g x = f (g x)
let fold_list f = Fun.flip (List.fold_left (Fun.flip f))
let failwithf = Format.kasprintf failwith

let get_install_dir () =
let rec loop dir sw acc =
(match Filename.basename dir with
| "." | "/" -> failwith "Cannot determine toplevel build directory."
| "_build" -> acc ^/ "_build/install" ^/ sw
| sw -> loop (Filename.dirname dir) sw (acc ^/ ".."))
in
let cwd = Sys.getcwd () in
loop (Filename.dirname cwd) (Filename.basename cwd) "."

let plugin_deps public_name =
let libdir = get_install_dir () ^/ "lib" in
let priv_name = String.map (function '-' | '.' -> '_' | c -> c) public_name in
let plugin_name = String.map (function '.' -> '-' | c -> c) public_name in
let package, library_dir =
(match String.split_on_char '.' public_name with
| [] -> assert false
| (package :: _) as comps -> (package, String.concat "/" comps))
in
[
libdir ^/ package ^/ "META";
libdir ^/ library_dir ^/ priv_name ^ ".cma";
libdir ^/ library_dir ^/ priv_name ^ ".cmxs";
libdir ^/ "caqti/plugins" ^/ plugin_name ^/ "META";
]

let deps_of_uri uri =
(match Uri.scheme uri with
| Some "mariadb" ->
plugin_deps "caqti-driver-mariadb"
| Some ("postgres" | "postgresql") ->
plugin_deps "caqti-driver-postgresql"
| Some "pgx" ->
plugin_deps "caqti-driver-pgx"
| Some "sqlite3" ->
plugin_deps "caqti-driver-sqlite3"
| _ ->
failwithf "Cannot determine driver dependency for %a." Uri.pp uri)

let main common_args tls_library =
let tls_deps =
let tls_configured =
Caqti_connect_config.mem_name "tls" common_args.connect_config
in
(match tls_configured, tls_library with
| false, _ | _, None -> []
| true, Some tls_library -> plugin_deps tls_library)
in
String_set.of_list tls_deps
|> fold_list (fold_list String_set.add % deps_of_uri) common_args.uris
|> String_set.elements
|> String.concat " "
|> Printf.printf "(%s)\n"

let main_cmd =
let open Cmdliner in
let tls_library =
let docv = "public-library-name" in
Arg.(value @@ opt (some string) None @@ info ~docv ["tls-library"])
in
Cmd.v (Cmd.info "deps_of_uris")
Term.(const main $ Testlib.common_args () $ tls_library)

let () =
exit (Cmdliner.Cmd.eval main_cmd)
Loading

0 comments on commit 31b9396

Please sign in to comment.