BNFC / bnfc

BNF Converter
http://bnfc.digitalgrammars.com/
586 stars 165 forks source link

Issue in Makefile of Haskell parser #427

Closed daneshvar-amrollahi closed 1 year ago

daneshvar-amrollahi commented 2 years ago

Hi,

I was executing the Mini tutorial in the README and I faced a linking error related to pthread as followed:

$ bnfc -d -m Sum.cf
2 rules accepted

Use Alex 3 to compile Sum/Lex.x.
writing new file ./Sum/Abs.hs
writing new file ./Sum/Print.hs
writing new file ./Sum/Lex.x
writing new file ./Sum/Par.y
writing new file ./Sum/Test.hs
writing new file ./Sum/ErrM.hs
writing new file ./Sum/Skel.hs
writing new file ./Sum/Doc.txt
writing new file ./Makefile

$ make
alex --ghc Sum/Lex.x
happy --array --info --ghc --coerce Sum/Par.y
Grammar info written to: Sum/Par.info
ghc  Sum/Test
[1 of 7] Compiling Sum.Abs          ( Sum/Abs.hs, Sum/Abs.o )
[2 of 7] Compiling Sum.Lex          ( Sum/Lex.hs, Sum/Lex.o )
[3 of 7] Compiling Sum.Par          ( Sum/Par.hs, Sum/Par.o )
[4 of 7] Compiling Sum.Print        ( Sum/Print.hs, Sum/Print.o )
[5 of 7] Compiling Sum.Skel         ( Sum/Skel.hs, Sum/Skel.o )
[6 of 7] Compiling Main             ( Sum/Test.hs, Sum/Test.o )
[7 of 7] Linking Sum/Test

rts/posix/ticker/Pthread.c:214:0: error:
     error: undefined reference to 'pthread_sigmask'

rts/posix/ticker/Pthread.c:216:0: error:
     error: undefined reference to 'pthread_create'

rts/posix/ticker/Pthread.c:226:0: error:
     error: undefined reference to 'pthread_setname_np'

rts/posix/ticker/Pthread.c:219:0: error:
     error: undefined reference to 'pthread_sigmask'

rts/posix/ticker/Pthread.c:266:0: error:
     error: undefined reference to 'pthread_join'

rts/posix/ticker/Pthread.c:272:0: error:
     error: undefined reference to 'pthread_detach'

rts/posix/OSMem.c:570:0: error:
     error: undefined reference to 'pthread_attr_getstacksize'

rts/posix/OSThreads.c:114:0: error:
     error: undefined reference to 'pthread_condattr_setclock'

rts/posix/OSThreads.c:193:0: error:
     error: undefined reference to 'pthread_create'

rts/posix/OSThreads.c:195:0: error:
     error: undefined reference to 'pthread_detach'

rts/posix/OSThreads.c:199:0: error:
     error: undefined reference to 'pthread_setname_np'

rts/posix/OSThreads.c:246:0: error:
     error: undefined reference to 'pthread_key_create'

rts/posix/OSThreads.c:265:0: error:
     error: undefined reference to 'pthread_setspecific'

rts/posix/OSThreads.c:274:0: error:
     error: undefined reference to 'pthread_key_delete'

rts/posix/OSThreads.c:475:0: error:
     error: undefined reference to 'pthread_join'

rts/posix/OSThreads.c:254:0: error:
     error: undefined reference to 'pthread_getspecific'

rts/posix/OSThreads.c:469:0: error:
     error: undefined reference to 'pthread_kill'

rts/include/rts/OSThreads.h:69:0: error:
     error: undefined reference to 'pthread_mutex_trylock'
collect2: error: ld returned 1 exit status
ghc-9.4.2: `gcc' failed in phase `Linker'. (Exit code: 1)
make: *** [Makefile:31: Sum/Test] Error 1

The error is raising from this line of the Makefile: ${GHC} ${GHC_OPTS} $@

It needs a -lpthread to be fixed. I will soon open a pull request to fix this.

andreasabel commented 2 years ago

Thanks for the report! Can you provide some info about your OS and happy and alex versions? GHC is 9.4.2, as I can see.

daneshvar-amrollahi commented 2 years ago

Thanks for the report! Can you provide some info about your OS and happy and alex versions? GHC is 9.4.2, as I can see.

The OS is Ubuntu 20.04.4 LTS. Here is the info on alex and happy:

$ alex --version
Alex version 3.2.7.1, (c) 2003 Chris Dornan and Simon Marlow
$ happy --version
Happy Version 1.20.0 Copyright (c) 1993-1996 Andy Gill, Simon Marlow (c) 1997-2005 Simon Marlow

Happy is a Yacc for Haskell, and comes with ABSOLUTELY NO WARRANTY.
This program is free software; you can redistribute it and/or modify
it under the terms given in the file 'LICENSE' distributed with
the Happy sources.
andreasabel commented 2 years ago

These are the latest version of happy and alex. On my system (macOS Mojave) no special flag -lpthread is needed, but it also does not harm. I'd like some more evidence though that the addition is portable (Windows and other common OSs). Note that one can always pass in extra flags via:

make GHC_OPTS='-lpthread'...
andreasabel commented 2 years ago

These are old issues I found on the topic, but this was GHC 8.2:

There is an open GHC issue but this displays different symptoms:

daneshvar-amrollahi commented 2 years ago

These are the latest version of happy and alex. On my system (macOS Mojave) no special flag -lpthread is needed, but it also does not harm. I'd like some more evidence though that the addition is portable (Windows and other common OSs). Note that one can always pass in extra flags via:

make GHC_OPTS='-lpthread'...

In Windows, Makefiles are usually setup using tools like MinGw and Cygwin, which allows users to install a bunch of unix-based programs. I guess it should be compatible with the makefiles working on Linux.

andreasabel commented 1 year ago

Thanks, @daneshvar-amrollahi for your report and efforts. I still remain unconvinced that this is a general problem (rather than something on your machine, maybe). If this was a common problem, one of my students in my course using BNFC would have had it and alerted me, I suppose. Also, having to pass extra -l flags to GHC is something you usually don't have to do. Thus, I am closing this issue for now, waiting for more evidence. Please use the workaround I describe.