From c9935c4e9479f419fb4e3036e9bf2960fec74da5 Mon Sep 17 00:00:00 2001 From: zlonast Date: Mon, 12 May 2025 17:56:08 +0300 Subject: [PATCH] ghc-options added to js, cmm and asm add GhcOptions test partial fix for #10721 --- Cabal/src/Distribution/Simple/GHC/Internal.hs | 4 +++- .../PackageTests/JS/GhcOptions/cabal.project | 1 + .../PackageTests/JS/GhcOptions/demo/Main.hs | 6 ++++++ .../JS/GhcOptions/js-arch.test.hs | 9 +++++++++ .../PackageTests/JS/GhcOptions/jsbits/lib.js | 9 +++++++++ .../JS/GhcOptions/jsghcoptions.cabal | 19 +++++++++++++++++++ .../PackageTests/JS/GhcOptions/other-arch.out | 14 ++++++++++++++ .../JS/GhcOptions/other-arch.test.hs | 8 ++++++++ .../PackageTests/JS/GhcOptions/src/Lib.hs | 9 +++++++++ changelog.d/pr-10949.md | 13 +++++++++++++ 10 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 cabal-testsuite/PackageTests/JS/GhcOptions/cabal.project create mode 100644 cabal-testsuite/PackageTests/JS/GhcOptions/demo/Main.hs create mode 100644 cabal-testsuite/PackageTests/JS/GhcOptions/js-arch.test.hs create mode 100644 cabal-testsuite/PackageTests/JS/GhcOptions/jsbits/lib.js create mode 100644 cabal-testsuite/PackageTests/JS/GhcOptions/jsghcoptions.cabal create mode 100644 cabal-testsuite/PackageTests/JS/GhcOptions/other-arch.out create mode 100644 cabal-testsuite/PackageTests/JS/GhcOptions/other-arch.test.hs create mode 100644 cabal-testsuite/PackageTests/JS/GhcOptions/src/Lib.hs create mode 100644 changelog.d/pr-10949.md diff --git a/Cabal/src/Distribution/Simple/GHC/Internal.hs b/Cabal/src/Distribution/Simple/GHC/Internal.hs index 59ca82ba483..121e34549cb 100644 --- a/Cabal/src/Distribution/Simple/GHC/Internal.hs +++ b/Cabal/src/Distribution/Simple/GHC/Internal.hs @@ -487,6 +487,7 @@ componentAsmGhcOptions verbosity lbi bi clbi odir filename = ) ++ asmOptions bi , ghcOptObjDir = toFlag odir + , ghcOptExtra = hcOptions GHC bi } componentJsGhcOptions @@ -509,6 +510,7 @@ componentJsGhcOptions verbosity lbi bi clbi odir filename = , ghcOptPackageDBs = withPackageDB lbi , ghcOptPackages = toNubListR $ mkGhcOptPackages (promisedPkgs lbi) clbi , ghcOptObjDir = toFlag odir + , ghcOptExtra = hcOptions GHC bi } componentGhcOptions @@ -622,7 +624,7 @@ componentCmmGhcOptions verbosity lbi bi clbi odir filename = , ghcOptPackages = toNubListR $ mkGhcOptPackages (promisedPkgs lbi) clbi , ghcOptOptimisation = toGhcOptimisation (withOptimization lbi) , ghcOptDebugInfo = toFlag (withDebugInfo lbi) - , ghcOptExtra = cmmOptions bi + , ghcOptExtra = hcOptions GHC bi <> cmmOptions bi , ghcOptObjDir = toFlag odir } diff --git a/cabal-testsuite/PackageTests/JS/GhcOptions/cabal.project b/cabal-testsuite/PackageTests/JS/GhcOptions/cabal.project new file mode 100644 index 00000000000..e6fdbadb439 --- /dev/null +++ b/cabal-testsuite/PackageTests/JS/GhcOptions/cabal.project @@ -0,0 +1 @@ +packages: . diff --git a/cabal-testsuite/PackageTests/JS/GhcOptions/demo/Main.hs b/cabal-testsuite/PackageTests/JS/GhcOptions/demo/Main.hs new file mode 100644 index 00000000000..19e67f001f8 --- /dev/null +++ b/cabal-testsuite/PackageTests/JS/GhcOptions/demo/Main.hs @@ -0,0 +1,6 @@ +module Main where + +import Lib + +main :: IO () +main = foo diff --git a/cabal-testsuite/PackageTests/JS/GhcOptions/js-arch.test.hs b/cabal-testsuite/PackageTests/JS/GhcOptions/js-arch.test.hs new file mode 100644 index 00000000000..c7df6e8e986 --- /dev/null +++ b/cabal-testsuite/PackageTests/JS/GhcOptions/js-arch.test.hs @@ -0,0 +1,9 @@ +import Test.Cabal.Prelude + +main = do + skipUnlessJavaScript + skipIfWindows "" + setupAndCabalTest $ do + skipUnlessGhcVersion ">= 9.12" + res <- cabal' "v2-run" ["demo"] + assertOutputContains "Hello definition!" res diff --git a/cabal-testsuite/PackageTests/JS/GhcOptions/jsbits/lib.js b/cabal-testsuite/PackageTests/JS/GhcOptions/jsbits/lib.js new file mode 100644 index 00000000000..b3b8beb78fb --- /dev/null +++ b/cabal-testsuite/PackageTests/JS/GhcOptions/jsbits/lib.js @@ -0,0 +1,9 @@ +//#OPTIONS: CPP + +function foo() { +#ifdef PRINT_DEF + console.log("Hello definition!"); +#else + console.log("Hello!"); +#endif +} diff --git a/cabal-testsuite/PackageTests/JS/GhcOptions/jsghcoptions.cabal b/cabal-testsuite/PackageTests/JS/GhcOptions/jsghcoptions.cabal new file mode 100644 index 00000000000..3b8bce887c1 --- /dev/null +++ b/cabal-testsuite/PackageTests/JS/GhcOptions/jsghcoptions.cabal @@ -0,0 +1,19 @@ +cabal-version: 3.0 +name: jsghcoptions +version: 0 +build-type: Simple + +library + default-language: Haskell2010 + js-sources: jsbits/lib.js + if arch(JavaScript) + ghc-options: -optJSP-DPRINT_DEF + hs-source-dirs: src + exposed-modules: Lib + build-depends: base + +executable demo + default-language: Haskell2010 + main-is: Main.hs + hs-source-dirs: demo + build-depends: base, jsghcoptions diff --git a/cabal-testsuite/PackageTests/JS/GhcOptions/other-arch.out b/cabal-testsuite/PackageTests/JS/GhcOptions/other-arch.out new file mode 100644 index 00000000000..adbab42f608 --- /dev/null +++ b/cabal-testsuite/PackageTests/JS/GhcOptions/other-arch.out @@ -0,0 +1,14 @@ +# cabal v2-run +Configuration is affected by the following files: +- cabal.project +Resolving dependencies... +Build profile: -w ghc- -O1 +In order, the following will be built: + - jsghcoptions-0 (lib) (first run) + - jsghcoptions-0 (exe:demo) (first run) +Configuring library for jsghcoptions-0... +Preprocessing library for jsghcoptions-0... +Building library for jsghcoptions-0... +Configuring executable 'demo' for jsghcoptions-0... +Preprocessing executable 'demo' for jsghcoptions-0... +Building executable 'demo' for jsghcoptions-0... diff --git a/cabal-testsuite/PackageTests/JS/GhcOptions/other-arch.test.hs b/cabal-testsuite/PackageTests/JS/GhcOptions/other-arch.test.hs new file mode 100644 index 00000000000..5f1f0caa20b --- /dev/null +++ b/cabal-testsuite/PackageTests/JS/GhcOptions/other-arch.test.hs @@ -0,0 +1,8 @@ +import Test.Cabal.Prelude + +main = do + skipIfJavaScript + cabalTest $ do + -- Ensure the field `js-sources` does not raise issues + res <- cabal' "v2-run" ["demo"] + assertOutputContains "Not JS foo" res diff --git a/cabal-testsuite/PackageTests/JS/GhcOptions/src/Lib.hs b/cabal-testsuite/PackageTests/JS/GhcOptions/src/Lib.hs new file mode 100644 index 00000000000..5cd25eb2f87 --- /dev/null +++ b/cabal-testsuite/PackageTests/JS/GhcOptions/src/Lib.hs @@ -0,0 +1,9 @@ +{-# LANGUAGE CPP #-} +module Lib where + +#if defined(javascript_HOST_ARCH) +foreign import javascript foo :: IO () +#else +foo :: IO () +foo = putStrLn "Not JS foo" +#endif diff --git a/changelog.d/pr-10949.md b/changelog.d/pr-10949.md new file mode 100644 index 00000000000..ccb15cd0b36 --- /dev/null +++ b/changelog.d/pr-10949.md @@ -0,0 +1,13 @@ +--- +synopsis: "ghc-options added to js, cmm and asm" +packages: [Cabal] +prs: 10949 +--- + +Now we have the ability to pass ghc-options flags to all component. + +``` + js-sources: jsbits/lib.js + ghc-options: -optJSP-your_flag +``` +