Skip to content

Commit

Permalink
[ new ] Support for dumping a package's install location (idris-lang#…
Browse files Browse the repository at this point in the history
…3381)

* qol improvements to buildIdris function

* remove env var export that does not have an impact on install process

* Add support for asking the compiler where it will install a package

* allow buildIdris to find C libs installed per Pack standard practice

* Add changelog entry

* add test that exercises new installdir command

* support testing on windows
  • Loading branch information
mattpolzin authored Sep 14, 2024
1 parent 53f448c commit 09cb83d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ This CHANGELOG describes the merged but unreleased changes. Please see [CHANGELO
* The `idris2 --init` command now ensures that package names are
valid Idris2 identifiers.

* A new `idris2 --dump-installdir {ipkg-filename}` command outputs the file path
where Idris2 will install the given package if `idris2 --install
{ipkg-filename}` is called.

### Building/Packaging changes

* The Nix flake's `buildIdris` function now returns a set with `executable` and
Expand Down
13 changes: 10 additions & 3 deletions nix/buildIdris.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let
nativeBuildInputs = [ idris2 makeWrapper jq ] ++ attrs.nativeBuildInputs or [];
buildInputs = propagatedIdrisLibraries ++ attrs.buildInputs or [];

IDRIS2_PACKAGE_PATH = libDirs;
env.IDRIS2_PACKAGE_PATH = libDirs;

buildPhase = ''
runHook preBuild
Expand All @@ -60,10 +60,10 @@ let

passthru = {
inherit propagatedIdrisLibraries;
};
} // (attrs.passthru or {});

shellHook = ''
export IDRIS2_PACKAGE_PATH="${finalAttrs.IDRIS2_PACKAGE_PATH}"
export IDRIS2_PACKAGE_PATH="${finalAttrs.env.IDRIS2_PACKAGE_PATH}"
'';
}
);
Expand Down Expand Up @@ -112,6 +112,13 @@ in rec {
mkdir -p $out/${libSuffix}
export IDRIS2_PREFIX=$out/lib
idris2 ${installCmd} ${ipkgFileName}
# check if the package has installed any C libs to ./lib
# (a practice popularized by the Pack package manager)
for file in ./lib/*{.so,.dylib,.h}; do
installDir="$(idris2 --dump-installdir "${ipkgFileName}")/lib"
mkdir -p "$installDir"
mv -- "$file" "$installDir"/
done
runHook postInstall
'';
};
Expand Down
5 changes: 5 additions & 0 deletions src/Idris/CommandLine.idr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data PkgCommand
| REPL
| Init
| DumpJson
| DumpInstallDir

export
Show PkgCommand where
Expand All @@ -37,6 +38,7 @@ Show PkgCommand where
show REPL = "--repl"
show Init = "--init"
show DumpJson = "--dump-ipkg-json"
show DumpInstallDir = "--dump-installdir"

public export
data DirCommand
Expand Down Expand Up @@ -284,6 +286,9 @@ options = [MkOpt ["--check", "-c"] [] [CheckOnly]
MkOpt ["--dump-ipkg-json"] [Optional "package file"]
(\ f => [Package DumpJson f])
(Just "Dump an Idris2 package file in the JSON format"),
MkOpt ["--dump-installdir"] [Optional "package file"]
(\ f => [Package DumpInstallDir f])
(Just "Dump the location where the given package will be installed"),
MkOpt ["--build"] [Optional "package file"]
(\f => [Package Build f])
(Just "Build modules/executable for the given package"),
Expand Down
18 changes: 13 additions & 5 deletions src/Idris/Package.idr
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,12 @@ installSrcFrom wdir destdir (ns, srcRelPath)
| Left err => throw $ UserError (show err)
pure ()

absoluteInstallDir : (relativeInstallDir : String) -> Core String
absoluteInstallDir relativeInstallDir = do
mdestdir <- coreLift $ getEnv "DESTDIR"
let destdir = fromMaybe "" mdestdir
pure $ destdir ++ relativeInstallDir

-- Install all the built modules in prefix/package/
-- We've already built and checked for success, so if any don't exist that's
-- an internal error.
Expand All @@ -617,11 +623,9 @@ install pkg opts installSrc -- not used but might be in the future
= do defs <- get Ctxt
build <- ttcBuildDirectory
let lib = installDir pkg
mdestdir <- coreLift $ getEnv "DESTDIR"
let destdir = fromMaybe "" mdestdir
libTargetDir <- (destdir ++) <$> libInstallDirectory lib
ttcTargetDir <- (destdir ++) <$> ttcInstallDirectory lib
srcTargetDir <- (destdir ++) <$> srcInstallDirectory lib
libTargetDir <- absoluteInstallDir =<< libInstallDirectory lib
ttcTargetDir <- absoluteInstallDir =<< ttcInstallDirectory lib
srcTargetDir <- absoluteInstallDir =<< srcInstallDirectory lib

let src = source_dir (dirs (options defs))
runScript (preinstall pkg)
Expand Down Expand Up @@ -959,6 +963,10 @@ processPackage opts (cmd, mfile)
runRepl (map snd $ mainmod pkg)
Init => pure () -- already handled earlier
DumpJson => coreLift . putStrLn $ toJson pkg
DumpInstallDir => do
libInstallDir <- libInstallDirectory (installDir pkg)
dir <- absoluteInstallDir libInstallDir
coreLift (putStrLn dir)

record PackageOpts where
constructor MkPFR
Expand Down
2 changes: 2 additions & 0 deletions tests/idris2/pkg/pkg020/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/idris2-0.7.0/test-0.3.0
/idris2-0.7.0
14 changes: 14 additions & 0 deletions tests/idris2/pkg/pkg020/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
. ../../../testutils.sh

windows_tweaks() {
sed 's#C:.msys64##' | sed 's#\\#/#g'
}

# pretend Idris2 is installed at root for reproducible
# installdirs:
IDRIS2_PREFIX=/ idris2 --dump-installdir test.ipkg \
| windows_tweaks

# by contrast, the location containing all installed packages:
IDRIS2_PREFIX=/ idris2 --libdir \
| windows_tweaks
5 changes: 5 additions & 0 deletions tests/idris2/pkg/pkg020/src/Stuff.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Stuff

export
say : String
say = "Stuff"
8 changes: 8 additions & 0 deletions tests/idris2/pkg/pkg020/test.ipkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test
version = 0.3.0

-- modules to install
modules = Stuff

sourcedir = "src"

0 comments on commit 09cb83d

Please sign in to comment.