Skip to content

Commit

Permalink
Generate CPP macro version only for library/binary with version att…
Browse files Browse the repository at this point in the history
…ribute

fix #414
  • Loading branch information
guibou committed Sep 25, 2018
1 parent 8ed7e85 commit c8186e7
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 15 deletions.
3 changes: 3 additions & 0 deletions haskell/haskell.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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. Used as metadata.",
),
_dummy_static_lib = attr.label(
default = Label("@io_tweag_rules_haskell//haskell:dummy_static_lib"),
allow_single_file = True,
Expand Down
2 changes: 1 addition & 1 deletion haskell/lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _haskell_lint_aspect_impl(target, ctx):
"--make",
])

args.add(expose_packages(build_info, lib_info, use_direct = False, use_my_pkg_id = None))
args.add(expose_packages(build_info, lib_info, use_direct = False, use_my_pkg_id = None, version = ctx.rule.attr.version))

sources = set.to_list(
lib_info.source_files if lib_info != None else bin_info.source_files,
Expand Down
10 changes: 5 additions & 5 deletions haskell/private/actions/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -123,7 +123,7 @@ def _compilation_defaults(hs, cc, java, dep_info, srcs, import_dir_map, extra_sr
if hs.toolchain.is_darwin:
ghc_args += ["-optl-Wl,-dead_strip_dylibs"]

ghc_args.extend(expose_packages(dep_info, lib_info = None, use_direct = True, use_my_pkg_id = my_pkg_id))
ghc_args.extend(expose_packages(dep_info, lib_info = None, use_direct = True, use_my_pkg_id = my_pkg_id, version = version))

header_files = []
boot_files = []
Expand Down Expand Up @@ -263,7 +263,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:
Expand All @@ -273,7 +273,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(
Expand Down Expand Up @@ -329,7 +329,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,
Expand Down
13 changes: 10 additions & 3 deletions haskell/private/actions/link.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -132,7 +133,7 @@ def link_binary(
# directly rather than doing multiple reversals with temporary
# lists.

args.add(expose_packages(dep_info, lib_info = None, use_direct = False, use_my_pkg_id = None))
args.add(expose_packages(dep_info, lib_info = None, use_direct = False, use_my_pkg_id = None, version = version))

_add_external_libraries(args, dep_info.external_libraries.values())

Expand Down Expand Up @@ -302,7 +303,13 @@ def link_library_dynamic(hs, cc, dep_info, extra_srcs, objects_dir, my_pkg_id):
if hs.toolchain.is_darwin:
args.add(["-optl-Wl,-dead_strip_dylibs"])

args.add(expose_packages(dep_info, lib_info = None, use_direct = False, use_my_pkg_id = None))
args.add(expose_packages(
dep_info,
lib_info = None,
use_direct = False,
use_my_pkg_id = None,
version = my_pkg_id.version if my_pkg_id else None,
))

_add_external_libraries(args, dep_info.external_libraries.values())

Expand Down
3 changes: 2 additions & 1 deletion haskell/private/actions/repl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def build_haskell_repl(
build_info,
output,
package_caches,
version,
lib_info = None,
bin_info = None):
"""Build REPL script.
Expand All @@ -52,7 +53,7 @@ def build_haskell_repl(
None.
"""

args = expose_packages(build_info, lib_info, use_direct = False, use_my_pkg_id = None)
args = expose_packages(build_info, lib_info, use_direct = False, use_my_pkg_id = None, version = version)

if lib_info != None:
for idir in set.to_list(lib_info.import_dirs):
Expand Down
5 changes: 5 additions & 0 deletions haskell/private/haskell_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down
19 changes: 14 additions & 5 deletions haskell/private/packages.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

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

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

args = [
# In compile.bzl, we pass this just before all -package-id
# arguments. Not doing so leads to bizarre compile-time failures.
Expand All @@ -17,10 +16,20 @@ def expose_packages(build_info, lib_info, use_direct, use_my_pkg_id):
"-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
# 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
Expand Down
1 change: 1 addition & 0 deletions tests/package-name/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c8186e7

Please sign in to comment.