Skip to content

Support asm/cmm/js sources in executable components (#8639) #9061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
405 changes: 133 additions & 272 deletions Cabal/src/Distribution/Simple/GHC.hs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions cabal-testsuite/PackageTests/CmmSourcesExe/cabal.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# cabal v2-run
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- cmmexperiment-0 (exe:demo) (first run)
Configuring executable 'demo' for cmmexperiment-0...
Preprocessing executable 'demo' for cmmexperiment-0...
Building executable 'demo' for cmmexperiment-0...
1 change: 1 addition & 0 deletions cabal-testsuite/PackageTests/CmmSourcesExe/cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: .
6 changes: 6 additions & 0 deletions cabal-testsuite/PackageTests/CmmSourcesExe/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Test.Cabal.Prelude

main = cabalTest $ do
res <- cabal' "v2-run" ["demo"]
assertOutputContains "= Post common block elimination =" res
assertOutputContains "In Box we have 0x" res
6 changes: 6 additions & 0 deletions cabal-testsuite/PackageTests/CmmSourcesExe/cbits/HeapPrim.cmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Cmm.h"

aToMyWordzh (P_ clos)
{
return (clos);
}
23 changes: 23 additions & 0 deletions cabal-testsuite/PackageTests/CmmSourcesExe/cmmexperiment.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cabal-version: 3.0
name: cmmexperiment
version: 0
build-type: Simple

-- This code is extracted form ghc-heap
-- Copyright (c) 2012-2013, Joachim Breitner
-- (and probably -2020 GHC Team)
-- Under BSD-3-Clause

executable demo
default-language: Haskell2010
main-is: Main.hs
hs-source-dirs: demo, src
build-depends: base
other-modules: Demo
cmm-sources: cbits/HeapPrim.cmm
if impl(ghc >=8.2)
cmm-options: -ddump-cmm-verbose
else
cmm-options: -ddump-cmm


2 changes: 2 additions & 0 deletions cabal-testsuite/PackageTests/CmmSourcesExe/demo/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Main (module Demo) where
import Demo (main)
5 changes: 5 additions & 0 deletions cabal-testsuite/PackageTests/CmmSourcesExe/setup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Setup configure
Configuring cmmexperiment-0...
# Setup build
Preprocessing executable 'demo' for cmmexperiment-0...
Building executable 'demo' for cmmexperiment-0...
7 changes: 7 additions & 0 deletions cabal-testsuite/PackageTests/CmmSourcesExe/setup.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Test.Cabal.Prelude

main = setupTest $ do
skipUnlessGhcVersion ">= 7.8"
setup "configure" []
res <- setup' "build" []
assertOutputContains "= Post common block elimination =" res
37 changes: 37 additions & 0 deletions cabal-testsuite/PackageTests/CmmSourcesExe/src/Demo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE GHCForeignImportPrim #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnliftedFFITypes #-}
module Demo (main) where

#include "MachDeps.h"

import Data.Bits
import GHC.Exts
import Numeric (showHex)

foreign import prim "aToMyWordzh" aToWord# :: Any -> Word#

tAG_MASK :: Int
tAG_MASK = (1 `shift` TAG_BITS) - 1

data Box = Box Any

instance Show Box where
showsPrec _ (Box a) rs =
-- unsafePerformIO (print "↓" >> pClosure a) `seq`
pad_out (showHex addr "") ++ (if tag>0 then "/" ++ show tag else "") ++ rs
where
ptr = W# (aToWord# a)
tag = ptr .&. fromIntegral tAG_MASK -- ((1 `shiftL` TAG_BITS) -1)
addr = ptr - tag
pad_out ls = '0':'x':ls

asBox :: a -> Box
asBox x = Box (unsafeCoerce# x)

main :: IO ()
main = do
let box = asBox "foobar"
putStrLn $ "In Box we have " ++ show box
1 change: 1 addition & 0 deletions cabal-testsuite/PackageTests/JS/JsSourcesExe/cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: .
6 changes: 6 additions & 0 deletions cabal-testsuite/PackageTests/JS/JsSourcesExe/demo/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main where

import Lib

main :: IO ()
main = foo
9 changes: 9 additions & 0 deletions cabal-testsuite/PackageTests/JS/JsSourcesExe/js-arch.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Test.Cabal.Prelude

main = setupAndCabalTest $ do
skipUnlessGhcVersion ">= 9.6"
skipUnlessJavaScript
skipIfWindows

res <- cabal' "v2-run" ["demo"]
assertOutputContains "Hello JS!" res
3 changes: 3 additions & 0 deletions cabal-testsuite/PackageTests/JS/JsSourcesExe/jsbits/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function foo() {
console.log("Hello JS!");
}
20 changes: 20 additions & 0 deletions cabal-testsuite/PackageTests/JS/JsSourcesExe/jssources-exe.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cabal-version: 3.0
name: jssources-exe
version: 0
build-type: Simple

library
default-language: Haskell2010
if arch(JavaScript)
hs-source-dirs: srcJS
else
hs-source-dirs: src
exposed-modules: Lib
build-depends: base

executable demo
default-language: Haskell2010
main-is: Main.hs
hs-source-dirs: demo
js-sources: jsbits/lib.js
build-depends: base, jssources-exe
12 changes: 12 additions & 0 deletions cabal-testsuite/PackageTests/JS/JsSourcesExe/other-arch.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# cabal v2-run
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- jssources-exe-0 (lib) (first run)
- jssources-exe-0 (exe:demo) (first run)
Configuring library for jssources-exe-0...
Preprocessing library for jssources-exe-0...
Building library for jssources-exe-0...
Configuring executable 'demo' for jssources-exe-0...
Preprocessing executable 'demo' for jssources-exe-0...
Building executable 'demo' for jssources-exe-0...
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Test.Cabal.Prelude

main = cabalTest $ do
skipIfJavaScript
-- Ensure the field `js-sources` does not raise issues
res <- cabal' "v2-run" ["demo"]
assertOutputContains "Hello Not JS!" res
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/JS/JsSourcesExe/src/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Lib where

foo :: IO ()
foo = putStrLn "Hello Not JS!"
3 changes: 3 additions & 0 deletions cabal-testsuite/PackageTests/JS/JsSourcesExe/srcJS/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Lib where

foreign import javascript foo :: IO ()
12 changes: 12 additions & 0 deletions changelog.d/issue-8639
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
synopsis: Add support for asm, cmm, and js sources in executable components
packages: Cabal
prs: #9061
issues: #8639
significance: significant

description: {

Executable components now support the inclusion of asm, cmm, and js source files in a cabal file using the same syntax as is used for these sources in library components, similar to how c and c++ sources are supported in both types of components. This syntax was already parsed in cabal files, but was silently ignored in the build step, so no changes to syntax are made.

}