Skip to content

Commit 605a3c6

Browse files
authored
Add support for js-sources (#8636)
* Add support for js-sources * Add changelog * Add comment about C-like sources * Add tests * Fix test * Fix skipUnless and factorize * Fix skipIfGhcVersion
1 parent 09f918b commit 605a3c6

File tree

12 files changed

+120
-4
lines changed

12 files changed

+120
-4
lines changed

Cabal/src/Distribution/Simple/Build.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ buildComponent verbosity numJobs pkg_descr lbi suffixes
277277
$ flip addExtraCmmSources extras
278278
$ flip addExtraCxxSources extras
279279
$ flip addExtraCSources extras
280+
$ flip addExtraJsSources extras
280281
$ libbi
281282
}
282283

@@ -454,6 +455,13 @@ addExtraAsmSources :: BuildInfo -> [FilePath] -> BuildInfo
454455
addExtraAsmSources bi extras = bi { asmSources = new }
455456
where new = ordNub (extras ++ asmSources bi)
456457

458+
-- | Add extra JS sources generated by preprocessing to build
459+
-- information.
460+
addExtraJsSources :: BuildInfo -> [FilePath] -> BuildInfo
461+
addExtraJsSources bi extras = bi { jsSources = new }
462+
where new = ordNub (extras ++ jsSources bi)
463+
464+
457465
-- | Add extra HS modules generated by preprocessing to build
458466
-- information.
459467
addExtraOtherModules :: BuildInfo -> [ModuleName.ModuleName] -> BuildInfo

Cabal/src/Distribution/Simple/GHC.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ buildOrReplLib mReplFlags verbosity numJobs pkg_descr lbi lib clbi = do
567567
, toNubListR (cxxSources libBi)
568568
, toNubListR (cmmSources libBi)
569569
, toNubListR (asmSources libBi)
570+
, toNubListR (jsSources libBi)
571+
-- JS files are C-like with GHC's JS backend: they are
572+
-- "compiled" into `.o` files (renamed with a header).
573+
-- This is a difference from GHCJS, for which we only
574+
-- pass the JS files at link time.
570575
]
571576
cLikeObjs = map (`replaceExtension` objExtension) cLikeSources
572577
baseOpts = componentGhcOptions verbosity lbi libBi clbi libTargetDir
@@ -724,6 +729,25 @@ buildOrReplLib mReplFlags verbosity numJobs pkg_descr lbi lib clbi = do
724729
unless forRepl $ whenProfLib (runGhcProgIfNeeded profCcOpts)
725730
| filename <- cSources libBi]
726731

732+
-- build any JS sources
733+
unless (not has_code || null (jsSources libBi)) $ do
734+
info verbosity "Building JS Sources..."
735+
sequence_
736+
[ do let vanillaJsOpts = Internal.componentJsGhcOptions verbosity implInfo
737+
lbi libBi clbi relLibTargetDir filename
738+
profJsOpts = vanillaJsOpts `mappend` mempty {
739+
ghcOptProfilingMode = toFlag True,
740+
ghcOptObjSuffix = toFlag "p_o"
741+
}
742+
odir = fromFlag (ghcOptObjDir vanillaJsOpts)
743+
createDirectoryIfMissingVerbose verbosity True odir
744+
let runGhcProgIfNeeded jsOpts = do
745+
needsRecomp <- checkNeedsRecompilation filename jsOpts
746+
when needsRecomp $ runGhcProg jsOpts
747+
runGhcProgIfNeeded vanillaJsOpts
748+
unless forRepl $ whenProfLib (runGhcProgIfNeeded profJsOpts)
749+
| filename <- jsSources libBi]
750+
727751
-- build any ASM sources
728752
unless (not has_code || null (asmSources libBi)) $ do
729753
info verbosity "Building Assembler Sources..."
@@ -2063,6 +2087,7 @@ installLib verbosity lbi targetDir dynlibTargetDir _builtDir pkg lib clbi = do
20632087
&& null (cxxSources (libBuildInfo lib))
20642088
&& null (cmmSources (libBuildInfo lib))
20652089
&& null (asmSources (libBuildInfo lib))
2090+
&& null (jsSources (libBuildInfo lib))
20662091
has_code = not (componentIsIndefinite clbi)
20672092
whenHasCode = when has_code
20682093
whenVanilla = when (hasLib && withVanillaLib lbi)

Cabal/src/Distribution/Simple/GHC/Internal.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Distribution.Simple.GHC.Internal (
2222
componentCmmGhcOptions,
2323
componentCxxGhcOptions,
2424
componentAsmGhcOptions,
25+
componentJsGhcOptions,
2526
componentGhcOptions,
2627
mkGHCiLibName,
2728
mkGHCiProfLibName,
@@ -382,6 +383,32 @@ componentAsmGhcOptions verbosity _implInfo lbi bi clbi odir filename =
382383
ghcOptObjDir = toFlag odir
383384
}
384385

386+
componentJsGhcOptions :: Verbosity -> GhcImplInfo -> LocalBuildInfo
387+
-> BuildInfo -> ComponentLocalBuildInfo
388+
-> FilePath -> FilePath
389+
-> GhcOptions
390+
componentJsGhcOptions verbosity _implInfo lbi bi clbi odir filename =
391+
mempty {
392+
-- Respect -v0, but don't crank up verbosity on GHC if
393+
-- Cabal verbosity is requested. For that, use --ghc-option=-v instead!
394+
ghcOptVerbosity = toFlag (min verbosity normal),
395+
ghcOptMode = toFlag GhcModeCompile,
396+
ghcOptInputFiles = toNubListR [filename],
397+
398+
ghcOptCppIncludePath = toNubListR $ [autogenComponentModulesDir lbi clbi
399+
,autogenPackageModulesDir lbi
400+
,odir]
401+
-- includes relative to the package
402+
++ includeDirs bi
403+
-- potential includes generated by `configure'
404+
-- in the build directory
405+
++ [buildDir lbi </> dir | dir <- includeDirs bi],
406+
ghcOptHideAllPackages= toFlag True,
407+
ghcOptPackageDBs = withPackageDB lbi,
408+
ghcOptPackages = toNubListR $ mkGhcOptPackages clbi,
409+
ghcOptObjDir = toFlag odir
410+
}
411+
385412

386413
componentGhcOptions :: Verbosity -> GhcImplInfo -> LocalBuildInfo
387414
-> BuildInfo -> ComponentLocalBuildInfo -> FilePath

Cabal/src/Distribution/Simple/Register.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,7 @@ generalInstalledPackageInfo adjustRelIncDirs pkg abi_hash lib lbi clbi installDi
466466
|| not (null (asmSources bi))
467467
|| not (null (cmmSources bi))
468468
|| not (null (cxxSources bi))
469-
|| (not (null (jsSources bi)) &&
470-
compilerFlavor comp == GHCJS))
469+
|| not (null (jsSources bi)))
471470
&& not (componentIsIndefinite clbi)
472471
libdirsStatic
473472
| hasLibrary = libdir installDirs : extraLibDirsStaticOrFallback
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Test.Cabal.Prelude
2+
3+
main = setupAndCabalTest $ do
4+
skipUnlessGhcVersion ">= 9.6"
5+
skipUnlessJavaScript
6+
7+
res <- cabal' "v2-run" ["demo"]
8+
assertOutputContains "Hello JS!" res
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: 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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cabal-version: 3.0
2+
name: jssources
3+
version: 0
4+
build-type: Simple
5+
6+
library
7+
default-language: Haskell2010
8+
js-sources: jsbits/lib.js
9+
hs-source-dirs: src
10+
exposed-modules: Lib
11+
build-depends: base
12+
13+
executable demo
14+
default-language: Haskell2010
15+
main-is: Main.hs
16+
hs-source-dirs: demo
17+
build-depends: base, jssources
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 ()

cabal-testsuite/src/Test/Cabal/Prelude.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Distribution.Simple.PackageDescription (readGenericPackageDescription)
2828
import Distribution.Simple.Program.Types
2929
import Distribution.Simple.Program.Db
3030
import Distribution.Simple.Program
31-
import Distribution.System (OS(Windows,Linux,OSX), buildOS)
31+
import Distribution.System (OS(Windows,Linux,OSX), Arch(JavaScript), buildOS, buildArch)
3232
import Distribution.Simple.Utils
3333
( withFileContents, withTempDirectory, tryFindPackageDesc )
3434
import Distribution.Simple.Configure
@@ -889,7 +889,13 @@ skipUnlessGhcVersion :: String -> TestM ()
889889
skipUnlessGhcVersion range = skipUnless ("needs ghc " ++ range) =<< isGhcVersion range
890890

891891
skipIfGhcVersion :: String -> TestM ()
892-
skipIfGhcVersion range = skipUnless ("incompatible with ghc " ++ range) =<< isGhcVersion range
892+
skipIfGhcVersion range = skipIf ("incompatible with ghc " ++ range) =<< isGhcVersion range
893+
894+
skipUnlessJavaScript :: TestM ()
895+
skipUnlessJavaScript = skipUnless "needs the JavaScript backend" =<< isJavaScript
896+
897+
skipIfJavaScript :: TestM ()
898+
skipIfJavaScript = skipIf "incompatible with the JavaScript backend" =<< isJavaScript
893899

894900
isWindows :: TestM Bool
895901
isWindows = return (buildOS == Windows)
@@ -900,6 +906,11 @@ isOSX = return (buildOS == OSX)
900906
isLinux :: TestM Bool
901907
isLinux = return (buildOS == Linux)
902908

909+
isJavaScript :: TestM Bool
910+
isJavaScript = return (buildArch == JavaScript)
911+
-- should probably be `hostArch` but Cabal doesn't distinguish build platform
912+
-- and host platform
913+
903914
skipIfWindows :: TestM ()
904915
skipIfWindows = skipIf "Windows" =<< isWindows
905916

changelog.d/pr-8636

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
synopsis: Support `js-sources` with GHC, not only with GHCJS
2+
prs: #8636
3+
description: {
4+
5+
- Take into account js-sources when building library components with GHC
6+
- Missing support for js-sources in executable components is tracked in #8639
7+
8+
}

0 commit comments

Comments
 (0)