Skip to content

Instantly share code, notes, and snippets.

@hsyl20
Created October 7, 2024 15:40
Show Gist options
  • Save hsyl20/5f4f43d37d7f48a8fdb8d2fee3d4f00f to your computer and use it in GitHub Desktop.
Save hsyl20/5f4f43d37d7f48a8fdb8d2fee3d4f00f to your computer and use it in GitHub Desktop.

GHC + Cosmopolitain

Cf https://x.com/4ngerman/status/1842858115881935227

Build

./configure CC=/home/hsyl20/cosmocc/bin/x86_64-unknown-cosmo-cc LD=/home/hsyl20/cosmocc/bin/x86_64-linux-cosmo-ld CXX=/home/hsyl20/cosmocc/bin/x86_64-unknown-cosmo-c++

./hadrian/build --flavour=default+no_profiled_libs+no_dynamic_ghc+no_dynamic_libs -j --docs=none --bignum=native

Failures and fixes

Configure: mismatch on GCC version.

Fix:

diff --git m4/fp_gcc_version.m4 m4/fp_gcc_version.m4
index c1a7f8c5b74..df581a69f4e 100644
--- m4/fp_gcc_version.m4
+++ m4/fp_gcc_version.m4
@@ -8,7 +8,7 @@ AC_DEFUN([FP_GCC_VERSION], [
     AC_MSG_ERROR([C compiler is required])
   fi

-  if $CC --version | grep -qi gcc; then
+  if $CC --version | grep -q gcc; then
     AC_CACHE_CHECK([version of gcc], [fp_cv_gcc_version],
     [
         # Be sure only to look at the first occurrence of the "version " string;

Configure: can’t link C++ libs

Workaround:

diff --git m4/fp_find_cxx_std_lib.m4 m4/fp_find_cxx_std_lib.m4
index 65357fe19eb..89345581e43 100644
--- m4/fp_find_cxx_std_lib.m4
+++ m4/fp_find_cxx_std_lib.m4
@@ -82,10 +82,11 @@ EOF
         }
         case $CXX_STD_LIB_FLAVOUR in
         c++)
-            try_libs "-lc++ -lc++abi" "libc++.so" "c++ c++abi" || \
-            try_libs "-lc++ -lc++abi -lpthread" "libc++.so" "c++ c++abi pthread" || \
-            try_libs "-lc++ -lcxxrt" "libc++.so" "c++ cxxrt" ||
-            AC_MSG_ERROR([Failed to find C++ standard library]) ;;
+            dnl try_libs "-lc++ -lc++abi" "libc++.so" "c++ c++abi" || \
+            dnl try_libs "-lc++ -lc++abi -lpthread" "libc++.so" "c++ c++abi pthread" || \
+            dnl try_libs "-lc++ -lcxxrt" "libc++.so" "c++ cxxrt" ||
+            dnl AC_MSG_ERROR([Failed to find C++ standard library])
+            ;;

CPP of primops.txt.pp fails

While running:

Command line: /home/hsyl20/cosmocc/bin/x86_64-unknown-cosmo-cc -P -Irts/include -I_build/stage0/compiler/build -x c compiler/GHC/Builtin/primops.txt.pp it fails with a lot of:
 compiler/GHC/Builtin/primops.txt.pp:4397:76: error: stray '#' in program
 4397 |     f :: State# RealWorld -> (# State# RealWorld, (# Int, String, Int8# #) #)
      |                                                                            ^
compiler/GHC/Builtin/primops.txt.pp:4400:60: error: stray '`' in program
 4400 |   primop. Specifically, due to the operational behavior of `catch#` the result
      |                                                            ^
compiler/GHC/Builtin/primops.txt.pp:4404:7: error: stray '#' in program
 4404 |   See #21868.

Fix:

diff --git hadrian/src/Settings/Builders/HsCpp.hs hadrian/src/Settings/Builders/HsCpp.hs
index c30477d059a..3ddfde5da6e 100644
--- hadrian/src/Settings/Builders/HsCpp.hs
+++ hadrian/src/Settings/Builders/HsCpp.hs
@@ -11,6 +11,7 @@ hsCppBuilderArgs = builder HsCpp ? do
     ghcPath <- expr $ buildPath (vanillaContext stage compiler)
     mconcat [ prgFlags . T.hsCppProgram . T.tgtHsCPreprocessor <$> getStagedTarget
             , arg "-P"
+            , arg "-E"
             , arg "-Irts/include"
             , arg $ "-I" ++ ghcPath
             , arg "-x", arg "c"

Errors in GMP

Fix: use --bignum=native

Libffi doesn’t build: errors in src/tramp.c

It only supports Linux and Cygwin…

Workaround:

  • enable most headers in _build/stage1/libffi/build/src/tramp.c
  • replace most #if defined (linux) || defined (CYGWIN) with #if defined (linux) || defined (CYGWIN) || 1 in this file

-shared not supported

Failure:

-shared: x86_64-unknown-cosmo-cc: -shared not supported

Even with flavour transformers no_dynamic_ghc and no_dynamic_libs, it fails to build libffi.

Force non-shared:

--- hadrian/src/Settings/Builders/Configure.hs
+++ hadrian/src/Settings/Builders/Configure.hs
@@ -39,8 +39,8 @@ configureBuilderArgs = do
                 pure [ "--prefix=" ++ top -/- libffiPath -/- "inst"
                      , "--libdir=" ++ top -/- libffiPath -/- "inst/lib"
                      , "--enable-static=yes"
-                     , "--enable-shared="
-                            ++ (if wayUnit Dynamic way
-                                    then "yes"
-                                    else "no")
+                     , "--enable-shared=no"
+                            -- ++ (if wayUnit Dynamic way
+                            --         then "yes"
+                            --         else "no")
                      , "--host=" ++ targetPlatform ] ]

CPP issues in hs files because of # symbol

--- compiler/GHC/SysTools/Cpp.hs
+++ compiler/GHC/SysTools/Cpp.hs
@@ -210,6 +210,7 @@ doCpp logger tmpfs dflags unit_env opts input_fn output_fn = do
                     ++ map GHC.SysTools.Option avx_defs
                     ++ map GHC.SysTools.Option io_manager_defs
                     ++ mb_macro_include
+                    ++ [ GHC.SysTools.Option     "-E"]
                     ++ line_pragmas

Elf32_Word not defined

--- rts/linker/Elf.c
+++ rts/linker/Elf.c
@@ -114,6 +114,10 @@
 #  include "elf_reloc.h"
 #endif

+#if !defined(Elf32_Word)
+#define Elf32_Word uint32_t
+#endif
+

clock_getcpuclockid not defined

--- rts/posix/GetTime.c
+++ rts/posix/GetTime.c
@@ -94,6 +94,7 @@ Time getCurrentThreadCPUTime(void)
 #elif defined(HAVE_CLOCK_GETTIME)        &&  \
        defined(CLOCK_PROCESS_CPUTIME_ID) &&  \
        defined(HAVE_SYSCONF)
+#if defined(clock_getcpuclockid)
     static bool have_checked_usability = false;
     // The RELAXED operation is fine here as it's okay if we do the check below
     // more than once.
@@ -109,6 +110,7 @@ Time getCurrentThreadCPUTime(void)
         }
         RELAXED_STORE(&have_checked_usability, true);
     }
+#endif
     return getClockTime(CLOCK_THREAD_CPUTIME_ID);

EMC_SPARC and EM_PCC not defined

--- rts/linker/Elf.c
+++ rts/linker/Elf.c
@@ -409,11 +413,15 @@ ocVerifyImage_ELF ( ObjectCode* oc )
 #if defined(EM_SPARC32PLUS)
       case EM_SPARC32PLUS:
 #endif
+#if defined(EM_SPARC)
       case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
+#endif
 #if defined(EM_IA_64)
       case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
 #endif
+#if defined(EM_PPC)
       case EM_PPC:   IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
+#endif
 #if defined(EM_PPC64)
       case EM_PPC64: IF_DEBUG(linker,debugBelch( "powerpc64" ));

CPP issues with HS code again

Adding -trigraphs helps.

--- compiler/GHC/SysTools/Cpp.hs
+++ compiler/GHC/SysTools/Cpp.hs
@@ -210,6 +210,8 @@ doCpp logger tmpfs dflags unit_env opts input_fn output_fn = do
                     ++ map GHC.SysTools.Option avx_defs
                     ++ map GHC.SysTools.Option io_manager_defs
                     ++ mb_macro_include
+                    ++ [ GHC.SysTools.Option     "-E"]
+                    ++ [ GHC.SysTools.Option     "-trigraphs"]
                     ++ line_pragmas

Still failing because it nukes the spaces... It looks like -x assembler-with-cpp isn't correctly supported...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment