From 1f1dd579efc9082211bdea4decc3218f8dd1441b Mon Sep 17 00:00:00 2001 From: Lars Kuhtz Date: Thu, 12 Sep 2024 23:24:34 -0700 Subject: [PATCH] Fix build and cleanup Setup.hs and cabal file --- Cargo.toml | 2 +- Setup.hs | 101 +++++++++++++++++++++++++++++++++++++++++++++ Setup.lhs | 81 ------------------------------------ plonk-verify.cabal | 99 ++++++++++++-------------------------------- 4 files changed, 129 insertions(+), 154 deletions(-) create mode 100644 Setup.hs delete mode 100644 Setup.lhs diff --git a/Cargo.toml b/Cargo.toml index 5a767fe..5bd62e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ sha2 = "0.10.8" hex = "0.4.3" [lib] -crate-type = ["staticlib"] +crate-type = ["staticlib", "cdylib"] [dev-dependencies] hex = "0.4.3" diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..b2df6c9 --- /dev/null +++ b/Setup.hs @@ -0,0 +1,101 @@ +-- This file was generated by `cargo-cabal`, its goal is to define few hooks to +-- call `cargo` on the fly and link correctly the generated library. +-- +-- While it's an acceptable hack as this project is currently a prototype, this +-- should be removed before `cargo-cabal` stable release. + +import Data.Maybe +import qualified Distribution.PackageDescription as PD +import Distribution.Simple + ( Args + , UserHooks (confHook, preConf) + , defaultMainWithHooks + , simpleUserHooks + ) +import Distribution.Simple.Program + ( ghcProgram + ) +import Distribution.Simple.LocalBuildInfo + ( LocalBuildInfo (localPkgDescr) + , buildDir + , compiler + ) +import Distribution.Simple.Setup + ( BuildFlags (buildVerbosity) + , ConfigFlags (configVerbosity) + , CleanFlags(..) + , fromFlag + ) +import Distribution.Simple.UserHooks + ( UserHooks (buildHook, confHook, cleanHook) + ) +import Distribution.Simple.Utils (rawSystemExit) +import System.Directory (getCurrentDirectory) +import Distribution.Text (display) +import Distribution.Simple.Compiler +import Distribution.System (buildPlatform) +import Distribution.Simple.BuildPaths + ( mkGenericSharedBundledLibName + , mkGenericStaticLibName + ) + +main :: IO () +main = + defaultMainWithHooks + simpleUserHooks + { buildHook = rustBuildHook + , cleanHook = rustCleanHook + } + +rustCleanHook + :: PD.PackageDescription + -> () + -> UserHooks + -> CleanFlags + -> IO () +rustCleanHook desc () hooks flags = do + pwd <- getCurrentDirectory + putStrLn $ "cleanup rust build artifacts: delete " <> pwd <> "/target" + rawSystemExit (fromFlag $ cleanVerbosity flags) "rm" ["-r", "./target"] + cleanHook simpleUserHooks desc () hooks flags + +rustBuildHook + :: PD.PackageDescription + -> LocalBuildInfo + -> UserHooks + -> BuildFlags + -> IO () +rustBuildHook description localBuildInfo hooks flags = do + -- run Rust build + -- FIXME add cargo and rust to program db during configure step + -- FIXME: add `--target $TARGET` flag to support cross-compiling to $TARGET + putStrLn "Call `cargo build --release` to build a dependency written in Rust" + rawSystemExit (fromFlag $ buildVerbosity flags) "cargo" ["build","--release"] + + -- copy static lib + putStrLn $ "Copy libplonk_verify.a to " <> staticTarget + rawSystemExit (fromFlag $ buildVerbosity flags) "cp" + [ "target/release/libplonk_verify.a" + , staticTarget + ] + + -- copy dyn lib + putStrLn $ "Copy libplonk_verify.dylib to " <> dynTarget + rawSystemExit (fromFlag $ buildVerbosity flags) "cp" + [ "target/release/libplonk_verify.dylib" + , dynTarget + ] + + putStrLn "rustc compilation succeeded" + buildHook simpleUserHooks description localBuildInfo hooks flags + where + targetLibname = "Cplonk_verify" + c = compiler localBuildInfo + ghcVersion = display (compilerFlavor c) <> display (compilerVersion c) + staticTarget = buildDir localBuildInfo + <> "/" + <> mkGenericStaticLibName targetLibname + dynTarget = buildDir localBuildInfo + <> "/" + <> mkGenericSharedBundledLibName buildPlatform (compilerId c) targetLibname + diff --git a/Setup.lhs b/Setup.lhs deleted file mode 100644 index 6111746..0000000 --- a/Setup.lhs +++ /dev/null @@ -1,81 +0,0 @@ -This file was generated by `cargo-cabal`, its goal is to define few hooks to -call `cargo` on the fly and link correctly the generated library. - -While it's an acceptable hack as this project is currently a prototype, this -should be removed before `cargo-cabal` stable release. - -> import Data.Maybe -> import qualified Distribution.PackageDescription as PD -> import Distribution.Simple -> ( Args, -> UserHooks (confHook, preConf), -> defaultMainWithHooks, -> simpleUserHooks, -> ) -> import Distribution.Simple.LocalBuildInfo -> ( LocalBuildInfo (localPkgDescr), -> ) -> import Distribution.Simple.Setup -> ( BuildFlags (buildVerbosity), -> ConfigFlags (configVerbosity), -> fromFlag, -> ) -> import Distribution.Simple.UserHooks -> ( UserHooks (buildHook, confHook), -> ) -> import Distribution.Simple.Utils (rawSystemExit) -> import System.Directory (getCurrentDirectory) -> -> main :: IO () -> main = -> defaultMainWithHooks -> simpleUserHooks -> { confHook = rustConfHook -> , buildHook = rustBuildHook -> } - -This hook could be remove if at some point, likely if this issue is resolved -https://github.com/haskell/cabal/issues/2641 - -> rustConfHook :: -> (PD.GenericPackageDescription, PD.HookedBuildInfo) -> -> ConfigFlags -> -> IO LocalBuildInfo -> rustConfHook (description, buildInfo) flags = do -> localBuildInfo <- confHook simpleUserHooks (description, buildInfo) flags -> let packageDescription = localPkgDescr localBuildInfo -> library = fromJust $ PD.library packageDescription -> libraryBuildInfo = PD.libBuildInfo library -> dir <- getCurrentDirectory -> return localBuildInfo -> { localPkgDescr = packageDescription -> { PD.library = Just $ library -> { PD.libBuildInfo = libraryBuildInfo -> { PD.extraLibDirs = (dir ++ "/target/release") : -> (dir ++ "/target/debug") : -> PD.extraLibDirs libraryBuildInfo -> } } } } - -It would be nice to remove this hook ot some point, e.g., if this RFC is merged -in Cabal https://github.com/haskell/cabal/issues/7906 - -> rustBuildHook :: -> PD.PackageDescription -> -> LocalBuildInfo -> -> UserHooks -> -> BuildFlags -> -> IO () -> rustBuildHook description localBuildInfo hooks flags = do -> putStrLn "******************************************************************" -> putStrLn "Call `cargo build --release` to build a dependency written in Rust" -> -- FIXME: add `--target $TARGET` flag to support cross-compiling to $TARGET -> rawSystemExit (fromFlag $ buildVerbosity flags) "cargo" ["build","--release"] -> putStrLn "... `rustc` compilation seems to succeed 🦀! Back to Cabal build:" -> putStrLn "******************************************************************" -> putStrLn "Back to Cabal build" -> buildHook simpleUserHooks description localBuildInfo hooks flags - -This handy automation (particularly useful when you want to quickly prototype -without having to spawn manually `cargo` commands) is disabled by default. -Feel free to re-enable it while debugging your library, but I discourage you -strongly to publish anything on Hackage that contains this hook! diff --git a/plonk-verify.cabal b/plonk-verify.cabal index a909a66..c975ab8 100644 --- a/plonk-verify.cabal +++ b/plonk-verify.cabal @@ -1,25 +1,6 @@ -cabal-version: 2.4 --- The cabal-version field refers to the version of the .cabal specification, --- and can be different from the cabal-install (the tool) version and the --- Cabal (the library) version you are using. As such, the Cabal (the library) --- version used must be equal or greater than the version stated in this field. --- Starting from the specification version 2.2, the cabal-version field must be --- the first thing in the cabal file. - --- Initial package description generated by 'cabal init'. For further --- documentation, see: http://haskell.org/cabal/users-guide/ --- --- The name of the package. -name: plonk-verify - --- The package version. --- See the Haskell package versioning policy (PVP) for standards --- guiding when and how versions should be incremented. --- https://pvp.haskell.org --- PVP summary: +-+------- breaking API changes --- | | +----- non-breaking API additions --- | | | +--- code changes with no API change -version: 0.1.0 +cabal-version: 3.0 +name: plonk-verify +version: 0.1.0 -- A short (one-line) description of the package. -- synopsis: @@ -27,8 +8,7 @@ version: 0.1.0 -- A longer description of the package. -- description: --- The license under which the package is released. --- license: +license: MIT -- The package author(s). -- author: @@ -37,69 +17,44 @@ version: 0.1.0 -- patches. -- maintainer: --- A copyright notice. --- copyright: +copyright: Copyright (c) 2024 Argument Computer Corporation -extra-source-files: Cargo.toml Cargo.lock src/lib.rs +extra-doc-files: + README.md + +extra-source-files: + Cargo.toml + Cargo.lock + src/lib.rs + hsbindgen.toml data-files: verifier-assets/v1.0.8-testnet/vk.bin --- This let us hook Cabal steps to Setup.lhs script. -build-type: Custom +build-type: Custom custom-setup - setup-depends: Cabal, base, directory, process - --- Extra doc files to be distributed with the package, such as a CHANGELOG or a --- README. --- extra-doc-files: - --- Extra source files to be distributed with the package, such as examples, or --- a tutorial module. --- extra-source-files: --- --- FIXME: It's still unclear to me what would be the best strategy to let users --- publish packages generated by `cargo-cabal` on Hackage. While it is pretty --- hazardous to put Rust code in sdist archive (because that would require that --- the library end-user have a Rust developer environment on this machine and --- that wouldn't play well with cross-compilation), is it a good idea to --- package generated platform-dependent library as source? + setup-depends: + , Cabal + , base + , directory + , process common warnings ghc-options: -Wall library - -- Import common warning flags. - import: warnings - - -- Modules exported by the library. - exposed-modules: PlonkVerify - - -- Modules included in this library but not exported. - -- other-modules: - - -- LANGUAGE extensions used by modules in this package. - -- other-extensions: - - -- Other library packages from which modules are imported. - build-depends: base - - -- Directories containing source files. - hs-source-dirs: src - - -- Base language which the package is written in. + import: warnings default-language: Haskell2010 - extra-libraries: - plonk_verify + hs-source-dirs: src + exposed-modules: + PlonkVerify + build-depends: + base frameworks: CoreFoundation Security - --- This file was generated by `cargo-cabal`, but please don't hesitate to edit it! - --- We would rather rely on `cabal init --non-interactive` to generate this file --- but there is no CLI arg to set `build-type: Custom` on which it sadly --- currently have to rely on. + extra-bundled-libraries: + Cplonk_verify test-suite tests import: warnings