Skip to content

Commit 7302733

Browse files
committed
cabal: Add ignore_setup flag to cabal_args
It can occur that a module has a source file named "Setup.hs" that isn't a cabal setup module. The current setup finder will find such a module and then setup will break. Add a flag to cabal_args to indicate that any such module should be ignored.
1 parent 50e37a9 commit 7302733

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

haskell/cabal.bzl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,14 @@ def _find_cabal(srcs):
103103
fail("A .cabal file was not found in the srcs attribute.")
104104
return cabal
105105

106-
def _find_setup(hs, cabal, srcs):
106+
def _find_setup(hs, cabal, srcs, ignore_setup=False):
107107
"""Check that a Setup script exists. If not, create a default one."""
108108
setup = None
109-
for f in srcs:
110-
if f.basename in ["Setup.hs", "Setup.lhs"]:
111-
if not setup or f.dirname < setup.dirname:
112-
setup = f
109+
if not ignore_setup:
110+
for f in srcs:
111+
if f.basename in ["Setup.hs", "Setup.lhs"]:
112+
if not setup or f.dirname < setup.dirname:
113+
setup = f
113114
if not setup:
114115
setup = hs.actions.declare_file("Setup.hs", sibling = cabal)
115116
hs.actions.write(
@@ -424,8 +425,10 @@ def _shorten_library_symlink(dynamic_library):
424425

425426
def _haskell_cabal_args_impl(ctx):
426427
is_empty = ctx.attr.is_empty
428+
ignore_setup = ctx.attr.ignore_setup
427429
cabal_args = HaskellCabalArgs(
428430
is_empty = is_empty,
431+
ignore_setup = ignore_setup
429432
)
430433
return [cabal_args]
431434

@@ -437,6 +440,10 @@ haskell_cabal_args = rule(
437440
doc = """True if this (sub) library is empty, with only re-exports, and no source files of its own.
438441
It is necessary to set this, otherwise bazel will complain about missing "*libHS.a" files.""",
439442
),
443+
"ignore_setup": attr.bool(
444+
default = False,
445+
doc = """True if this package has a "Setup.hs" that is not a cabal "Setup.hs". """,
446+
),
440447
},
441448
provides = [HaskellCabalArgs],
442449
)
@@ -452,8 +459,10 @@ def _haskell_cabal_library_impl(ctx):
452459
)
453460

454461
is_empty = False
462+
ignore_setup = False
455463
if ctx.attr.cabal_args:
456464
is_empty = ctx.attr.cabal_args[HaskellCabalArgs].is_empty
465+
ignore_setup = ctx.attr.cabal_args[HaskellCabalArgs].ignore_setup
457466

458467
# All C and Haskell library dependencies.
459468
cc_info = cc_common.merge_cc_infos(
@@ -482,7 +491,7 @@ def _haskell_cabal_library_impl(ctx):
482491
fail("ERROR: `compiler_flags` attribute was removed. Use `cabalopts` with `--ghc-option` instead.")
483492

484493
cabal = _find_cabal(ctx.files.srcs)
485-
setup = _find_setup(hs, cabal, ctx.files.srcs)
494+
setup = _find_setup(hs, cabal, ctx.files.srcs, ignore_setup)
486495
package_database = hs.actions.declare_file(
487496
"_install/{}.conf.d/package.cache".format(package_id),
488497
sibling = cabal,
@@ -823,6 +832,10 @@ def _haskell_cabal_binary_impl(ctx):
823832
override_cc_toolchain = hs.tools_config.maybe_exec_cc_toolchain,
824833
)
825834

835+
ignore_setup = False
836+
if ctx.attr.cabal_args:
837+
ignore_setup = ctx.attr.cabal_args[HaskellCabalArgs].ignore_setup
838+
826839
# All C and Haskell library dependencies.
827840
cc_info = cc_common.merge_cc_infos(
828841
cc_infos = [dep[CcInfo] for dep in ctx.attr.deps if CcInfo in dep],
@@ -844,7 +857,7 @@ def _haskell_cabal_binary_impl(ctx):
844857
fail("ERROR: `compiler_flags` attribute was removed. Use `cabalopts` with `--ghc-option` instead.")
845858

846859
cabal = _find_cabal(ctx.files.srcs)
847-
setup = _find_setup(hs, cabal, ctx.files.srcs)
860+
setup = _find_setup(hs, cabal, ctx.files.srcs, ignore_setup)
848861
package_database = hs.actions.declare_file(
849862
"_install/{}.conf.d/package.cache".format(hs.label.name),
850863
sibling = cabal,
@@ -984,6 +997,10 @@ haskell_cabal_binary = rule(
984997
"flags": attr.string_list(
985998
doc = "List of Cabal flags, will be passed to `Setup.hs configure --flags=...`.",
986999
),
1000+
"cabal_args": attr.label(
1001+
doc = """A haskell_cabal_args target with cabal specific settings for this package.""",
1002+
providers = [[HaskellCabalArgs]],
1003+
),
9871004
"_cabal_wrapper": attr.label(
9881005
executable = True,
9891006
cfg = "exec",

haskell/providers.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ HaskellCabalArgs = provider(
3838
doc = "Settings for a haskell_cabal_library",
3939
fields = {
4040
"is_empty": "True if this (sub) library is empty, with only re-exports, and no source files of its own.",
41+
"ignore_setup": "True if this package contains a \"Setup.hs\" that isn't a cabal Setup module.",
4142
},
4243
)
4344

0 commit comments

Comments
 (0)