haskellmakefilecompiler-errorsgnu-makeghc

GHC(Haskell) not picking up imports from makefile


I have been making a Haskell project that I want to continue on in C at some point through the FFI. I wanted to create a makefile to compile all the source with Clang for C and GHC for Haskell.

The make file is pictured below:

#Directory Definitions
SRCDIR:=./src
HDIR:=$(SRCDIR)/haskell
CDIR:=$(SRCDIR)/c
LDIR:=./lib
HLDIR:=$(LDIR)/haskell
CLDIR:=$(LDIR)/c
ODIR:=./obj
CODIR:=$(ODIR)/c
HODIR:=$(ODIR)/haskell
INTDIR:=./interface/haskell
INCDIR:=./include
BDIR:=./bin
ITARGET:=CalcLang.exe
# Toolchain definitions
CLANG:=clang
GHC:=ghc
# C compilation directives
CFLAGS += -fPIC
CFLAGS += -std=c99
CFLAGS += -g
CFLAGS += -I$(INCDIR)
# Haskell generate interface file Flags
HIFLAGS += -hidir $(INTDIR)
HIFLAGS += -fno-code
HIFLAGS += -i$(HDIR)
# Haskell compilation directives
HFLAGS += -XForeignFunctionInterface
HFLAGS += -fPIC
HFLAGS += -flink-rts
HFLAGS += -hidir $(INTDIR)
#Here is a specification of the Src
HISRC:=$(HDIR)/CalcLangParser.hs $(HDIR)/CalcLangInterpreter.hs $(HDIR)/CalcLangMain.hs
HIINT:=$(HISRC:$(HDIR)/%.hs=$(INTDIR)/%.hi)
HIOBJS:=$(HISRC:$(HDIR)/%.hs=$(HODIR)/%.o)

$(INTDIR)/%.hi: $(HDIR)/%.hs
    $(GHC) -c $< -o $@ $(HIFLAGS)
$(HODIR)/%.o: $(HDIR)/%.hs $(HIINT)
    $(GHC) -c $< -o $@ $(HFLAGS)
$(BDIR)/$(ITARGET): $(HIOBJS)
    $(GHC) $^ -o $@

.PHONY: clean
clean:
    rm -f $(HIOBJS) $(HIINT) ./*~ ./*# ./*/*/*~ ./*/*/*#  $(BDIR)/*

I keep trying to run make, and this is the result I get:

ghc -c src/haskell/CalcLangParser.hs -o interface/haskell/CalcLangParser.hi -hidir ./interface/haskell -fno-code -i./src/haskell
ghc -c src/haskell/CalcLangInterpreter.hs -o interface/haskell/CalcLangInterpreter.hi -hidir ./interface/haskell -fno-code -i./src/haskell
src\haskell\CalcLangInterpreter.hs:3:1: error: [GHC-87110]
    Could not find module `CalcLangParser'.
    Use -v to see a list of the files searched for.
  |
3 | import CalcLangParser

For the first step, I am just compiling to all the interface files first or the files in the hi format. Because I thought the issue was that I needed all those files before compiling into object files. But anyway, even though CalcLangParser compiles first, the import is not working correctly in the CalcLangInterpreter file. If I am missing some flag or doing something wrong, I would like to know. I am not super experienced with Haskell yet.


Solution

  • If I'm reading the documentation correctly, instead of

    -c -o foo.hi -fno-code
    

    you will need:

    -ohi foo.hi -fno-code -fwrite-interface
    

    Optionally, I believe you can omit the -ohi foo.hi part. The file to write will be computed from your -hidir flag and the module name.

    I think the command you tried created an object file, not an interface, and gave it a name ending in .hi. More details in the separate compilation section of the GHC manual.