Skip to content

Commit c206998

Browse files
JoshMeredithmergify[bot]
authored andcommitted
Support asm/cmm/js sources in executable components (#8639) (#9061)
* WIP support asm/cmm/js sources in executable components (#8639) * Factorise extra src code for lib/exe and add extra exe src tests * Add extra sources to linking step * lint * lint * Don't build js sources for executables on non-js hosts * Fix cabal.out for CmmSourcesExe test and lint * Update changelog * Slight changes (cherry picked from commit 3350a6c) # Conflicts: # Cabal/src/Distribution/Simple/GHC.hs
1 parent 529f850 commit c206998

File tree

20 files changed

+511
-0
lines changed

20 files changed

+511
-0
lines changed

Cabal/src/Distribution/Simple/GHC.hs

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# cabal v2-run
2+
Resolving dependencies...
3+
Build profile: -w ghc-<GHCVER> -O1
4+
In order, the following will be built:
5+
- cmmexperiment-0 (exe:demo) (first run)
6+
Configuring executable 'demo' for cmmexperiment-0...
7+
Preprocessing executable 'demo' for cmmexperiment-0...
8+
Building executable 'demo' for cmmexperiment-0...
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Test.Cabal.Prelude
2+
3+
main = cabalTest $ do
4+
res <- cabal' "v2-run" ["demo"]
5+
assertOutputContains "= Post common block elimination =" res
6+
assertOutputContains "In Box we have 0x" res
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "Cmm.h"
2+
3+
aToMyWordzh (P_ clos)
4+
{
5+
return (clos);
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cabal-version: 3.0
2+
name: cmmexperiment
3+
version: 0
4+
build-type: Simple
5+
6+
-- This code is extracted form ghc-heap
7+
-- Copyright (c) 2012-2013, Joachim Breitner
8+
-- (and probably -2020 GHC Team)
9+
-- Under BSD-3-Clause
10+
11+
executable demo
12+
default-language: Haskell2010
13+
main-is: Main.hs
14+
hs-source-dirs: demo, src
15+
build-depends: base
16+
other-modules: Demo
17+
cmm-sources: cbits/HeapPrim.cmm
18+
if impl(ghc >=8.2)
19+
cmm-options: -ddump-cmm-verbose
20+
else
21+
cmm-options: -ddump-cmm
22+
23+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module Main (module Demo) where
2+
import Demo (main)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Setup configure
2+
Configuring cmmexperiment-0...
3+
# Setup build
4+
Preprocessing executable 'demo' for cmmexperiment-0...
5+
Building executable 'demo' for cmmexperiment-0...
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Test.Cabal.Prelude
2+
3+
main = setupTest $ do
4+
skipUnlessGhcVersion ">= 7.8"
5+
setup "configure" []
6+
res <- setup' "build" []
7+
assertOutputContains "= Post common block elimination =" res
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE ForeignFunctionInterface #-}
3+
{-# LANGUAGE GHCForeignImportPrim #-}
4+
{-# LANGUAGE MagicHash #-}
5+
{-# LANGUAGE UnliftedFFITypes #-}
6+
module Demo (main) where
7+
8+
#include "MachDeps.h"
9+
10+
import Data.Bits
11+
import GHC.Exts
12+
import Numeric (showHex)
13+
14+
foreign import prim "aToMyWordzh" aToWord# :: Any -> Word#
15+
16+
tAG_MASK :: Int
17+
tAG_MASK = (1 `shift` TAG_BITS) - 1
18+
19+
data Box = Box Any
20+
21+
instance Show Box where
22+
showsPrec _ (Box a) rs =
23+
-- unsafePerformIO (print "↓" >> pClosure a) `seq`
24+
pad_out (showHex addr "") ++ (if tag>0 then "/" ++ show tag else "") ++ rs
25+
where
26+
ptr = W# (aToWord# a)
27+
tag = ptr .&. fromIntegral tAG_MASK -- ((1 `shiftL` TAG_BITS) -1)
28+
addr = ptr - tag
29+
pad_out ls = '0':'x':ls
30+
31+
asBox :: a -> Box
32+
asBox x = Box (unsafeCoerce# x)
33+
34+
main :: IO ()
35+
main = do
36+
let box = asBox "foobar"
37+
putStrLn $ "In Box we have " ++ show box
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Main where
2+
3+
import Lib
4+
5+
main :: IO ()
6+
main = foo
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Test.Cabal.Prelude
2+
3+
main = setupAndCabalTest $ do
4+
skipUnlessGhcVersion ">= 9.6"
5+
skipUnlessJavaScript
6+
skipIfWindows
7+
8+
res <- cabal' "v2-run" ["demo"]
9+
assertOutputContains "Hello JS!" res
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function foo() {
2+
console.log("Hello JS!");
3+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cabal-version: 3.0
2+
name: jssources-exe
3+
version: 0
4+
build-type: Simple
5+
6+
library
7+
default-language: Haskell2010
8+
if arch(JavaScript)
9+
hs-source-dirs: srcJS
10+
else
11+
hs-source-dirs: src
12+
exposed-modules: Lib
13+
build-depends: base
14+
15+
executable demo
16+
default-language: Haskell2010
17+
main-is: Main.hs
18+
hs-source-dirs: demo
19+
js-sources: jsbits/lib.js
20+
build-depends: base, jssources-exe
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# cabal v2-run
2+
Resolving dependencies...
3+
Build profile: -w ghc-<GHCVER> -O1
4+
In order, the following will be built:
5+
- jssources-exe-0 (lib) (first run)
6+
- jssources-exe-0 (exe:demo) (first run)
7+
Configuring library for jssources-exe-0...
8+
Preprocessing library for jssources-exe-0...
9+
Building library for jssources-exe-0...
10+
Configuring executable 'demo' for jssources-exe-0...
11+
Preprocessing executable 'demo' for jssources-exe-0...
12+
Building executable 'demo' for jssources-exe-0...
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Test.Cabal.Prelude
2+
3+
main = cabalTest $ do
4+
skipIfJavaScript
5+
-- Ensure the field `js-sources` does not raise issues
6+
res <- cabal' "v2-run" ["demo"]
7+
assertOutputContains "Hello Not JS!" res
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Lib where
2+
3+
foo :: IO ()
4+
foo = putStrLn "Hello Not JS!"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Lib where
2+
3+
foreign import javascript foo :: IO ()

changelog.d/issue-8639

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
synopsis: Add support for asm, cmm, and js sources in executable components
2+
packages: Cabal
3+
prs: #9061
4+
issues: #8639
5+
significance: significant
6+
7+
description: {
8+
9+
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.
10+
11+
}
12+

0 commit comments

Comments
 (0)