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

CPP macro are only generated when 'version' attribute is specified. #446

Merged
merged 2 commits into from
Oct 1, 2018
Merged
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
5 changes: 4 additions & 1 deletion haskell/haskell.bzl
Original file line number Diff line number Diff line change
@@ -119,6 +119,9 @@ def _mk_binary_rule(**kwargs):
allow_single_file = FileType([".hs", ".hsc", ".lhs"]),
doc = "File containing `Main` module (deprecated).",
),
version = attr.string(
doc = "Executable version. If this is specified, CPP version macros will be generated for this build.",
),
_dummy_static_lib = attr.label(
default = Label("@io_tweag_rules_haskell//haskell:dummy_static_lib"),
allow_single_file = True,
@@ -189,7 +192,7 @@ haskell_library = rule(
),
version = attr.string(
doc = """Library version. Not normally necessary unless to build a library
originally defined as a Cabal package.""",
originally defined as a Cabal package. If this is specified, CPP version macro will be generated.""",
),
),
outputs = {
1 change: 1 addition & 0 deletions haskell/lint.bzl
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@ def _haskell_lint_aspect_impl(target, ctx):
use_direct = False,
use_my_pkg_id = None,
custom_package_caches = None,
version = ctx.rule.attr.version,
))

sources = set.to_list(
9 changes: 5 additions & 4 deletions haskell/private/actions/compile.bzl
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ def _process_hsc_file(hs, cc, hsc_file):

return hs_out, idir

def _compilation_defaults(hs, cc, java, dep_info, srcs, import_dir_map, extra_srcs, compiler_flags, with_profiling, my_pkg_id = None):
def _compilation_defaults(hs, cc, java, dep_info, srcs, import_dir_map, extra_srcs, compiler_flags, with_profiling, my_pkg_id, version):
"""Declare default compilation targets and create default compiler arguments.

Returns:
@@ -129,6 +129,7 @@ def _compilation_defaults(hs, cc, java, dep_info, srcs, import_dir_map, extra_sr
use_direct = True,
use_my_pkg_id = my_pkg_id,
custom_package_caches = None,
version = version,
))

header_files = []
@@ -269,7 +270,7 @@ def _compilation_defaults(hs, cc, java, dep_info, srcs, import_dir_map, extra_sr
),
)

def compile_binary(hs, cc, java, dep_info, srcs, ls_modules, import_dir_map, extra_srcs, compiler_flags, with_profiling, main_function):
def compile_binary(hs, cc, java, dep_info, srcs, ls_modules, import_dir_map, extra_srcs, compiler_flags, with_profiling, main_function, version):
"""Compile a Haskell target into object files suitable for linking.

Returns:
@@ -279,7 +280,7 @@ def compile_binary(hs, cc, java, dep_info, srcs, ls_modules, import_dir_map, ext
modules: set of module names
source_files: set of Haskell source files
"""
c = _compilation_defaults(hs, cc, java, dep_info, srcs, import_dir_map, extra_srcs, compiler_flags, with_profiling)
c = _compilation_defaults(hs, cc, java, dep_info, srcs, import_dir_map, extra_srcs, compiler_flags, with_profiling, my_pkg_id = None, version = version)
c.args.add(["-main-is", main_function])

hs.toolchain.actions.run_ghc(
@@ -335,7 +336,7 @@ def compile_library(hs, cc, java, dep_info, srcs, ls_modules, other_modules, exp
source_files: set of Haskell module files
import_dirs: import directories that should make all modules visible (for GHCi)
"""
c = _compilation_defaults(hs, cc, java, dep_info, srcs, import_dir_map, extra_srcs, compiler_flags, with_profiling, my_pkg_id = my_pkg_id)
c = _compilation_defaults(hs, cc, java, dep_info, srcs, import_dir_map, extra_srcs, compiler_flags, with_profiling, my_pkg_id = my_pkg_id, version = my_pkg_id.version)

hs.toolchain.actions.run_ghc(
hs,
5 changes: 4 additions & 1 deletion haskell/private/actions/link.bzl
Original file line number Diff line number Diff line change
@@ -84,7 +84,8 @@ def link_binary(
compiler_flags,
objects_dir,
with_profiling,
dummy_static_lib):
dummy_static_lib,
version):
"""Link Haskell binary from static object files.

Returns:
@@ -138,6 +139,7 @@ def link_binary(
use_direct = False,
use_my_pkg_id = None,
custom_package_caches = None,
version = version,
))

_add_external_libraries(args, dep_info.external_libraries.values())
@@ -314,6 +316,7 @@ def link_library_dynamic(hs, cc, dep_info, extra_srcs, objects_dir, my_pkg_id):
use_direct = False,
use_my_pkg_id = None,
custom_package_caches = None,
version = my_pkg_id.version if my_pkg_id else None,
))

_add_external_libraries(args, dep_info.external_libraries.values())
2 changes: 2 additions & 0 deletions haskell/private/actions/repl.bzl
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ def build_haskell_repl(
build_info,
output,
package_caches,
version,
lib_info = None,
bin_info = None):
"""Build REPL script.
@@ -58,6 +59,7 @@ def build_haskell_repl(
use_direct = False,
use_my_pkg_id = None,
custom_package_caches = package_caches,
version = version,
)

if lib_info != None:
5 changes: 5 additions & 0 deletions haskell/private/haskell_impl.bzl
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ use the 'haskell_import' rule instead.
compiler_flags = ctx.attr.compiler_flags,
with_profiling = False,
main_function = ctx.attr.main_function,
version = ctx.attr.version,
)

c_p = None
@@ -100,6 +101,7 @@ use the 'haskell_import' rule instead.
compiler_flags = ctx.attr.compiler_flags,
with_profiling = True,
main_function = ctx.attr.main_function,
version = ctx.attr.version,
)

binary = link_binary(
@@ -111,6 +113,7 @@ use the 'haskell_import' rule instead.
c_p.objects_dir if with_profiling else c.objects_dir,
with_profiling,
ctx.file._dummy_static_lib,
version = ctx.attr.version,
)

solibs = set.union(
@@ -137,6 +140,7 @@ use the 'haskell_import' rule instead.
repl_ghci_args = ctx.attr.repl_ghci_args,
output = ctx.outputs.repl,
package_caches = dep_info.package_caches,
version = ctx.attr.version,
build_info = build_info,
bin_info = bin_info,
)
@@ -308,6 +312,7 @@ use the 'haskell_import' rule instead.
compiler_flags = ctx.attr.compiler_flags,
output = ctx.outputs.repl,
package_caches = dep_info.package_caches,
version = ctx.attr.version,
build_info = build_info,
lib_info = lib_info,
)
18 changes: 14 additions & 4 deletions haskell/private/packages.bzl
Original file line number Diff line number Diff line change
@@ -2,12 +2,13 @@

load(":private/set.bzl", "set")

def expose_packages(build_info, lib_info, use_direct, use_my_pkg_id, custom_package_caches):
def expose_packages(build_info, lib_info, use_direct, use_my_pkg_id, custom_package_caches, version):
"""
Returns the list of command line argument which should be passed
to GHC in order to enable haskell packages.

build_info: is common to all builds
version: if the rule contains a version, we will export the CPP version macro

All the other arguments are not understood well:

@@ -16,7 +17,6 @@ def expose_packages(build_info, lib_info, use_direct, use_my_pkg_id, custom_pack
use_my_pkg_id: only used for one specific task in compile.bzl
custom_package_caches: override the package_caches of build_info, used only by the repl
"""

args = [
# In compile.bzl, we pass this just before all -package-id
# arguments. Not doing so leads to bizarre compile-time failures.
@@ -26,11 +26,21 @@ def expose_packages(build_info, lib_info, use_direct, use_my_pkg_id, custom_pack
"-hide-all-packages",
]

# Expose all prebuilt dependencies
has_version = version != None and version != ""

if not has_version:
args.extend([
# Macro version are disabled for all packages by default
# and enabled for package with version
# see https://github.com/tweag/rules_haskell/issues/414
"-fno-version-macros",
])

# Expose all prebuilt dependencies
#
# We have to remember to specify all (transitive) wired-in
# dependencies or we can't find objects for linking

#
# XXX: This should be really dep_info.direct_prebuilt_deps, but since we
# cannot add prebuilt_dependencies to the "depends" field on package
# registration (see a comment there), we have to pass all transitive
1 change: 1 addition & 0 deletions tests/cpp_macro_conflict/BS.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module BS where
29 changes: 29 additions & 0 deletions tests/cpp_macro_conflict/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package(default_testonly = 1)

load(
"@io_tweag_rules_haskell//haskell:haskell.bzl",
"haskell_library",
"haskell_binary",
)

# empty library with package name "bytestring"
haskell_library(
name = "bytestring",
srcs = ["BS.hs"],
deps = ["//tests:base"],
)

# This depends on two packages "bytestring"
# There should be no CPP macro conflict
haskell_binary(
name = "macro_conflict",
srcs = ["Main.hs"],
deps = ["//tests:base", "//tests:bytestring", ":bytestring"],
compiler_flags = ["-XCPP", "-Werror"] + select({
# clang on darwin fails because of unused command line argument, it fails because of -Werror
"@bazel_tools//src/conditions:darwin": [
"-optP-Wno-unused-command-line-argument",
],
"//conditions:default": [],
}),
)
4 changes: 4 additions & 0 deletions tests/cpp_macro_conflict/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import qualified Data.ByteString
import BS

main = putStrLn "hello"
1 change: 1 addition & 0 deletions tests/package-name/BUILD
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ haskell_test(
name = "bin",
size = "small",
srcs = ["Main.hs"],
version = "0.0.0", # This flags triggers the `MIN_VERSION` macro generation
deps = [
":lib-a_Z",
"//tests:base",