Opened 2 years ago

Last modified 3 months ago

#6132 new bug

Can't use both shebang line and #ifdef declarations in the same file.

Reported by: gfxmonk Owned by:
Priority: normal Milestone: 7.10.1
Component: Compiler Version: 7.0.4
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

I have an (admittedly awkward) script which can be compiled or interpreted.

If it's compiled, I want the full goodness. If it's interpreted, I want to run a "minimal" version (because if I don't have the compiled version, I probably don't have the required libraries either).

The following almost works:

module Main (main) where
#ifdef FANCY
import qualified System.Console.ANSI as Term
start = Term.setSGR [Term.SetColor Term.Foreground Term.Dull Term.Green]
end = Term.setSGR []
#else
start = return ()
end = return ()
#endif
main :: IO ()
main = do
	start
	putStrLn "hello world"
	end

and then I can do:

$ runghc -cpp main.hs
hello world
^^ plain text
$ ghc -O -cpp -DFANCY main.hs
$ ./main
hello world
^^ green text (a.k.a "fancy")

I attempted to make this directly runnable by adding a shebang line of

#!/usr/bin/runghc -cpp

But unfortunately that chokes with -cpp:

$ ghc -O -cpp -DFANCY main.hs

main.hs:1:0:  error: invalid preprocessing directive #!

Change History (2)

comment:1 Changed 21 months ago by igloo

  • Difficulty set to Unknown
  • Milestone set to 7.8.1

If you put the conditional part into a separate module with a {-# LANGUAGE CPP #-} pragma then it would work, but I suspect you don't want to do that so that it's easy to move the script around. Similarly a custom preprocessor would make moving it around harder.

I can't see a better immediate fix than:

  • When running CPP, if the file starts with #! then copy all but the first line to a temporary file and run CPP on that instead. (actually, we'd want a blank first line so that line numbers work out).

comment:2 Changed 3 months ago by thoughtpolice

  • Milestone changed from 7.8.3 to 7.10.1

Moving to 7.10.1.

Note: See TracTickets for help on using tickets.