Skip to content
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

Add js-options field to cabal file to allow passing custom preprocessing options for js (#10721) #10722

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ buildInfoFieldGrammar =
<*> monoidalFieldAla "cc-options" (alaList' NoCommaFSep Token') L.ccOptions
<*> monoidalFieldAla "cxx-options" (alaList' NoCommaFSep Token') L.cxxOptions
^^^ availableSince CabalSpecV2_2 []
<*> monoidalFieldAla "js-options" (alaList' NoCommaFSep Token') L.jsOptions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, needs to be guarded by availableSince.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it but got confused by a fact js-sources are not guarded. Is it an omission?

Which value should I put here?

Copy link
Author

@Swordlash Swordlash Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpickering ? Should I add version 3.16?

^^^ availableSince CabalSpecV3_14 []
<*> monoidalFieldAla "ld-options" (alaList' NoCommaFSep Token') L.ldOptions
<*> monoidalFieldAla "hsc2hs-options" (alaList' NoCommaFSep Token') L.hsc2hsOptions
^^^ availableSince CabalSpecV3_6 []
Expand Down
4 changes: 4 additions & 0 deletions Cabal-syntax/src/Distribution/Types/BuildInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ data BuildInfo = BuildInfo
-- ^ options for C compiler
, cxxOptions :: [String]
-- ^ options for C++ compiler
, jsOptions :: [String]
-- ^ options for pre-processing JavaScript code
, ldOptions :: [String]
-- ^ options for linker
, hsc2hsOptions :: [String]
Expand Down Expand Up @@ -161,6 +163,7 @@ instance Monoid BuildInfo where
, cmmOptions = []
, ccOptions = []
, cxxOptions = []
, jsOptions = []
, ldOptions = []
, hsc2hsOptions = []
, pkgconfigDepends = []
Expand Down Expand Up @@ -214,6 +217,7 @@ instance Semigroup BuildInfo where
, cmmOptions = combine cmmOptions
, ccOptions = combine ccOptions
, cxxOptions = combine cxxOptions
, jsOptions = combine jsOptions
, ldOptions = combine ldOptions
, hsc2hsOptions = combine hsc2hsOptions
, pkgconfigDepends = combine pkgconfigDepends
Expand Down
4 changes: 4 additions & 0 deletions Cabal-syntax/src/Distribution/Types/BuildInfo/Lens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class HasBuildInfo a where
cxxOptions = buildInfo . cxxOptions
{-# INLINE cxxOptions #-}

jsOptions :: Lens' a [String]
jsOptions = buildInfo . jsOptions
{-# INLINE jsOptions #-}

ldOptions :: Lens' a [String]
ldOptions = buildInfo . ldOptions
{-# INLINE ldOptions #-}
Expand Down
1 change: 1 addition & 0 deletions Cabal/src/Distribution/Simple/GHC/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ componentJsGhcOptions verbosity lbi bi clbi odir filename =
, ghcOptPackageDBs = withPackageDB lbi
, ghcOptPackages = toNubListR $ mkGhcOptPackages (promisedPkgs lbi) clbi
, ghcOptObjDir = toFlag odir
, ghcOptExtra = jsOptions bi
}

componentGhcOptions
Expand Down
1 change: 1 addition & 0 deletions cabal-testsuite/PackageTests/JS/JsOptions/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/JsOptions/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/JsOptions/js.test.hs
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions cabal-testsuite/PackageTests/JS/JsOptions/jsbits/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//#OPTIONS: CPP

function foo() {
#ifdef PRINT_DEF
console.log("Hello definition!");
#else
console.log("Hello!");
#endif
}
19 changes: 19 additions & 0 deletions cabal-testsuite/PackageTests/JS/JsOptions/jsoptions.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cabal-version: 3.14
name: jsoptions
version: 0
build-type: Simple

library
default-language: Haskell2010
exposed-modules: Lib
build-depends: base

if impl(javascript)
js-sources: jsbits/lib.js
js-options: -optJSP-DPRINT_DEF

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

main = do
skipIfJavaScript
cabalTest $ do
-- Ensure the field `js-options` does not raise issues
res <- cabal' "v2-run" ["demo"]
assertOutputContains "foo_fallback" res
9 changes: 9 additions & 0 deletions cabal-testsuite/PackageTests/JS/JsOptions/src/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{-# LANGUAGE CPP #-}
module Lib where

#if defined(javascript_HOST_ARCH)
foreign import javascript foo :: IO ()
#else
foo :: IO ()
foo = putStrLn "foo_fallback"
#endif
8 changes: 8 additions & 0 deletions doc/buildinfo-fields-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ cxx-options
.. math::
{\left\{ \mathop{\mathit{hs\text{-}string}}\mid{{[\mathop{\mathord{``}\mathtt{\ }\mathord{"}}]^c}}^+_{} \right\}}^\ast_{\bullet}

js-options
* Monoidal field
* Available since ``cabal-version: 3.14``.
* Documentation of :pkg-field:`library:js-options`

.. math::
{\left\{ \mathop{\mathit{hs\text{-}string}}\mid{{[\mathop{\mathord{``}\mathtt{\ }\mathord{"}}]^c}}^+_{} \right\}}^\ast_{\bullet}

cxx-sources
* Monoidal field
* Available since ``cabal-version: 2.2``.
Expand Down
1 change: 1 addition & 0 deletions editors/vim/syntax/cabal.vim
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ syn keyword cabalFieldName contained
\ copyright
\ cpp-options
\ cxx-options
\ js-options
\ cxx-sources
\ data-dir
\ data-files
Expand Down
Loading