aboutsummaryrefslogtreecommitdiff
path: root/Setup.hs
blob: f22351052fb45fac38dd8bc31b9617a7c5ed8e22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-- Copyright 2017 Google LLC
--
-- Licensed under the Apache License, Version 2.0 (the "License"); you may not
-- use this file except in compliance with the License. You may obtain a copy of
-- the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-- License for the specific language governing permissions and limitations under
-- the License.

module Main (main) where

import qualified Distribution.PackageDescription as PackageDescription
import qualified Distribution.Simple as Simple
import qualified Distribution.Simple.LocalBuildInfo as LocalBuildInfo
import qualified Distribution.Simple.Setup as Setup
import qualified Distribution.Simple.Utils as Utils
import qualified Gtk2HsSetup
import System.Directory (getCurrentDirectory)
import System.FilePath ((</>))

main =
  let h = Simple.simpleUserHooks in
  Simple.defaultMainWithHooks
    h { Simple.preConf = \args flags -> do
          -- Cabal expects to find BoringSSL's libraries already built at the
          -- time of configuration, so we must build BoringSSL completely here.
          boringsslBuild flags
          Simple.preConf h args flags
      , Simple.confHook = \info flags -> do
          buildinfo <- Simple.confHook h info flags
          boringsslUpdateExtraLibDirs buildinfo
      , Simple.buildHook = Simple.buildHook Gtk2HsSetup.gtk2hsUserHooks
      }

boringsslDir = "third_party" </> "boringssl"

boringsslLibDir = boringsslDir </> "lib"

boringsslBuild flags = do
  -- Build BoringSSL.
  let buildDir = boringsslDir </> "build"
  mkdir buildDir
  let cryptoTarget = "crypto" </> "libcrypto.a"
  cmd
    [ "cmake"
    , "-GNinja"
    , "-DCMAKE_BUILD_TYPE=Release"
    , "-DCMAKE_POSITION_INDEPENDENT_CODE=TRUE"
    , "-B" ++ buildDir
    , "-H" ++ boringsslDir </> "src"
    ]
  cmd ["ninja", "-C", buildDir, cryptoTarget]
  -- Rename BoringSSL's libraries so we don't accidentally grab OpenSSL.
  mkdir boringsslLibDir
  Utils.installOrdinaryFile v
    (buildDir </> cryptoTarget)
    (boringsslLibDir </> "libbtls_crypto.a")
  where
    v = Setup.fromFlag (Setup.configVerbosity flags)
    mkdir = Utils.createDirectoryIfMissingVerbose v True
    cmd (bin:args) = Utils.rawSystemExit v bin args

boringsslUpdateExtraLibDirs buildinfo = do
  let pkg = LocalBuildInfo.localPkgDescr buildinfo
      Just lib = PackageDescription.library pkg
      libBuild = PackageDescription.libBuildInfo lib
      dirs = PackageDescription.extraLibDirs libBuild
  root <- getCurrentDirectory
  return
    buildinfo
    { LocalBuildInfo.localPkgDescr =
        pkg
        { PackageDescription.library =
            Just $
              lib
              { PackageDescription.libBuildInfo =
                  libBuild
                  { PackageDescription.extraLibDirs =
                      (root </> boringsslLibDir) : dirs
                  }
              }
        }
    }