[Git][ghc/ghc][wip/bytecode-lib-shared-object] Also link in other object files passed on the command line
Matthew Pickering pushed to branch wip/bytecode-lib-shared-object at Glasgow Haskell Compiler / GHC Commits: 25af941a by Matthew Pickering at 2025-10-28T09:19:20+00:00 Also link in other object files passed on the command line - - - - - 8 changed files: - compiler/GHC/Linker/ByteCode.hs - testsuite/tests/cabal/BytecodeForeign.hs - testsuite/tests/cabal/Makefile - testsuite/tests/cabal/all.T - + testsuite/tests/cabal/pkg_bytecode_with_gbc.stderr - + testsuite/tests/cabal/pkg_bytecode_with_gbc.stdout - + testsuite/tests/cabal/pkg_bytecode_with_o.stderr - + testsuite/tests/cabal/pkg_bytecode_with_o.stdout Changes: ===================================== compiler/GHC/Linker/ByteCode.hs ===================================== @@ -9,13 +9,17 @@ import GHC.Driver.Env import GHC.Utils.Outputable import GHC.Linker.Loader import qualified Data.ByteString as BS +import Data.List (partition) +import GHC.Driver.Phases (isObjectFilename) linkBytecodeLib :: HscEnv -> [ModuleByteCode] -> IO () linkBytecodeLib hsc_env gbcs = do let dflags = hsc_dflags hsc_env -- The .gbc files from the command line - let bytecodeObjects = [f | FileOption _ f <- ldInputs dflags] + let fileArguments = [f | FileOption _ f <- ldInputs dflags] + + let (objectFiles, bytecodeObjects) = partition (isObjectFilename (targetPlatform dflags)) fileArguments let logger = hsc_logger hsc_env let allFiles = (map text bytecodeObjects) ++ [ angleBrackets (text "in-memory" <+> ppr (gbc_module bco)) | bco <- gbcs ] @@ -28,7 +32,7 @@ linkBytecodeLib hsc_env gbcs = do let (all_cbcs, foreign_stubs) = unzip [ (bs, fs) | ModuleByteCode _m bs fs <- on_disk_bcos ++ gbcs] - foreign_stub_lib <- mkDynLoadLib hsc_env id [] [{-TODO-}] (concat foreign_stubs) + foreign_stub_lib <- mkDynLoadLib hsc_env id [] [{-TODO-}] (concat foreign_stubs ++ objectFiles) fc <- case foreign_stub_lib of Just (fp, _, _) -> do ===================================== testsuite/tests/cabal/BytecodeForeign.hs ===================================== @@ -24,5 +24,4 @@ testForeign = do pureFunction :: Int -> Int -> Int pureFunction x y = x + y -$(addForeignFilePath LangC "BytecodeForeign.c" >> return []) ===================================== testsuite/tests/cabal/Makefile ===================================== @@ -313,7 +313,38 @@ pkg_bytecode_foreign : $(LOCAL_GHC_PKG08) init $(PKGCONF08) mkdir outdir mv BytecodeForeign.hs outdir/ + echo '$$(addForeignFilePath LangC "BytecodeForeign.c" >> return [])' >> outdir/BytecodeForeign.hs mv BytecodeForeign.c outdir/ cd outdir && $(TEST_HC) -bytecodelib -hisuf=dyn_hi -dynamic -o testpkg-1.2.3.4-XXX.bytecode BytecodeForeign.hs -fbyte-code -fwrite-interface -fwrite-byte-code -this-unit-id=testpkg-1.2.3.4-XXX $(LOCAL_GHC_PKG09) register --force bytecode_foreign.pkg cat bytecode_foreign.script | $(TEST_HC) $(TEST_HC_OPTS_INTERACTIVE) -package testpkg -package-db $(PKGCONF09) + +# Test that bytecode generation works by first compiling C to .o, then passing .o to GHC +PKGCONF10=bytecode_with_o.package.conf +LOCAL_GHC_PKG10 = '$(GHC_PKG)' --no-user-package-db -f $(PKGCONF10) +pkg_bytecode_with_o : + $(LOCAL_GHC_PKG10) init $(PKGCONF10) + mkdir outdir + mv BytecodeForeign.hs outdir/ + mv BytecodeForeign.c outdir/ + cd outdir && $(TEST_HC) -c BytecodeForeign.c + cd outdir && $(TEST_HC) -bytecodelib -hisuf=dyn_hi -dynamic -o testpkg-1.2.3.4-XXX.bytecode BytecodeForeign.hs BytecodeForeign.o -fbyte-code -fwrite-interface -fwrite-byte-code -this-unit-id=testpkg-1.2.3.4-XXX + $(LOCAL_GHC_PKG10) register --force bytecode_foreign.pkg + cat bytecode_foreign.script | $(TEST_HC) $(TEST_HC_OPTS_INTERACTIVE) -package testpkg -package-db $(PKGCONF10) + +# Test that bytecode generation works by first compiling C to .o and Haskell to .gbc independently +PKGCONF11=bytecode_with_gbc.package.conf +LOCAL_GHC_PKG11 = '$(GHC_PKG)' --no-user-package-db -f $(PKGCONF11) +pkg_bytecode_with_gbc : + $(LOCAL_GHC_PKG11) init $(PKGCONF11) + mkdir outdir + mv BytecodeForeign.hs outdir/ + mv BytecodeForeign.c outdir/ + cd outdir && $(TEST_HC) -c BytecodeForeign.c + cd outdir && $(TEST_HC) BytecodeForeign.hs -fbyte-code -fwrite-interface -hisuf=dyn_hi -dynamic -fwrite-byte-code -this-unit-id=testpkg-1.2.3.4-XXX -o BytecodeForeign.gbc + cd outdir && $(TEST_HC) -bytecodelib -hisuf=dyn_hi -dynamic -o testpkg-1.2.3.4-XXX.bytecode BytecodeForeign.gbc BytecodeForeign.o + $(LOCAL_GHC_PKG11) register --force bytecode_foreign.pkg + cat bytecode_foreign.script | $(TEST_HC) $(TEST_HC_OPTS_INTERACTIVE) -package testpkg -package-db $(PKGCONF11) + + + ===================================== testsuite/tests/cabal/all.T ===================================== @@ -43,6 +43,14 @@ test('pkg_bytecode_foreign', [extra_files(['bytecode_foreign.pkg', "BytecodeFore , normalise_errmsg_fun(normaliseDynlibNames, ignore_warnings) , copy_files], makefile_test, []) +test('pkg_bytecode_with_o', [extra_files(['bytecode_foreign.pkg', "BytecodeForeign.hs", "BytecodeForeign.c", "bytecode_foreign.script"]) + , normalise_errmsg_fun(normaliseDynlibNames, ignore_warnings) + , copy_files], makefile_test, []) + +test('pkg_bytecode_with_gbc', [extra_files(['bytecode_foreign.pkg', "BytecodeForeign.hs", "BytecodeForeign.c", "bytecode_foreign.script"]) + , normalise_errmsg_fun(normaliseDynlibNames, ignore_warnings) + , copy_files], makefile_test, []) + # Test that we *can* compile a module that also belongs to a package # (this was disallowed in GHC 6.4 and earlier) test('pkg01', normal, compile, ['']) ===================================== testsuite/tests/cabal/pkg_bytecode_with_gbc.stderr ===================================== @@ -0,0 +1,4 @@ +when making flags consistent: warning: [GHC-74335] [-Winconsistent-flags] + Byte-code linking does not currently support linking an executable, enabling -no-link + +testpkg-1.2.3.4: cannot find any of ["libtestpkg-1.2.3.4-XXX.a","libtestpkg-1.2.3.4-XXX_p.a","libtestpkg-1.2.3.4-XXX-ghc9.15.20251010.so","libtestpkg-1.2.3.4-XXX_p-ghc9.15.20251010.so","libtestpkg-1.2.3.4-XXX-ghc9.15.20251010.dylib","libtestpkg-1.2.3.4-XXX_p-ghc9.15.20251010.dylib","testpkg-1.2.3.4-XXX-ghc9.15.20251010.dll","testpkg-1.2.3.4-XXX_p-ghc9.15.20251010.dll"] on library path (ignoring) ===================================== testsuite/tests/cabal/pkg_bytecode_with_gbc.stdout ===================================== @@ -0,0 +1,4 @@ +[1 of 1] Compiling BytecodeForeign ( BytecodeForeign.hs, BytecodeForeign.gbc ) +Reading package info from "bytecode_foreign.pkg" ... done. +8 +42 ===================================== testsuite/tests/cabal/pkg_bytecode_with_o.stderr ===================================== @@ -0,0 +1 @@ +testpkg-1.2.3.4: cannot find any of ["libtestpkg-1.2.3.4-XXX.a","libtestpkg-1.2.3.4-XXX_p.a","libtestpkg-1.2.3.4-XXX-ghc9.15.20251010.so","libtestpkg-1.2.3.4-XXX_p-ghc9.15.20251010.so","libtestpkg-1.2.3.4-XXX-ghc9.15.20251010.dylib","libtestpkg-1.2.3.4-XXX_p-ghc9.15.20251010.dylib","testpkg-1.2.3.4-XXX-ghc9.15.20251010.dll","testpkg-1.2.3.4-XXX_p-ghc9.15.20251010.dll"] on library path (ignoring) ===================================== testsuite/tests/cabal/pkg_bytecode_with_o.stdout ===================================== @@ -0,0 +1,5 @@ +[1 of 2] Compiling BytecodeForeign ( BytecodeForeign.hs, BytecodeForeign.gbc ) +[2 of 2] Linking testpkg-1.2.3.4-XXX.bytecode +Reading package info from "bytecode_foreign.pkg" ... done. +8 +42 View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/25af941ad306f08e115ab5f32f862605... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/25af941ad306f08e115ab5f32f862605... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Matthew Pickering (@mpickering)