----------------------------------------------------------------------
-- Copyright (c) 2009, 2011, 2012, ETH Zurich.
-- All rights reserved.
--
-- This file is distributed under the terms in the attached LICENSE file.
-- If you do not find this file, copies can be found by writing to:
-- ETH Zurich D-INFK CAB F.78, Universitaetstr 6, CH-8092 Zurich.
-- Attn: Systems Group.
--
-- Hakefile for /hake
--
----------------------------------------------------------------------

let
  hsInDir af tf dir = inDir af tf dir ".hs"
  hakesrc = find hsInDir "."
  hake_rtsopts = "-H128M -A4M -N4"
in
[ Rule ([ Str "ghc -O --make",
          Str "-XDeriveDataTypeable",
          Str "-XStandaloneDeriving",
          Str "-XScopedTypeVariables",
          Str "-package ghc",
          Str "-package ghc-mtl",
          Str "-package ghc-paths",
          Str "-package bytestring-trie",
          Str "-o", Out "hake" "/hake",
          Str "-outputdir", NoDep BuildTree "root" "/hake",
          NStr "-i", NoDep SrcTree "src" "",
          NStr "-i", NoDep BuildTree "hake" "/",
          Str "-rtsopts=all",
          Str ("-with-rtsopts=\"" ++ hake_rtsopts ++ "\""),
          Str "-threaded",
          In SrcTree "src" "Main.hs",
          Dep InstallTree "" "Config.hs",
          Str "$(LDFLAGS)"
        ]
        ++ [Dep SrcTree "src" f | f <- hakesrc]),

  -- Copy files required for interpreting Hakefiles
  includeFile [
    "ArchDefaults.hs",
    "Args.hs",
    "HakeTypes.hs",
    "RuleDefs.hs",
    "Tools.hs",
    "TreeDB.hs",
    "ARMv7.hs",
    "ARMv8.hs",
    "K1om.hs",
    "X86_64.hs"
  ],
  Phony "help" False $ concat [ [Str "@echo", Str ("\"" ++ x ++ "\""), NL] | x <- [
     "------------------------------------------------------------------"
    ,"This is the 'help' target for the Barrelfish Makefile.  This Makefile"
    ,"has been generated by hake in the top-level directory of your build tree."
    ,"See the Hake documentation for more information."
    ,""
    ,"This Makefile contains build targets for the following architectures:"
    ,""
    , (Data.List.intercalate ", " Config.architectures)
    ,""
    ,"To change configuration options, edit the Config.hs file in the hake"
    ,"subdirectory of this directory and run 'make Makefile'."
    ,""
    ,"Useful make targets:"
    ," - <file>:           any file which can be built in the Barrelfish tree"
    ," - 'help':           show this help text"
    ," - 'help-platforms': show the platforms (file collections) which can be built"
    ," - 'help-boot':      show the boot sequences which can be initiated"
    ," - 'help-tests':     show the available tests"
    ," - 'clean':          remove most generated files"
    ," - 'realclean':      remove all generated files (clears the build tree)"
    ," - 'Documentation':  build all Technical Notes"
    ," - 'Makefile':       attempt to re-run hake"
    ,""
  ] ],
  Phony "help-platforms-header" False $ concat [ [Str "@echo", Str ("\"" ++ x ++ "\""), NL] | x <- [
     "------------------------------------------------------------------"
    ,"Platforms supported by this Makefile.  Use 'make <platform name>':"
    ," (these are the platforms available with your architecture choices)"
  ] ],
  Phony "help-boot-header" False $ concat [ [Str "@echo", Str ("\"" ++ x ++ "\""), NL] | x <- [
     "------------------------------------------------------------------"
    ,"Boot instructions supported by this Makefile.  Use 'make <boot name>':"
    ," (these are the targets available with your architecture choices)"
    ,""
  ] ],
  Phony "clean" False (
    [ Str "$(RM)", Str "-r", Str "tools", Str "docs" ] ++
    (map Str Config.architectures) ++
    [ Dep BuildTree "root" "/clean-platform" ]),
  -- Legacy target for scalebench to install modules
  Phony "install" False ([
    Dep BuildTree "root" "/$(MODULES)",
    Str "@echo \"\";",
    Str "echo \"Installing modules...\" ;",
    Str "if [ $${#MODULES[@]} -eq 0 ]; then",
    Str "    echo \"   > Modules is empty, did you mean install_X86_64_Basic? \" ;",
    Str "    exit 0;",
    Str "fi;",
    Str "for m in ${MODULES}; do",
    Str "  if [ ! -f ${INSTALL_PREFIX}/$$m ] ||",
    Str "      [ $$(stat -c%Y $$m) -ne $$(stat -c%Y ${INSTALL_PREFIX}/$$m) ]; then",
    Str "         do_update=1;",
    Str "         echo \"  > Installing $$m\" ;",
    Str "         mkdir -p ${INSTALL_PREFIX}/$$(dirname $$m);",
    Str "         install -p $$m ${INSTALL_PREFIX}/$$m;",
    Str "  fi;",
    Str "done;",
    Str "if [ ! $$do_update ]; then",
    Str "    echo \"  > All up to date\" ;",
    Str "fi;",
    Str "echo \"\";",
    Str "echo \"done.\" ;"
  ]),
  Rule ([
    Str "sed -r",
    Str "'s#@CMAKE_SOURCE_DIR@#${SRCDIR}#g'",
    In SrcTree "root" "/doc/Doxyfile.cmake",
    Str ">",
    Out "root" "/Doxyfile"
  ]),
  Phony "doxygen" False ([
    Str "doxygen",
    In BuildTree "root" "/Doxyfile"
  ])
]
