From 28828979a6df073182bf039333cb46f8ad922b19 Mon Sep 17 00:00:00 2001
From: fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>
Date: Sat, 9 Aug 2008 23:45:40 +0000
Subject: [PATCH] Moved more of the build process from Makefile to Setup.hs:

+ tarball target now calls 'sdist'
+ Added to "Extra-source-files" and "Extra-tmp-files" in pandoc.cabal,
  so 'sdist' and 'clean' will work properly.
+ Makefile no longer generates man pages or reference.odt.
+ Setup.hs now generates man pages in a postbuild hook.
+ Added dependency-checking to Setup.hs, so it only rebuilds things
  that need rebuilding.


git-svn-id: https://pandoc.googlecode.com/svn/trunk@1389 788f1e2b-df1e-0410-8736-df70ead52e1b
---
 Makefile     | 14 ++--------
 Setup.hs     | 76 +++++++++++++++++++++++++++++++++++++++-------------
 pandoc.cabal | 76 +++++++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 132 insertions(+), 34 deletions(-)

diff --git a/Makefile b/Makefile
index 413fdc02f..bfbceca52 100644
--- a/Makefile
+++ b/Makefile
@@ -90,14 +90,6 @@ all: build-program
 	sh ./markdown2pdf $< || rm -f $@
 %.txt: %
 	perl -p -e 's/\n/\r\n/' $< > $@ || rm -f $@ # convert to DOS line endings
-%.1: %.1.md $(MAIN)
-	./$(MAIN) -s -S -w man $< >$@ || rm -f $@
-
-cleanup_files+=$(ODTREF)
-$(ODTREF): $(addprefix $(ODTSTYLES)/, meta.xml styles.xml content.xml mimetype \
-                                     settings.xml Thumbnails META-INF)
-	cd $(ODTSTYLES) ; \
-	zip -9 -q -r $(notdir $@) * -x $(notdir $@)
 
 .PHONY: configure
 cleanup_files+=Setup.hi Setup.o $(BUILDCMD) $(BUILDVARS)
@@ -272,10 +264,8 @@ tarball:=$(PKGID).tar.gz
 cleanup_files+=$(tarball)
 tarball: $(tarball)
 $(tarball):
-	svn export . $(PKGID)
-	$(MAKE) -C $(PKGID) wrappers
-	tar cvzf $(tarball) $(PKGID)
-	-rm -rf $(PKGID)
+	$(BUILDCMD) sdist
+	cp $(BUILDDIR)/$(tarball) .
 
 .PHONY: deb
 deb_name:=$(shell grep ^Package debian/control | cut -d' ' -f2 | head -n 1)
diff --git a/Setup.hs b/Setup.hs
index 9bcbaa78a..54d1bff85 100644
--- a/Setup.hs
+++ b/Setup.hs
@@ -1,44 +1,84 @@
 import Distribution.Simple
 import Distribution.PackageDescription ( emptyHookedBuildInfo )
 import Control.Exception ( bracket_ )
-import System.Process ( runCommand, waitForProcess )
-import System.FilePath ( (</>) )
-import System.Directory ( getCurrentDirectory, setCurrentDirectory, findExecutable, doesFileExist )
+import System.Process ( runCommand, runProcess, waitForProcess )
+import System.FilePath ( (</>), (<.>) )
+import System.Directory
 import System.IO ( stderr )
 import System.Exit
-import Data.Maybe ( fromJust )
+import System.Time
+import System.IO.Error ( isDoesNotExistError )
+import Data.Maybe ( fromJust, isNothing, catMaybes )
 
-main = defaultMainWithHooks (simpleUserHooks {runTests = runTestSuite, preConf = checkReferenceODT})
+main = defaultMainWithHooks $
+       simpleUserHooks { runTests  = runTestSuite
+                       , preConf   = checkReferenceODT
+                       , postBuild = makeManPages }
 
 -- | Run test suite.
 runTestSuite _ _ _ _ = do
   inDirectory "tests" $ runCommand "runhaskell RunTests.hs" >>= waitForProcess
   return ()
 
--- | If reference.odt does not exist, build it.
-checkReferenceODT _ _ = do
-  refODTexists <- doesFileExist ("odt-styles" </> "reference.odt")
-  if refODTexists
+-- | If reference.odt needs rebuilding, build it.
+checkReferenceODT _ _ = inDirectory "odt-styles" $ do
+  let refodt = "reference.odt"
+  let deps   = [ "meta.xml", "content.xml", "settings.xml", "META-INF/manifest.xml",
+                 "Thumbnails/thumbnail.png", "styles.xml", "mimetype" ]
+  modifiedDeps <- modifiedDependencies refodt deps
+  if null modifiedDeps
      then return ()
-     else makeReferenceODT
+     else makeReferenceODT modifiedDeps
   return emptyHookedBuildInfo
 
 -- | Create reference.odt by zipping up sources in odt-styles directory.
-makeReferenceODT :: IO ()
-makeReferenceODT = do
+makeReferenceODT :: [FilePath] -> IO ()
+makeReferenceODT sources = do
   zipPathMaybe <- findExecutable "zip"
-  if zipPathMaybe == Nothing
+  if isNothing zipPathMaybe
      then error $ "The 'zip' command, which is needed to build reference.odt\n" ++
                   "from sources in the odt-styles directory, was not found.\n" ++
                   "Try again after installing zip (http://www.info-zip.org/Zip.html).\n" ++
                   "Or use the pandoc source tarball, which contains a prebuilt reference.odt."
      else do
        putStrLn "Creating reference.odt:"
-       inDirectory "odt-styles" $ do
-         ec <- runCommand "zip -9 -r reference.odt *" >>= waitForProcess
-         case ec of
-              ExitSuccess -> putStrLn "Done."
-              _           -> error "Error creating ODT."
+       ec <- runProcess (fromJust zipPathMaybe) (["-9", "-r", "reference.odt"] ++ sources)
+                 Nothing Nothing Nothing Nothing (Just stderr) >>= waitForProcess
+       case ec of
+            ExitSuccess -> return ()
+            _           -> error "Error creating ODT."
+
+-- | Build man pages from markdown sources in man/man1/.
+makeManPages _ _ _ _ = do
+  mapM makeManPage ["pandoc.1", "hsmarkdown.1", "markdown2pdf.1", "html2markdown.1"]
+  return ()
+
+-- | Build a man page from markdown source in man/man1.
+makeManPage manpage = do
+  let manDir = "man" </> "man1"
+  let pandoc = "dist" </> "build" </> "pandoc" </> "pandoc"
+  let page = manDir </> manpage
+  let source = manDir </> manpage <.> "md"
+  modifiedDeps <- modifiedDependencies page [source]
+  if null modifiedDeps
+     then return ()
+     else do
+       ec <- runProcess pandoc ["-s", "-r", "markdown", "-w", "man", "-o", page, source]
+                   Nothing Nothing Nothing Nothing (Just stderr) >>= waitForProcess
+       case ec of
+            ExitSuccess -> putStrLn $ "Created " ++ manDir </> manpage
+            _           -> error $ "Error creating " ++ manDir </> manpage
+
+-- | Returns a list of 'dependencies' that have been modified after 'file'.
+modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
+modifiedDependencies file dependencies = do
+  fileModTime <- catch (getModificationTime file) $
+                 \e -> if isDoesNotExistError e
+                          then return (TOD 0 0)   -- the minimum ClockTime
+                          else ioError e
+  depModTimes <- mapM getModificationTime dependencies
+  let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes
+  return $ catMaybes modified
 
 -- | Perform an IO action in a directory.
 inDirectory :: FilePath -> IO a -> IO a
diff --git a/pandoc.cabal b/pandoc.cabal
index 846ef1399..1c39a2208 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -34,15 +34,27 @@ Description:     Pandoc is a Haskell library for converting from one markup
                  which convert this native representation into a target
                  format. Thus, adding an input or output format requires
                  only adding a reader or writer.
-Extra-Source-Files: README, INSTALL, COPYRIGHT, COPYING,
-                 data/ASCIIMathML.js.comment,
-                 data/ASCIIMathML.js.packed,
+Extra-Source-Files:
+                 -- documentation
+                 README, INSTALL, COPYRIGHT, COPYING, changelog,
+                 -- sources for man pages
+                 man/man1/pandoc.1.md, man/man1/markdown2pdf.1.md,
+                 man/man1/html2markdown.1.md, man/man1/hsmarkdown.1.md,
+                 -- Makefile
+                 Makefile,
+                 -- wrappers
+                 markdown2pdf, html2markdown, hsmarkdown,
+                 -- data for DefaultHeaders.hs
                  data/headers/ConTeXt.header,
                  data/headers/Docbook.header,
                  data/headers/LaTeX.header,
                  data/headers/OpenDocument.header,
                  data/headers/RTF.header,
                  data/headers/S5.header,
+                 -- data for ASCIIMathML writer
+                 data/ASCIIMathML.js.comment,
+                 data/ASCIIMathML.js.packed,
+                 -- data for S5 writer
                  data/ui/default/slides.js.comment,
                  data/ui/default/slides.js.packed,
                  data/ui/default/s5-core.css,
@@ -51,7 +63,63 @@ Extra-Source-Files: README, INSTALL, COPYRIGHT, COPYING,
                  data/ui/default/opera.css,
                  data/ui/default/outline.css,
                  data/ui/default/print.css,
-                 odt-styles/reference.odt
+                 -- data for ODT writer
+                 odt-styles/meta.xml,
+                 odt-styles/content.xml,
+                 odt-styles/settings.xml,
+                 odt-styles/META-INF/manifest.xml,
+                 odt-styles/Thumbnails/thumbnail.png,
+                 odt-styles/styles.xml,
+                 odt-styles/mimetype,
+                 odt-styles/reference.odt,
+                 -- tests
+                 tests/bodybg.gif,
+                 tests/writer.latex,
+                 tests/html-reader.html,
+                 tests/html-reader.native,
+                 tests/insert,
+                 tests/lalune.jpg,
+                 tests/latex-reader.latex,
+                 tests/latex-reader.native,
+                 tests/movie.jpg,
+                 tests/rst-reader.native,
+                 tests/rst-reader.rst,
+                 tests/s5.basic.html,
+                 tests/s5.fancy.html,
+                 tests/s5.fragment.html,
+                 tests/s5.inserts.html,
+                 tests/s5.native,
+                 tests/tables.context,
+                 tests/tables.docbook,
+                 tests/tables.html,
+                 tests/tables.latex,
+                 tests/tables.man,
+                 tests/tables.markdown,
+                 tests/tables.mediawiki,
+                 tests/tables.native,
+                 tests/tables.opendocument,
+                 tests/tables.texinfo,
+                 tests/tables.rst,
+                 tests/tables.rtf,
+                 tests/tables.txt,
+                 tests/testsuite.native,
+                 tests/testsuite.txt,
+                 tests/writer.context,
+                 tests/writer.docbook,
+                 tests/writer.html,
+                 tests/writer.man,
+                 tests/writer.markdown,
+                 tests/writer.mediawiki,
+                 tests/writer.native,
+                 tests/writer.opendocument,
+                 tests/writer.rst,
+                 tests/writer.rtf,
+                 tests/writer.texinfo,
+                 tests/Diff.hs,
+                 tests/RunTests.hs
+Extra-Tmp-Files: man/man1/pandoc.1, man/man1/hsmarkdown.1,
+                 man/man1/markdown2pdf.1, man/man1/html2markdown.1
+
 Flag splitBase
   Description:   Choose the new, smaller, split-up base package.
   Default:       True