Skip to content

Commit

Permalink
Only use cabal-2.x for building bdcs.
Browse files Browse the repository at this point in the history
This has one big advantage:  "scope: private" is now supported for
executables, which means they end up getting installed into libexecdir.
We don't have to do that ourselves, which means all that stuff in
Setup.hs can go away and it becomes the default simple version.

It also requires the autogen-modules stuff getting added, but that's not
a big deal.
  • Loading branch information
clumens committed May 16, 2018
1 parent a5e30c6 commit cb12ded
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 111 deletions.
107 changes: 3 additions & 104 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Copyright (C) 2016-2017 Red Hat, Inc.
-- Copyright (C) 2018 Red Hat, Inc.
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
Expand All @@ -13,106 +13,5 @@
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, see <http://www.gnu.org/licenses/>.

{-# LANGUAGE CPP #-}

module Main(main)
where

import Data.List(isPrefixOf)
import Distribution.PackageDescription(Executable(..), GenericPackageDescription(..), HookedBuildInfo, PackageDescription(..))
import Distribution.Simple(defaultMainWithHooks, simpleUserHooks)
import Distribution.Simple.InstallDirs(InstallDirs(..), fromPathTemplate, toPathTemplate)
import Distribution.Simple.LocalBuildInfo(LocalBuildInfo(..))
import Distribution.Simple.UserHooks(UserHooks(..))
import System.FilePath((</>))

#if defined(MIN_VERSION_Cabal) && MIN_VERSION_Cabal(2, 0, 0)
import Distribution.Types.UnqualComponentName(unUnqualComponentName)
import Distribution.Types.ExecutableScope(ExecutableScope(..))
import Distribution.Simple.Setup(ConfigFlags)
#else
import Distribution.Simple.Install(install)
import Distribution.Simple.Setup(ConfigFlags, CopyFlags)
#endif

main :: IO ()
main = defaultMainWithHooks bdcsHooks

-- Handle the libexec paths, and differences between Cabal-1.x and Cabal-2.x.
--
-- On Cabal-2.x, we can specify a scope of "private" on libexec executables.
-- This can be done directly in the .cabal file when we remove support for Cabal < 2.0.
#if defined(MIN_VERSION_Cabal) && MIN_VERSION_Cabal(2, 0, 0)
bdcsHooks :: UserHooks
bdcsHooks = simpleUserHooks { confHook = bdcsConf }

bdcsConf :: (GenericPackageDescription, HookedBuildInfo) -> ConfigFlags -> IO LocalBuildInfo
bdcsConf (descr, hbi) flags = do
lbi <- confHook simpleUserHooks (descr, hbi) flags

-- Modify the executable list to change the scope of the libexec executables
let localDesc = localPkgDescr lbi
scopedLbi = lbi { localPkgDescr = localDesc { executables = map mangleExec (executables localDesc) } }

-- Add weldr/ to the libexecdir
return $ addLibexecWeldr scopedLbi
where
mangleExec :: Executable -> Executable
mangleExec exe =
if isSubcommand (unUnqualComponentName $ exeName exe) then exe{exeScope = ExecutablePrivate}
else exe

#else
-- On Cabal-1.x, change the destination directory of subcomponents during the copy step
bdcsHooks :: UserHooks
bdcsHooks = simpleUserHooks { confHook = bdcsConf,
copyHook = bdcsCopy }

bdcsConf :: (GenericPackageDescription, HookedBuildInfo) -> ConfigFlags -> IO LocalBuildInfo
bdcsConf (descr, hbi) flags = addLibexecWeldr <$> confHook simpleUserHooks (descr, hbi) flags

bdcsCopy :: PackageDescription -> LocalBuildInfo -> UserHooks -> CopyFlags -> IO ()
bdcsCopy pkg lbi _ flags = do
-- First, install everything that's not a subcommand. That is almost everything
-- listed in the cabal file.
let (mainPkg, mainLbi) = extractMainComponents pkg lbi
install mainPkg mainLbi flags

-- Second, install only the subcommands into $prefix/libexec/weldr.
let (subPkg, subLbi) = extractSubComponents pkg lbi
install subPkg subLbi flags

extractMainComponents :: PackageDescription -> LocalBuildInfo -> (PackageDescription, LocalBuildInfo)
extractMainComponents pkg lbi = let
pkg' = pkg { executables = filter (not . isSubcommand . exeName)
(executables pkg) }
in
(pkg', lbi)

extractSubComponents :: PackageDescription -> LocalBuildInfo -> (PackageDescription, LocalBuildInfo)
extractSubComponents pkg lbi = let
dest = libexecdir (installDirTemplates lbi)

pkg' = pkg { executables = filter (isSubcommand . exeName)
(executables pkg) }
lbi' = lbi { installDirTemplates = (installDirTemplates lbi) { bindir = dest } }
in
(pkg', lbi')

#endif

isSubcommand :: String -> Bool
isSubcommand s = "inspect-" `isPrefixOf` s || "bdcs-" `isPrefixOf` s

-- Make sure $libexecdir gets "weldr/" appended to it. This will affect both the installation location
-- for anything that should go into $libexecdir (like our subcommand programs) as well as the auto
-- generated Paths_bdcs.hs file.
--
-- This also means if you pass --libexecdir= to "cabal configure" or any other program, you should not
-- add "weldr/" to it. Otherwise you'll get it in there twice.
addLibexecWeldr :: LocalBuildInfo -> LocalBuildInfo
addLibexecWeldr lbi =
let dest = mangle $ libexecdir $ installDirTemplates lbi
in lbi { installDirTemplates = (installDirTemplates lbi) { libexecdir = dest } }
where
mangle = toPathTemplate . (</> "weldr") . fromPathTemplate
import Distribution.Simple
main = defaultMain
20 changes: 13 additions & 7 deletions bdcs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ author: Chris Lumens
maintainer: clumens@redhat.com
license: LGPL-2.1
license-file: LICENSE
build-type: Custom
cabal-version: >= 1.10
build-type: Simple
cabal-version: >= 2.0

data-files: schema.sql,
data/*.conf
Expand All @@ -33,11 +33,6 @@ extra-source-files: ChangeLog.md,
src/tests/BDCS/RPM/*.hs,
tests/*.sh

custom-setup
setup-depends: base,
Cabal >= 1.10 && < 2.1,
filepath

source-repository head
type: git
location: https://github.com/weldr/bdcs
Expand Down Expand Up @@ -158,6 +153,7 @@ library

pkgconfig-depends: ostree-1 >= 2017.8

autogen-modules: Paths_bdcs
other-modules: Paths_bdcs
default-language: Haskell2010

Expand All @@ -167,6 +163,7 @@ executable bdcs
main-is: bdcs.hs
hs-source-dirs: src/tools

autogen-modules: Paths_bdcs
other-modules: Utils.GetOpt,
Utils.Subcommands,
Paths_bdcs
Expand All @@ -187,6 +184,7 @@ executable bdcs

executable bdcs-import
main-is: import.hs
scope: private
hs-source-dirs: src/tools

other-modules: Utils.GetOpt
Expand All @@ -206,9 +204,11 @@ executable bdcs-import

executable bdcs-inspect
main-is: inspect.hs
scope: private
hs-source-dirs: src/tools/inspect,
src/tools

autogen-modules: Paths_bdcs
other-modules: Utils.GetOpt,
Utils.IO,
Utils.Subcommands,
Expand All @@ -230,6 +230,7 @@ executable bdcs-inspect

executable inspect-groups
main-is: groups.hs
scope: private
hs-source-dirs: src/tools/inspect/subcommands,
src/tools/inspect,
src/tools
Expand Down Expand Up @@ -257,6 +258,7 @@ executable inspect-groups

executable inspect-ls
main-is: ls.hs
scope: private
hs-source-dirs: src/tools/inspect/subcommands,
src/tools/inspect,
src/tools
Expand Down Expand Up @@ -284,6 +286,7 @@ executable inspect-ls

executable inspect-nevras
main-is: nevras.hs
scope: private
hs-source-dirs: src/tools/inspect/subcommands,
src/tools/inspect,
src/tools
Expand All @@ -306,6 +309,7 @@ executable inspect-nevras

executable bdcs-export
main-is: export.hs
scope: private
hs-source-dirs: src/tools

other-modules: Utils.GetOpt
Expand All @@ -325,6 +329,7 @@ executable bdcs-export

executable bdcs-tmpfiles
main-is: bdcs-tmpfiles.hs
scope: private
hs-source-dirs: src/tools

build-depends: bdcs,
Expand All @@ -338,6 +343,7 @@ executable bdcs-tmpfiles

executable bdcs-depsolve
main-is: depsolve.hs
scope: private
hs-source-dirs: src/tools

other-modules: Utils.GetOpt
Expand Down

0 comments on commit cb12ded

Please sign in to comment.